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 linker.cpp 2601e04c3fSmrg * GLSL linker implementation 2701e04c3fSmrg * 2801e04c3fSmrg * Given a set of shaders that are to be linked to generate a final program, 2901e04c3fSmrg * there are three distinct stages. 3001e04c3fSmrg * 3101e04c3fSmrg * In the first stage shaders are partitioned into groups based on the shader 3201e04c3fSmrg * type. All shaders of a particular type (e.g., vertex shaders) are linked 3301e04c3fSmrg * together. 3401e04c3fSmrg * 3501e04c3fSmrg * - Undefined references in each shader are resolve to definitions in 3601e04c3fSmrg * another shader. 3701e04c3fSmrg * - Types and qualifiers of uniforms, outputs, and global variables defined 3801e04c3fSmrg * in multiple shaders with the same name are verified to be the same. 3901e04c3fSmrg * - Initializers for uniforms and global variables defined 4001e04c3fSmrg * in multiple shaders with the same name are verified to be the same. 4101e04c3fSmrg * 4201e04c3fSmrg * The result, in the terminology of the GLSL spec, is a set of shader 4301e04c3fSmrg * executables for each processing unit. 4401e04c3fSmrg * 4501e04c3fSmrg * After the first stage is complete, a series of semantic checks are performed 4601e04c3fSmrg * on each of the shader executables. 4701e04c3fSmrg * 4801e04c3fSmrg * - Each shader executable must define a \c main function. 4901e04c3fSmrg * - Each vertex shader executable must write to \c gl_Position. 5001e04c3fSmrg * - Each fragment shader executable must write to either \c gl_FragData or 5101e04c3fSmrg * \c gl_FragColor. 5201e04c3fSmrg * 5301e04c3fSmrg * In the final stage individual shader executables are linked to create a 5401e04c3fSmrg * complete exectuable. 5501e04c3fSmrg * 5601e04c3fSmrg * - Types of uniforms defined in multiple shader stages with the same name 5701e04c3fSmrg * are verified to be the same. 5801e04c3fSmrg * - Initializers for uniforms defined in multiple shader stages with the 5901e04c3fSmrg * same name are verified to be the same. 6001e04c3fSmrg * - Types and qualifiers of outputs defined in one stage are verified to 6101e04c3fSmrg * be the same as the types and qualifiers of inputs defined with the same 6201e04c3fSmrg * name in a later stage. 6301e04c3fSmrg * 6401e04c3fSmrg * \author Ian Romanick <ian.d.romanick@intel.com> 6501e04c3fSmrg */ 6601e04c3fSmrg 6701e04c3fSmrg#include <ctype.h> 6801e04c3fSmrg#include "util/strndup.h" 6901e04c3fSmrg#include "glsl_symbol_table.h" 7001e04c3fSmrg#include "glsl_parser_extras.h" 7101e04c3fSmrg#include "ir.h" 7201e04c3fSmrg#include "program.h" 7301e04c3fSmrg#include "program/prog_instruction.h" 7401e04c3fSmrg#include "program/program.h" 7501e04c3fSmrg#include "util/mesa-sha1.h" 7601e04c3fSmrg#include "util/set.h" 7701e04c3fSmrg#include "string_to_uint_map.h" 7801e04c3fSmrg#include "linker.h" 7901e04c3fSmrg#include "linker_util.h" 8001e04c3fSmrg#include "link_varyings.h" 8101e04c3fSmrg#include "ir_optimization.h" 8201e04c3fSmrg#include "ir_rvalue_visitor.h" 8301e04c3fSmrg#include "ir_uniform.h" 8401e04c3fSmrg#include "builtin_functions.h" 8501e04c3fSmrg#include "shader_cache.h" 8601e04c3fSmrg#include "util/u_string.h" 8701e04c3fSmrg#include "util/u_math.h" 8801e04c3fSmrg 897ec681f3Smrg 9001e04c3fSmrg#include "main/shaderobj.h" 9101e04c3fSmrg#include "main/enums.h" 9201e04c3fSmrg#include "main/mtypes.h" 9301e04c3fSmrg 9401e04c3fSmrg 9501e04c3fSmrgnamespace { 9601e04c3fSmrg 9701e04c3fSmrgstruct find_variable { 9801e04c3fSmrg const char *name; 9901e04c3fSmrg bool found; 10001e04c3fSmrg 10101e04c3fSmrg find_variable(const char *name) : name(name), found(false) {} 10201e04c3fSmrg}; 10301e04c3fSmrg 10401e04c3fSmrg/** 10501e04c3fSmrg * Visitor that determines whether or not a variable is ever written. 10601e04c3fSmrg * 10701e04c3fSmrg * Use \ref find_assignments for convenience. 10801e04c3fSmrg */ 10901e04c3fSmrgclass find_assignment_visitor : public ir_hierarchical_visitor { 11001e04c3fSmrgpublic: 11101e04c3fSmrg find_assignment_visitor(unsigned num_vars, 11201e04c3fSmrg find_variable * const *vars) 11301e04c3fSmrg : num_variables(num_vars), num_found(0), variables(vars) 11401e04c3fSmrg { 11501e04c3fSmrg } 11601e04c3fSmrg 11701e04c3fSmrg virtual ir_visitor_status visit_enter(ir_assignment *ir) 11801e04c3fSmrg { 11901e04c3fSmrg ir_variable *const var = ir->lhs->variable_referenced(); 12001e04c3fSmrg 12101e04c3fSmrg return check_variable_name(var->name); 12201e04c3fSmrg } 12301e04c3fSmrg 12401e04c3fSmrg virtual ir_visitor_status visit_enter(ir_call *ir) 12501e04c3fSmrg { 12601e04c3fSmrg foreach_two_lists(formal_node, &ir->callee->parameters, 12701e04c3fSmrg actual_node, &ir->actual_parameters) { 12801e04c3fSmrg ir_rvalue *param_rval = (ir_rvalue *) actual_node; 12901e04c3fSmrg ir_variable *sig_param = (ir_variable *) formal_node; 13001e04c3fSmrg 13101e04c3fSmrg if (sig_param->data.mode == ir_var_function_out || 13201e04c3fSmrg sig_param->data.mode == ir_var_function_inout) { 13301e04c3fSmrg ir_variable *var = param_rval->variable_referenced(); 13401e04c3fSmrg if (var && check_variable_name(var->name) == visit_stop) 13501e04c3fSmrg return visit_stop; 13601e04c3fSmrg } 13701e04c3fSmrg } 13801e04c3fSmrg 13901e04c3fSmrg if (ir->return_deref != NULL) { 14001e04c3fSmrg ir_variable *const var = ir->return_deref->variable_referenced(); 14101e04c3fSmrg 14201e04c3fSmrg if (check_variable_name(var->name) == visit_stop) 14301e04c3fSmrg return visit_stop; 14401e04c3fSmrg } 14501e04c3fSmrg 14601e04c3fSmrg return visit_continue_with_parent; 14701e04c3fSmrg } 14801e04c3fSmrg 14901e04c3fSmrgprivate: 15001e04c3fSmrg ir_visitor_status check_variable_name(const char *name) 15101e04c3fSmrg { 15201e04c3fSmrg for (unsigned i = 0; i < num_variables; ++i) { 15301e04c3fSmrg if (strcmp(variables[i]->name, name) == 0) { 15401e04c3fSmrg if (!variables[i]->found) { 15501e04c3fSmrg variables[i]->found = true; 15601e04c3fSmrg 15701e04c3fSmrg assert(num_found < num_variables); 15801e04c3fSmrg if (++num_found == num_variables) 15901e04c3fSmrg return visit_stop; 16001e04c3fSmrg } 16101e04c3fSmrg break; 16201e04c3fSmrg } 16301e04c3fSmrg } 16401e04c3fSmrg 16501e04c3fSmrg return visit_continue_with_parent; 16601e04c3fSmrg } 16701e04c3fSmrg 16801e04c3fSmrgprivate: 16901e04c3fSmrg unsigned num_variables; /**< Number of variables to find */ 17001e04c3fSmrg unsigned num_found; /**< Number of variables already found */ 17101e04c3fSmrg find_variable * const *variables; /**< Variables to find */ 17201e04c3fSmrg}; 17301e04c3fSmrg 17401e04c3fSmrg/** 17501e04c3fSmrg * Determine whether or not any of NULL-terminated list of variables is ever 17601e04c3fSmrg * written to. 17701e04c3fSmrg */ 17801e04c3fSmrgstatic void 17901e04c3fSmrgfind_assignments(exec_list *ir, find_variable * const *vars) 18001e04c3fSmrg{ 18101e04c3fSmrg unsigned num_variables = 0; 18201e04c3fSmrg 18301e04c3fSmrg for (find_variable * const *v = vars; *v; ++v) 18401e04c3fSmrg num_variables++; 18501e04c3fSmrg 18601e04c3fSmrg find_assignment_visitor visitor(num_variables, vars); 18701e04c3fSmrg visitor.run(ir); 18801e04c3fSmrg} 18901e04c3fSmrg 19001e04c3fSmrg/** 19101e04c3fSmrg * Determine whether or not the given variable is ever written to. 19201e04c3fSmrg */ 19301e04c3fSmrgstatic void 19401e04c3fSmrgfind_assignments(exec_list *ir, find_variable *var) 19501e04c3fSmrg{ 19601e04c3fSmrg find_assignment_visitor visitor(1, &var); 19701e04c3fSmrg visitor.run(ir); 19801e04c3fSmrg} 19901e04c3fSmrg 20001e04c3fSmrg/** 20101e04c3fSmrg * Visitor that determines whether or not a variable is ever read. 20201e04c3fSmrg */ 20301e04c3fSmrgclass find_deref_visitor : public ir_hierarchical_visitor { 20401e04c3fSmrgpublic: 20501e04c3fSmrg find_deref_visitor(const char *name) 20601e04c3fSmrg : name(name), found(false) 20701e04c3fSmrg { 20801e04c3fSmrg /* empty */ 20901e04c3fSmrg } 21001e04c3fSmrg 21101e04c3fSmrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 21201e04c3fSmrg { 21301e04c3fSmrg if (strcmp(this->name, ir->var->name) == 0) { 21401e04c3fSmrg this->found = true; 21501e04c3fSmrg return visit_stop; 21601e04c3fSmrg } 21701e04c3fSmrg 21801e04c3fSmrg return visit_continue; 21901e04c3fSmrg } 22001e04c3fSmrg 22101e04c3fSmrg bool variable_found() const 22201e04c3fSmrg { 22301e04c3fSmrg return this->found; 22401e04c3fSmrg } 22501e04c3fSmrg 22601e04c3fSmrgprivate: 22701e04c3fSmrg const char *name; /**< Find writes to a variable with this name. */ 22801e04c3fSmrg bool found; /**< Was a write to the variable found? */ 22901e04c3fSmrg}; 23001e04c3fSmrg 23101e04c3fSmrg 23201e04c3fSmrg/** 23301e04c3fSmrg * A visitor helper that provides methods for updating the types of 23401e04c3fSmrg * ir_dereferences. Classes that update variable types (say, updating 23501e04c3fSmrg * array sizes) will want to use this so that dereference types stay in sync. 23601e04c3fSmrg */ 23701e04c3fSmrgclass deref_type_updater : public ir_hierarchical_visitor { 23801e04c3fSmrgpublic: 23901e04c3fSmrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 24001e04c3fSmrg { 24101e04c3fSmrg ir->type = ir->var->type; 24201e04c3fSmrg return visit_continue; 24301e04c3fSmrg } 24401e04c3fSmrg 24501e04c3fSmrg virtual ir_visitor_status visit_leave(ir_dereference_array *ir) 24601e04c3fSmrg { 24701e04c3fSmrg const glsl_type *const vt = ir->array->type; 24801e04c3fSmrg if (vt->is_array()) 24901e04c3fSmrg ir->type = vt->fields.array; 25001e04c3fSmrg return visit_continue; 25101e04c3fSmrg } 25201e04c3fSmrg 25301e04c3fSmrg virtual ir_visitor_status visit_leave(ir_dereference_record *ir) 25401e04c3fSmrg { 25501e04c3fSmrg ir->type = ir->record->type->fields.structure[ir->field_idx].type; 25601e04c3fSmrg return visit_continue; 25701e04c3fSmrg } 25801e04c3fSmrg}; 25901e04c3fSmrg 26001e04c3fSmrg 26101e04c3fSmrgclass array_resize_visitor : public deref_type_updater { 26201e04c3fSmrgpublic: 2637ec681f3Smrg using deref_type_updater::visit; 2647ec681f3Smrg 26501e04c3fSmrg unsigned num_vertices; 26601e04c3fSmrg gl_shader_program *prog; 26701e04c3fSmrg gl_shader_stage stage; 26801e04c3fSmrg 26901e04c3fSmrg array_resize_visitor(unsigned num_vertices, 27001e04c3fSmrg gl_shader_program *prog, 27101e04c3fSmrg gl_shader_stage stage) 27201e04c3fSmrg { 27301e04c3fSmrg this->num_vertices = num_vertices; 27401e04c3fSmrg this->prog = prog; 27501e04c3fSmrg this->stage = stage; 27601e04c3fSmrg } 27701e04c3fSmrg 27801e04c3fSmrg virtual ~array_resize_visitor() 27901e04c3fSmrg { 28001e04c3fSmrg /* empty */ 28101e04c3fSmrg } 28201e04c3fSmrg 28301e04c3fSmrg virtual ir_visitor_status visit(ir_variable *var) 28401e04c3fSmrg { 28501e04c3fSmrg if (!var->type->is_array() || var->data.mode != ir_var_shader_in || 28601e04c3fSmrg var->data.patch) 28701e04c3fSmrg return visit_continue; 28801e04c3fSmrg 28901e04c3fSmrg unsigned size = var->type->length; 29001e04c3fSmrg 29101e04c3fSmrg if (stage == MESA_SHADER_GEOMETRY) { 29201e04c3fSmrg /* Generate a link error if the shader has declared this array with 29301e04c3fSmrg * an incorrect size. 29401e04c3fSmrg */ 29501e04c3fSmrg if (!var->data.implicit_sized_array && 29601e04c3fSmrg size && size != this->num_vertices) { 29701e04c3fSmrg linker_error(this->prog, "size of array %s declared as %u, " 29801e04c3fSmrg "but number of input vertices is %u\n", 29901e04c3fSmrg var->name, size, this->num_vertices); 30001e04c3fSmrg return visit_continue; 30101e04c3fSmrg } 30201e04c3fSmrg 30301e04c3fSmrg /* Generate a link error if the shader attempts to access an input 30401e04c3fSmrg * array using an index too large for its actual size assigned at 30501e04c3fSmrg * link time. 30601e04c3fSmrg */ 30701e04c3fSmrg if (var->data.max_array_access >= (int)this->num_vertices) { 30801e04c3fSmrg linker_error(this->prog, "%s shader accesses element %i of " 30901e04c3fSmrg "%s, but only %i input vertices\n", 31001e04c3fSmrg _mesa_shader_stage_to_string(this->stage), 31101e04c3fSmrg var->data.max_array_access, var->name, this->num_vertices); 31201e04c3fSmrg return visit_continue; 31301e04c3fSmrg } 31401e04c3fSmrg } 31501e04c3fSmrg 31601e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 31701e04c3fSmrg this->num_vertices); 31801e04c3fSmrg var->data.max_array_access = this->num_vertices - 1; 31901e04c3fSmrg 32001e04c3fSmrg return visit_continue; 32101e04c3fSmrg } 32201e04c3fSmrg}; 32301e04c3fSmrg 3247ec681f3Smrgclass array_length_to_const_visitor : public ir_rvalue_visitor { 3257ec681f3Smrgpublic: 3267ec681f3Smrg array_length_to_const_visitor() 3277ec681f3Smrg { 3287ec681f3Smrg this->progress = false; 3297ec681f3Smrg } 3307ec681f3Smrg 3317ec681f3Smrg virtual ~array_length_to_const_visitor() 3327ec681f3Smrg { 3337ec681f3Smrg /* empty */ 3347ec681f3Smrg } 3357ec681f3Smrg 3367ec681f3Smrg bool progress; 3377ec681f3Smrg 3387ec681f3Smrg virtual void handle_rvalue(ir_rvalue **rvalue) 3397ec681f3Smrg { 3407ec681f3Smrg if (*rvalue == NULL || (*rvalue)->ir_type != ir_type_expression) 3417ec681f3Smrg return; 3427ec681f3Smrg 3437ec681f3Smrg ir_expression *expr = (*rvalue)->as_expression(); 3447ec681f3Smrg if (expr) { 3457ec681f3Smrg if (expr->operation == ir_unop_implicitly_sized_array_length) { 3467ec681f3Smrg assert(!expr->operands[0]->type->is_unsized_array()); 3477ec681f3Smrg ir_constant *constant = new(expr) 3487ec681f3Smrg ir_constant(expr->operands[0]->type->array_size()); 3497ec681f3Smrg if (constant) { 3507ec681f3Smrg *rvalue = constant; 3517ec681f3Smrg } 3527ec681f3Smrg } 3537ec681f3Smrg } 3547ec681f3Smrg } 3557ec681f3Smrg}; 3567ec681f3Smrg 35701e04c3fSmrg/** 35801e04c3fSmrg * Visitor that determines the highest stream id to which a (geometry) shader 35901e04c3fSmrg * emits vertices. It also checks whether End{Stream}Primitive is ever called. 36001e04c3fSmrg */ 36101e04c3fSmrgclass find_emit_vertex_visitor : public ir_hierarchical_visitor { 36201e04c3fSmrgpublic: 36301e04c3fSmrg find_emit_vertex_visitor(int max_allowed) 36401e04c3fSmrg : max_stream_allowed(max_allowed), 36501e04c3fSmrg invalid_stream_id(0), 36601e04c3fSmrg invalid_stream_id_from_emit_vertex(false), 36701e04c3fSmrg end_primitive_found(false), 3687ec681f3Smrg used_streams(0) 36901e04c3fSmrg { 37001e04c3fSmrg /* empty */ 37101e04c3fSmrg } 37201e04c3fSmrg 37301e04c3fSmrg virtual ir_visitor_status visit_leave(ir_emit_vertex *ir) 37401e04c3fSmrg { 37501e04c3fSmrg int stream_id = ir->stream_id(); 37601e04c3fSmrg 37701e04c3fSmrg if (stream_id < 0) { 37801e04c3fSmrg invalid_stream_id = stream_id; 37901e04c3fSmrg invalid_stream_id_from_emit_vertex = true; 38001e04c3fSmrg return visit_stop; 38101e04c3fSmrg } 38201e04c3fSmrg 38301e04c3fSmrg if (stream_id > max_stream_allowed) { 38401e04c3fSmrg invalid_stream_id = stream_id; 38501e04c3fSmrg invalid_stream_id_from_emit_vertex = true; 38601e04c3fSmrg return visit_stop; 38701e04c3fSmrg } 38801e04c3fSmrg 3897ec681f3Smrg used_streams |= 1 << stream_id; 39001e04c3fSmrg 39101e04c3fSmrg return visit_continue; 39201e04c3fSmrg } 39301e04c3fSmrg 39401e04c3fSmrg virtual ir_visitor_status visit_leave(ir_end_primitive *ir) 39501e04c3fSmrg { 39601e04c3fSmrg end_primitive_found = true; 39701e04c3fSmrg 39801e04c3fSmrg int stream_id = ir->stream_id(); 39901e04c3fSmrg 40001e04c3fSmrg if (stream_id < 0) { 40101e04c3fSmrg invalid_stream_id = stream_id; 40201e04c3fSmrg invalid_stream_id_from_emit_vertex = false; 40301e04c3fSmrg return visit_stop; 40401e04c3fSmrg } 40501e04c3fSmrg 40601e04c3fSmrg if (stream_id > max_stream_allowed) { 40701e04c3fSmrg invalid_stream_id = stream_id; 40801e04c3fSmrg invalid_stream_id_from_emit_vertex = false; 40901e04c3fSmrg return visit_stop; 41001e04c3fSmrg } 41101e04c3fSmrg 4127ec681f3Smrg used_streams |= 1 << stream_id; 41301e04c3fSmrg 41401e04c3fSmrg return visit_continue; 41501e04c3fSmrg } 41601e04c3fSmrg 41701e04c3fSmrg bool error() 41801e04c3fSmrg { 41901e04c3fSmrg return invalid_stream_id != 0; 42001e04c3fSmrg } 42101e04c3fSmrg 42201e04c3fSmrg const char *error_func() 42301e04c3fSmrg { 42401e04c3fSmrg return invalid_stream_id_from_emit_vertex ? 42501e04c3fSmrg "EmitStreamVertex" : "EndStreamPrimitive"; 42601e04c3fSmrg } 42701e04c3fSmrg 42801e04c3fSmrg int error_stream() 42901e04c3fSmrg { 43001e04c3fSmrg return invalid_stream_id; 43101e04c3fSmrg } 43201e04c3fSmrg 4337ec681f3Smrg unsigned active_stream_mask() 43401e04c3fSmrg { 4357ec681f3Smrg return used_streams; 43601e04c3fSmrg } 43701e04c3fSmrg 43801e04c3fSmrg bool uses_end_primitive() 43901e04c3fSmrg { 44001e04c3fSmrg return end_primitive_found; 44101e04c3fSmrg } 44201e04c3fSmrg 44301e04c3fSmrgprivate: 44401e04c3fSmrg int max_stream_allowed; 44501e04c3fSmrg int invalid_stream_id; 44601e04c3fSmrg bool invalid_stream_id_from_emit_vertex; 44701e04c3fSmrg bool end_primitive_found; 4487ec681f3Smrg unsigned used_streams; 44901e04c3fSmrg}; 45001e04c3fSmrg 45101e04c3fSmrg/* Class that finds array derefs and check if indexes are dynamic. */ 45201e04c3fSmrgclass dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor 45301e04c3fSmrg{ 45401e04c3fSmrgpublic: 45501e04c3fSmrg dynamic_sampler_array_indexing_visitor() : 45601e04c3fSmrg dynamic_sampler_array_indexing(false) 45701e04c3fSmrg { 45801e04c3fSmrg } 45901e04c3fSmrg 46001e04c3fSmrg ir_visitor_status visit_enter(ir_dereference_array *ir) 46101e04c3fSmrg { 46201e04c3fSmrg if (!ir->variable_referenced()) 46301e04c3fSmrg return visit_continue; 46401e04c3fSmrg 46501e04c3fSmrg if (!ir->variable_referenced()->type->contains_sampler()) 46601e04c3fSmrg return visit_continue; 46701e04c3fSmrg 46801e04c3fSmrg if (!ir->array_index->constant_expression_value(ralloc_parent(ir))) { 46901e04c3fSmrg dynamic_sampler_array_indexing = true; 47001e04c3fSmrg return visit_stop; 47101e04c3fSmrg } 47201e04c3fSmrg return visit_continue; 47301e04c3fSmrg } 47401e04c3fSmrg 47501e04c3fSmrg bool uses_dynamic_sampler_array_indexing() 47601e04c3fSmrg { 47701e04c3fSmrg return dynamic_sampler_array_indexing; 47801e04c3fSmrg } 47901e04c3fSmrg 48001e04c3fSmrgprivate: 48101e04c3fSmrg bool dynamic_sampler_array_indexing; 48201e04c3fSmrg}; 48301e04c3fSmrg 48401e04c3fSmrg} /* anonymous namespace */ 48501e04c3fSmrg 48601e04c3fSmrgvoid 48701e04c3fSmrglinker_error(gl_shader_program *prog, const char *fmt, ...) 48801e04c3fSmrg{ 48901e04c3fSmrg va_list ap; 49001e04c3fSmrg 49101e04c3fSmrg ralloc_strcat(&prog->data->InfoLog, "error: "); 49201e04c3fSmrg va_start(ap, fmt); 49301e04c3fSmrg ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap); 49401e04c3fSmrg va_end(ap); 49501e04c3fSmrg 49601e04c3fSmrg prog->data->LinkStatus = LINKING_FAILURE; 49701e04c3fSmrg} 49801e04c3fSmrg 49901e04c3fSmrg 50001e04c3fSmrgvoid 50101e04c3fSmrglinker_warning(gl_shader_program *prog, const char *fmt, ...) 50201e04c3fSmrg{ 50301e04c3fSmrg va_list ap; 50401e04c3fSmrg 50501e04c3fSmrg ralloc_strcat(&prog->data->InfoLog, "warning: "); 50601e04c3fSmrg va_start(ap, fmt); 50701e04c3fSmrg ralloc_vasprintf_append(&prog->data->InfoLog, fmt, ap); 50801e04c3fSmrg va_end(ap); 50901e04c3fSmrg 51001e04c3fSmrg} 51101e04c3fSmrg 51201e04c3fSmrg 51301e04c3fSmrg/** 51401e04c3fSmrg * Given a string identifying a program resource, break it into a base name 51501e04c3fSmrg * and an optional array index in square brackets. 51601e04c3fSmrg * 51701e04c3fSmrg * If an array index is present, \c out_base_name_end is set to point to the 51801e04c3fSmrg * "[" that precedes the array index, and the array index itself is returned 51901e04c3fSmrg * as a long. 52001e04c3fSmrg * 52101e04c3fSmrg * If no array index is present (or if the array index is negative or 52201e04c3fSmrg * mal-formed), \c out_base_name_end, is set to point to the null terminator 52301e04c3fSmrg * at the end of the input string, and -1 is returned. 52401e04c3fSmrg * 52501e04c3fSmrg * Only the final array index is parsed; if the string contains other array 52601e04c3fSmrg * indices (or structure field accesses), they are left in the base name. 52701e04c3fSmrg * 52801e04c3fSmrg * No attempt is made to check that the base name is properly formed; 52901e04c3fSmrg * typically the caller will look up the base name in a hash table, so 53001e04c3fSmrg * ill-formed base names simply turn into hash table lookup failures. 53101e04c3fSmrg */ 53201e04c3fSmrglong 53301e04c3fSmrgparse_program_resource_name(const GLchar *name, 5347ec681f3Smrg const size_t len, 53501e04c3fSmrg const GLchar **out_base_name_end) 53601e04c3fSmrg{ 53701e04c3fSmrg /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says: 53801e04c3fSmrg * 53901e04c3fSmrg * "When an integer array element or block instance number is part of 54001e04c3fSmrg * the name string, it will be specified in decimal form without a "+" 54101e04c3fSmrg * or "-" sign or any extra leading zeroes. Additionally, the name 54201e04c3fSmrg * string will not include white space anywhere in the string." 54301e04c3fSmrg */ 54401e04c3fSmrg 54501e04c3fSmrg *out_base_name_end = name + len; 54601e04c3fSmrg 54701e04c3fSmrg if (len == 0 || name[len-1] != ']') 54801e04c3fSmrg return -1; 54901e04c3fSmrg 55001e04c3fSmrg /* Walk backwards over the string looking for a non-digit character. This 55101e04c3fSmrg * had better be the opening bracket for an array index. 55201e04c3fSmrg * 55301e04c3fSmrg * Initially, i specifies the location of the ']'. Since the string may 55401e04c3fSmrg * contain only the ']' charcater, walk backwards very carefully. 55501e04c3fSmrg */ 55601e04c3fSmrg unsigned i; 55701e04c3fSmrg for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i) 55801e04c3fSmrg /* empty */ ; 55901e04c3fSmrg 56001e04c3fSmrg if ((i == 0) || name[i-1] != '[') 56101e04c3fSmrg return -1; 56201e04c3fSmrg 56301e04c3fSmrg long array_index = strtol(&name[i], NULL, 10); 56401e04c3fSmrg if (array_index < 0) 56501e04c3fSmrg return -1; 56601e04c3fSmrg 56701e04c3fSmrg /* Check for leading zero */ 56801e04c3fSmrg if (name[i] == '0' && name[i+1] != ']') 56901e04c3fSmrg return -1; 57001e04c3fSmrg 57101e04c3fSmrg *out_base_name_end = name + (i - 1); 57201e04c3fSmrg return array_index; 57301e04c3fSmrg} 57401e04c3fSmrg 57501e04c3fSmrg 57601e04c3fSmrgvoid 57701e04c3fSmrglink_invalidate_variable_locations(exec_list *ir) 57801e04c3fSmrg{ 57901e04c3fSmrg foreach_in_list(ir_instruction, node, ir) { 58001e04c3fSmrg ir_variable *const var = node->as_variable(); 58101e04c3fSmrg 58201e04c3fSmrg if (var == NULL) 58301e04c3fSmrg continue; 58401e04c3fSmrg 58501e04c3fSmrg /* Only assign locations for variables that lack an explicit location. 58601e04c3fSmrg * Explicit locations are set for all built-in variables, generic vertex 58701e04c3fSmrg * shader inputs (via layout(location=...)), and generic fragment shader 58801e04c3fSmrg * outputs (also via layout(location=...)). 58901e04c3fSmrg */ 59001e04c3fSmrg if (!var->data.explicit_location) { 59101e04c3fSmrg var->data.location = -1; 59201e04c3fSmrg var->data.location_frac = 0; 59301e04c3fSmrg } 59401e04c3fSmrg 59501e04c3fSmrg /* ir_variable::is_unmatched_generic_inout is used by the linker while 59601e04c3fSmrg * connecting outputs from one stage to inputs of the next stage. 59701e04c3fSmrg */ 59801e04c3fSmrg if (var->data.explicit_location && 59901e04c3fSmrg var->data.location < VARYING_SLOT_VAR0) { 60001e04c3fSmrg var->data.is_unmatched_generic_inout = 0; 60101e04c3fSmrg } else { 60201e04c3fSmrg var->data.is_unmatched_generic_inout = 1; 60301e04c3fSmrg } 60401e04c3fSmrg } 60501e04c3fSmrg} 60601e04c3fSmrg 60701e04c3fSmrg 60801e04c3fSmrg/** 60901e04c3fSmrg * Set clip_distance_array_size based and cull_distance_array_size on the given 61001e04c3fSmrg * shader. 61101e04c3fSmrg * 61201e04c3fSmrg * Also check for errors based on incorrect usage of gl_ClipVertex and 61301e04c3fSmrg * gl_ClipDistance and gl_CullDistance. 61401e04c3fSmrg * Additionally test whether the arrays gl_ClipDistance and gl_CullDistance 61501e04c3fSmrg * exceed the maximum size defined by gl_MaxCombinedClipAndCullDistances. 61601e04c3fSmrg * 61701e04c3fSmrg * Return false if an error was reported. 61801e04c3fSmrg */ 61901e04c3fSmrgstatic void 62001e04c3fSmrganalyze_clip_cull_usage(struct gl_shader_program *prog, 62101e04c3fSmrg struct gl_linked_shader *shader, 62201e04c3fSmrg struct gl_context *ctx, 6237ec681f3Smrg struct shader_info *info) 62401e04c3fSmrg{ 6257ec681f3Smrg info->clip_distance_array_size = 0; 6267ec681f3Smrg info->cull_distance_array_size = 0; 62701e04c3fSmrg 62801e04c3fSmrg if (prog->data->Version >= (prog->IsES ? 300 : 130)) { 62901e04c3fSmrg /* From section 7.1 (Vertex Shader Special Variables) of the 63001e04c3fSmrg * GLSL 1.30 spec: 63101e04c3fSmrg * 63201e04c3fSmrg * "It is an error for a shader to statically write both 63301e04c3fSmrg * gl_ClipVertex and gl_ClipDistance." 63401e04c3fSmrg * 63501e04c3fSmrg * This does not apply to GLSL ES shaders, since GLSL ES defines neither 63601e04c3fSmrg * gl_ClipVertex nor gl_ClipDistance. However with 63701e04c3fSmrg * GL_EXT_clip_cull_distance, this functionality is exposed in ES 3.0. 63801e04c3fSmrg */ 63901e04c3fSmrg find_variable gl_ClipDistance("gl_ClipDistance"); 64001e04c3fSmrg find_variable gl_CullDistance("gl_CullDistance"); 64101e04c3fSmrg find_variable gl_ClipVertex("gl_ClipVertex"); 64201e04c3fSmrg find_variable * const variables[] = { 64301e04c3fSmrg &gl_ClipDistance, 64401e04c3fSmrg &gl_CullDistance, 64501e04c3fSmrg !prog->IsES ? &gl_ClipVertex : NULL, 64601e04c3fSmrg NULL 64701e04c3fSmrg }; 64801e04c3fSmrg find_assignments(shader->ir, variables); 64901e04c3fSmrg 65001e04c3fSmrg /* From the ARB_cull_distance spec: 65101e04c3fSmrg * 65201e04c3fSmrg * It is a compile-time or link-time error for the set of shaders forming 65301e04c3fSmrg * a program to statically read or write both gl_ClipVertex and either 65401e04c3fSmrg * gl_ClipDistance or gl_CullDistance. 65501e04c3fSmrg * 65601e04c3fSmrg * This does not apply to GLSL ES shaders, since GLSL ES doesn't define 65701e04c3fSmrg * gl_ClipVertex. 65801e04c3fSmrg */ 65901e04c3fSmrg if (!prog->IsES) { 66001e04c3fSmrg if (gl_ClipVertex.found && gl_ClipDistance.found) { 66101e04c3fSmrg linker_error(prog, "%s shader writes to both `gl_ClipVertex' " 66201e04c3fSmrg "and `gl_ClipDistance'\n", 66301e04c3fSmrg _mesa_shader_stage_to_string(shader->Stage)); 66401e04c3fSmrg return; 66501e04c3fSmrg } 66601e04c3fSmrg if (gl_ClipVertex.found && gl_CullDistance.found) { 66701e04c3fSmrg linker_error(prog, "%s shader writes to both `gl_ClipVertex' " 66801e04c3fSmrg "and `gl_CullDistance'\n", 66901e04c3fSmrg _mesa_shader_stage_to_string(shader->Stage)); 67001e04c3fSmrg return; 67101e04c3fSmrg } 67201e04c3fSmrg } 67301e04c3fSmrg 67401e04c3fSmrg if (gl_ClipDistance.found) { 67501e04c3fSmrg ir_variable *clip_distance_var = 67601e04c3fSmrg shader->symbols->get_variable("gl_ClipDistance"); 67701e04c3fSmrg assert(clip_distance_var); 6787ec681f3Smrg info->clip_distance_array_size = clip_distance_var->type->length; 67901e04c3fSmrg } 68001e04c3fSmrg if (gl_CullDistance.found) { 68101e04c3fSmrg ir_variable *cull_distance_var = 68201e04c3fSmrg shader->symbols->get_variable("gl_CullDistance"); 68301e04c3fSmrg assert(cull_distance_var); 6847ec681f3Smrg info->cull_distance_array_size = cull_distance_var->type->length; 68501e04c3fSmrg } 68601e04c3fSmrg /* From the ARB_cull_distance spec: 68701e04c3fSmrg * 68801e04c3fSmrg * It is a compile-time or link-time error for the set of shaders forming 68901e04c3fSmrg * a program to have the sum of the sizes of the gl_ClipDistance and 69001e04c3fSmrg * gl_CullDistance arrays to be larger than 69101e04c3fSmrg * gl_MaxCombinedClipAndCullDistances. 69201e04c3fSmrg */ 6937ec681f3Smrg if ((uint32_t)(info->clip_distance_array_size + info->cull_distance_array_size) > 69401e04c3fSmrg ctx->Const.MaxClipPlanes) { 69501e04c3fSmrg linker_error(prog, "%s shader: the combined size of " 69601e04c3fSmrg "'gl_ClipDistance' and 'gl_CullDistance' size cannot " 69701e04c3fSmrg "be larger than " 69801e04c3fSmrg "gl_MaxCombinedClipAndCullDistances (%u)", 69901e04c3fSmrg _mesa_shader_stage_to_string(shader->Stage), 70001e04c3fSmrg ctx->Const.MaxClipPlanes); 70101e04c3fSmrg } 70201e04c3fSmrg } 70301e04c3fSmrg} 70401e04c3fSmrg 70501e04c3fSmrg 70601e04c3fSmrg/** 70701e04c3fSmrg * Verify that a vertex shader executable meets all semantic requirements. 70801e04c3fSmrg * 70901e04c3fSmrg * Also sets info.clip_distance_array_size and 71001e04c3fSmrg * info.cull_distance_array_size as a side effect. 71101e04c3fSmrg * 71201e04c3fSmrg * \param shader Vertex shader executable to be verified 71301e04c3fSmrg */ 71401e04c3fSmrgstatic void 71501e04c3fSmrgvalidate_vertex_shader_executable(struct gl_shader_program *prog, 71601e04c3fSmrg struct gl_linked_shader *shader, 71701e04c3fSmrg struct gl_context *ctx) 71801e04c3fSmrg{ 71901e04c3fSmrg if (shader == NULL) 72001e04c3fSmrg return; 72101e04c3fSmrg 72201e04c3fSmrg /* From the GLSL 1.10 spec, page 48: 72301e04c3fSmrg * 72401e04c3fSmrg * "The variable gl_Position is available only in the vertex 72501e04c3fSmrg * language and is intended for writing the homogeneous vertex 72601e04c3fSmrg * position. All executions of a well-formed vertex shader 72701e04c3fSmrg * executable must write a value into this variable. [...] The 72801e04c3fSmrg * variable gl_Position is available only in the vertex 72901e04c3fSmrg * language and is intended for writing the homogeneous vertex 73001e04c3fSmrg * position. All executions of a well-formed vertex shader 73101e04c3fSmrg * executable must write a value into this variable." 73201e04c3fSmrg * 73301e04c3fSmrg * while in GLSL 1.40 this text is changed to: 73401e04c3fSmrg * 73501e04c3fSmrg * "The variable gl_Position is available only in the vertex 73601e04c3fSmrg * language and is intended for writing the homogeneous vertex 73701e04c3fSmrg * position. It can be written at any time during shader 73801e04c3fSmrg * execution. It may also be read back by a vertex shader 73901e04c3fSmrg * after being written. This value will be used by primitive 74001e04c3fSmrg * assembly, clipping, culling, and other fixed functionality 74101e04c3fSmrg * operations, if present, that operate on primitives after 74201e04c3fSmrg * vertex processing has occurred. Its value is undefined if 74301e04c3fSmrg * the vertex shader executable does not write gl_Position." 74401e04c3fSmrg * 74501e04c3fSmrg * All GLSL ES Versions are similar to GLSL 1.40--failing to write to 74601e04c3fSmrg * gl_Position is not an error. 74701e04c3fSmrg */ 74801e04c3fSmrg if (prog->data->Version < (prog->IsES ? 300 : 140)) { 74901e04c3fSmrg find_variable gl_Position("gl_Position"); 75001e04c3fSmrg find_assignments(shader->ir, &gl_Position); 75101e04c3fSmrg if (!gl_Position.found) { 75201e04c3fSmrg if (prog->IsES) { 75301e04c3fSmrg linker_warning(prog, 75401e04c3fSmrg "vertex shader does not write to `gl_Position'. " 75501e04c3fSmrg "Its value is undefined. \n"); 75601e04c3fSmrg } else { 75701e04c3fSmrg linker_error(prog, 75801e04c3fSmrg "vertex shader does not write to `gl_Position'. \n"); 75901e04c3fSmrg } 76001e04c3fSmrg return; 76101e04c3fSmrg } 76201e04c3fSmrg } 76301e04c3fSmrg 7647ec681f3Smrg analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); 76501e04c3fSmrg} 76601e04c3fSmrg 76701e04c3fSmrgstatic void 76801e04c3fSmrgvalidate_tess_eval_shader_executable(struct gl_shader_program *prog, 76901e04c3fSmrg struct gl_linked_shader *shader, 77001e04c3fSmrg struct gl_context *ctx) 77101e04c3fSmrg{ 77201e04c3fSmrg if (shader == NULL) 77301e04c3fSmrg return; 77401e04c3fSmrg 7757ec681f3Smrg analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); 77601e04c3fSmrg} 77701e04c3fSmrg 77801e04c3fSmrg 77901e04c3fSmrg/** 78001e04c3fSmrg * Verify that a fragment shader executable meets all semantic requirements 78101e04c3fSmrg * 78201e04c3fSmrg * \param shader Fragment shader executable to be verified 78301e04c3fSmrg */ 78401e04c3fSmrgstatic void 78501e04c3fSmrgvalidate_fragment_shader_executable(struct gl_shader_program *prog, 78601e04c3fSmrg struct gl_linked_shader *shader) 78701e04c3fSmrg{ 78801e04c3fSmrg if (shader == NULL) 78901e04c3fSmrg return; 79001e04c3fSmrg 79101e04c3fSmrg find_variable gl_FragColor("gl_FragColor"); 79201e04c3fSmrg find_variable gl_FragData("gl_FragData"); 79301e04c3fSmrg find_variable * const variables[] = { &gl_FragColor, &gl_FragData, NULL }; 79401e04c3fSmrg find_assignments(shader->ir, variables); 79501e04c3fSmrg 79601e04c3fSmrg if (gl_FragColor.found && gl_FragData.found) { 79701e04c3fSmrg linker_error(prog, "fragment shader writes to both " 79801e04c3fSmrg "`gl_FragColor' and `gl_FragData'\n"); 79901e04c3fSmrg } 80001e04c3fSmrg} 80101e04c3fSmrg 80201e04c3fSmrg/** 80301e04c3fSmrg * Verify that a geometry shader executable meets all semantic requirements 80401e04c3fSmrg * 80501e04c3fSmrg * Also sets prog->Geom.VerticesIn, and info.clip_distance_array_sizeand 80601e04c3fSmrg * info.cull_distance_array_size as a side effect. 80701e04c3fSmrg * 80801e04c3fSmrg * \param shader Geometry shader executable to be verified 80901e04c3fSmrg */ 81001e04c3fSmrgstatic void 81101e04c3fSmrgvalidate_geometry_shader_executable(struct gl_shader_program *prog, 81201e04c3fSmrg struct gl_linked_shader *shader, 81301e04c3fSmrg struct gl_context *ctx) 81401e04c3fSmrg{ 81501e04c3fSmrg if (shader == NULL) 81601e04c3fSmrg return; 81701e04c3fSmrg 81801e04c3fSmrg unsigned num_vertices = 81901e04c3fSmrg vertices_per_prim(shader->Program->info.gs.input_primitive); 82001e04c3fSmrg prog->Geom.VerticesIn = num_vertices; 82101e04c3fSmrg 8227ec681f3Smrg analyze_clip_cull_usage(prog, shader, ctx, &shader->Program->info); 82301e04c3fSmrg} 82401e04c3fSmrg 82501e04c3fSmrg/** 82601e04c3fSmrg * Check if geometry shaders emit to non-zero streams and do corresponding 82701e04c3fSmrg * validations. 82801e04c3fSmrg */ 82901e04c3fSmrgstatic void 83001e04c3fSmrgvalidate_geometry_shader_emissions(struct gl_context *ctx, 83101e04c3fSmrg struct gl_shader_program *prog) 83201e04c3fSmrg{ 83301e04c3fSmrg struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_GEOMETRY]; 83401e04c3fSmrg 83501e04c3fSmrg if (sh != NULL) { 83601e04c3fSmrg find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1); 83701e04c3fSmrg emit_vertex.run(sh->ir); 83801e04c3fSmrg if (emit_vertex.error()) { 83901e04c3fSmrg linker_error(prog, "Invalid call %s(%d). Accepted values for the " 84001e04c3fSmrg "stream parameter are in the range [0, %d].\n", 84101e04c3fSmrg emit_vertex.error_func(), 84201e04c3fSmrg emit_vertex.error_stream(), 84301e04c3fSmrg ctx->Const.MaxVertexStreams - 1); 84401e04c3fSmrg } 8457ec681f3Smrg prog->Geom.ActiveStreamMask = emit_vertex.active_stream_mask(); 84601e04c3fSmrg prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive(); 84701e04c3fSmrg 84801e04c3fSmrg /* From the ARB_gpu_shader5 spec: 84901e04c3fSmrg * 85001e04c3fSmrg * "Multiple vertex streams are supported only if the output primitive 85101e04c3fSmrg * type is declared to be "points". A program will fail to link if it 85201e04c3fSmrg * contains a geometry shader calling EmitStreamVertex() or 85301e04c3fSmrg * EndStreamPrimitive() if its output primitive type is not "points". 85401e04c3fSmrg * 85501e04c3fSmrg * However, in the same spec: 85601e04c3fSmrg * 85701e04c3fSmrg * "The function EmitVertex() is equivalent to calling EmitStreamVertex() 85801e04c3fSmrg * with <stream> set to zero." 85901e04c3fSmrg * 86001e04c3fSmrg * And: 86101e04c3fSmrg * 86201e04c3fSmrg * "The function EndPrimitive() is equivalent to calling 86301e04c3fSmrg * EndStreamPrimitive() with <stream> set to zero." 86401e04c3fSmrg * 86501e04c3fSmrg * Since we can call EmitVertex() and EndPrimitive() when we output 86601e04c3fSmrg * primitives other than points, calling EmitStreamVertex(0) or 86701e04c3fSmrg * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia 8687ec681f3Smrg * does. We can use prog->Geom.ActiveStreamMask to check whether only the 8697ec681f3Smrg * first (zero) stream is active. 87001e04c3fSmrg * stream. 87101e04c3fSmrg */ 8727ec681f3Smrg if (prog->Geom.ActiveStreamMask & ~(1 << 0) && 87301e04c3fSmrg sh->Program->info.gs.output_primitive != GL_POINTS) { 87401e04c3fSmrg linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) " 87501e04c3fSmrg "with n>0 requires point output\n"); 87601e04c3fSmrg } 87701e04c3fSmrg } 87801e04c3fSmrg} 87901e04c3fSmrg 88001e04c3fSmrgbool 88101e04c3fSmrgvalidate_intrastage_arrays(struct gl_shader_program *prog, 88201e04c3fSmrg ir_variable *const var, 8837ec681f3Smrg ir_variable *const existing, 8847ec681f3Smrg bool match_precision) 88501e04c3fSmrg{ 88601e04c3fSmrg /* Consider the types to be "the same" if both types are arrays 88701e04c3fSmrg * of the same type and one of the arrays is implicitly sized. 88801e04c3fSmrg * In addition, set the type of the linked variable to the 88901e04c3fSmrg * explicitly sized array. 89001e04c3fSmrg */ 89101e04c3fSmrg if (var->type->is_array() && existing->type->is_array()) { 8927ec681f3Smrg const glsl_type *no_array_var = var->type->fields.array; 8937ec681f3Smrg const glsl_type *no_array_existing = existing->type->fields.array; 8947ec681f3Smrg bool type_matches; 8957ec681f3Smrg 8967ec681f3Smrg type_matches = (match_precision ? 8977ec681f3Smrg no_array_var == no_array_existing : 8987ec681f3Smrg no_array_var->compare_no_precision(no_array_existing)); 8997ec681f3Smrg 9007ec681f3Smrg if (type_matches && 90101e04c3fSmrg ((var->type->length == 0)|| (existing->type->length == 0))) { 90201e04c3fSmrg if (var->type->length != 0) { 90301e04c3fSmrg if ((int)var->type->length <= existing->data.max_array_access) { 90401e04c3fSmrg linker_error(prog, "%s `%s' declared as type " 90501e04c3fSmrg "`%s' but outermost dimension has an index" 90601e04c3fSmrg " of `%i'\n", 90701e04c3fSmrg mode_string(var), 90801e04c3fSmrg var->name, var->type->name, 90901e04c3fSmrg existing->data.max_array_access); 91001e04c3fSmrg } 91101e04c3fSmrg existing->type = var->type; 91201e04c3fSmrg return true; 91301e04c3fSmrg } else if (existing->type->length != 0) { 91401e04c3fSmrg if((int)existing->type->length <= var->data.max_array_access && 91501e04c3fSmrg !existing->data.from_ssbo_unsized_array) { 91601e04c3fSmrg linker_error(prog, "%s `%s' declared as type " 91701e04c3fSmrg "`%s' but outermost dimension has an index" 91801e04c3fSmrg " of `%i'\n", 91901e04c3fSmrg mode_string(var), 92001e04c3fSmrg var->name, existing->type->name, 92101e04c3fSmrg var->data.max_array_access); 92201e04c3fSmrg } 92301e04c3fSmrg return true; 92401e04c3fSmrg } 92501e04c3fSmrg } 92601e04c3fSmrg } 92701e04c3fSmrg return false; 92801e04c3fSmrg} 92901e04c3fSmrg 93001e04c3fSmrg 93101e04c3fSmrg/** 93201e04c3fSmrg * Perform validation of global variables used across multiple shaders 93301e04c3fSmrg */ 93401e04c3fSmrgstatic void 93501e04c3fSmrgcross_validate_globals(struct gl_context *ctx, struct gl_shader_program *prog, 93601e04c3fSmrg struct exec_list *ir, glsl_symbol_table *variables, 93701e04c3fSmrg bool uniforms_only) 93801e04c3fSmrg{ 93901e04c3fSmrg foreach_in_list(ir_instruction, node, ir) { 94001e04c3fSmrg ir_variable *const var = node->as_variable(); 94101e04c3fSmrg 94201e04c3fSmrg if (var == NULL) 94301e04c3fSmrg continue; 94401e04c3fSmrg 94501e04c3fSmrg if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage)) 94601e04c3fSmrg continue; 94701e04c3fSmrg 94801e04c3fSmrg /* don't cross validate subroutine uniforms */ 94901e04c3fSmrg if (var->type->contains_subroutine()) 95001e04c3fSmrg continue; 95101e04c3fSmrg 95201e04c3fSmrg /* Don't cross validate interface instances. These are only relevant 95301e04c3fSmrg * inside a shader. The cross validation is done at the Interface Block 95401e04c3fSmrg * name level. 95501e04c3fSmrg */ 95601e04c3fSmrg if (var->is_interface_instance()) 95701e04c3fSmrg continue; 95801e04c3fSmrg 95901e04c3fSmrg /* Don't cross validate temporaries that are at global scope. These 96001e04c3fSmrg * will eventually get pulled into the shaders 'main'. 96101e04c3fSmrg */ 96201e04c3fSmrg if (var->data.mode == ir_var_temporary) 96301e04c3fSmrg continue; 96401e04c3fSmrg 96501e04c3fSmrg /* If a global with this name has already been seen, verify that the 96601e04c3fSmrg * new instance has the same type. In addition, if the globals have 96701e04c3fSmrg * initializers, the values of the initializers must be the same. 96801e04c3fSmrg */ 96901e04c3fSmrg ir_variable *const existing = variables->get_variable(var->name); 97001e04c3fSmrg if (existing != NULL) { 97101e04c3fSmrg /* Check if types match. */ 97201e04c3fSmrg if (var->type != existing->type) { 97301e04c3fSmrg if (!validate_intrastage_arrays(prog, var, existing)) { 97401e04c3fSmrg /* If it is an unsized array in a Shader Storage Block, 97501e04c3fSmrg * two different shaders can access to different elements. 97601e04c3fSmrg * Because of that, they might be converted to different 97701e04c3fSmrg * sized arrays, then check that they are compatible but 97801e04c3fSmrg * ignore the array size. 97901e04c3fSmrg */ 98001e04c3fSmrg if (!(var->data.mode == ir_var_shader_storage && 98101e04c3fSmrg var->data.from_ssbo_unsized_array && 98201e04c3fSmrg existing->data.mode == ir_var_shader_storage && 98301e04c3fSmrg existing->data.from_ssbo_unsized_array && 98401e04c3fSmrg var->type->gl_type == existing->type->gl_type)) { 98501e04c3fSmrg linker_error(prog, "%s `%s' declared as type " 98601e04c3fSmrg "`%s' and type `%s'\n", 98701e04c3fSmrg mode_string(var), 98801e04c3fSmrg var->name, var->type->name, 98901e04c3fSmrg existing->type->name); 99001e04c3fSmrg return; 99101e04c3fSmrg } 99201e04c3fSmrg } 99301e04c3fSmrg } 99401e04c3fSmrg 99501e04c3fSmrg if (var->data.explicit_location) { 99601e04c3fSmrg if (existing->data.explicit_location 99701e04c3fSmrg && (var->data.location != existing->data.location)) { 99801e04c3fSmrg linker_error(prog, "explicit locations for %s " 99901e04c3fSmrg "`%s' have differing values\n", 100001e04c3fSmrg mode_string(var), var->name); 100101e04c3fSmrg return; 100201e04c3fSmrg } 100301e04c3fSmrg 100401e04c3fSmrg if (var->data.location_frac != existing->data.location_frac) { 100501e04c3fSmrg linker_error(prog, "explicit components for %s `%s' have " 100601e04c3fSmrg "differing values\n", mode_string(var), var->name); 100701e04c3fSmrg return; 100801e04c3fSmrg } 100901e04c3fSmrg 101001e04c3fSmrg existing->data.location = var->data.location; 101101e04c3fSmrg existing->data.explicit_location = true; 101201e04c3fSmrg } else { 101301e04c3fSmrg /* Check if uniform with implicit location was marked explicit 101401e04c3fSmrg * by earlier shader stage. If so, mark it explicit in this stage 101501e04c3fSmrg * too to make sure later processing does not treat it as 101601e04c3fSmrg * implicit one. 101701e04c3fSmrg */ 101801e04c3fSmrg if (existing->data.explicit_location) { 101901e04c3fSmrg var->data.location = existing->data.location; 102001e04c3fSmrg var->data.explicit_location = true; 102101e04c3fSmrg } 102201e04c3fSmrg } 102301e04c3fSmrg 102401e04c3fSmrg /* From the GLSL 4.20 specification: 102501e04c3fSmrg * "A link error will result if two compilation units in a program 102601e04c3fSmrg * specify different integer-constant bindings for the same 102701e04c3fSmrg * opaque-uniform name. However, it is not an error to specify a 102801e04c3fSmrg * binding on some but not all declarations for the same name" 102901e04c3fSmrg */ 103001e04c3fSmrg if (var->data.explicit_binding) { 103101e04c3fSmrg if (existing->data.explicit_binding && 103201e04c3fSmrg var->data.binding != existing->data.binding) { 103301e04c3fSmrg linker_error(prog, "explicit bindings for %s " 103401e04c3fSmrg "`%s' have differing values\n", 103501e04c3fSmrg mode_string(var), var->name); 103601e04c3fSmrg return; 103701e04c3fSmrg } 103801e04c3fSmrg 103901e04c3fSmrg existing->data.binding = var->data.binding; 104001e04c3fSmrg existing->data.explicit_binding = true; 104101e04c3fSmrg } 104201e04c3fSmrg 104301e04c3fSmrg if (var->type->contains_atomic() && 104401e04c3fSmrg var->data.offset != existing->data.offset) { 104501e04c3fSmrg linker_error(prog, "offset specifications for %s " 104601e04c3fSmrg "`%s' have differing values\n", 104701e04c3fSmrg mode_string(var), var->name); 104801e04c3fSmrg return; 104901e04c3fSmrg } 105001e04c3fSmrg 105101e04c3fSmrg /* Validate layout qualifiers for gl_FragDepth. 105201e04c3fSmrg * 105301e04c3fSmrg * From the AMD/ARB_conservative_depth specs: 105401e04c3fSmrg * 105501e04c3fSmrg * "If gl_FragDepth is redeclared in any fragment shader in a 105601e04c3fSmrg * program, it must be redeclared in all fragment shaders in 105701e04c3fSmrg * that program that have static assignments to 105801e04c3fSmrg * gl_FragDepth. All redeclarations of gl_FragDepth in all 105901e04c3fSmrg * fragment shaders in a single program must have the same set 106001e04c3fSmrg * of qualifiers." 106101e04c3fSmrg */ 106201e04c3fSmrg if (strcmp(var->name, "gl_FragDepth") == 0) { 106301e04c3fSmrg bool layout_declared = var->data.depth_layout != ir_depth_layout_none; 106401e04c3fSmrg bool layout_differs = 106501e04c3fSmrg var->data.depth_layout != existing->data.depth_layout; 106601e04c3fSmrg 106701e04c3fSmrg if (layout_declared && layout_differs) { 106801e04c3fSmrg linker_error(prog, 106901e04c3fSmrg "All redeclarations of gl_FragDepth in all " 107001e04c3fSmrg "fragment shaders in a single program must have " 107101e04c3fSmrg "the same set of qualifiers.\n"); 107201e04c3fSmrg } 107301e04c3fSmrg 107401e04c3fSmrg if (var->data.used && layout_differs) { 107501e04c3fSmrg linker_error(prog, 107601e04c3fSmrg "If gl_FragDepth is redeclared with a layout " 107701e04c3fSmrg "qualifier in any fragment shader, it must be " 107801e04c3fSmrg "redeclared with the same layout qualifier in " 107901e04c3fSmrg "all fragment shaders that have assignments to " 108001e04c3fSmrg "gl_FragDepth\n"); 108101e04c3fSmrg } 108201e04c3fSmrg } 108301e04c3fSmrg 108401e04c3fSmrg /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says: 108501e04c3fSmrg * 108601e04c3fSmrg * "If a shared global has multiple initializers, the 108701e04c3fSmrg * initializers must all be constant expressions, and they 108801e04c3fSmrg * must all have the same value. Otherwise, a link error will 108901e04c3fSmrg * result. (A shared global having only one initializer does 109001e04c3fSmrg * not require that initializer to be a constant expression.)" 109101e04c3fSmrg * 109201e04c3fSmrg * Previous to 4.20 the GLSL spec simply said that initializers 109301e04c3fSmrg * must have the same value. In this case of non-constant 109401e04c3fSmrg * initializers, this was impossible to determine. As a result, 109501e04c3fSmrg * no vendor actually implemented that behavior. The 4.20 109601e04c3fSmrg * behavior matches the implemented behavior of at least one other 109701e04c3fSmrg * vendor, so we'll implement that for all GLSL versions. 10987ec681f3Smrg * If (at least) one of these constant expressions is implicit, 10997ec681f3Smrg * because it was added by glsl_zero_init, we skip the verification. 110001e04c3fSmrg */ 110101e04c3fSmrg if (var->constant_initializer != NULL) { 11027ec681f3Smrg if (existing->constant_initializer != NULL && 11037ec681f3Smrg !existing->data.is_implicit_initializer && 11047ec681f3Smrg !var->data.is_implicit_initializer) { 110501e04c3fSmrg if (!var->constant_initializer->has_value(existing->constant_initializer)) { 110601e04c3fSmrg linker_error(prog, "initializers for %s " 110701e04c3fSmrg "`%s' have differing values\n", 110801e04c3fSmrg mode_string(var), var->name); 110901e04c3fSmrg return; 111001e04c3fSmrg } 111101e04c3fSmrg } else { 111201e04c3fSmrg /* If the first-seen instance of a particular uniform did 111301e04c3fSmrg * not have an initializer but a later instance does, 111401e04c3fSmrg * replace the former with the later. 111501e04c3fSmrg */ 11167ec681f3Smrg if (!var->data.is_implicit_initializer) 11177ec681f3Smrg variables->replace_variable(existing->name, var); 111801e04c3fSmrg } 111901e04c3fSmrg } 112001e04c3fSmrg 112101e04c3fSmrg if (var->data.has_initializer) { 112201e04c3fSmrg if (existing->data.has_initializer 112301e04c3fSmrg && (var->constant_initializer == NULL 112401e04c3fSmrg || existing->constant_initializer == NULL)) { 112501e04c3fSmrg linker_error(prog, 112601e04c3fSmrg "shared global variable `%s' has multiple " 112701e04c3fSmrg "non-constant initializers.\n", 112801e04c3fSmrg var->name); 112901e04c3fSmrg return; 113001e04c3fSmrg } 113101e04c3fSmrg } 113201e04c3fSmrg 1133993e1d59Smrg if (existing->data.explicit_invariant != var->data.explicit_invariant) { 113401e04c3fSmrg linker_error(prog, "declarations for %s `%s' have " 113501e04c3fSmrg "mismatching invariant qualifiers\n", 113601e04c3fSmrg mode_string(var), var->name); 113701e04c3fSmrg return; 113801e04c3fSmrg } 113901e04c3fSmrg if (existing->data.centroid != var->data.centroid) { 114001e04c3fSmrg linker_error(prog, "declarations for %s `%s' have " 114101e04c3fSmrg "mismatching centroid qualifiers\n", 114201e04c3fSmrg mode_string(var), var->name); 114301e04c3fSmrg return; 114401e04c3fSmrg } 114501e04c3fSmrg if (existing->data.sample != var->data.sample) { 114601e04c3fSmrg linker_error(prog, "declarations for %s `%s` have " 114701e04c3fSmrg "mismatching sample qualifiers\n", 114801e04c3fSmrg mode_string(var), var->name); 114901e04c3fSmrg return; 115001e04c3fSmrg } 115101e04c3fSmrg if (existing->data.image_format != var->data.image_format) { 115201e04c3fSmrg linker_error(prog, "declarations for %s `%s` have " 115301e04c3fSmrg "mismatching image format qualifiers\n", 115401e04c3fSmrg mode_string(var), var->name); 115501e04c3fSmrg return; 115601e04c3fSmrg } 115701e04c3fSmrg 115801e04c3fSmrg /* Check the precision qualifier matches for uniform variables on 115901e04c3fSmrg * GLSL ES. 116001e04c3fSmrg */ 116101e04c3fSmrg if (!ctx->Const.AllowGLSLRelaxedES && 116201e04c3fSmrg prog->IsES && !var->get_interface_type() && 116301e04c3fSmrg existing->data.precision != var->data.precision) { 116401e04c3fSmrg if ((existing->data.used && var->data.used) || prog->data->Version >= 300) { 116501e04c3fSmrg linker_error(prog, "declarations for %s `%s` have " 116601e04c3fSmrg "mismatching precision qualifiers\n", 116701e04c3fSmrg mode_string(var), var->name); 116801e04c3fSmrg return; 116901e04c3fSmrg } else { 117001e04c3fSmrg linker_warning(prog, "declarations for %s `%s` have " 117101e04c3fSmrg "mismatching precision qualifiers\n", 117201e04c3fSmrg mode_string(var), var->name); 117301e04c3fSmrg } 117401e04c3fSmrg } 117501e04c3fSmrg 117601e04c3fSmrg /* In OpenGL GLSL 3.20 spec, section 4.3.9: 117701e04c3fSmrg * 117801e04c3fSmrg * "It is a link-time error if any particular shader interface 117901e04c3fSmrg * contains: 118001e04c3fSmrg * 118101e04c3fSmrg * - two different blocks, each having no instance name, and each 118201e04c3fSmrg * having a member of the same name, or 118301e04c3fSmrg * 118401e04c3fSmrg * - a variable outside a block, and a block with no instance name, 118501e04c3fSmrg * where the variable has the same name as a member in the block." 118601e04c3fSmrg */ 118701e04c3fSmrg const glsl_type *var_itype = var->get_interface_type(); 118801e04c3fSmrg const glsl_type *existing_itype = existing->get_interface_type(); 118901e04c3fSmrg if (var_itype != existing_itype) { 119001e04c3fSmrg if (!var_itype || !existing_itype) { 119101e04c3fSmrg linker_error(prog, "declarations for %s `%s` are inside block " 119201e04c3fSmrg "`%s` and outside a block", 119301e04c3fSmrg mode_string(var), var->name, 119401e04c3fSmrg var_itype ? var_itype->name : existing_itype->name); 119501e04c3fSmrg return; 119601e04c3fSmrg } else if (strcmp(var_itype->name, existing_itype->name) != 0) { 119701e04c3fSmrg linker_error(prog, "declarations for %s `%s` are inside blocks " 119801e04c3fSmrg "`%s` and `%s`", 119901e04c3fSmrg mode_string(var), var->name, 120001e04c3fSmrg existing_itype->name, 120101e04c3fSmrg var_itype->name); 120201e04c3fSmrg return; 120301e04c3fSmrg } 120401e04c3fSmrg } 120501e04c3fSmrg } else 120601e04c3fSmrg variables->add_variable(var); 120701e04c3fSmrg } 120801e04c3fSmrg} 120901e04c3fSmrg 121001e04c3fSmrg 121101e04c3fSmrg/** 121201e04c3fSmrg * Perform validation of uniforms used across multiple shader stages 121301e04c3fSmrg */ 121401e04c3fSmrgstatic void 121501e04c3fSmrgcross_validate_uniforms(struct gl_context *ctx, 121601e04c3fSmrg struct gl_shader_program *prog) 121701e04c3fSmrg{ 121801e04c3fSmrg glsl_symbol_table variables; 121901e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 122001e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 122101e04c3fSmrg continue; 122201e04c3fSmrg 122301e04c3fSmrg cross_validate_globals(ctx, prog, prog->_LinkedShaders[i]->ir, 122401e04c3fSmrg &variables, true); 122501e04c3fSmrg } 122601e04c3fSmrg} 122701e04c3fSmrg 122801e04c3fSmrg/** 122901e04c3fSmrg * Accumulates the array of buffer blocks and checks that all definitions of 123001e04c3fSmrg * blocks agree on their contents. 123101e04c3fSmrg */ 123201e04c3fSmrgstatic bool 123301e04c3fSmrginterstage_cross_validate_uniform_blocks(struct gl_shader_program *prog, 123401e04c3fSmrg bool validate_ssbo) 123501e04c3fSmrg{ 12367ec681f3Smrg int *ifc_blk_stage_idx[MESA_SHADER_STAGES]; 123701e04c3fSmrg struct gl_uniform_block *blks = NULL; 123801e04c3fSmrg unsigned *num_blks = validate_ssbo ? &prog->data->NumShaderStorageBlocks : 123901e04c3fSmrg &prog->data->NumUniformBlocks; 124001e04c3fSmrg 124101e04c3fSmrg unsigned max_num_buffer_blocks = 0; 124201e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 124301e04c3fSmrg if (prog->_LinkedShaders[i]) { 124401e04c3fSmrg if (validate_ssbo) { 124501e04c3fSmrg max_num_buffer_blocks += 124601e04c3fSmrg prog->_LinkedShaders[i]->Program->info.num_ssbos; 124701e04c3fSmrg } else { 124801e04c3fSmrg max_num_buffer_blocks += 124901e04c3fSmrg prog->_LinkedShaders[i]->Program->info.num_ubos; 125001e04c3fSmrg } 125101e04c3fSmrg } 125201e04c3fSmrg } 125301e04c3fSmrg 125401e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 125501e04c3fSmrg struct gl_linked_shader *sh = prog->_LinkedShaders[i]; 125601e04c3fSmrg 12577ec681f3Smrg ifc_blk_stage_idx[i] = 12587ec681f3Smrg (int *) malloc(sizeof(int) * max_num_buffer_blocks); 125901e04c3fSmrg for (unsigned int j = 0; j < max_num_buffer_blocks; j++) 12607ec681f3Smrg ifc_blk_stage_idx[i][j] = -1; 126101e04c3fSmrg 126201e04c3fSmrg if (sh == NULL) 126301e04c3fSmrg continue; 126401e04c3fSmrg 126501e04c3fSmrg unsigned sh_num_blocks; 126601e04c3fSmrg struct gl_uniform_block **sh_blks; 126701e04c3fSmrg if (validate_ssbo) { 126801e04c3fSmrg sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ssbos; 126901e04c3fSmrg sh_blks = sh->Program->sh.ShaderStorageBlocks; 127001e04c3fSmrg } else { 127101e04c3fSmrg sh_num_blocks = prog->_LinkedShaders[i]->Program->info.num_ubos; 127201e04c3fSmrg sh_blks = sh->Program->sh.UniformBlocks; 127301e04c3fSmrg } 127401e04c3fSmrg 127501e04c3fSmrg for (unsigned int j = 0; j < sh_num_blocks; j++) { 127601e04c3fSmrg int index = link_cross_validate_uniform_block(prog->data, &blks, 127701e04c3fSmrg num_blks, sh_blks[j]); 127801e04c3fSmrg 127901e04c3fSmrg if (index == -1) { 128001e04c3fSmrg linker_error(prog, "buffer block `%s' has mismatching " 128101e04c3fSmrg "definitions\n", sh_blks[j]->Name); 128201e04c3fSmrg 128301e04c3fSmrg for (unsigned k = 0; k <= i; k++) { 12847ec681f3Smrg free(ifc_blk_stage_idx[k]); 128501e04c3fSmrg } 128601e04c3fSmrg 128701e04c3fSmrg /* Reset the block count. This will help avoid various segfaults 128801e04c3fSmrg * from api calls that assume the array exists due to the count 128901e04c3fSmrg * being non-zero. 129001e04c3fSmrg */ 129101e04c3fSmrg *num_blks = 0; 129201e04c3fSmrg return false; 129301e04c3fSmrg } 129401e04c3fSmrg 12957ec681f3Smrg ifc_blk_stage_idx[i][index] = j; 129601e04c3fSmrg } 129701e04c3fSmrg } 129801e04c3fSmrg 129901e04c3fSmrg /* Update per stage block pointers to point to the program list. 130001e04c3fSmrg * FIXME: We should be able to free the per stage blocks here. 130101e04c3fSmrg */ 130201e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 130301e04c3fSmrg for (unsigned j = 0; j < *num_blks; j++) { 13047ec681f3Smrg int stage_index = ifc_blk_stage_idx[i][j]; 130501e04c3fSmrg 130601e04c3fSmrg if (stage_index != -1) { 130701e04c3fSmrg struct gl_linked_shader *sh = prog->_LinkedShaders[i]; 130801e04c3fSmrg 130901e04c3fSmrg struct gl_uniform_block **sh_blks = validate_ssbo ? 131001e04c3fSmrg sh->Program->sh.ShaderStorageBlocks : 131101e04c3fSmrg sh->Program->sh.UniformBlocks; 131201e04c3fSmrg 131301e04c3fSmrg blks[j].stageref |= sh_blks[stage_index]->stageref; 131401e04c3fSmrg sh_blks[stage_index] = &blks[j]; 131501e04c3fSmrg } 131601e04c3fSmrg } 131701e04c3fSmrg } 131801e04c3fSmrg 131901e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 13207ec681f3Smrg free(ifc_blk_stage_idx[i]); 132101e04c3fSmrg } 132201e04c3fSmrg 132301e04c3fSmrg if (validate_ssbo) 132401e04c3fSmrg prog->data->ShaderStorageBlocks = blks; 132501e04c3fSmrg else 132601e04c3fSmrg prog->data->UniformBlocks = blks; 132701e04c3fSmrg 132801e04c3fSmrg return true; 132901e04c3fSmrg} 133001e04c3fSmrg 133101e04c3fSmrg/** 133201e04c3fSmrg * Verifies the invariance of built-in special variables. 133301e04c3fSmrg */ 133401e04c3fSmrgstatic bool 133501e04c3fSmrgvalidate_invariant_builtins(struct gl_shader_program *prog, 133601e04c3fSmrg const gl_linked_shader *vert, 133701e04c3fSmrg const gl_linked_shader *frag) 133801e04c3fSmrg{ 133901e04c3fSmrg const ir_variable *var_vert; 134001e04c3fSmrg const ir_variable *var_frag; 134101e04c3fSmrg 134201e04c3fSmrg if (!vert || !frag) 134301e04c3fSmrg return true; 134401e04c3fSmrg 134501e04c3fSmrg /* 134601e04c3fSmrg * From OpenGL ES Shading Language 1.0 specification 134701e04c3fSmrg * (4.6.4 Invariance and Linkage): 134801e04c3fSmrg * "The invariance of varyings that are declared in both the vertex and 134901e04c3fSmrg * fragment shaders must match. For the built-in special variables, 135001e04c3fSmrg * gl_FragCoord can only be declared invariant if and only if 135101e04c3fSmrg * gl_Position is declared invariant. Similarly gl_PointCoord can only 135201e04c3fSmrg * be declared invariant if and only if gl_PointSize is declared 135301e04c3fSmrg * invariant. It is an error to declare gl_FrontFacing as invariant. 135401e04c3fSmrg * The invariance of gl_FrontFacing is the same as the invariance of 135501e04c3fSmrg * gl_Position." 135601e04c3fSmrg */ 135701e04c3fSmrg var_frag = frag->symbols->get_variable("gl_FragCoord"); 135801e04c3fSmrg if (var_frag && var_frag->data.invariant) { 135901e04c3fSmrg var_vert = vert->symbols->get_variable("gl_Position"); 136001e04c3fSmrg if (var_vert && !var_vert->data.invariant) { 136101e04c3fSmrg linker_error(prog, 136201e04c3fSmrg "fragment shader built-in `%s' has invariant qualifier, " 136301e04c3fSmrg "but vertex shader built-in `%s' lacks invariant qualifier\n", 136401e04c3fSmrg var_frag->name, var_vert->name); 136501e04c3fSmrg return false; 136601e04c3fSmrg } 136701e04c3fSmrg } 136801e04c3fSmrg 136901e04c3fSmrg var_frag = frag->symbols->get_variable("gl_PointCoord"); 137001e04c3fSmrg if (var_frag && var_frag->data.invariant) { 137101e04c3fSmrg var_vert = vert->symbols->get_variable("gl_PointSize"); 137201e04c3fSmrg if (var_vert && !var_vert->data.invariant) { 137301e04c3fSmrg linker_error(prog, 137401e04c3fSmrg "fragment shader built-in `%s' has invariant qualifier, " 137501e04c3fSmrg "but vertex shader built-in `%s' lacks invariant qualifier\n", 137601e04c3fSmrg var_frag->name, var_vert->name); 137701e04c3fSmrg return false; 137801e04c3fSmrg } 137901e04c3fSmrg } 138001e04c3fSmrg 138101e04c3fSmrg var_frag = frag->symbols->get_variable("gl_FrontFacing"); 138201e04c3fSmrg if (var_frag && var_frag->data.invariant) { 138301e04c3fSmrg linker_error(prog, 138401e04c3fSmrg "fragment shader built-in `%s' can not be declared as invariant\n", 138501e04c3fSmrg var_frag->name); 138601e04c3fSmrg return false; 138701e04c3fSmrg } 138801e04c3fSmrg 138901e04c3fSmrg return true; 139001e04c3fSmrg} 139101e04c3fSmrg 139201e04c3fSmrg/** 139301e04c3fSmrg * Populates a shaders symbol table with all global declarations 139401e04c3fSmrg */ 139501e04c3fSmrgstatic void 139601e04c3fSmrgpopulate_symbol_table(gl_linked_shader *sh, glsl_symbol_table *symbols) 139701e04c3fSmrg{ 139801e04c3fSmrg sh->symbols = new(sh) glsl_symbol_table; 139901e04c3fSmrg 140001e04c3fSmrg _mesa_glsl_copy_symbols_from_table(sh->ir, symbols, sh->symbols); 140101e04c3fSmrg} 140201e04c3fSmrg 140301e04c3fSmrg 140401e04c3fSmrg/** 140501e04c3fSmrg * Remap variables referenced in an instruction tree 140601e04c3fSmrg * 140701e04c3fSmrg * This is used when instruction trees are cloned from one shader and placed in 140801e04c3fSmrg * another. These trees will contain references to \c ir_variable nodes that 140901e04c3fSmrg * do not exist in the target shader. This function finds these \c ir_variable 141001e04c3fSmrg * references and replaces the references with matching variables in the target 141101e04c3fSmrg * shader. 141201e04c3fSmrg * 141301e04c3fSmrg * If there is no matching variable in the target shader, a clone of the 141401e04c3fSmrg * \c ir_variable is made and added to the target shader. The new variable is 141501e04c3fSmrg * added to \b both the instruction stream and the symbol table. 141601e04c3fSmrg * 141701e04c3fSmrg * \param inst IR tree that is to be processed. 141801e04c3fSmrg * \param symbols Symbol table containing global scope symbols in the 141901e04c3fSmrg * linked shader. 142001e04c3fSmrg * \param instructions Instruction stream where new variable declarations 142101e04c3fSmrg * should be added. 142201e04c3fSmrg */ 142301e04c3fSmrgstatic void 142401e04c3fSmrgremap_variables(ir_instruction *inst, struct gl_linked_shader *target, 142501e04c3fSmrg hash_table *temps) 142601e04c3fSmrg{ 142701e04c3fSmrg class remap_visitor : public ir_hierarchical_visitor { 142801e04c3fSmrg public: 142901e04c3fSmrg remap_visitor(struct gl_linked_shader *target, hash_table *temps) 143001e04c3fSmrg { 143101e04c3fSmrg this->target = target; 143201e04c3fSmrg this->symbols = target->symbols; 143301e04c3fSmrg this->instructions = target->ir; 143401e04c3fSmrg this->temps = temps; 143501e04c3fSmrg } 143601e04c3fSmrg 143701e04c3fSmrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 143801e04c3fSmrg { 143901e04c3fSmrg if (ir->var->data.mode == ir_var_temporary) { 144001e04c3fSmrg hash_entry *entry = _mesa_hash_table_search(temps, ir->var); 144101e04c3fSmrg ir_variable *var = entry ? (ir_variable *) entry->data : NULL; 144201e04c3fSmrg 144301e04c3fSmrg assert(var != NULL); 144401e04c3fSmrg ir->var = var; 144501e04c3fSmrg return visit_continue; 144601e04c3fSmrg } 144701e04c3fSmrg 144801e04c3fSmrg ir_variable *const existing = 144901e04c3fSmrg this->symbols->get_variable(ir->var->name); 145001e04c3fSmrg if (existing != NULL) 145101e04c3fSmrg ir->var = existing; 145201e04c3fSmrg else { 145301e04c3fSmrg ir_variable *copy = ir->var->clone(this->target, NULL); 145401e04c3fSmrg 145501e04c3fSmrg this->symbols->add_variable(copy); 145601e04c3fSmrg this->instructions->push_head(copy); 145701e04c3fSmrg ir->var = copy; 145801e04c3fSmrg } 145901e04c3fSmrg 146001e04c3fSmrg return visit_continue; 146101e04c3fSmrg } 146201e04c3fSmrg 146301e04c3fSmrg private: 146401e04c3fSmrg struct gl_linked_shader *target; 146501e04c3fSmrg glsl_symbol_table *symbols; 146601e04c3fSmrg exec_list *instructions; 146701e04c3fSmrg hash_table *temps; 146801e04c3fSmrg }; 146901e04c3fSmrg 147001e04c3fSmrg remap_visitor v(target, temps); 147101e04c3fSmrg 147201e04c3fSmrg inst->accept(&v); 147301e04c3fSmrg} 147401e04c3fSmrg 147501e04c3fSmrg 147601e04c3fSmrg/** 147701e04c3fSmrg * Move non-declarations from one instruction stream to another 147801e04c3fSmrg * 147901e04c3fSmrg * The intended usage pattern of this function is to pass the pointer to the 148001e04c3fSmrg * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node 148101e04c3fSmrg * pointer) for \c last and \c false for \c make_copies on the first 148201e04c3fSmrg * call. Successive calls pass the return value of the previous call for 148301e04c3fSmrg * \c last and \c true for \c make_copies. 148401e04c3fSmrg * 148501e04c3fSmrg * \param instructions Source instruction stream 148601e04c3fSmrg * \param last Instruction after which new instructions should be 148701e04c3fSmrg * inserted in the target instruction stream 148801e04c3fSmrg * \param make_copies Flag selecting whether instructions in \c instructions 148901e04c3fSmrg * should be copied (via \c ir_instruction::clone) into the 149001e04c3fSmrg * target list or moved. 149101e04c3fSmrg * 149201e04c3fSmrg * \return 149301e04c3fSmrg * The new "last" instruction in the target instruction stream. This pointer 149401e04c3fSmrg * is suitable for use as the \c last parameter of a later call to this 149501e04c3fSmrg * function. 149601e04c3fSmrg */ 149701e04c3fSmrgstatic exec_node * 149801e04c3fSmrgmove_non_declarations(exec_list *instructions, exec_node *last, 149901e04c3fSmrg bool make_copies, gl_linked_shader *target) 150001e04c3fSmrg{ 150101e04c3fSmrg hash_table *temps = NULL; 150201e04c3fSmrg 150301e04c3fSmrg if (make_copies) 15047e102996Smaya temps = _mesa_pointer_hash_table_create(NULL); 150501e04c3fSmrg 150601e04c3fSmrg foreach_in_list_safe(ir_instruction, inst, instructions) { 150701e04c3fSmrg if (inst->as_function()) 150801e04c3fSmrg continue; 150901e04c3fSmrg 151001e04c3fSmrg ir_variable *var = inst->as_variable(); 151101e04c3fSmrg if ((var != NULL) && (var->data.mode != ir_var_temporary)) 151201e04c3fSmrg continue; 151301e04c3fSmrg 151401e04c3fSmrg assert(inst->as_assignment() 151501e04c3fSmrg || inst->as_call() 151601e04c3fSmrg || inst->as_if() /* for initializers with the ?: operator */ 151701e04c3fSmrg || ((var != NULL) && (var->data.mode == ir_var_temporary))); 151801e04c3fSmrg 151901e04c3fSmrg if (make_copies) { 152001e04c3fSmrg inst = inst->clone(target, NULL); 152101e04c3fSmrg 152201e04c3fSmrg if (var != NULL) 152301e04c3fSmrg _mesa_hash_table_insert(temps, var, inst); 152401e04c3fSmrg else 152501e04c3fSmrg remap_variables(inst, target, temps); 152601e04c3fSmrg } else { 152701e04c3fSmrg inst->remove(); 152801e04c3fSmrg } 152901e04c3fSmrg 153001e04c3fSmrg last->insert_after(inst); 153101e04c3fSmrg last = inst; 153201e04c3fSmrg } 153301e04c3fSmrg 153401e04c3fSmrg if (make_copies) 153501e04c3fSmrg _mesa_hash_table_destroy(temps, NULL); 153601e04c3fSmrg 153701e04c3fSmrg return last; 153801e04c3fSmrg} 153901e04c3fSmrg 154001e04c3fSmrg 154101e04c3fSmrg/** 154201e04c3fSmrg * This class is only used in link_intrastage_shaders() below but declaring 154301e04c3fSmrg * it inside that function leads to compiler warnings with some versions of 154401e04c3fSmrg * gcc. 154501e04c3fSmrg */ 154601e04c3fSmrgclass array_sizing_visitor : public deref_type_updater { 154701e04c3fSmrgpublic: 15487ec681f3Smrg using deref_type_updater::visit; 15497ec681f3Smrg 155001e04c3fSmrg array_sizing_visitor() 155101e04c3fSmrg : mem_ctx(ralloc_context(NULL)), 15527e102996Smaya unnamed_interfaces(_mesa_pointer_hash_table_create(NULL)) 155301e04c3fSmrg { 155401e04c3fSmrg } 155501e04c3fSmrg 155601e04c3fSmrg ~array_sizing_visitor() 155701e04c3fSmrg { 155801e04c3fSmrg _mesa_hash_table_destroy(this->unnamed_interfaces, NULL); 155901e04c3fSmrg ralloc_free(this->mem_ctx); 156001e04c3fSmrg } 156101e04c3fSmrg 156201e04c3fSmrg virtual ir_visitor_status visit(ir_variable *var) 156301e04c3fSmrg { 156401e04c3fSmrg const glsl_type *type_without_array; 156501e04c3fSmrg bool implicit_sized_array = var->data.implicit_sized_array; 156601e04c3fSmrg fixup_type(&var->type, var->data.max_array_access, 156701e04c3fSmrg var->data.from_ssbo_unsized_array, 156801e04c3fSmrg &implicit_sized_array); 156901e04c3fSmrg var->data.implicit_sized_array = implicit_sized_array; 157001e04c3fSmrg type_without_array = var->type->without_array(); 157101e04c3fSmrg if (var->type->is_interface()) { 157201e04c3fSmrg if (interface_contains_unsized_arrays(var->type)) { 157301e04c3fSmrg const glsl_type *new_type = 157401e04c3fSmrg resize_interface_members(var->type, 157501e04c3fSmrg var->get_max_ifc_array_access(), 157601e04c3fSmrg var->is_in_shader_storage_block()); 157701e04c3fSmrg var->type = new_type; 157801e04c3fSmrg var->change_interface_type(new_type); 157901e04c3fSmrg } 158001e04c3fSmrg } else if (type_without_array->is_interface()) { 158101e04c3fSmrg if (interface_contains_unsized_arrays(type_without_array)) { 158201e04c3fSmrg const glsl_type *new_type = 158301e04c3fSmrg resize_interface_members(type_without_array, 158401e04c3fSmrg var->get_max_ifc_array_access(), 158501e04c3fSmrg var->is_in_shader_storage_block()); 158601e04c3fSmrg var->change_interface_type(new_type); 158701e04c3fSmrg var->type = update_interface_members_array(var->type, new_type); 158801e04c3fSmrg } 158901e04c3fSmrg } else if (const glsl_type *ifc_type = var->get_interface_type()) { 159001e04c3fSmrg /* Store a pointer to the variable in the unnamed_interfaces 159101e04c3fSmrg * hashtable. 159201e04c3fSmrg */ 159301e04c3fSmrg hash_entry *entry = 159401e04c3fSmrg _mesa_hash_table_search(this->unnamed_interfaces, 159501e04c3fSmrg ifc_type); 159601e04c3fSmrg 159701e04c3fSmrg ir_variable **interface_vars = entry ? (ir_variable **) entry->data : NULL; 159801e04c3fSmrg 159901e04c3fSmrg if (interface_vars == NULL) { 160001e04c3fSmrg interface_vars = rzalloc_array(mem_ctx, ir_variable *, 160101e04c3fSmrg ifc_type->length); 160201e04c3fSmrg _mesa_hash_table_insert(this->unnamed_interfaces, ifc_type, 160301e04c3fSmrg interface_vars); 160401e04c3fSmrg } 160501e04c3fSmrg unsigned index = ifc_type->field_index(var->name); 160601e04c3fSmrg assert(index < ifc_type->length); 160701e04c3fSmrg assert(interface_vars[index] == NULL); 160801e04c3fSmrg interface_vars[index] = var; 160901e04c3fSmrg } 161001e04c3fSmrg return visit_continue; 161101e04c3fSmrg } 161201e04c3fSmrg 161301e04c3fSmrg /** 161401e04c3fSmrg * For each unnamed interface block that was discovered while running the 161501e04c3fSmrg * visitor, adjust the interface type to reflect the newly assigned array 161601e04c3fSmrg * sizes, and fix up the ir_variable nodes to point to the new interface 161701e04c3fSmrg * type. 161801e04c3fSmrg */ 161901e04c3fSmrg void fixup_unnamed_interface_types() 162001e04c3fSmrg { 162101e04c3fSmrg hash_table_call_foreach(this->unnamed_interfaces, 162201e04c3fSmrg fixup_unnamed_interface_type, NULL); 162301e04c3fSmrg } 162401e04c3fSmrg 162501e04c3fSmrgprivate: 162601e04c3fSmrg /** 162701e04c3fSmrg * If the type pointed to by \c type represents an unsized array, replace 162801e04c3fSmrg * it with a sized array whose size is determined by max_array_access. 162901e04c3fSmrg */ 163001e04c3fSmrg static void fixup_type(const glsl_type **type, unsigned max_array_access, 163101e04c3fSmrg bool from_ssbo_unsized_array, bool *implicit_sized) 163201e04c3fSmrg { 163301e04c3fSmrg if (!from_ssbo_unsized_array && (*type)->is_unsized_array()) { 163401e04c3fSmrg *type = glsl_type::get_array_instance((*type)->fields.array, 163501e04c3fSmrg max_array_access + 1); 163601e04c3fSmrg *implicit_sized = true; 163701e04c3fSmrg assert(*type != NULL); 163801e04c3fSmrg } 163901e04c3fSmrg } 164001e04c3fSmrg 164101e04c3fSmrg static const glsl_type * 164201e04c3fSmrg update_interface_members_array(const glsl_type *type, 164301e04c3fSmrg const glsl_type *new_interface_type) 164401e04c3fSmrg { 164501e04c3fSmrg const glsl_type *element_type = type->fields.array; 164601e04c3fSmrg if (element_type->is_array()) { 164701e04c3fSmrg const glsl_type *new_array_type = 164801e04c3fSmrg update_interface_members_array(element_type, new_interface_type); 164901e04c3fSmrg return glsl_type::get_array_instance(new_array_type, type->length); 165001e04c3fSmrg } else { 165101e04c3fSmrg return glsl_type::get_array_instance(new_interface_type, 165201e04c3fSmrg type->length); 165301e04c3fSmrg } 165401e04c3fSmrg } 165501e04c3fSmrg 165601e04c3fSmrg /** 165701e04c3fSmrg * Determine whether the given interface type contains unsized arrays (if 165801e04c3fSmrg * it doesn't, array_sizing_visitor doesn't need to process it). 165901e04c3fSmrg */ 166001e04c3fSmrg static bool interface_contains_unsized_arrays(const glsl_type *type) 166101e04c3fSmrg { 166201e04c3fSmrg for (unsigned i = 0; i < type->length; i++) { 166301e04c3fSmrg const glsl_type *elem_type = type->fields.structure[i].type; 166401e04c3fSmrg if (elem_type->is_unsized_array()) 166501e04c3fSmrg return true; 166601e04c3fSmrg } 166701e04c3fSmrg return false; 166801e04c3fSmrg } 166901e04c3fSmrg 167001e04c3fSmrg /** 167101e04c3fSmrg * Create a new interface type based on the given type, with unsized arrays 167201e04c3fSmrg * replaced by sized arrays whose size is determined by 167301e04c3fSmrg * max_ifc_array_access. 167401e04c3fSmrg */ 167501e04c3fSmrg static const glsl_type * 167601e04c3fSmrg resize_interface_members(const glsl_type *type, 167701e04c3fSmrg const int *max_ifc_array_access, 167801e04c3fSmrg bool is_ssbo) 167901e04c3fSmrg { 168001e04c3fSmrg unsigned num_fields = type->length; 168101e04c3fSmrg glsl_struct_field *fields = new glsl_struct_field[num_fields]; 168201e04c3fSmrg memcpy(fields, type->fields.structure, 168301e04c3fSmrg num_fields * sizeof(*fields)); 168401e04c3fSmrg for (unsigned i = 0; i < num_fields; i++) { 168501e04c3fSmrg bool implicit_sized_array = fields[i].implicit_sized_array; 168601e04c3fSmrg /* If SSBO last member is unsized array, we don't replace it by a sized 168701e04c3fSmrg * array. 168801e04c3fSmrg */ 168901e04c3fSmrg if (is_ssbo && i == (num_fields - 1)) 169001e04c3fSmrg fixup_type(&fields[i].type, max_ifc_array_access[i], 169101e04c3fSmrg true, &implicit_sized_array); 169201e04c3fSmrg else 169301e04c3fSmrg fixup_type(&fields[i].type, max_ifc_array_access[i], 169401e04c3fSmrg false, &implicit_sized_array); 169501e04c3fSmrg fields[i].implicit_sized_array = implicit_sized_array; 169601e04c3fSmrg } 169701e04c3fSmrg glsl_interface_packing packing = 169801e04c3fSmrg (glsl_interface_packing) type->interface_packing; 169901e04c3fSmrg bool row_major = (bool) type->interface_row_major; 170001e04c3fSmrg const glsl_type *new_ifc_type = 170101e04c3fSmrg glsl_type::get_interface_instance(fields, num_fields, 170201e04c3fSmrg packing, row_major, type->name); 170301e04c3fSmrg delete [] fields; 170401e04c3fSmrg return new_ifc_type; 170501e04c3fSmrg } 170601e04c3fSmrg 170701e04c3fSmrg static void fixup_unnamed_interface_type(const void *key, void *data, 170801e04c3fSmrg void *) 170901e04c3fSmrg { 171001e04c3fSmrg const glsl_type *ifc_type = (const glsl_type *) key; 171101e04c3fSmrg ir_variable **interface_vars = (ir_variable **) data; 171201e04c3fSmrg unsigned num_fields = ifc_type->length; 171301e04c3fSmrg glsl_struct_field *fields = new glsl_struct_field[num_fields]; 171401e04c3fSmrg memcpy(fields, ifc_type->fields.structure, 171501e04c3fSmrg num_fields * sizeof(*fields)); 171601e04c3fSmrg bool interface_type_changed = false; 171701e04c3fSmrg for (unsigned i = 0; i < num_fields; i++) { 171801e04c3fSmrg if (interface_vars[i] != NULL && 171901e04c3fSmrg fields[i].type != interface_vars[i]->type) { 172001e04c3fSmrg fields[i].type = interface_vars[i]->type; 172101e04c3fSmrg interface_type_changed = true; 172201e04c3fSmrg } 172301e04c3fSmrg } 172401e04c3fSmrg if (!interface_type_changed) { 172501e04c3fSmrg delete [] fields; 172601e04c3fSmrg return; 172701e04c3fSmrg } 172801e04c3fSmrg glsl_interface_packing packing = 172901e04c3fSmrg (glsl_interface_packing) ifc_type->interface_packing; 173001e04c3fSmrg bool row_major = (bool) ifc_type->interface_row_major; 173101e04c3fSmrg const glsl_type *new_ifc_type = 173201e04c3fSmrg glsl_type::get_interface_instance(fields, num_fields, packing, 173301e04c3fSmrg row_major, ifc_type->name); 173401e04c3fSmrg delete [] fields; 173501e04c3fSmrg for (unsigned i = 0; i < num_fields; i++) { 173601e04c3fSmrg if (interface_vars[i] != NULL) 173701e04c3fSmrg interface_vars[i]->change_interface_type(new_ifc_type); 173801e04c3fSmrg } 173901e04c3fSmrg } 174001e04c3fSmrg 174101e04c3fSmrg /** 174201e04c3fSmrg * Memory context used to allocate the data in \c unnamed_interfaces. 174301e04c3fSmrg */ 174401e04c3fSmrg void *mem_ctx; 174501e04c3fSmrg 174601e04c3fSmrg /** 174701e04c3fSmrg * Hash table from const glsl_type * to an array of ir_variable *'s 174801e04c3fSmrg * pointing to the ir_variables constituting each unnamed interface block. 174901e04c3fSmrg */ 175001e04c3fSmrg hash_table *unnamed_interfaces; 175101e04c3fSmrg}; 175201e04c3fSmrg 175301e04c3fSmrgstatic bool 175401e04c3fSmrgvalidate_xfb_buffer_stride(struct gl_context *ctx, unsigned idx, 175501e04c3fSmrg struct gl_shader_program *prog) 175601e04c3fSmrg{ 175701e04c3fSmrg /* We will validate doubles at a later stage */ 175801e04c3fSmrg if (prog->TransformFeedback.BufferStride[idx] % 4) { 175901e04c3fSmrg linker_error(prog, "invalid qualifier xfb_stride=%d must be a " 176001e04c3fSmrg "multiple of 4 or if its applied to a type that is " 176101e04c3fSmrg "or contains a double a multiple of 8.", 176201e04c3fSmrg prog->TransformFeedback.BufferStride[idx]); 176301e04c3fSmrg return false; 176401e04c3fSmrg } 176501e04c3fSmrg 176601e04c3fSmrg if (prog->TransformFeedback.BufferStride[idx] / 4 > 176701e04c3fSmrg ctx->Const.MaxTransformFeedbackInterleavedComponents) { 176801e04c3fSmrg linker_error(prog, "The MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS " 176901e04c3fSmrg "limit has been exceeded."); 177001e04c3fSmrg return false; 177101e04c3fSmrg } 177201e04c3fSmrg 177301e04c3fSmrg return true; 177401e04c3fSmrg} 177501e04c3fSmrg 177601e04c3fSmrg/** 177701e04c3fSmrg * Check for conflicting xfb_stride default qualifiers and store buffer stride 177801e04c3fSmrg * for later use. 177901e04c3fSmrg */ 178001e04c3fSmrgstatic void 178101e04c3fSmrglink_xfb_stride_layout_qualifiers(struct gl_context *ctx, 178201e04c3fSmrg struct gl_shader_program *prog, 178301e04c3fSmrg struct gl_shader **shader_list, 178401e04c3fSmrg unsigned num_shaders) 178501e04c3fSmrg{ 178601e04c3fSmrg for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) { 178701e04c3fSmrg prog->TransformFeedback.BufferStride[i] = 0; 178801e04c3fSmrg } 178901e04c3fSmrg 179001e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 179101e04c3fSmrg struct gl_shader *shader = shader_list[i]; 179201e04c3fSmrg 179301e04c3fSmrg for (unsigned j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { 179401e04c3fSmrg if (shader->TransformFeedbackBufferStride[j]) { 179501e04c3fSmrg if (prog->TransformFeedback.BufferStride[j] == 0) { 179601e04c3fSmrg prog->TransformFeedback.BufferStride[j] = 179701e04c3fSmrg shader->TransformFeedbackBufferStride[j]; 179801e04c3fSmrg if (!validate_xfb_buffer_stride(ctx, j, prog)) 179901e04c3fSmrg return; 180001e04c3fSmrg } else if (prog->TransformFeedback.BufferStride[j] != 180101e04c3fSmrg shader->TransformFeedbackBufferStride[j]){ 180201e04c3fSmrg linker_error(prog, 180301e04c3fSmrg "intrastage shaders defined with conflicting " 180401e04c3fSmrg "xfb_stride for buffer %d (%d and %d)\n", j, 180501e04c3fSmrg prog->TransformFeedback.BufferStride[j], 180601e04c3fSmrg shader->TransformFeedbackBufferStride[j]); 180701e04c3fSmrg return; 180801e04c3fSmrg } 180901e04c3fSmrg } 181001e04c3fSmrg } 181101e04c3fSmrg } 181201e04c3fSmrg} 181301e04c3fSmrg 181401e04c3fSmrg/** 181501e04c3fSmrg * Check for conflicting bindless/bound sampler/image layout qualifiers at 181601e04c3fSmrg * global scope. 181701e04c3fSmrg */ 181801e04c3fSmrgstatic void 181901e04c3fSmrglink_bindless_layout_qualifiers(struct gl_shader_program *prog, 182001e04c3fSmrg struct gl_shader **shader_list, 182101e04c3fSmrg unsigned num_shaders) 182201e04c3fSmrg{ 182301e04c3fSmrg bool bindless_sampler, bindless_image; 182401e04c3fSmrg bool bound_sampler, bound_image; 182501e04c3fSmrg 182601e04c3fSmrg bindless_sampler = bindless_image = false; 182701e04c3fSmrg bound_sampler = bound_image = false; 182801e04c3fSmrg 182901e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 183001e04c3fSmrg struct gl_shader *shader = shader_list[i]; 183101e04c3fSmrg 183201e04c3fSmrg if (shader->bindless_sampler) 183301e04c3fSmrg bindless_sampler = true; 183401e04c3fSmrg if (shader->bindless_image) 183501e04c3fSmrg bindless_image = true; 183601e04c3fSmrg if (shader->bound_sampler) 183701e04c3fSmrg bound_sampler = true; 183801e04c3fSmrg if (shader->bound_image) 183901e04c3fSmrg bound_image = true; 184001e04c3fSmrg 184101e04c3fSmrg if ((bindless_sampler && bound_sampler) || 184201e04c3fSmrg (bindless_image && bound_image)) { 184301e04c3fSmrg /* From section 4.4.6 of the ARB_bindless_texture spec: 184401e04c3fSmrg * 184501e04c3fSmrg * "If both bindless_sampler and bound_sampler, or bindless_image 184601e04c3fSmrg * and bound_image, are declared at global scope in any 184701e04c3fSmrg * compilation unit, a link- time error will be generated." 184801e04c3fSmrg */ 184901e04c3fSmrg linker_error(prog, "both bindless_sampler and bound_sampler, or " 185001e04c3fSmrg "bindless_image and bound_image, can't be declared at " 185101e04c3fSmrg "global scope"); 185201e04c3fSmrg } 185301e04c3fSmrg } 185401e04c3fSmrg} 185501e04c3fSmrg 18567ec681f3Smrg/** 18577ec681f3Smrg * Check for conflicting viewport_relative settings across shaders, and sets 18587ec681f3Smrg * the value for the linked shader. 18597ec681f3Smrg */ 18607ec681f3Smrgstatic void 18617ec681f3Smrglink_layer_viewport_relative_qualifier(struct gl_shader_program *prog, 18627ec681f3Smrg struct gl_program *gl_prog, 18637ec681f3Smrg struct gl_shader **shader_list, 18647ec681f3Smrg unsigned num_shaders) 18657ec681f3Smrg{ 18667ec681f3Smrg unsigned i; 18677ec681f3Smrg 18687ec681f3Smrg /* Find first shader with explicit layer declaration */ 18697ec681f3Smrg for (i = 0; i < num_shaders; i++) { 18707ec681f3Smrg if (shader_list[i]->redeclares_gl_layer) { 18717ec681f3Smrg gl_prog->info.layer_viewport_relative = 18727ec681f3Smrg shader_list[i]->layer_viewport_relative; 18737ec681f3Smrg break; 18747ec681f3Smrg } 18757ec681f3Smrg } 18767ec681f3Smrg 18777ec681f3Smrg /* Now make sure that each subsequent shader's explicit layer declaration 18787ec681f3Smrg * matches the first one's. 18797ec681f3Smrg */ 18807ec681f3Smrg for (; i < num_shaders; i++) { 18817ec681f3Smrg if (shader_list[i]->redeclares_gl_layer && 18827ec681f3Smrg shader_list[i]->layer_viewport_relative != 18837ec681f3Smrg gl_prog->info.layer_viewport_relative) { 18847ec681f3Smrg linker_error(prog, "all gl_Layer redeclarations must have identical " 18857ec681f3Smrg "viewport_relative settings"); 18867ec681f3Smrg } 18877ec681f3Smrg } 18887ec681f3Smrg} 18897ec681f3Smrg 189001e04c3fSmrg/** 189101e04c3fSmrg * Performs the cross-validation of tessellation control shader vertices and 189201e04c3fSmrg * layout qualifiers for the attached tessellation control shaders, 189301e04c3fSmrg * and propagates them to the linked TCS and linked shader program. 189401e04c3fSmrg */ 189501e04c3fSmrgstatic void 189601e04c3fSmrglink_tcs_out_layout_qualifiers(struct gl_shader_program *prog, 189701e04c3fSmrg struct gl_program *gl_prog, 189801e04c3fSmrg struct gl_shader **shader_list, 189901e04c3fSmrg unsigned num_shaders) 190001e04c3fSmrg{ 190101e04c3fSmrg if (gl_prog->info.stage != MESA_SHADER_TESS_CTRL) 190201e04c3fSmrg return; 190301e04c3fSmrg 190401e04c3fSmrg gl_prog->info.tess.tcs_vertices_out = 0; 190501e04c3fSmrg 190601e04c3fSmrg /* From the GLSL 4.0 spec (chapter 4.3.8.2): 190701e04c3fSmrg * 190801e04c3fSmrg * "All tessellation control shader layout declarations in a program 190901e04c3fSmrg * must specify the same output patch vertex count. There must be at 191001e04c3fSmrg * least one layout qualifier specifying an output patch vertex count 191101e04c3fSmrg * in any program containing tessellation control shaders; however, 191201e04c3fSmrg * such a declaration is not required in all tessellation control 191301e04c3fSmrg * shaders." 191401e04c3fSmrg */ 191501e04c3fSmrg 191601e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 191701e04c3fSmrg struct gl_shader *shader = shader_list[i]; 191801e04c3fSmrg 191901e04c3fSmrg if (shader->info.TessCtrl.VerticesOut != 0) { 192001e04c3fSmrg if (gl_prog->info.tess.tcs_vertices_out != 0 && 192101e04c3fSmrg gl_prog->info.tess.tcs_vertices_out != 192201e04c3fSmrg (unsigned) shader->info.TessCtrl.VerticesOut) { 192301e04c3fSmrg linker_error(prog, "tessellation control shader defined with " 192401e04c3fSmrg "conflicting output vertex count (%d and %d)\n", 192501e04c3fSmrg gl_prog->info.tess.tcs_vertices_out, 192601e04c3fSmrg shader->info.TessCtrl.VerticesOut); 192701e04c3fSmrg return; 192801e04c3fSmrg } 192901e04c3fSmrg gl_prog->info.tess.tcs_vertices_out = 193001e04c3fSmrg shader->info.TessCtrl.VerticesOut; 193101e04c3fSmrg } 193201e04c3fSmrg } 193301e04c3fSmrg 193401e04c3fSmrg /* Just do the intrastage -> interstage propagation right now, 193501e04c3fSmrg * since we already know we're in the right type of shader program 193601e04c3fSmrg * for doing it. 193701e04c3fSmrg */ 193801e04c3fSmrg if (gl_prog->info.tess.tcs_vertices_out == 0) { 193901e04c3fSmrg linker_error(prog, "tessellation control shader didn't declare " 194001e04c3fSmrg "vertices out layout qualifier\n"); 194101e04c3fSmrg return; 194201e04c3fSmrg } 194301e04c3fSmrg} 194401e04c3fSmrg 194501e04c3fSmrg 194601e04c3fSmrg/** 194701e04c3fSmrg * Performs the cross-validation of tessellation evaluation shader 194801e04c3fSmrg * primitive type, vertex spacing, ordering and point_mode layout qualifiers 194901e04c3fSmrg * for the attached tessellation evaluation shaders, and propagates them 195001e04c3fSmrg * to the linked TES and linked shader program. 195101e04c3fSmrg */ 195201e04c3fSmrgstatic void 195301e04c3fSmrglink_tes_in_layout_qualifiers(struct gl_shader_program *prog, 195401e04c3fSmrg struct gl_program *gl_prog, 195501e04c3fSmrg struct gl_shader **shader_list, 195601e04c3fSmrg unsigned num_shaders) 195701e04c3fSmrg{ 195801e04c3fSmrg if (gl_prog->info.stage != MESA_SHADER_TESS_EVAL) 195901e04c3fSmrg return; 196001e04c3fSmrg 196101e04c3fSmrg int point_mode = -1; 196201e04c3fSmrg unsigned vertex_order = 0; 196301e04c3fSmrg 196401e04c3fSmrg gl_prog->info.tess.primitive_mode = PRIM_UNKNOWN; 196501e04c3fSmrg gl_prog->info.tess.spacing = TESS_SPACING_UNSPECIFIED; 196601e04c3fSmrg 196701e04c3fSmrg /* From the GLSL 4.0 spec (chapter 4.3.8.1): 196801e04c3fSmrg * 196901e04c3fSmrg * "At least one tessellation evaluation shader (compilation unit) in 197001e04c3fSmrg * a program must declare a primitive mode in its input layout. 197101e04c3fSmrg * Declaration vertex spacing, ordering, and point mode identifiers is 197201e04c3fSmrg * optional. It is not required that all tessellation evaluation 197301e04c3fSmrg * shaders in a program declare a primitive mode. If spacing or 197401e04c3fSmrg * vertex ordering declarations are omitted, the tessellation 197501e04c3fSmrg * primitive generator will use equal spacing or counter-clockwise 197601e04c3fSmrg * vertex ordering, respectively. If a point mode declaration is 197701e04c3fSmrg * omitted, the tessellation primitive generator will produce lines or 197801e04c3fSmrg * triangles according to the primitive mode." 197901e04c3fSmrg */ 198001e04c3fSmrg 198101e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 198201e04c3fSmrg struct gl_shader *shader = shader_list[i]; 198301e04c3fSmrg 198401e04c3fSmrg if (shader->info.TessEval.PrimitiveMode != PRIM_UNKNOWN) { 198501e04c3fSmrg if (gl_prog->info.tess.primitive_mode != PRIM_UNKNOWN && 198601e04c3fSmrg gl_prog->info.tess.primitive_mode != 198701e04c3fSmrg shader->info.TessEval.PrimitiveMode) { 198801e04c3fSmrg linker_error(prog, "tessellation evaluation shader defined with " 198901e04c3fSmrg "conflicting input primitive modes.\n"); 199001e04c3fSmrg return; 199101e04c3fSmrg } 199201e04c3fSmrg gl_prog->info.tess.primitive_mode = 199301e04c3fSmrg shader->info.TessEval.PrimitiveMode; 199401e04c3fSmrg } 199501e04c3fSmrg 199601e04c3fSmrg if (shader->info.TessEval.Spacing != 0) { 199701e04c3fSmrg if (gl_prog->info.tess.spacing != 0 && gl_prog->info.tess.spacing != 199801e04c3fSmrg shader->info.TessEval.Spacing) { 199901e04c3fSmrg linker_error(prog, "tessellation evaluation shader defined with " 200001e04c3fSmrg "conflicting vertex spacing.\n"); 200101e04c3fSmrg return; 200201e04c3fSmrg } 200301e04c3fSmrg gl_prog->info.tess.spacing = shader->info.TessEval.Spacing; 200401e04c3fSmrg } 200501e04c3fSmrg 200601e04c3fSmrg if (shader->info.TessEval.VertexOrder != 0) { 200701e04c3fSmrg if (vertex_order != 0 && 200801e04c3fSmrg vertex_order != shader->info.TessEval.VertexOrder) { 200901e04c3fSmrg linker_error(prog, "tessellation evaluation shader defined with " 201001e04c3fSmrg "conflicting ordering.\n"); 201101e04c3fSmrg return; 201201e04c3fSmrg } 201301e04c3fSmrg vertex_order = shader->info.TessEval.VertexOrder; 201401e04c3fSmrg } 201501e04c3fSmrg 201601e04c3fSmrg if (shader->info.TessEval.PointMode != -1) { 201701e04c3fSmrg if (point_mode != -1 && 201801e04c3fSmrg point_mode != shader->info.TessEval.PointMode) { 201901e04c3fSmrg linker_error(prog, "tessellation evaluation shader defined with " 202001e04c3fSmrg "conflicting point modes.\n"); 202101e04c3fSmrg return; 202201e04c3fSmrg } 202301e04c3fSmrg point_mode = shader->info.TessEval.PointMode; 202401e04c3fSmrg } 202501e04c3fSmrg 202601e04c3fSmrg } 202701e04c3fSmrg 202801e04c3fSmrg /* Just do the intrastage -> interstage propagation right now, 202901e04c3fSmrg * since we already know we're in the right type of shader program 203001e04c3fSmrg * for doing it. 203101e04c3fSmrg */ 203201e04c3fSmrg if (gl_prog->info.tess.primitive_mode == PRIM_UNKNOWN) { 203301e04c3fSmrg linker_error(prog, 203401e04c3fSmrg "tessellation evaluation shader didn't declare input " 203501e04c3fSmrg "primitive modes.\n"); 203601e04c3fSmrg return; 203701e04c3fSmrg } 203801e04c3fSmrg 203901e04c3fSmrg if (gl_prog->info.tess.spacing == TESS_SPACING_UNSPECIFIED) 204001e04c3fSmrg gl_prog->info.tess.spacing = TESS_SPACING_EQUAL; 204101e04c3fSmrg 204201e04c3fSmrg if (vertex_order == 0 || vertex_order == GL_CCW) 204301e04c3fSmrg gl_prog->info.tess.ccw = true; 204401e04c3fSmrg else 204501e04c3fSmrg gl_prog->info.tess.ccw = false; 204601e04c3fSmrg 204701e04c3fSmrg 204801e04c3fSmrg if (point_mode == -1 || point_mode == GL_FALSE) 204901e04c3fSmrg gl_prog->info.tess.point_mode = false; 205001e04c3fSmrg else 205101e04c3fSmrg gl_prog->info.tess.point_mode = true; 205201e04c3fSmrg} 205301e04c3fSmrg 205401e04c3fSmrg 205501e04c3fSmrg/** 205601e04c3fSmrg * Performs the cross-validation of layout qualifiers specified in 205701e04c3fSmrg * redeclaration of gl_FragCoord for the attached fragment shaders, 205801e04c3fSmrg * and propagates them to the linked FS and linked shader program. 205901e04c3fSmrg */ 206001e04c3fSmrgstatic void 206101e04c3fSmrglink_fs_inout_layout_qualifiers(struct gl_shader_program *prog, 206201e04c3fSmrg struct gl_linked_shader *linked_shader, 206301e04c3fSmrg struct gl_shader **shader_list, 206401e04c3fSmrg unsigned num_shaders) 206501e04c3fSmrg{ 206601e04c3fSmrg bool redeclares_gl_fragcoord = false; 206701e04c3fSmrg bool uses_gl_fragcoord = false; 206801e04c3fSmrg bool origin_upper_left = false; 206901e04c3fSmrg bool pixel_center_integer = false; 207001e04c3fSmrg 207101e04c3fSmrg if (linked_shader->Stage != MESA_SHADER_FRAGMENT || 207201e04c3fSmrg (prog->data->Version < 150 && 207301e04c3fSmrg !prog->ARB_fragment_coord_conventions_enable)) 207401e04c3fSmrg return; 207501e04c3fSmrg 207601e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 207701e04c3fSmrg struct gl_shader *shader = shader_list[i]; 207801e04c3fSmrg /* From the GLSL 1.50 spec, page 39: 207901e04c3fSmrg * 208001e04c3fSmrg * "If gl_FragCoord is redeclared in any fragment shader in a program, 208101e04c3fSmrg * it must be redeclared in all the fragment shaders in that program 208201e04c3fSmrg * that have a static use gl_FragCoord." 208301e04c3fSmrg */ 208401e04c3fSmrg if ((redeclares_gl_fragcoord && !shader->redeclares_gl_fragcoord && 208501e04c3fSmrg shader->uses_gl_fragcoord) 208601e04c3fSmrg || (shader->redeclares_gl_fragcoord && !redeclares_gl_fragcoord && 208701e04c3fSmrg uses_gl_fragcoord)) { 208801e04c3fSmrg linker_error(prog, "fragment shader defined with conflicting " 208901e04c3fSmrg "layout qualifiers for gl_FragCoord\n"); 209001e04c3fSmrg } 209101e04c3fSmrg 209201e04c3fSmrg /* From the GLSL 1.50 spec, page 39: 209301e04c3fSmrg * 209401e04c3fSmrg * "All redeclarations of gl_FragCoord in all fragment shaders in a 209501e04c3fSmrg * single program must have the same set of qualifiers." 209601e04c3fSmrg */ 209701e04c3fSmrg if (redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord && 209801e04c3fSmrg (shader->origin_upper_left != origin_upper_left || 209901e04c3fSmrg shader->pixel_center_integer != pixel_center_integer)) { 210001e04c3fSmrg linker_error(prog, "fragment shader defined with conflicting " 210101e04c3fSmrg "layout qualifiers for gl_FragCoord\n"); 210201e04c3fSmrg } 210301e04c3fSmrg 210401e04c3fSmrg /* Update the linked shader state. Note that uses_gl_fragcoord should 210501e04c3fSmrg * accumulate the results. The other values should replace. If there 210601e04c3fSmrg * are multiple redeclarations, all the fields except uses_gl_fragcoord 210701e04c3fSmrg * are already known to be the same. 210801e04c3fSmrg */ 210901e04c3fSmrg if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) { 211001e04c3fSmrg redeclares_gl_fragcoord = shader->redeclares_gl_fragcoord; 211101e04c3fSmrg uses_gl_fragcoord |= shader->uses_gl_fragcoord; 211201e04c3fSmrg origin_upper_left = shader->origin_upper_left; 211301e04c3fSmrg pixel_center_integer = shader->pixel_center_integer; 211401e04c3fSmrg } 211501e04c3fSmrg 211601e04c3fSmrg linked_shader->Program->info.fs.early_fragment_tests |= 211701e04c3fSmrg shader->EarlyFragmentTests || shader->PostDepthCoverage; 211801e04c3fSmrg linked_shader->Program->info.fs.inner_coverage |= shader->InnerCoverage; 211901e04c3fSmrg linked_shader->Program->info.fs.post_depth_coverage |= 212001e04c3fSmrg shader->PostDepthCoverage; 212101e04c3fSmrg linked_shader->Program->info.fs.pixel_interlock_ordered |= 212201e04c3fSmrg shader->PixelInterlockOrdered; 212301e04c3fSmrg linked_shader->Program->info.fs.pixel_interlock_unordered |= 212401e04c3fSmrg shader->PixelInterlockUnordered; 212501e04c3fSmrg linked_shader->Program->info.fs.sample_interlock_ordered |= 212601e04c3fSmrg shader->SampleInterlockOrdered; 212701e04c3fSmrg linked_shader->Program->info.fs.sample_interlock_unordered |= 212801e04c3fSmrg shader->SampleInterlockUnordered; 21297ec681f3Smrg linked_shader->Program->info.fs.advanced_blend_modes |= shader->BlendSupport; 213001e04c3fSmrg } 21317e102996Smaya 21327e102996Smaya linked_shader->Program->info.fs.pixel_center_integer = pixel_center_integer; 21337e102996Smaya linked_shader->Program->info.fs.origin_upper_left = origin_upper_left; 213401e04c3fSmrg} 213501e04c3fSmrg 213601e04c3fSmrg/** 213701e04c3fSmrg * Performs the cross-validation of geometry shader max_vertices and 213801e04c3fSmrg * primitive type layout qualifiers for the attached geometry shaders, 213901e04c3fSmrg * and propagates them to the linked GS and linked shader program. 214001e04c3fSmrg */ 214101e04c3fSmrgstatic void 214201e04c3fSmrglink_gs_inout_layout_qualifiers(struct gl_shader_program *prog, 214301e04c3fSmrg struct gl_program *gl_prog, 214401e04c3fSmrg struct gl_shader **shader_list, 214501e04c3fSmrg unsigned num_shaders) 214601e04c3fSmrg{ 214701e04c3fSmrg /* No in/out qualifiers defined for anything but GLSL 1.50+ 214801e04c3fSmrg * geometry shaders so far. 214901e04c3fSmrg */ 215001e04c3fSmrg if (gl_prog->info.stage != MESA_SHADER_GEOMETRY || 215101e04c3fSmrg prog->data->Version < 150) 215201e04c3fSmrg return; 215301e04c3fSmrg 215401e04c3fSmrg int vertices_out = -1; 215501e04c3fSmrg 215601e04c3fSmrg gl_prog->info.gs.invocations = 0; 215701e04c3fSmrg gl_prog->info.gs.input_primitive = PRIM_UNKNOWN; 215801e04c3fSmrg gl_prog->info.gs.output_primitive = PRIM_UNKNOWN; 215901e04c3fSmrg 216001e04c3fSmrg /* From the GLSL 1.50 spec, page 46: 216101e04c3fSmrg * 216201e04c3fSmrg * "All geometry shader output layout declarations in a program 216301e04c3fSmrg * must declare the same layout and same value for 216401e04c3fSmrg * max_vertices. There must be at least one geometry output 216501e04c3fSmrg * layout declaration somewhere in a program, but not all 216601e04c3fSmrg * geometry shaders (compilation units) are required to 216701e04c3fSmrg * declare it." 216801e04c3fSmrg */ 216901e04c3fSmrg 217001e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 217101e04c3fSmrg struct gl_shader *shader = shader_list[i]; 217201e04c3fSmrg 217301e04c3fSmrg if (shader->info.Geom.InputType != PRIM_UNKNOWN) { 217401e04c3fSmrg if (gl_prog->info.gs.input_primitive != PRIM_UNKNOWN && 217501e04c3fSmrg gl_prog->info.gs.input_primitive != 217601e04c3fSmrg shader->info.Geom.InputType) { 217701e04c3fSmrg linker_error(prog, "geometry shader defined with conflicting " 217801e04c3fSmrg "input types\n"); 217901e04c3fSmrg return; 218001e04c3fSmrg } 218101e04c3fSmrg gl_prog->info.gs.input_primitive = shader->info.Geom.InputType; 218201e04c3fSmrg } 218301e04c3fSmrg 218401e04c3fSmrg if (shader->info.Geom.OutputType != PRIM_UNKNOWN) { 218501e04c3fSmrg if (gl_prog->info.gs.output_primitive != PRIM_UNKNOWN && 218601e04c3fSmrg gl_prog->info.gs.output_primitive != 218701e04c3fSmrg shader->info.Geom.OutputType) { 218801e04c3fSmrg linker_error(prog, "geometry shader defined with conflicting " 218901e04c3fSmrg "output types\n"); 219001e04c3fSmrg return; 219101e04c3fSmrg } 219201e04c3fSmrg gl_prog->info.gs.output_primitive = shader->info.Geom.OutputType; 219301e04c3fSmrg } 219401e04c3fSmrg 219501e04c3fSmrg if (shader->info.Geom.VerticesOut != -1) { 219601e04c3fSmrg if (vertices_out != -1 && 219701e04c3fSmrg vertices_out != shader->info.Geom.VerticesOut) { 219801e04c3fSmrg linker_error(prog, "geometry shader defined with conflicting " 219901e04c3fSmrg "output vertex count (%d and %d)\n", 220001e04c3fSmrg vertices_out, shader->info.Geom.VerticesOut); 220101e04c3fSmrg return; 220201e04c3fSmrg } 220301e04c3fSmrg vertices_out = shader->info.Geom.VerticesOut; 220401e04c3fSmrg } 220501e04c3fSmrg 220601e04c3fSmrg if (shader->info.Geom.Invocations != 0) { 220701e04c3fSmrg if (gl_prog->info.gs.invocations != 0 && 220801e04c3fSmrg gl_prog->info.gs.invocations != 220901e04c3fSmrg (unsigned) shader->info.Geom.Invocations) { 221001e04c3fSmrg linker_error(prog, "geometry shader defined with conflicting " 221101e04c3fSmrg "invocation count (%d and %d)\n", 221201e04c3fSmrg gl_prog->info.gs.invocations, 221301e04c3fSmrg shader->info.Geom.Invocations); 221401e04c3fSmrg return; 221501e04c3fSmrg } 221601e04c3fSmrg gl_prog->info.gs.invocations = shader->info.Geom.Invocations; 221701e04c3fSmrg } 221801e04c3fSmrg } 221901e04c3fSmrg 222001e04c3fSmrg /* Just do the intrastage -> interstage propagation right now, 222101e04c3fSmrg * since we already know we're in the right type of shader program 222201e04c3fSmrg * for doing it. 222301e04c3fSmrg */ 222401e04c3fSmrg if (gl_prog->info.gs.input_primitive == PRIM_UNKNOWN) { 222501e04c3fSmrg linker_error(prog, 222601e04c3fSmrg "geometry shader didn't declare primitive input type\n"); 222701e04c3fSmrg return; 222801e04c3fSmrg } 222901e04c3fSmrg 223001e04c3fSmrg if (gl_prog->info.gs.output_primitive == PRIM_UNKNOWN) { 223101e04c3fSmrg linker_error(prog, 223201e04c3fSmrg "geometry shader didn't declare primitive output type\n"); 223301e04c3fSmrg return; 223401e04c3fSmrg } 223501e04c3fSmrg 223601e04c3fSmrg if (vertices_out == -1) { 223701e04c3fSmrg linker_error(prog, 223801e04c3fSmrg "geometry shader didn't declare max_vertices\n"); 223901e04c3fSmrg return; 224001e04c3fSmrg } else { 224101e04c3fSmrg gl_prog->info.gs.vertices_out = vertices_out; 224201e04c3fSmrg } 224301e04c3fSmrg 224401e04c3fSmrg if (gl_prog->info.gs.invocations == 0) 224501e04c3fSmrg gl_prog->info.gs.invocations = 1; 224601e04c3fSmrg} 224701e04c3fSmrg 224801e04c3fSmrg 224901e04c3fSmrg/** 22507e102996Smaya * Perform cross-validation of compute shader local_size_{x,y,z} layout and 22517e102996Smaya * derivative arrangement qualifiers for the attached compute shaders, and 22527e102996Smaya * propagate them to the linked CS and linked shader program. 225301e04c3fSmrg */ 225401e04c3fSmrgstatic void 225501e04c3fSmrglink_cs_input_layout_qualifiers(struct gl_shader_program *prog, 225601e04c3fSmrg struct gl_program *gl_prog, 225701e04c3fSmrg struct gl_shader **shader_list, 225801e04c3fSmrg unsigned num_shaders) 225901e04c3fSmrg{ 226001e04c3fSmrg /* This function is called for all shader stages, but it only has an effect 226101e04c3fSmrg * for compute shaders. 226201e04c3fSmrg */ 226301e04c3fSmrg if (gl_prog->info.stage != MESA_SHADER_COMPUTE) 226401e04c3fSmrg return; 226501e04c3fSmrg 226601e04c3fSmrg for (int i = 0; i < 3; i++) 22677ec681f3Smrg gl_prog->info.workgroup_size[i] = 0; 226801e04c3fSmrg 22697ec681f3Smrg gl_prog->info.workgroup_size_variable = false; 227001e04c3fSmrg 22717e102996Smaya gl_prog->info.cs.derivative_group = DERIVATIVE_GROUP_NONE; 22727e102996Smaya 227301e04c3fSmrg /* From the ARB_compute_shader spec, in the section describing local size 227401e04c3fSmrg * declarations: 227501e04c3fSmrg * 227601e04c3fSmrg * If multiple compute shaders attached to a single program object 227701e04c3fSmrg * declare local work-group size, the declarations must be identical; 227801e04c3fSmrg * otherwise a link-time error results. Furthermore, if a program 227901e04c3fSmrg * object contains any compute shaders, at least one must contain an 228001e04c3fSmrg * input layout qualifier specifying the local work sizes of the 228101e04c3fSmrg * program, or a link-time error will occur. 228201e04c3fSmrg */ 228301e04c3fSmrg for (unsigned sh = 0; sh < num_shaders; sh++) { 228401e04c3fSmrg struct gl_shader *shader = shader_list[sh]; 228501e04c3fSmrg 228601e04c3fSmrg if (shader->info.Comp.LocalSize[0] != 0) { 22877ec681f3Smrg if (gl_prog->info.workgroup_size[0] != 0) { 228801e04c3fSmrg for (int i = 0; i < 3; i++) { 22897ec681f3Smrg if (gl_prog->info.workgroup_size[i] != 229001e04c3fSmrg shader->info.Comp.LocalSize[i]) { 229101e04c3fSmrg linker_error(prog, "compute shader defined with conflicting " 229201e04c3fSmrg "local sizes\n"); 229301e04c3fSmrg return; 229401e04c3fSmrg } 229501e04c3fSmrg } 229601e04c3fSmrg } 229701e04c3fSmrg for (int i = 0; i < 3; i++) { 22987ec681f3Smrg gl_prog->info.workgroup_size[i] = 229901e04c3fSmrg shader->info.Comp.LocalSize[i]; 230001e04c3fSmrg } 230101e04c3fSmrg } else if (shader->info.Comp.LocalSizeVariable) { 23027ec681f3Smrg if (gl_prog->info.workgroup_size[0] != 0) { 230301e04c3fSmrg /* The ARB_compute_variable_group_size spec says: 230401e04c3fSmrg * 230501e04c3fSmrg * If one compute shader attached to a program declares a 230601e04c3fSmrg * variable local group size and a second compute shader 230701e04c3fSmrg * attached to the same program declares a fixed local group 230801e04c3fSmrg * size, a link-time error results. 230901e04c3fSmrg */ 231001e04c3fSmrg linker_error(prog, "compute shader defined with both fixed and " 231101e04c3fSmrg "variable local group size\n"); 231201e04c3fSmrg return; 231301e04c3fSmrg } 23147ec681f3Smrg gl_prog->info.workgroup_size_variable = true; 231501e04c3fSmrg } 23167e102996Smaya 23177e102996Smaya enum gl_derivative_group group = shader->info.Comp.DerivativeGroup; 23187e102996Smaya if (group != DERIVATIVE_GROUP_NONE) { 23197e102996Smaya if (gl_prog->info.cs.derivative_group != DERIVATIVE_GROUP_NONE && 23207e102996Smaya gl_prog->info.cs.derivative_group != group) { 23217e102996Smaya linker_error(prog, "compute shader defined with conflicting " 23227e102996Smaya "derivative groups\n"); 23237e102996Smaya return; 23247e102996Smaya } 23257e102996Smaya gl_prog->info.cs.derivative_group = group; 23267e102996Smaya } 232701e04c3fSmrg } 232801e04c3fSmrg 232901e04c3fSmrg /* Just do the intrastage -> interstage propagation right now, 233001e04c3fSmrg * since we already know we're in the right type of shader program 233101e04c3fSmrg * for doing it. 233201e04c3fSmrg */ 23337ec681f3Smrg if (gl_prog->info.workgroup_size[0] == 0 && 23347ec681f3Smrg !gl_prog->info.workgroup_size_variable) { 233501e04c3fSmrg linker_error(prog, "compute shader must contain a fixed or a variable " 233601e04c3fSmrg "local group size\n"); 233701e04c3fSmrg return; 233801e04c3fSmrg } 23397e102996Smaya 23407e102996Smaya if (gl_prog->info.cs.derivative_group == DERIVATIVE_GROUP_QUADS) { 23417ec681f3Smrg if (gl_prog->info.workgroup_size[0] % 2 != 0) { 23427e102996Smaya linker_error(prog, "derivative_group_quadsNV must be used with a " 23437e102996Smaya "local group size whose first dimension " 23447e102996Smaya "is a multiple of 2\n"); 23457e102996Smaya return; 23467e102996Smaya } 23477ec681f3Smrg if (gl_prog->info.workgroup_size[1] % 2 != 0) { 23487e102996Smaya linker_error(prog, "derivative_group_quadsNV must be used with a local" 23497e102996Smaya "group size whose second dimension " 23507e102996Smaya "is a multiple of 2\n"); 23517e102996Smaya return; 23527e102996Smaya } 23537e102996Smaya } else if (gl_prog->info.cs.derivative_group == DERIVATIVE_GROUP_LINEAR) { 23547ec681f3Smrg if ((gl_prog->info.workgroup_size[0] * 23557ec681f3Smrg gl_prog->info.workgroup_size[1] * 23567ec681f3Smrg gl_prog->info.workgroup_size[2]) % 4 != 0) { 23577e102996Smaya linker_error(prog, "derivative_group_linearNV must be used with a " 23587e102996Smaya "local group size whose total number of invocations " 23597e102996Smaya "is a multiple of 4\n"); 23607e102996Smaya return; 23617e102996Smaya } 23627e102996Smaya } 236301e04c3fSmrg} 236401e04c3fSmrg 236501e04c3fSmrg/** 236601e04c3fSmrg * Link all out variables on a single stage which are not 236701e04c3fSmrg * directly used in a shader with the main function. 236801e04c3fSmrg */ 236901e04c3fSmrgstatic void 237001e04c3fSmrglink_output_variables(struct gl_linked_shader *linked_shader, 237101e04c3fSmrg struct gl_shader **shader_list, 237201e04c3fSmrg unsigned num_shaders) 237301e04c3fSmrg{ 237401e04c3fSmrg struct glsl_symbol_table *symbols = linked_shader->symbols; 237501e04c3fSmrg 237601e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 237701e04c3fSmrg 237801e04c3fSmrg /* Skip shader object with main function */ 237901e04c3fSmrg if (shader_list[i]->symbols->get_function("main")) 238001e04c3fSmrg continue; 238101e04c3fSmrg 238201e04c3fSmrg foreach_in_list(ir_instruction, ir, shader_list[i]->ir) { 238301e04c3fSmrg if (ir->ir_type != ir_type_variable) 238401e04c3fSmrg continue; 238501e04c3fSmrg 238601e04c3fSmrg ir_variable *var = (ir_variable *) ir; 238701e04c3fSmrg 238801e04c3fSmrg if (var->data.mode == ir_var_shader_out && 238901e04c3fSmrg !symbols->get_variable(var->name)) { 239001e04c3fSmrg var = var->clone(linked_shader, NULL); 239101e04c3fSmrg symbols->add_variable(var); 239201e04c3fSmrg linked_shader->ir->push_head(var); 239301e04c3fSmrg } 239401e04c3fSmrg } 239501e04c3fSmrg } 239601e04c3fSmrg 239701e04c3fSmrg return; 239801e04c3fSmrg} 239901e04c3fSmrg 240001e04c3fSmrg 240101e04c3fSmrg/** 240201e04c3fSmrg * Combine a group of shaders for a single stage to generate a linked shader 240301e04c3fSmrg * 240401e04c3fSmrg * \note 240501e04c3fSmrg * If this function is supplied a single shader, it is cloned, and the new 240601e04c3fSmrg * shader is returned. 240701e04c3fSmrg */ 240801e04c3fSmrgstruct gl_linked_shader * 240901e04c3fSmrglink_intrastage_shaders(void *mem_ctx, 241001e04c3fSmrg struct gl_context *ctx, 241101e04c3fSmrg struct gl_shader_program *prog, 241201e04c3fSmrg struct gl_shader **shader_list, 241301e04c3fSmrg unsigned num_shaders, 241401e04c3fSmrg bool allow_missing_main) 241501e04c3fSmrg{ 241601e04c3fSmrg struct gl_uniform_block *ubo_blocks = NULL; 241701e04c3fSmrg struct gl_uniform_block *ssbo_blocks = NULL; 241801e04c3fSmrg unsigned num_ubo_blocks = 0; 241901e04c3fSmrg unsigned num_ssbo_blocks = 0; 242001e04c3fSmrg 242101e04c3fSmrg /* Check that global variables defined in multiple shaders are consistent. 242201e04c3fSmrg */ 242301e04c3fSmrg glsl_symbol_table variables; 242401e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 242501e04c3fSmrg if (shader_list[i] == NULL) 242601e04c3fSmrg continue; 242701e04c3fSmrg cross_validate_globals(ctx, prog, shader_list[i]->ir, &variables, 242801e04c3fSmrg false); 242901e04c3fSmrg } 243001e04c3fSmrg 243101e04c3fSmrg if (!prog->data->LinkStatus) 243201e04c3fSmrg return NULL; 243301e04c3fSmrg 243401e04c3fSmrg /* Check that interface blocks defined in multiple shaders are consistent. 243501e04c3fSmrg */ 243601e04c3fSmrg validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list, 243701e04c3fSmrg num_shaders); 243801e04c3fSmrg if (!prog->data->LinkStatus) 243901e04c3fSmrg return NULL; 244001e04c3fSmrg 244101e04c3fSmrg /* Check that there is only a single definition of each function signature 244201e04c3fSmrg * across all shaders. 244301e04c3fSmrg */ 244401e04c3fSmrg for (unsigned i = 0; i < (num_shaders - 1); i++) { 244501e04c3fSmrg foreach_in_list(ir_instruction, node, shader_list[i]->ir) { 244601e04c3fSmrg ir_function *const f = node->as_function(); 244701e04c3fSmrg 244801e04c3fSmrg if (f == NULL) 244901e04c3fSmrg continue; 245001e04c3fSmrg 245101e04c3fSmrg for (unsigned j = i + 1; j < num_shaders; j++) { 245201e04c3fSmrg ir_function *const other = 245301e04c3fSmrg shader_list[j]->symbols->get_function(f->name); 245401e04c3fSmrg 245501e04c3fSmrg /* If the other shader has no function (and therefore no function 245601e04c3fSmrg * signatures) with the same name, skip to the next shader. 245701e04c3fSmrg */ 245801e04c3fSmrg if (other == NULL) 245901e04c3fSmrg continue; 246001e04c3fSmrg 246101e04c3fSmrg foreach_in_list(ir_function_signature, sig, &f->signatures) { 246201e04c3fSmrg if (!sig->is_defined) 246301e04c3fSmrg continue; 246401e04c3fSmrg 246501e04c3fSmrg ir_function_signature *other_sig = 246601e04c3fSmrg other->exact_matching_signature(NULL, &sig->parameters); 246701e04c3fSmrg 246801e04c3fSmrg if (other_sig != NULL && other_sig->is_defined) { 246901e04c3fSmrg linker_error(prog, "function `%s' is multiply defined\n", 247001e04c3fSmrg f->name); 247101e04c3fSmrg return NULL; 247201e04c3fSmrg } 247301e04c3fSmrg } 247401e04c3fSmrg } 247501e04c3fSmrg } 247601e04c3fSmrg } 247701e04c3fSmrg 247801e04c3fSmrg /* Find the shader that defines main, and make a clone of it. 247901e04c3fSmrg * 248001e04c3fSmrg * Starting with the clone, search for undefined references. If one is 248101e04c3fSmrg * found, find the shader that defines it. Clone the reference and add 248201e04c3fSmrg * it to the shader. Repeat until there are no undefined references or 248301e04c3fSmrg * until a reference cannot be resolved. 248401e04c3fSmrg */ 248501e04c3fSmrg gl_shader *main = NULL; 248601e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 248701e04c3fSmrg if (_mesa_get_main_function_signature(shader_list[i]->symbols)) { 248801e04c3fSmrg main = shader_list[i]; 248901e04c3fSmrg break; 249001e04c3fSmrg } 249101e04c3fSmrg } 249201e04c3fSmrg 249301e04c3fSmrg if (main == NULL && allow_missing_main) 249401e04c3fSmrg main = shader_list[0]; 249501e04c3fSmrg 249601e04c3fSmrg if (main == NULL) { 249701e04c3fSmrg linker_error(prog, "%s shader lacks `main'\n", 249801e04c3fSmrg _mesa_shader_stage_to_string(shader_list[0]->Stage)); 249901e04c3fSmrg return NULL; 250001e04c3fSmrg } 250101e04c3fSmrg 250201e04c3fSmrg gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader); 250301e04c3fSmrg linked->Stage = shader_list[0]->Stage; 250401e04c3fSmrg 250501e04c3fSmrg /* Create program and attach it to the linked shader */ 250601e04c3fSmrg struct gl_program *gl_prog = 25077ec681f3Smrg ctx->Driver.NewProgram(ctx, shader_list[0]->Stage, prog->Name, false); 250801e04c3fSmrg if (!gl_prog) { 250901e04c3fSmrg prog->data->LinkStatus = LINKING_FAILURE; 251001e04c3fSmrg _mesa_delete_linked_shader(ctx, linked); 251101e04c3fSmrg return NULL; 251201e04c3fSmrg } 251301e04c3fSmrg 251401e04c3fSmrg _mesa_reference_shader_program_data(ctx, &gl_prog->sh.data, prog->data); 251501e04c3fSmrg 251601e04c3fSmrg /* Don't use _mesa_reference_program() just take ownership */ 251701e04c3fSmrg linked->Program = gl_prog; 251801e04c3fSmrg 251901e04c3fSmrg linked->ir = new(linked) exec_list; 252001e04c3fSmrg clone_ir_list(mem_ctx, linked->ir, main->ir); 252101e04c3fSmrg 252201e04c3fSmrg link_fs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders); 252301e04c3fSmrg link_tcs_out_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); 252401e04c3fSmrg link_tes_in_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); 252501e04c3fSmrg link_gs_inout_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); 252601e04c3fSmrg link_cs_input_layout_qualifiers(prog, gl_prog, shader_list, num_shaders); 252701e04c3fSmrg 252801e04c3fSmrg if (linked->Stage != MESA_SHADER_FRAGMENT) 252901e04c3fSmrg link_xfb_stride_layout_qualifiers(ctx, prog, shader_list, num_shaders); 253001e04c3fSmrg 253101e04c3fSmrg link_bindless_layout_qualifiers(prog, shader_list, num_shaders); 253201e04c3fSmrg 25337ec681f3Smrg link_layer_viewport_relative_qualifier(prog, gl_prog, shader_list, num_shaders); 25347ec681f3Smrg 253501e04c3fSmrg populate_symbol_table(linked, shader_list[0]->symbols); 253601e04c3fSmrg 253701e04c3fSmrg /* The pointer to the main function in the final linked shader (i.e., the 253801e04c3fSmrg * copy of the original shader that contained the main function). 253901e04c3fSmrg */ 254001e04c3fSmrg ir_function_signature *const main_sig = 254101e04c3fSmrg _mesa_get_main_function_signature(linked->symbols); 254201e04c3fSmrg 254301e04c3fSmrg /* Move any instructions other than variable declarations or function 254401e04c3fSmrg * declarations into main. 254501e04c3fSmrg */ 254601e04c3fSmrg if (main_sig != NULL) { 254701e04c3fSmrg exec_node *insertion_point = 254801e04c3fSmrg move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false, 254901e04c3fSmrg linked); 255001e04c3fSmrg 255101e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 255201e04c3fSmrg if (shader_list[i] == main) 255301e04c3fSmrg continue; 255401e04c3fSmrg 255501e04c3fSmrg insertion_point = move_non_declarations(shader_list[i]->ir, 255601e04c3fSmrg insertion_point, true, linked); 255701e04c3fSmrg } 255801e04c3fSmrg } 255901e04c3fSmrg 256001e04c3fSmrg if (!link_function_calls(prog, linked, shader_list, num_shaders)) { 256101e04c3fSmrg _mesa_delete_linked_shader(ctx, linked); 256201e04c3fSmrg return NULL; 256301e04c3fSmrg } 256401e04c3fSmrg 256501e04c3fSmrg if (linked->Stage != MESA_SHADER_FRAGMENT) 256601e04c3fSmrg link_output_variables(linked, shader_list, num_shaders); 256701e04c3fSmrg 256801e04c3fSmrg /* Make a pass over all variable declarations to ensure that arrays with 256901e04c3fSmrg * unspecified sizes have a size specified. The size is inferred from the 257001e04c3fSmrg * max_array_access field. 257101e04c3fSmrg */ 257201e04c3fSmrg array_sizing_visitor v; 257301e04c3fSmrg v.run(linked->ir); 257401e04c3fSmrg v.fixup_unnamed_interface_types(); 257501e04c3fSmrg 25767ec681f3Smrg /* Now that we know the sizes of all the arrays, we can replace .length() 25777ec681f3Smrg * calls with a constant expression. 25787ec681f3Smrg */ 25797ec681f3Smrg array_length_to_const_visitor len_v; 25807ec681f3Smrg len_v.run(linked->ir); 25817ec681f3Smrg 258201e04c3fSmrg /* Link up uniform blocks defined within this stage. */ 258301e04c3fSmrg link_uniform_blocks(mem_ctx, ctx, prog, linked, &ubo_blocks, 258401e04c3fSmrg &num_ubo_blocks, &ssbo_blocks, &num_ssbo_blocks); 258501e04c3fSmrg 25867ec681f3Smrg const unsigned max_uniform_blocks = 25877ec681f3Smrg ctx->Const.Program[linked->Stage].MaxUniformBlocks; 25887ec681f3Smrg if (num_ubo_blocks > max_uniform_blocks) { 25897ec681f3Smrg linker_error(prog, "Too many %s uniform blocks (%d/%d)\n", 25907ec681f3Smrg _mesa_shader_stage_to_string(linked->Stage), 25917ec681f3Smrg num_ubo_blocks, max_uniform_blocks); 25927ec681f3Smrg } 25937ec681f3Smrg 25947ec681f3Smrg const unsigned max_shader_storage_blocks = 25957ec681f3Smrg ctx->Const.Program[linked->Stage].MaxShaderStorageBlocks; 25967ec681f3Smrg if (num_ssbo_blocks > max_shader_storage_blocks) { 25977ec681f3Smrg linker_error(prog, "Too many %s shader storage blocks (%d/%d)\n", 25987ec681f3Smrg _mesa_shader_stage_to_string(linked->Stage), 25997ec681f3Smrg num_ssbo_blocks, max_shader_storage_blocks); 26007ec681f3Smrg } 26017ec681f3Smrg 260201e04c3fSmrg if (!prog->data->LinkStatus) { 260301e04c3fSmrg _mesa_delete_linked_shader(ctx, linked); 260401e04c3fSmrg return NULL; 260501e04c3fSmrg } 260601e04c3fSmrg 260701e04c3fSmrg /* Copy ubo blocks to linked shader list */ 260801e04c3fSmrg linked->Program->sh.UniformBlocks = 260901e04c3fSmrg ralloc_array(linked, gl_uniform_block *, num_ubo_blocks); 261001e04c3fSmrg ralloc_steal(linked, ubo_blocks); 261101e04c3fSmrg for (unsigned i = 0; i < num_ubo_blocks; i++) { 261201e04c3fSmrg linked->Program->sh.UniformBlocks[i] = &ubo_blocks[i]; 261301e04c3fSmrg } 26147ec681f3Smrg linked->Program->sh.NumUniformBlocks = num_ubo_blocks; 261501e04c3fSmrg linked->Program->info.num_ubos = num_ubo_blocks; 261601e04c3fSmrg 261701e04c3fSmrg /* Copy ssbo blocks to linked shader list */ 261801e04c3fSmrg linked->Program->sh.ShaderStorageBlocks = 261901e04c3fSmrg ralloc_array(linked, gl_uniform_block *, num_ssbo_blocks); 262001e04c3fSmrg ralloc_steal(linked, ssbo_blocks); 262101e04c3fSmrg for (unsigned i = 0; i < num_ssbo_blocks; i++) { 262201e04c3fSmrg linked->Program->sh.ShaderStorageBlocks[i] = &ssbo_blocks[i]; 262301e04c3fSmrg } 262401e04c3fSmrg linked->Program->info.num_ssbos = num_ssbo_blocks; 262501e04c3fSmrg 262601e04c3fSmrg /* At this point linked should contain all of the linked IR, so 262701e04c3fSmrg * validate it to make sure nothing went wrong. 262801e04c3fSmrg */ 262901e04c3fSmrg validate_ir_tree(linked->ir); 263001e04c3fSmrg 263101e04c3fSmrg /* Set the size of geometry shader input arrays */ 263201e04c3fSmrg if (linked->Stage == MESA_SHADER_GEOMETRY) { 263301e04c3fSmrg unsigned num_vertices = 263401e04c3fSmrg vertices_per_prim(gl_prog->info.gs.input_primitive); 263501e04c3fSmrg array_resize_visitor input_resize_visitor(num_vertices, prog, 263601e04c3fSmrg MESA_SHADER_GEOMETRY); 263701e04c3fSmrg foreach_in_list(ir_instruction, ir, linked->ir) { 263801e04c3fSmrg ir->accept(&input_resize_visitor); 263901e04c3fSmrg } 264001e04c3fSmrg } 264101e04c3fSmrg 264201e04c3fSmrg if (ctx->Const.VertexID_is_zero_based) 264301e04c3fSmrg lower_vertex_id(linked); 264401e04c3fSmrg 264501e04c3fSmrg if (ctx->Const.LowerCsDerivedVariables) 264601e04c3fSmrg lower_cs_derived(linked); 264701e04c3fSmrg 264801e04c3fSmrg#ifdef DEBUG 264901e04c3fSmrg /* Compute the source checksum. */ 265001e04c3fSmrg linked->SourceChecksum = 0; 265101e04c3fSmrg for (unsigned i = 0; i < num_shaders; i++) { 265201e04c3fSmrg if (shader_list[i] == NULL) 265301e04c3fSmrg continue; 265401e04c3fSmrg linked->SourceChecksum ^= shader_list[i]->SourceChecksum; 265501e04c3fSmrg } 265601e04c3fSmrg#endif 265701e04c3fSmrg 265801e04c3fSmrg return linked; 265901e04c3fSmrg} 266001e04c3fSmrg 266101e04c3fSmrg/** 266201e04c3fSmrg * Update the sizes of linked shader uniform arrays to the maximum 266301e04c3fSmrg * array index used. 266401e04c3fSmrg * 266501e04c3fSmrg * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec: 266601e04c3fSmrg * 266701e04c3fSmrg * If one or more elements of an array are active, 266801e04c3fSmrg * GetActiveUniform will return the name of the array in name, 266901e04c3fSmrg * subject to the restrictions listed above. The type of the array 267001e04c3fSmrg * is returned in type. The size parameter contains the highest 267101e04c3fSmrg * array element index used, plus one. The compiler or linker 267201e04c3fSmrg * determines the highest index used. There will be only one 267301e04c3fSmrg * active uniform reported by the GL per uniform array. 267401e04c3fSmrg 267501e04c3fSmrg */ 267601e04c3fSmrgstatic void 267701e04c3fSmrgupdate_array_sizes(struct gl_shader_program *prog) 267801e04c3fSmrg{ 267901e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 268001e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 268101e04c3fSmrg continue; 268201e04c3fSmrg 268301e04c3fSmrg bool types_were_updated = false; 268401e04c3fSmrg 268501e04c3fSmrg foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { 268601e04c3fSmrg ir_variable *const var = node->as_variable(); 268701e04c3fSmrg 268801e04c3fSmrg if ((var == NULL) || (var->data.mode != ir_var_uniform) || 268901e04c3fSmrg !var->type->is_array()) 269001e04c3fSmrg continue; 269101e04c3fSmrg 269201e04c3fSmrg /* GL_ARB_uniform_buffer_object says that std140 uniforms 269301e04c3fSmrg * will not be eliminated. Since we always do std140, just 269401e04c3fSmrg * don't resize arrays in UBOs. 269501e04c3fSmrg * 269601e04c3fSmrg * Atomic counters are supposed to get deterministic 269701e04c3fSmrg * locations assigned based on the declaration ordering and 269801e04c3fSmrg * sizes, array compaction would mess that up. 269901e04c3fSmrg * 270001e04c3fSmrg * Subroutine uniforms are not removed. 270101e04c3fSmrg */ 270201e04c3fSmrg if (var->is_in_buffer_block() || var->type->contains_atomic() || 270301e04c3fSmrg var->type->contains_subroutine() || var->constant_initializer) 270401e04c3fSmrg continue; 270501e04c3fSmrg 270601e04c3fSmrg int size = var->data.max_array_access; 270701e04c3fSmrg for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) { 270801e04c3fSmrg if (prog->_LinkedShaders[j] == NULL) 270901e04c3fSmrg continue; 271001e04c3fSmrg 271101e04c3fSmrg foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) { 271201e04c3fSmrg ir_variable *other_var = node2->as_variable(); 271301e04c3fSmrg if (!other_var) 271401e04c3fSmrg continue; 271501e04c3fSmrg 271601e04c3fSmrg if (strcmp(var->name, other_var->name) == 0 && 271701e04c3fSmrg other_var->data.max_array_access > size) { 271801e04c3fSmrg size = other_var->data.max_array_access; 271901e04c3fSmrg } 272001e04c3fSmrg } 272101e04c3fSmrg } 272201e04c3fSmrg 272301e04c3fSmrg if (size + 1 != (int)var->type->length) { 272401e04c3fSmrg /* If this is a built-in uniform (i.e., it's backed by some 272501e04c3fSmrg * fixed-function state), adjust the number of state slots to 272601e04c3fSmrg * match the new array size. The number of slots per array entry 272701e04c3fSmrg * is not known. It seems safe to assume that the total number of 272801e04c3fSmrg * slots is an integer multiple of the number of array elements. 272901e04c3fSmrg * Determine the number of slots per array element by dividing by 273001e04c3fSmrg * the old (total) size. 273101e04c3fSmrg */ 273201e04c3fSmrg const unsigned num_slots = var->get_num_state_slots(); 273301e04c3fSmrg if (num_slots > 0) { 273401e04c3fSmrg var->set_num_state_slots((size + 1) 273501e04c3fSmrg * (num_slots / var->type->length)); 273601e04c3fSmrg } 273701e04c3fSmrg 273801e04c3fSmrg var->type = glsl_type::get_array_instance(var->type->fields.array, 273901e04c3fSmrg size + 1); 274001e04c3fSmrg types_were_updated = true; 274101e04c3fSmrg } 274201e04c3fSmrg } 274301e04c3fSmrg 274401e04c3fSmrg /* Update the types of dereferences in case we changed any. */ 274501e04c3fSmrg if (types_were_updated) { 274601e04c3fSmrg deref_type_updater v; 274701e04c3fSmrg v.run(prog->_LinkedShaders[i]->ir); 274801e04c3fSmrg } 274901e04c3fSmrg } 275001e04c3fSmrg} 275101e04c3fSmrg 275201e04c3fSmrg/** 275301e04c3fSmrg * Resize tessellation evaluation per-vertex inputs to the size of 275401e04c3fSmrg * tessellation control per-vertex outputs. 275501e04c3fSmrg */ 275601e04c3fSmrgstatic void 275701e04c3fSmrgresize_tes_inputs(struct gl_context *ctx, 275801e04c3fSmrg struct gl_shader_program *prog) 275901e04c3fSmrg{ 276001e04c3fSmrg if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL) 276101e04c3fSmrg return; 276201e04c3fSmrg 276301e04c3fSmrg gl_linked_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]; 276401e04c3fSmrg gl_linked_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL]; 276501e04c3fSmrg 276601e04c3fSmrg /* If no control shader is present, then the TES inputs are statically 276701e04c3fSmrg * sized to MaxPatchVertices; the actual size of the arrays won't be 276801e04c3fSmrg * known until draw time. 276901e04c3fSmrg */ 277001e04c3fSmrg const int num_vertices = tcs 277101e04c3fSmrg ? tcs->Program->info.tess.tcs_vertices_out 277201e04c3fSmrg : ctx->Const.MaxPatchVertices; 277301e04c3fSmrg 277401e04c3fSmrg array_resize_visitor input_resize_visitor(num_vertices, prog, 277501e04c3fSmrg MESA_SHADER_TESS_EVAL); 277601e04c3fSmrg foreach_in_list(ir_instruction, ir, tes->ir) { 277701e04c3fSmrg ir->accept(&input_resize_visitor); 277801e04c3fSmrg } 277901e04c3fSmrg 278001e04c3fSmrg if (tcs) { 278101e04c3fSmrg /* Convert the gl_PatchVerticesIn system value into a constant, since 278201e04c3fSmrg * the value is known at this point. 278301e04c3fSmrg */ 278401e04c3fSmrg foreach_in_list(ir_instruction, ir, tes->ir) { 278501e04c3fSmrg ir_variable *var = ir->as_variable(); 278601e04c3fSmrg if (var && var->data.mode == ir_var_system_value && 278701e04c3fSmrg var->data.location == SYSTEM_VALUE_VERTICES_IN) { 278801e04c3fSmrg void *mem_ctx = ralloc_parent(var); 278901e04c3fSmrg var->data.location = 0; 279001e04c3fSmrg var->data.explicit_location = false; 279101e04c3fSmrg var->data.mode = ir_var_auto; 279201e04c3fSmrg var->constant_value = new(mem_ctx) ir_constant(num_vertices); 279301e04c3fSmrg } 279401e04c3fSmrg } 279501e04c3fSmrg } 279601e04c3fSmrg} 279701e04c3fSmrg 279801e04c3fSmrg/** 279901e04c3fSmrg * Find a contiguous set of available bits in a bitmask. 280001e04c3fSmrg * 280101e04c3fSmrg * \param used_mask Bits representing used (1) and unused (0) locations 280201e04c3fSmrg * \param needed_count Number of contiguous bits needed. 280301e04c3fSmrg * 280401e04c3fSmrg * \return 280501e04c3fSmrg * Base location of the available bits on success or -1 on failure. 280601e04c3fSmrg */ 280701e04c3fSmrgstatic int 280801e04c3fSmrgfind_available_slots(unsigned used_mask, unsigned needed_count) 280901e04c3fSmrg{ 281001e04c3fSmrg unsigned needed_mask = (1 << needed_count) - 1; 281101e04c3fSmrg const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count; 281201e04c3fSmrg 281301e04c3fSmrg /* The comparison to 32 is redundant, but without it GCC emits "warning: 281401e04c3fSmrg * cannot optimize possibly infinite loops" for the loop below. 281501e04c3fSmrg */ 281601e04c3fSmrg if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32)) 281701e04c3fSmrg return -1; 281801e04c3fSmrg 281901e04c3fSmrg for (int i = 0; i <= max_bit_to_test; i++) { 282001e04c3fSmrg if ((needed_mask & ~used_mask) == needed_mask) 282101e04c3fSmrg return i; 282201e04c3fSmrg 282301e04c3fSmrg needed_mask <<= 1; 282401e04c3fSmrg } 282501e04c3fSmrg 282601e04c3fSmrg return -1; 282701e04c3fSmrg} 282801e04c3fSmrg 282901e04c3fSmrg 283001e04c3fSmrg#define SAFE_MASK_FROM_INDEX(i) (((i) >= 32) ? ~0 : ((1 << (i)) - 1)) 283101e04c3fSmrg 283201e04c3fSmrg/** 28337e102996Smaya * Assign locations for either VS inputs or FS outputs. 283401e04c3fSmrg * 28357e102996Smaya * \param mem_ctx Temporary ralloc context used for linking. 28367e102996Smaya * \param prog Shader program whose variables need locations 28377e102996Smaya * assigned. 28387e102996Smaya * \param constants Driver specific constant values for the program. 28397e102996Smaya * \param target_index Selector for the program target to receive location 28407e102996Smaya * assignmnets. Must be either \c MESA_SHADER_VERTEX or 28417e102996Smaya * \c MESA_SHADER_FRAGMENT. 28427e102996Smaya * \param do_assignment Whether we are actually marking the assignment or we 28437e102996Smaya * are just doing a dry-run checking. 284401e04c3fSmrg * 284501e04c3fSmrg * \return 28467e102996Smaya * If locations are (or can be, in case of dry-running) successfully assigned, 28477e102996Smaya * true is returned. Otherwise an error is emitted to the shader link log and 28487e102996Smaya * false is returned. 284901e04c3fSmrg */ 285001e04c3fSmrgstatic bool 285101e04c3fSmrgassign_attribute_or_color_locations(void *mem_ctx, 285201e04c3fSmrg gl_shader_program *prog, 285301e04c3fSmrg struct gl_constants *constants, 285401e04c3fSmrg unsigned target_index, 285501e04c3fSmrg bool do_assignment) 285601e04c3fSmrg{ 285701e04c3fSmrg /* Maximum number of generic locations. This corresponds to either the 285801e04c3fSmrg * maximum number of draw buffers or the maximum number of generic 285901e04c3fSmrg * attributes. 286001e04c3fSmrg */ 286101e04c3fSmrg unsigned max_index = (target_index == MESA_SHADER_VERTEX) ? 286201e04c3fSmrg constants->Program[target_index].MaxAttribs : 286301e04c3fSmrg MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers); 286401e04c3fSmrg 286501e04c3fSmrg /* Mark invalid locations as being used. 286601e04c3fSmrg */ 286701e04c3fSmrg unsigned used_locations = ~SAFE_MASK_FROM_INDEX(max_index); 286801e04c3fSmrg unsigned double_storage_locations = 0; 286901e04c3fSmrg 287001e04c3fSmrg assert((target_index == MESA_SHADER_VERTEX) 287101e04c3fSmrg || (target_index == MESA_SHADER_FRAGMENT)); 287201e04c3fSmrg 287301e04c3fSmrg gl_linked_shader *const sh = prog->_LinkedShaders[target_index]; 287401e04c3fSmrg if (sh == NULL) 287501e04c3fSmrg return true; 287601e04c3fSmrg 287701e04c3fSmrg /* Operate in a total of four passes. 287801e04c3fSmrg * 287901e04c3fSmrg * 1. Invalidate the location assignments for all vertex shader inputs. 288001e04c3fSmrg * 288101e04c3fSmrg * 2. Assign locations for inputs that have user-defined (via 288201e04c3fSmrg * glBindVertexAttribLocation) locations and outputs that have 288301e04c3fSmrg * user-defined locations (via glBindFragDataLocation). 288401e04c3fSmrg * 288501e04c3fSmrg * 3. Sort the attributes without assigned locations by number of slots 288601e04c3fSmrg * required in decreasing order. Fragmentation caused by attribute 288701e04c3fSmrg * locations assigned by the application may prevent large attributes 288801e04c3fSmrg * from having enough contiguous space. 288901e04c3fSmrg * 289001e04c3fSmrg * 4. Assign locations to any inputs without assigned locations. 289101e04c3fSmrg */ 289201e04c3fSmrg 289301e04c3fSmrg const int generic_base = (target_index == MESA_SHADER_VERTEX) 289401e04c3fSmrg ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0; 289501e04c3fSmrg 289601e04c3fSmrg const enum ir_variable_mode direction = 289701e04c3fSmrg (target_index == MESA_SHADER_VERTEX) 289801e04c3fSmrg ? ir_var_shader_in : ir_var_shader_out; 289901e04c3fSmrg 290001e04c3fSmrg 290101e04c3fSmrg /* Temporary storage for the set of attributes that need locations assigned. 290201e04c3fSmrg */ 290301e04c3fSmrg struct temp_attr { 290401e04c3fSmrg unsigned slots; 290501e04c3fSmrg ir_variable *var; 290601e04c3fSmrg 290701e04c3fSmrg /* Used below in the call to qsort. */ 290801e04c3fSmrg static int compare(const void *a, const void *b) 290901e04c3fSmrg { 291001e04c3fSmrg const temp_attr *const l = (const temp_attr *) a; 291101e04c3fSmrg const temp_attr *const r = (const temp_attr *) b; 291201e04c3fSmrg 291301e04c3fSmrg /* Reversed because we want a descending order sort below. */ 291401e04c3fSmrg return r->slots - l->slots; 291501e04c3fSmrg } 291601e04c3fSmrg } to_assign[32]; 291701e04c3fSmrg assert(max_index <= 32); 291801e04c3fSmrg 291901e04c3fSmrg /* Temporary array for the set of attributes that have locations assigned, 292001e04c3fSmrg * for the purpose of checking overlapping slots/components of (non-ES) 292101e04c3fSmrg * fragment shader outputs. 292201e04c3fSmrg */ 292301e04c3fSmrg ir_variable *assigned[12 * 4]; /* (max # of FS outputs) * # components */ 292401e04c3fSmrg unsigned assigned_attr = 0; 292501e04c3fSmrg 292601e04c3fSmrg unsigned num_attr = 0; 292701e04c3fSmrg 292801e04c3fSmrg foreach_in_list(ir_instruction, node, sh->ir) { 292901e04c3fSmrg ir_variable *const var = node->as_variable(); 293001e04c3fSmrg 293101e04c3fSmrg if ((var == NULL) || (var->data.mode != (unsigned) direction)) 293201e04c3fSmrg continue; 293301e04c3fSmrg 293401e04c3fSmrg if (var->data.explicit_location) { 293501e04c3fSmrg var->data.is_unmatched_generic_inout = 0; 293601e04c3fSmrg if ((var->data.location >= (int)(max_index + generic_base)) 293701e04c3fSmrg || (var->data.location < 0)) { 293801e04c3fSmrg linker_error(prog, 293901e04c3fSmrg "invalid explicit location %d specified for `%s'\n", 294001e04c3fSmrg (var->data.location < 0) 294101e04c3fSmrg ? var->data.location 294201e04c3fSmrg : var->data.location - generic_base, 294301e04c3fSmrg var->name); 294401e04c3fSmrg return false; 294501e04c3fSmrg } 294601e04c3fSmrg } else if (target_index == MESA_SHADER_VERTEX) { 294701e04c3fSmrg unsigned binding; 294801e04c3fSmrg 294901e04c3fSmrg if (prog->AttributeBindings->get(binding, var->name)) { 295001e04c3fSmrg assert(binding >= VERT_ATTRIB_GENERIC0); 295101e04c3fSmrg var->data.location = binding; 295201e04c3fSmrg var->data.is_unmatched_generic_inout = 0; 295301e04c3fSmrg } 295401e04c3fSmrg } else if (target_index == MESA_SHADER_FRAGMENT) { 295501e04c3fSmrg unsigned binding; 295601e04c3fSmrg unsigned index; 295701e04c3fSmrg const char *name = var->name; 295801e04c3fSmrg const glsl_type *type = var->type; 295901e04c3fSmrg 296001e04c3fSmrg while (type) { 296101e04c3fSmrg /* Check if there's a binding for the variable name */ 296201e04c3fSmrg if (prog->FragDataBindings->get(binding, name)) { 296301e04c3fSmrg assert(binding >= FRAG_RESULT_DATA0); 296401e04c3fSmrg var->data.location = binding; 296501e04c3fSmrg var->data.is_unmatched_generic_inout = 0; 296601e04c3fSmrg 296701e04c3fSmrg if (prog->FragDataIndexBindings->get(index, name)) { 296801e04c3fSmrg var->data.index = index; 296901e04c3fSmrg } 297001e04c3fSmrg break; 297101e04c3fSmrg } 297201e04c3fSmrg 297301e04c3fSmrg /* If not, but it's an array type, look for name[0] */ 297401e04c3fSmrg if (type->is_array()) { 297501e04c3fSmrg name = ralloc_asprintf(mem_ctx, "%s[0]", name); 297601e04c3fSmrg type = type->fields.array; 297701e04c3fSmrg continue; 297801e04c3fSmrg } 297901e04c3fSmrg 298001e04c3fSmrg break; 298101e04c3fSmrg } 298201e04c3fSmrg } 298301e04c3fSmrg 298401e04c3fSmrg if (strcmp(var->name, "gl_LastFragData") == 0) 298501e04c3fSmrg continue; 298601e04c3fSmrg 298701e04c3fSmrg /* From GL4.5 core spec, section 15.2 (Shader Execution): 298801e04c3fSmrg * 298901e04c3fSmrg * "Output binding assignments will cause LinkProgram to fail: 299001e04c3fSmrg * ... 299101e04c3fSmrg * If the program has an active output assigned to a location greater 299201e04c3fSmrg * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has 299301e04c3fSmrg * an active output assigned an index greater than or equal to one;" 299401e04c3fSmrg */ 299501e04c3fSmrg if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 && 299601e04c3fSmrg var->data.location - generic_base >= 299701e04c3fSmrg (int) constants->MaxDualSourceDrawBuffers) { 299801e04c3fSmrg linker_error(prog, 299901e04c3fSmrg "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS " 300001e04c3fSmrg "with index %u for %s\n", 300101e04c3fSmrg var->data.location - generic_base, var->data.index, 300201e04c3fSmrg var->name); 300301e04c3fSmrg return false; 300401e04c3fSmrg } 300501e04c3fSmrg 300601e04c3fSmrg const unsigned slots = var->type->count_attribute_slots(target_index == MESA_SHADER_VERTEX); 300701e04c3fSmrg 300801e04c3fSmrg /* If the variable is not a built-in and has a location statically 300901e04c3fSmrg * assigned in the shader (presumably via a layout qualifier), make sure 301001e04c3fSmrg * that it doesn't collide with other assigned locations. Otherwise, 301101e04c3fSmrg * add it to the list of variables that need linker-assigned locations. 301201e04c3fSmrg */ 301301e04c3fSmrg if (var->data.location != -1) { 301401e04c3fSmrg if (var->data.location >= generic_base && var->data.index < 1) { 301501e04c3fSmrg /* From page 61 of the OpenGL 4.0 spec: 301601e04c3fSmrg * 301701e04c3fSmrg * "LinkProgram will fail if the attribute bindings assigned 301801e04c3fSmrg * by BindAttribLocation do not leave not enough space to 301901e04c3fSmrg * assign a location for an active matrix attribute or an 302001e04c3fSmrg * active attribute array, both of which require multiple 302101e04c3fSmrg * contiguous generic attributes." 302201e04c3fSmrg * 302301e04c3fSmrg * I think above text prohibits the aliasing of explicit and 302401e04c3fSmrg * automatic assignments. But, aliasing is allowed in manual 302501e04c3fSmrg * assignments of attribute locations. See below comments for 302601e04c3fSmrg * the details. 302701e04c3fSmrg * 302801e04c3fSmrg * From OpenGL 4.0 spec, page 61: 302901e04c3fSmrg * 303001e04c3fSmrg * "It is possible for an application to bind more than one 303101e04c3fSmrg * attribute name to the same location. This is referred to as 303201e04c3fSmrg * aliasing. This will only work if only one of the aliased 303301e04c3fSmrg * attributes is active in the executable program, or if no 303401e04c3fSmrg * path through the shader consumes more than one attribute of 303501e04c3fSmrg * a set of attributes aliased to the same location. A link 303601e04c3fSmrg * error can occur if the linker determines that every path 303701e04c3fSmrg * through the shader consumes multiple aliased attributes, 303801e04c3fSmrg * but implementations are not required to generate an error 303901e04c3fSmrg * in this case." 304001e04c3fSmrg * 304101e04c3fSmrg * From GLSL 4.30 spec, page 54: 304201e04c3fSmrg * 304301e04c3fSmrg * "A program will fail to link if any two non-vertex shader 304401e04c3fSmrg * input variables are assigned to the same location. For 304501e04c3fSmrg * vertex shaders, multiple input variables may be assigned 304601e04c3fSmrg * to the same location using either layout qualifiers or via 304701e04c3fSmrg * the OpenGL API. However, such aliasing is intended only to 304801e04c3fSmrg * support vertex shaders where each execution path accesses 304901e04c3fSmrg * at most one input per each location. Implementations are 305001e04c3fSmrg * permitted, but not required, to generate link-time errors 305101e04c3fSmrg * if they detect that every path through the vertex shader 305201e04c3fSmrg * executable accesses multiple inputs assigned to any single 305301e04c3fSmrg * location. For all shader types, a program will fail to link 305401e04c3fSmrg * if explicit location assignments leave the linker unable 305501e04c3fSmrg * to find space for other variables without explicit 305601e04c3fSmrg * assignments." 305701e04c3fSmrg * 305801e04c3fSmrg * From OpenGL ES 3.0 spec, page 56: 305901e04c3fSmrg * 306001e04c3fSmrg * "Binding more than one attribute name to the same location 306101e04c3fSmrg * is referred to as aliasing, and is not permitted in OpenGL 306201e04c3fSmrg * ES Shading Language 3.00 vertex shaders. LinkProgram will 306301e04c3fSmrg * fail when this condition exists. However, aliasing is 306401e04c3fSmrg * possible in OpenGL ES Shading Language 1.00 vertex shaders. 306501e04c3fSmrg * This will only work if only one of the aliased attributes 306601e04c3fSmrg * is active in the executable program, or if no path through 306701e04c3fSmrg * the shader consumes more than one attribute of a set of 306801e04c3fSmrg * attributes aliased to the same location. A link error can 306901e04c3fSmrg * occur if the linker determines that every path through the 307001e04c3fSmrg * shader consumes multiple aliased attributes, but implemen- 307101e04c3fSmrg * tations are not required to generate an error in this case." 307201e04c3fSmrg * 307301e04c3fSmrg * After looking at above references from OpenGL, OpenGL ES and 307401e04c3fSmrg * GLSL specifications, we allow aliasing of vertex input variables 307501e04c3fSmrg * in: OpenGL 2.0 (and above) and OpenGL ES 2.0. 307601e04c3fSmrg * 307701e04c3fSmrg * NOTE: This is not required by the spec but its worth mentioning 307801e04c3fSmrg * here that we're not doing anything to make sure that no path 307901e04c3fSmrg * through the vertex shader executable accesses multiple inputs 308001e04c3fSmrg * assigned to any single location. 308101e04c3fSmrg */ 308201e04c3fSmrg 308301e04c3fSmrg /* Mask representing the contiguous slots that will be used by 308401e04c3fSmrg * this attribute. 308501e04c3fSmrg */ 308601e04c3fSmrg const unsigned attr = var->data.location - generic_base; 308701e04c3fSmrg const unsigned use_mask = (1 << slots) - 1; 308801e04c3fSmrg const char *const string = (target_index == MESA_SHADER_VERTEX) 308901e04c3fSmrg ? "vertex shader input" : "fragment shader output"; 309001e04c3fSmrg 309101e04c3fSmrg /* Generate a link error if the requested locations for this 309201e04c3fSmrg * attribute exceed the maximum allowed attribute location. 309301e04c3fSmrg */ 309401e04c3fSmrg if (attr + slots > max_index) { 309501e04c3fSmrg linker_error(prog, 309601e04c3fSmrg "insufficient contiguous locations " 309701e04c3fSmrg "available for %s `%s' %d %d %d\n", string, 309801e04c3fSmrg var->name, used_locations, use_mask, attr); 309901e04c3fSmrg return false; 310001e04c3fSmrg } 310101e04c3fSmrg 310201e04c3fSmrg /* Generate a link error if the set of bits requested for this 310301e04c3fSmrg * attribute overlaps any previously allocated bits. 310401e04c3fSmrg */ 310501e04c3fSmrg if ((~(use_mask << attr) & used_locations) != used_locations) { 310601e04c3fSmrg if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) { 310701e04c3fSmrg /* From section 4.4.2 (Output Layout Qualifiers) of the GLSL 310801e04c3fSmrg * 4.40 spec: 310901e04c3fSmrg * 311001e04c3fSmrg * "Additionally, for fragment shader outputs, if two 311101e04c3fSmrg * variables are placed within the same location, they 311201e04c3fSmrg * must have the same underlying type (floating-point or 311301e04c3fSmrg * integer). No component aliasing of output variables or 311401e04c3fSmrg * members is allowed. 311501e04c3fSmrg */ 311601e04c3fSmrg for (unsigned i = 0; i < assigned_attr; i++) { 311701e04c3fSmrg unsigned assigned_slots = 311801e04c3fSmrg assigned[i]->type->count_attribute_slots(false); 311901e04c3fSmrg unsigned assig_attr = 312001e04c3fSmrg assigned[i]->data.location - generic_base; 312101e04c3fSmrg unsigned assigned_use_mask = (1 << assigned_slots) - 1; 312201e04c3fSmrg 312301e04c3fSmrg if ((assigned_use_mask << assig_attr) & 312401e04c3fSmrg (use_mask << attr)) { 312501e04c3fSmrg 312601e04c3fSmrg const glsl_type *assigned_type = 312701e04c3fSmrg assigned[i]->type->without_array(); 312801e04c3fSmrg const glsl_type *type = var->type->without_array(); 312901e04c3fSmrg if (assigned_type->base_type != type->base_type) { 313001e04c3fSmrg linker_error(prog, "types do not match for aliased" 313101e04c3fSmrg " %ss %s and %s\n", string, 313201e04c3fSmrg assigned[i]->name, var->name); 313301e04c3fSmrg return false; 313401e04c3fSmrg } 313501e04c3fSmrg 313601e04c3fSmrg unsigned assigned_component_mask = 313701e04c3fSmrg ((1 << assigned_type->vector_elements) - 1) << 313801e04c3fSmrg assigned[i]->data.location_frac; 313901e04c3fSmrg unsigned component_mask = 314001e04c3fSmrg ((1 << type->vector_elements) - 1) << 314101e04c3fSmrg var->data.location_frac; 314201e04c3fSmrg if (assigned_component_mask & component_mask) { 314301e04c3fSmrg linker_error(prog, "overlapping component is " 314401e04c3fSmrg "assigned to %ss %s and %s " 314501e04c3fSmrg "(component=%d)\n", 314601e04c3fSmrg string, assigned[i]->name, var->name, 314701e04c3fSmrg var->data.location_frac); 314801e04c3fSmrg return false; 314901e04c3fSmrg } 315001e04c3fSmrg } 315101e04c3fSmrg } 315201e04c3fSmrg } else if (target_index == MESA_SHADER_FRAGMENT || 315301e04c3fSmrg (prog->IsES && prog->data->Version >= 300)) { 315401e04c3fSmrg linker_error(prog, "overlapping location is assigned " 315501e04c3fSmrg "to %s `%s' %d %d %d\n", string, var->name, 315601e04c3fSmrg used_locations, use_mask, attr); 315701e04c3fSmrg return false; 315801e04c3fSmrg } else { 315901e04c3fSmrg linker_warning(prog, "overlapping location is assigned " 316001e04c3fSmrg "to %s `%s' %d %d %d\n", string, var->name, 316101e04c3fSmrg used_locations, use_mask, attr); 316201e04c3fSmrg } 316301e04c3fSmrg } 316401e04c3fSmrg 316501e04c3fSmrg if (target_index == MESA_SHADER_FRAGMENT && !prog->IsES) { 316601e04c3fSmrg /* Only track assigned variables for non-ES fragment shaders 316701e04c3fSmrg * to avoid overflowing the array. 316801e04c3fSmrg * 316901e04c3fSmrg * At most one variable per fragment output component should 317001e04c3fSmrg * reach this. 317101e04c3fSmrg */ 317201e04c3fSmrg assert(assigned_attr < ARRAY_SIZE(assigned)); 317301e04c3fSmrg assigned[assigned_attr] = var; 317401e04c3fSmrg assigned_attr++; 317501e04c3fSmrg } 317601e04c3fSmrg 317701e04c3fSmrg used_locations |= (use_mask << attr); 317801e04c3fSmrg 317901e04c3fSmrg /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes): 318001e04c3fSmrg * 318101e04c3fSmrg * "A program with more than the value of MAX_VERTEX_ATTRIBS 318201e04c3fSmrg * active attribute variables may fail to link, unless 318301e04c3fSmrg * device-dependent optimizations are able to make the program 318401e04c3fSmrg * fit within available hardware resources. For the purposes 318501e04c3fSmrg * of this test, attribute variables of the type dvec3, dvec4, 318601e04c3fSmrg * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may 318701e04c3fSmrg * count as consuming twice as many attributes as equivalent 318801e04c3fSmrg * single-precision types. While these types use the same number 318901e04c3fSmrg * of generic attributes as their single-precision equivalents, 319001e04c3fSmrg * implementations are permitted to consume two single-precision 319101e04c3fSmrg * vectors of internal storage for each three- or four-component 319201e04c3fSmrg * double-precision vector." 319301e04c3fSmrg * 319401e04c3fSmrg * Mark this attribute slot as taking up twice as much space 319501e04c3fSmrg * so we can count it properly against limits. According to 319601e04c3fSmrg * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this 319701e04c3fSmrg * is optional behavior, but it seems preferable. 319801e04c3fSmrg */ 319901e04c3fSmrg if (var->type->without_array()->is_dual_slot()) 320001e04c3fSmrg double_storage_locations |= (use_mask << attr); 320101e04c3fSmrg } 320201e04c3fSmrg 320301e04c3fSmrg continue; 320401e04c3fSmrg } 320501e04c3fSmrg 320601e04c3fSmrg if (num_attr >= max_index) { 320701e04c3fSmrg linker_error(prog, "too many %s (max %u)", 320801e04c3fSmrg target_index == MESA_SHADER_VERTEX ? 320901e04c3fSmrg "vertex shader inputs" : "fragment shader outputs", 321001e04c3fSmrg max_index); 321101e04c3fSmrg return false; 321201e04c3fSmrg } 321301e04c3fSmrg to_assign[num_attr].slots = slots; 321401e04c3fSmrg to_assign[num_attr].var = var; 321501e04c3fSmrg num_attr++; 321601e04c3fSmrg } 321701e04c3fSmrg 321801e04c3fSmrg if (!do_assignment) 321901e04c3fSmrg return true; 322001e04c3fSmrg 322101e04c3fSmrg if (target_index == MESA_SHADER_VERTEX) { 322201e04c3fSmrg unsigned total_attribs_size = 322301e04c3fSmrg util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) + 322401e04c3fSmrg util_bitcount(double_storage_locations); 322501e04c3fSmrg if (total_attribs_size > max_index) { 322601e04c3fSmrg linker_error(prog, 322701e04c3fSmrg "attempt to use %d vertex attribute slots only %d available ", 322801e04c3fSmrg total_attribs_size, max_index); 322901e04c3fSmrg return false; 323001e04c3fSmrg } 323101e04c3fSmrg } 323201e04c3fSmrg 323301e04c3fSmrg /* If all of the attributes were assigned locations by the application (or 323401e04c3fSmrg * are built-in attributes with fixed locations), return early. This should 323501e04c3fSmrg * be the common case. 323601e04c3fSmrg */ 323701e04c3fSmrg if (num_attr == 0) 323801e04c3fSmrg return true; 323901e04c3fSmrg 324001e04c3fSmrg qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare); 324101e04c3fSmrg 324201e04c3fSmrg if (target_index == MESA_SHADER_VERTEX) { 324301e04c3fSmrg /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can 324401e04c3fSmrg * only be explicitly assigned by via glBindAttribLocation. Mark it as 324501e04c3fSmrg * reserved to prevent it from being automatically allocated below. 324601e04c3fSmrg */ 324701e04c3fSmrg find_deref_visitor find("gl_Vertex"); 324801e04c3fSmrg find.run(sh->ir); 324901e04c3fSmrg if (find.variable_found()) 325001e04c3fSmrg used_locations |= (1 << 0); 325101e04c3fSmrg } 325201e04c3fSmrg 325301e04c3fSmrg for (unsigned i = 0; i < num_attr; i++) { 325401e04c3fSmrg /* Mask representing the contiguous slots that will be used by this 325501e04c3fSmrg * attribute. 325601e04c3fSmrg */ 325701e04c3fSmrg const unsigned use_mask = (1 << to_assign[i].slots) - 1; 325801e04c3fSmrg 325901e04c3fSmrg int location = find_available_slots(used_locations, to_assign[i].slots); 326001e04c3fSmrg 326101e04c3fSmrg if (location < 0) { 326201e04c3fSmrg const char *const string = (target_index == MESA_SHADER_VERTEX) 326301e04c3fSmrg ? "vertex shader input" : "fragment shader output"; 326401e04c3fSmrg 326501e04c3fSmrg linker_error(prog, 326601e04c3fSmrg "insufficient contiguous locations " 326701e04c3fSmrg "available for %s `%s'\n", 326801e04c3fSmrg string, to_assign[i].var->name); 326901e04c3fSmrg return false; 327001e04c3fSmrg } 327101e04c3fSmrg 327201e04c3fSmrg to_assign[i].var->data.location = generic_base + location; 327301e04c3fSmrg to_assign[i].var->data.is_unmatched_generic_inout = 0; 327401e04c3fSmrg used_locations |= (use_mask << location); 327501e04c3fSmrg 327601e04c3fSmrg if (to_assign[i].var->type->without_array()->is_dual_slot()) 327701e04c3fSmrg double_storage_locations |= (use_mask << location); 327801e04c3fSmrg } 327901e04c3fSmrg 328001e04c3fSmrg /* Now that we have all the locations, from the GL 4.5 core spec, section 328101e04c3fSmrg * 11.1.1 (Vertex Attributes), dvec3, dvec4, dmat2x3, dmat2x4, dmat3, 328201e04c3fSmrg * dmat3x4, dmat4x3, and dmat4 count as consuming twice as many attributes 328301e04c3fSmrg * as equivalent single-precision types. 328401e04c3fSmrg */ 328501e04c3fSmrg if (target_index == MESA_SHADER_VERTEX) { 328601e04c3fSmrg unsigned total_attribs_size = 328701e04c3fSmrg util_bitcount(used_locations & SAFE_MASK_FROM_INDEX(max_index)) + 328801e04c3fSmrg util_bitcount(double_storage_locations); 328901e04c3fSmrg if (total_attribs_size > max_index) { 329001e04c3fSmrg linker_error(prog, 329101e04c3fSmrg "attempt to use %d vertex attribute slots only %d available ", 329201e04c3fSmrg total_attribs_size, max_index); 329301e04c3fSmrg return false; 329401e04c3fSmrg } 329501e04c3fSmrg } 329601e04c3fSmrg 329701e04c3fSmrg return true; 329801e04c3fSmrg} 329901e04c3fSmrg 330001e04c3fSmrg/** 330101e04c3fSmrg * Match explicit locations of outputs to inputs and deactivate the 330201e04c3fSmrg * unmatch flag if found so we don't optimise them away. 330301e04c3fSmrg */ 330401e04c3fSmrgstatic void 330501e04c3fSmrgmatch_explicit_outputs_to_inputs(gl_linked_shader *producer, 330601e04c3fSmrg gl_linked_shader *consumer) 330701e04c3fSmrg{ 330801e04c3fSmrg glsl_symbol_table parameters; 330901e04c3fSmrg ir_variable *explicit_locations[MAX_VARYINGS_INCL_PATCH][4] = 331001e04c3fSmrg { {NULL, NULL} }; 331101e04c3fSmrg 331201e04c3fSmrg /* Find all shader outputs in the "producer" stage. 331301e04c3fSmrg */ 331401e04c3fSmrg foreach_in_list(ir_instruction, node, producer->ir) { 331501e04c3fSmrg ir_variable *const var = node->as_variable(); 331601e04c3fSmrg 331701e04c3fSmrg if ((var == NULL) || (var->data.mode != ir_var_shader_out)) 331801e04c3fSmrg continue; 331901e04c3fSmrg 332001e04c3fSmrg if (var->data.explicit_location && 332101e04c3fSmrg var->data.location >= VARYING_SLOT_VAR0) { 332201e04c3fSmrg const unsigned idx = var->data.location - VARYING_SLOT_VAR0; 332301e04c3fSmrg if (explicit_locations[idx][var->data.location_frac] == NULL) 332401e04c3fSmrg explicit_locations[idx][var->data.location_frac] = var; 3325993e1d59Smrg 3326993e1d59Smrg /* Always match TCS outputs. They are shared by all invocations 3327993e1d59Smrg * within a patch and can be used as shared memory. 3328993e1d59Smrg */ 3329993e1d59Smrg if (producer->Stage == MESA_SHADER_TESS_CTRL) 3330993e1d59Smrg var->data.is_unmatched_generic_inout = 0; 333101e04c3fSmrg } 333201e04c3fSmrg } 333301e04c3fSmrg 333401e04c3fSmrg /* Match inputs to outputs */ 333501e04c3fSmrg foreach_in_list(ir_instruction, node, consumer->ir) { 333601e04c3fSmrg ir_variable *const input = node->as_variable(); 333701e04c3fSmrg 333801e04c3fSmrg if ((input == NULL) || (input->data.mode != ir_var_shader_in)) 333901e04c3fSmrg continue; 334001e04c3fSmrg 334101e04c3fSmrg ir_variable *output = NULL; 334201e04c3fSmrg if (input->data.explicit_location 334301e04c3fSmrg && input->data.location >= VARYING_SLOT_VAR0) { 334401e04c3fSmrg output = explicit_locations[input->data.location - VARYING_SLOT_VAR0] 334501e04c3fSmrg [input->data.location_frac]; 334601e04c3fSmrg 334701e04c3fSmrg if (output != NULL){ 334801e04c3fSmrg input->data.is_unmatched_generic_inout = 0; 334901e04c3fSmrg output->data.is_unmatched_generic_inout = 0; 335001e04c3fSmrg } 335101e04c3fSmrg } 335201e04c3fSmrg } 335301e04c3fSmrg} 335401e04c3fSmrg 335501e04c3fSmrg/** 335601e04c3fSmrg * Store the gl_FragDepth layout in the gl_shader_program struct. 335701e04c3fSmrg */ 335801e04c3fSmrgstatic void 335901e04c3fSmrgstore_fragdepth_layout(struct gl_shader_program *prog) 336001e04c3fSmrg{ 336101e04c3fSmrg if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { 336201e04c3fSmrg return; 336301e04c3fSmrg } 336401e04c3fSmrg 336501e04c3fSmrg struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir; 336601e04c3fSmrg 336701e04c3fSmrg /* We don't look up the gl_FragDepth symbol directly because if 336801e04c3fSmrg * gl_FragDepth is not used in the shader, it's removed from the IR. 336901e04c3fSmrg * However, the symbol won't be removed from the symbol table. 337001e04c3fSmrg * 337101e04c3fSmrg * We're only interested in the cases where the variable is NOT removed 337201e04c3fSmrg * from the IR. 337301e04c3fSmrg */ 337401e04c3fSmrg foreach_in_list(ir_instruction, node, ir) { 337501e04c3fSmrg ir_variable *const var = node->as_variable(); 337601e04c3fSmrg 337701e04c3fSmrg if (var == NULL || var->data.mode != ir_var_shader_out) { 337801e04c3fSmrg continue; 337901e04c3fSmrg } 338001e04c3fSmrg 338101e04c3fSmrg if (strcmp(var->name, "gl_FragDepth") == 0) { 338201e04c3fSmrg switch (var->data.depth_layout) { 338301e04c3fSmrg case ir_depth_layout_none: 338401e04c3fSmrg prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE; 338501e04c3fSmrg return; 338601e04c3fSmrg case ir_depth_layout_any: 338701e04c3fSmrg prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY; 338801e04c3fSmrg return; 338901e04c3fSmrg case ir_depth_layout_greater: 339001e04c3fSmrg prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER; 339101e04c3fSmrg return; 339201e04c3fSmrg case ir_depth_layout_less: 339301e04c3fSmrg prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS; 339401e04c3fSmrg return; 339501e04c3fSmrg case ir_depth_layout_unchanged: 339601e04c3fSmrg prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED; 339701e04c3fSmrg return; 339801e04c3fSmrg default: 339901e04c3fSmrg assert(0); 340001e04c3fSmrg return; 340101e04c3fSmrg } 340201e04c3fSmrg } 340301e04c3fSmrg } 340401e04c3fSmrg} 340501e04c3fSmrg 340601e04c3fSmrg/** 340701e04c3fSmrg * Validate shader image resources. 340801e04c3fSmrg */ 340901e04c3fSmrgstatic void 341001e04c3fSmrgcheck_image_resources(struct gl_context *ctx, struct gl_shader_program *prog) 341101e04c3fSmrg{ 341201e04c3fSmrg unsigned total_image_units = 0; 341301e04c3fSmrg unsigned fragment_outputs = 0; 341401e04c3fSmrg unsigned total_shader_storage_blocks = 0; 341501e04c3fSmrg 341601e04c3fSmrg if (!ctx->Extensions.ARB_shader_image_load_store) 341701e04c3fSmrg return; 341801e04c3fSmrg 341901e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 342001e04c3fSmrg struct gl_linked_shader *sh = prog->_LinkedShaders[i]; 342101e04c3fSmrg 342201e04c3fSmrg if (sh) { 342301e04c3fSmrg total_image_units += sh->Program->info.num_images; 342401e04c3fSmrg total_shader_storage_blocks += sh->Program->info.num_ssbos; 342501e04c3fSmrg 342601e04c3fSmrg if (i == MESA_SHADER_FRAGMENT) { 342701e04c3fSmrg foreach_in_list(ir_instruction, node, sh->ir) { 342801e04c3fSmrg ir_variable *var = node->as_variable(); 342901e04c3fSmrg if (var && var->data.mode == ir_var_shader_out) 343001e04c3fSmrg /* since there are no double fs outputs - pass false */ 343101e04c3fSmrg fragment_outputs += var->type->count_attribute_slots(false); 343201e04c3fSmrg } 343301e04c3fSmrg } 343401e04c3fSmrg } 343501e04c3fSmrg } 343601e04c3fSmrg 343701e04c3fSmrg if (total_image_units > ctx->Const.MaxCombinedImageUniforms) 343801e04c3fSmrg linker_error(prog, "Too many combined image uniforms\n"); 343901e04c3fSmrg 344001e04c3fSmrg if (total_image_units + fragment_outputs + total_shader_storage_blocks > 344101e04c3fSmrg ctx->Const.MaxCombinedShaderOutputResources) 344201e04c3fSmrg linker_error(prog, "Too many combined image uniforms, shader storage " 344301e04c3fSmrg " buffers and fragment outputs\n"); 344401e04c3fSmrg} 344501e04c3fSmrg 344601e04c3fSmrg 344701e04c3fSmrg/** 344801e04c3fSmrg * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION 344901e04c3fSmrg * for a variable, checks for overlaps between other uniforms using explicit 345001e04c3fSmrg * locations. 345101e04c3fSmrg */ 345201e04c3fSmrgstatic int 345301e04c3fSmrgreserve_explicit_locations(struct gl_shader_program *prog, 345401e04c3fSmrg string_to_uint_map *map, ir_variable *var) 345501e04c3fSmrg{ 345601e04c3fSmrg unsigned slots = var->type->uniform_locations(); 345701e04c3fSmrg unsigned max_loc = var->data.location + slots - 1; 345801e04c3fSmrg unsigned return_value = slots; 345901e04c3fSmrg 346001e04c3fSmrg /* Resize remap table if locations do not fit in the current one. */ 346101e04c3fSmrg if (max_loc + 1 > prog->NumUniformRemapTable) { 346201e04c3fSmrg prog->UniformRemapTable = 346301e04c3fSmrg reralloc(prog, prog->UniformRemapTable, 346401e04c3fSmrg gl_uniform_storage *, 346501e04c3fSmrg max_loc + 1); 346601e04c3fSmrg 346701e04c3fSmrg if (!prog->UniformRemapTable) { 346801e04c3fSmrg linker_error(prog, "Out of memory during linking.\n"); 346901e04c3fSmrg return -1; 347001e04c3fSmrg } 347101e04c3fSmrg 347201e04c3fSmrg /* Initialize allocated space. */ 347301e04c3fSmrg for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++) 347401e04c3fSmrg prog->UniformRemapTable[i] = NULL; 347501e04c3fSmrg 347601e04c3fSmrg prog->NumUniformRemapTable = max_loc + 1; 347701e04c3fSmrg } 347801e04c3fSmrg 347901e04c3fSmrg for (unsigned i = 0; i < slots; i++) { 348001e04c3fSmrg unsigned loc = var->data.location + i; 348101e04c3fSmrg 348201e04c3fSmrg /* Check if location is already used. */ 348301e04c3fSmrg if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { 348401e04c3fSmrg 348501e04c3fSmrg /* Possibly same uniform from a different stage, this is ok. */ 348601e04c3fSmrg unsigned hash_loc; 348701e04c3fSmrg if (map->get(hash_loc, var->name) && hash_loc == loc - i) { 348801e04c3fSmrg return_value = 0; 348901e04c3fSmrg continue; 349001e04c3fSmrg } 349101e04c3fSmrg 349201e04c3fSmrg /* ARB_explicit_uniform_location specification states: 349301e04c3fSmrg * 349401e04c3fSmrg * "No two default-block uniform variables in the program can have 349501e04c3fSmrg * the same location, even if they are unused, otherwise a compiler 349601e04c3fSmrg * or linker error will be generated." 349701e04c3fSmrg */ 349801e04c3fSmrg linker_error(prog, 349901e04c3fSmrg "location qualifier for uniform %s overlaps " 350001e04c3fSmrg "previously used location\n", 350101e04c3fSmrg var->name); 350201e04c3fSmrg return -1; 350301e04c3fSmrg } 350401e04c3fSmrg 350501e04c3fSmrg /* Initialize location as inactive before optimization 350601e04c3fSmrg * rounds and location assignment. 350701e04c3fSmrg */ 350801e04c3fSmrg prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; 350901e04c3fSmrg } 351001e04c3fSmrg 351101e04c3fSmrg /* Note, base location used for arrays. */ 351201e04c3fSmrg map->put(var->data.location, var->name); 351301e04c3fSmrg 351401e04c3fSmrg return return_value; 351501e04c3fSmrg} 351601e04c3fSmrg 351701e04c3fSmrgstatic bool 351801e04c3fSmrgreserve_subroutine_explicit_locations(struct gl_shader_program *prog, 351901e04c3fSmrg struct gl_program *p, 352001e04c3fSmrg ir_variable *var) 352101e04c3fSmrg{ 352201e04c3fSmrg unsigned slots = var->type->uniform_locations(); 352301e04c3fSmrg unsigned max_loc = var->data.location + slots - 1; 352401e04c3fSmrg 352501e04c3fSmrg /* Resize remap table if locations do not fit in the current one. */ 352601e04c3fSmrg if (max_loc + 1 > p->sh.NumSubroutineUniformRemapTable) { 352701e04c3fSmrg p->sh.SubroutineUniformRemapTable = 352801e04c3fSmrg reralloc(p, p->sh.SubroutineUniformRemapTable, 352901e04c3fSmrg gl_uniform_storage *, 353001e04c3fSmrg max_loc + 1); 353101e04c3fSmrg 353201e04c3fSmrg if (!p->sh.SubroutineUniformRemapTable) { 353301e04c3fSmrg linker_error(prog, "Out of memory during linking.\n"); 353401e04c3fSmrg return false; 353501e04c3fSmrg } 353601e04c3fSmrg 353701e04c3fSmrg /* Initialize allocated space. */ 353801e04c3fSmrg for (unsigned i = p->sh.NumSubroutineUniformRemapTable; i < max_loc + 1; i++) 353901e04c3fSmrg p->sh.SubroutineUniformRemapTable[i] = NULL; 354001e04c3fSmrg 354101e04c3fSmrg p->sh.NumSubroutineUniformRemapTable = max_loc + 1; 354201e04c3fSmrg } 354301e04c3fSmrg 354401e04c3fSmrg for (unsigned i = 0; i < slots; i++) { 354501e04c3fSmrg unsigned loc = var->data.location + i; 354601e04c3fSmrg 354701e04c3fSmrg /* Check if location is already used. */ 354801e04c3fSmrg if (p->sh.SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) { 354901e04c3fSmrg 355001e04c3fSmrg /* ARB_explicit_uniform_location specification states: 355101e04c3fSmrg * "No two subroutine uniform variables can have the same location 355201e04c3fSmrg * in the same shader stage, otherwise a compiler or linker error 355301e04c3fSmrg * will be generated." 355401e04c3fSmrg */ 355501e04c3fSmrg linker_error(prog, 355601e04c3fSmrg "location qualifier for uniform %s overlaps " 355701e04c3fSmrg "previously used location\n", 355801e04c3fSmrg var->name); 355901e04c3fSmrg return false; 356001e04c3fSmrg } 356101e04c3fSmrg 356201e04c3fSmrg /* Initialize location as inactive before optimization 356301e04c3fSmrg * rounds and location assignment. 356401e04c3fSmrg */ 356501e04c3fSmrg p->sh.SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION; 356601e04c3fSmrg } 356701e04c3fSmrg 356801e04c3fSmrg return true; 356901e04c3fSmrg} 357001e04c3fSmrg/** 357101e04c3fSmrg * Check and reserve all explicit uniform locations, called before 357201e04c3fSmrg * any optimizations happen to handle also inactive uniforms and 357301e04c3fSmrg * inactive array elements that may get trimmed away. 357401e04c3fSmrg */ 357501e04c3fSmrgstatic void 357601e04c3fSmrgcheck_explicit_uniform_locations(struct gl_context *ctx, 357701e04c3fSmrg struct gl_shader_program *prog) 357801e04c3fSmrg{ 357901e04c3fSmrg prog->NumExplicitUniformLocations = 0; 358001e04c3fSmrg 358101e04c3fSmrg if (!ctx->Extensions.ARB_explicit_uniform_location) 358201e04c3fSmrg return; 358301e04c3fSmrg 358401e04c3fSmrg /* This map is used to detect if overlapping explicit locations 358501e04c3fSmrg * occur with the same uniform (from different stage) or a different one. 358601e04c3fSmrg */ 358701e04c3fSmrg string_to_uint_map *uniform_map = new string_to_uint_map; 358801e04c3fSmrg 358901e04c3fSmrg if (!uniform_map) { 359001e04c3fSmrg linker_error(prog, "Out of memory during linking.\n"); 359101e04c3fSmrg return; 359201e04c3fSmrg } 359301e04c3fSmrg 359401e04c3fSmrg unsigned entries_total = 0; 359501e04c3fSmrg unsigned mask = prog->data->linked_stages; 359601e04c3fSmrg while (mask) { 359701e04c3fSmrg const int i = u_bit_scan(&mask); 359801e04c3fSmrg struct gl_program *p = prog->_LinkedShaders[i]->Program; 359901e04c3fSmrg 360001e04c3fSmrg foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { 360101e04c3fSmrg ir_variable *var = node->as_variable(); 360201e04c3fSmrg if (!var || var->data.mode != ir_var_uniform) 360301e04c3fSmrg continue; 360401e04c3fSmrg 360501e04c3fSmrg if (var->data.explicit_location) { 360601e04c3fSmrg bool ret = false; 360701e04c3fSmrg if (var->type->without_array()->is_subroutine()) 360801e04c3fSmrg ret = reserve_subroutine_explicit_locations(prog, p, var); 360901e04c3fSmrg else { 361001e04c3fSmrg int slots = reserve_explicit_locations(prog, uniform_map, 361101e04c3fSmrg var); 361201e04c3fSmrg if (slots != -1) { 361301e04c3fSmrg ret = true; 361401e04c3fSmrg entries_total += slots; 361501e04c3fSmrg } 361601e04c3fSmrg } 361701e04c3fSmrg if (!ret) { 361801e04c3fSmrg delete uniform_map; 361901e04c3fSmrg return; 362001e04c3fSmrg } 362101e04c3fSmrg } 362201e04c3fSmrg } 362301e04c3fSmrg } 362401e04c3fSmrg 362501e04c3fSmrg link_util_update_empty_uniform_locations(prog); 362601e04c3fSmrg 362701e04c3fSmrg delete uniform_map; 362801e04c3fSmrg prog->NumExplicitUniformLocations = entries_total; 362901e04c3fSmrg} 363001e04c3fSmrg 363101e04c3fSmrg/* Function checks if a variable var is a packed varying and 363201e04c3fSmrg * if given name is part of packed varying's list. 363301e04c3fSmrg * 363401e04c3fSmrg * If a variable is a packed varying, it has a name like 363501e04c3fSmrg * 'packed:a,b,c' where a, b and c are separate variables. 363601e04c3fSmrg */ 363701e04c3fSmrgstatic bool 363801e04c3fSmrgincluded_in_packed_varying(ir_variable *var, const char *name) 363901e04c3fSmrg{ 364001e04c3fSmrg if (strncmp(var->name, "packed:", 7) != 0) 364101e04c3fSmrg return false; 364201e04c3fSmrg 364301e04c3fSmrg char *list = strdup(var->name + 7); 364401e04c3fSmrg assert(list); 364501e04c3fSmrg 364601e04c3fSmrg bool found = false; 364701e04c3fSmrg char *saveptr; 364801e04c3fSmrg char *token = strtok_r(list, ",", &saveptr); 364901e04c3fSmrg while (token) { 365001e04c3fSmrg if (strcmp(token, name) == 0) { 365101e04c3fSmrg found = true; 365201e04c3fSmrg break; 365301e04c3fSmrg } 365401e04c3fSmrg token = strtok_r(NULL, ",", &saveptr); 365501e04c3fSmrg } 365601e04c3fSmrg free(list); 365701e04c3fSmrg return found; 365801e04c3fSmrg} 365901e04c3fSmrg 366001e04c3fSmrg/** 366101e04c3fSmrg * Function builds a stage reference bitmask from variable name. 366201e04c3fSmrg */ 366301e04c3fSmrgstatic uint8_t 366401e04c3fSmrgbuild_stageref(struct gl_shader_program *shProg, const char *name, 366501e04c3fSmrg unsigned mode) 366601e04c3fSmrg{ 366701e04c3fSmrg uint8_t stages = 0; 366801e04c3fSmrg 366901e04c3fSmrg /* Note, that we assume MAX 8 stages, if there will be more stages, type 367001e04c3fSmrg * used for reference mask in gl_program_resource will need to be changed. 367101e04c3fSmrg */ 367201e04c3fSmrg assert(MESA_SHADER_STAGES < 8); 367301e04c3fSmrg 367401e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 367501e04c3fSmrg struct gl_linked_shader *sh = shProg->_LinkedShaders[i]; 367601e04c3fSmrg if (!sh) 367701e04c3fSmrg continue; 367801e04c3fSmrg 367901e04c3fSmrg /* Shader symbol table may contain variables that have 368001e04c3fSmrg * been optimized away. Search IR for the variable instead. 368101e04c3fSmrg */ 368201e04c3fSmrg foreach_in_list(ir_instruction, node, sh->ir) { 368301e04c3fSmrg ir_variable *var = node->as_variable(); 368401e04c3fSmrg if (var) { 368501e04c3fSmrg unsigned baselen = strlen(var->name); 368601e04c3fSmrg 368701e04c3fSmrg if (included_in_packed_varying(var, name)) { 368801e04c3fSmrg stages |= (1 << i); 368901e04c3fSmrg break; 369001e04c3fSmrg } 369101e04c3fSmrg 369201e04c3fSmrg /* Type needs to match if specified, otherwise we might 369301e04c3fSmrg * pick a variable with same name but different interface. 369401e04c3fSmrg */ 369501e04c3fSmrg if (var->data.mode != mode) 369601e04c3fSmrg continue; 369701e04c3fSmrg 369801e04c3fSmrg if (strncmp(var->name, name, baselen) == 0) { 369901e04c3fSmrg /* Check for exact name matches but also check for arrays and 370001e04c3fSmrg * structs. 370101e04c3fSmrg */ 370201e04c3fSmrg if (name[baselen] == '\0' || 370301e04c3fSmrg name[baselen] == '[' || 370401e04c3fSmrg name[baselen] == '.') { 370501e04c3fSmrg stages |= (1 << i); 370601e04c3fSmrg break; 370701e04c3fSmrg } 370801e04c3fSmrg } 370901e04c3fSmrg } 371001e04c3fSmrg } 371101e04c3fSmrg } 371201e04c3fSmrg return stages; 371301e04c3fSmrg} 371401e04c3fSmrg 371501e04c3fSmrg/** 371601e04c3fSmrg * Create gl_shader_variable from ir_variable class. 371701e04c3fSmrg */ 371801e04c3fSmrgstatic gl_shader_variable * 371901e04c3fSmrgcreate_shader_variable(struct gl_shader_program *shProg, 372001e04c3fSmrg const ir_variable *in, 372101e04c3fSmrg const char *name, const glsl_type *type, 372201e04c3fSmrg const glsl_type *interface_type, 372301e04c3fSmrg bool use_implicit_location, int location, 372401e04c3fSmrg const glsl_type *outermost_struct_type) 372501e04c3fSmrg{ 372601e04c3fSmrg /* Allocate zero-initialized memory to ensure that bitfield padding 372701e04c3fSmrg * is zero. 372801e04c3fSmrg */ 372901e04c3fSmrg gl_shader_variable *out = rzalloc(shProg, struct gl_shader_variable); 373001e04c3fSmrg if (!out) 373101e04c3fSmrg return NULL; 373201e04c3fSmrg 373301e04c3fSmrg /* Since gl_VertexID may be lowered to gl_VertexIDMESA, but applications 373401e04c3fSmrg * expect to see gl_VertexID in the program resource list. Pretend. 373501e04c3fSmrg */ 373601e04c3fSmrg if (in->data.mode == ir_var_system_value && 373701e04c3fSmrg in->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) { 373801e04c3fSmrg out->name = ralloc_strdup(shProg, "gl_VertexID"); 373901e04c3fSmrg } else if ((in->data.mode == ir_var_shader_out && 374001e04c3fSmrg in->data.location == VARYING_SLOT_TESS_LEVEL_OUTER) || 374101e04c3fSmrg (in->data.mode == ir_var_system_value && 374201e04c3fSmrg in->data.location == SYSTEM_VALUE_TESS_LEVEL_OUTER)) { 374301e04c3fSmrg out->name = ralloc_strdup(shProg, "gl_TessLevelOuter"); 374401e04c3fSmrg type = glsl_type::get_array_instance(glsl_type::float_type, 4); 374501e04c3fSmrg } else if ((in->data.mode == ir_var_shader_out && 374601e04c3fSmrg in->data.location == VARYING_SLOT_TESS_LEVEL_INNER) || 374701e04c3fSmrg (in->data.mode == ir_var_system_value && 374801e04c3fSmrg in->data.location == SYSTEM_VALUE_TESS_LEVEL_INNER)) { 374901e04c3fSmrg out->name = ralloc_strdup(shProg, "gl_TessLevelInner"); 375001e04c3fSmrg type = glsl_type::get_array_instance(glsl_type::float_type, 2); 375101e04c3fSmrg } else { 375201e04c3fSmrg out->name = ralloc_strdup(shProg, name); 375301e04c3fSmrg } 375401e04c3fSmrg 375501e04c3fSmrg if (!out->name) 375601e04c3fSmrg return NULL; 375701e04c3fSmrg 375801e04c3fSmrg /* The ARB_program_interface_query spec says: 375901e04c3fSmrg * 376001e04c3fSmrg * "Not all active variables are assigned valid locations; the 376101e04c3fSmrg * following variables will have an effective location of -1: 376201e04c3fSmrg * 376301e04c3fSmrg * * uniforms declared as atomic counters; 376401e04c3fSmrg * 376501e04c3fSmrg * * members of a uniform block; 376601e04c3fSmrg * 376701e04c3fSmrg * * built-in inputs, outputs, and uniforms (starting with "gl_"); and 376801e04c3fSmrg * 376901e04c3fSmrg * * inputs or outputs not declared with a "location" layout 377001e04c3fSmrg * qualifier, except for vertex shader inputs and fragment shader 377101e04c3fSmrg * outputs." 377201e04c3fSmrg */ 377301e04c3fSmrg if (in->type->is_atomic_uint() || is_gl_identifier(in->name) || 377401e04c3fSmrg !(in->data.explicit_location || use_implicit_location)) { 377501e04c3fSmrg out->location = -1; 377601e04c3fSmrg } else { 377701e04c3fSmrg out->location = location; 377801e04c3fSmrg } 377901e04c3fSmrg 378001e04c3fSmrg out->type = type; 378101e04c3fSmrg out->outermost_struct_type = outermost_struct_type; 378201e04c3fSmrg out->interface_type = interface_type; 378301e04c3fSmrg out->component = in->data.location_frac; 378401e04c3fSmrg out->index = in->data.index; 378501e04c3fSmrg out->patch = in->data.patch; 378601e04c3fSmrg out->mode = in->data.mode; 378701e04c3fSmrg out->interpolation = in->data.interpolation; 378801e04c3fSmrg out->explicit_location = in->data.explicit_location; 378901e04c3fSmrg out->precision = in->data.precision; 379001e04c3fSmrg 379101e04c3fSmrg return out; 379201e04c3fSmrg} 379301e04c3fSmrg 379401e04c3fSmrgstatic bool 379501e04c3fSmrgadd_shader_variable(const struct gl_context *ctx, 379601e04c3fSmrg struct gl_shader_program *shProg, 379701e04c3fSmrg struct set *resource_set, 379801e04c3fSmrg unsigned stage_mask, 379901e04c3fSmrg GLenum programInterface, ir_variable *var, 380001e04c3fSmrg const char *name, const glsl_type *type, 380101e04c3fSmrg bool use_implicit_location, int location, 380201e04c3fSmrg bool inouts_share_location, 380301e04c3fSmrg const glsl_type *outermost_struct_type = NULL) 380401e04c3fSmrg{ 380501e04c3fSmrg const glsl_type *interface_type = var->get_interface_type(); 380601e04c3fSmrg 380701e04c3fSmrg if (outermost_struct_type == NULL) { 380801e04c3fSmrg if (var->data.from_named_ifc_block) { 380901e04c3fSmrg const char *interface_name = interface_type->name; 381001e04c3fSmrg 381101e04c3fSmrg if (interface_type->is_array()) { 381201e04c3fSmrg /* Issue #16 of the ARB_program_interface_query spec says: 381301e04c3fSmrg * 381401e04c3fSmrg * "* If a variable is a member of an interface block without an 381501e04c3fSmrg * instance name, it is enumerated using just the variable name. 381601e04c3fSmrg * 381701e04c3fSmrg * * If a variable is a member of an interface block with an 381801e04c3fSmrg * instance name, it is enumerated as "BlockName.Member", where 381901e04c3fSmrg * "BlockName" is the name of the interface block (not the 382001e04c3fSmrg * instance name) and "Member" is the name of the variable." 382101e04c3fSmrg * 382201e04c3fSmrg * In particular, it indicates that it should be "BlockName", 382301e04c3fSmrg * not "BlockName[array length]". The conformance suite and 382401e04c3fSmrg * dEQP both require this behavior. 382501e04c3fSmrg * 382601e04c3fSmrg * Here, we unwrap the extra array level added by named interface 382701e04c3fSmrg * block array lowering so we have the correct variable type. We 382801e04c3fSmrg * also unwrap the interface type when constructing the name. 382901e04c3fSmrg * 383001e04c3fSmrg * We leave interface_type the same so that ES 3.x SSO pipeline 383101e04c3fSmrg * validation can enforce the rules requiring array length to 383201e04c3fSmrg * match on interface blocks. 383301e04c3fSmrg */ 383401e04c3fSmrg type = type->fields.array; 383501e04c3fSmrg 383601e04c3fSmrg interface_name = interface_type->fields.array->name; 383701e04c3fSmrg } 383801e04c3fSmrg 383901e04c3fSmrg name = ralloc_asprintf(shProg, "%s.%s", interface_name, name); 384001e04c3fSmrg } 384101e04c3fSmrg } 384201e04c3fSmrg 384301e04c3fSmrg switch (type->base_type) { 384401e04c3fSmrg case GLSL_TYPE_STRUCT: { 384501e04c3fSmrg /* The ARB_program_interface_query spec says: 384601e04c3fSmrg * 384701e04c3fSmrg * "For an active variable declared as a structure, a separate entry 384801e04c3fSmrg * will be generated for each active structure member. The name of 384901e04c3fSmrg * each entry is formed by concatenating the name of the structure, 385001e04c3fSmrg * the "." character, and the name of the structure member. If a 385101e04c3fSmrg * structure member to enumerate is itself a structure or array, 385201e04c3fSmrg * these enumeration rules are applied recursively." 385301e04c3fSmrg */ 385401e04c3fSmrg if (outermost_struct_type == NULL) 385501e04c3fSmrg outermost_struct_type = type; 385601e04c3fSmrg 385701e04c3fSmrg unsigned field_location = location; 385801e04c3fSmrg for (unsigned i = 0; i < type->length; i++) { 385901e04c3fSmrg const struct glsl_struct_field *field = &type->fields.structure[i]; 386001e04c3fSmrg char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name); 386101e04c3fSmrg if (!add_shader_variable(ctx, shProg, resource_set, 386201e04c3fSmrg stage_mask, programInterface, 386301e04c3fSmrg var, field_name, field->type, 386401e04c3fSmrg use_implicit_location, field_location, 386501e04c3fSmrg false, outermost_struct_type)) 386601e04c3fSmrg return false; 386701e04c3fSmrg 386801e04c3fSmrg field_location += field->type->count_attribute_slots(false); 386901e04c3fSmrg } 387001e04c3fSmrg return true; 387101e04c3fSmrg } 387201e04c3fSmrg 387301e04c3fSmrg case GLSL_TYPE_ARRAY: { 387401e04c3fSmrg /* The ARB_program_interface_query spec says: 387501e04c3fSmrg * 387601e04c3fSmrg * "For an active variable declared as an array of basic types, a 387701e04c3fSmrg * single entry will be generated, with its name string formed by 387801e04c3fSmrg * concatenating the name of the array and the string "[0]"." 387901e04c3fSmrg * 388001e04c3fSmrg * "For an active variable declared as an array of an aggregate data 388101e04c3fSmrg * type (structures or arrays), a separate entry will be generated 388201e04c3fSmrg * for each active array element, unless noted immediately below. 388301e04c3fSmrg * The name of each entry is formed by concatenating the name of 388401e04c3fSmrg * the array, the "[" character, an integer identifying the element 388501e04c3fSmrg * number, and the "]" character. These enumeration rules are 388601e04c3fSmrg * applied recursively, treating each enumerated array element as a 388701e04c3fSmrg * separate active variable." 388801e04c3fSmrg */ 388901e04c3fSmrg const struct glsl_type *array_type = type->fields.array; 389001e04c3fSmrg if (array_type->base_type == GLSL_TYPE_STRUCT || 389101e04c3fSmrg array_type->base_type == GLSL_TYPE_ARRAY) { 389201e04c3fSmrg unsigned elem_location = location; 389301e04c3fSmrg unsigned stride = inouts_share_location ? 0 : 389401e04c3fSmrg array_type->count_attribute_slots(false); 389501e04c3fSmrg for (unsigned i = 0; i < type->length; i++) { 389601e04c3fSmrg char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i); 389701e04c3fSmrg if (!add_shader_variable(ctx, shProg, resource_set, 389801e04c3fSmrg stage_mask, programInterface, 389901e04c3fSmrg var, elem, array_type, 390001e04c3fSmrg use_implicit_location, elem_location, 390101e04c3fSmrg false, outermost_struct_type)) 390201e04c3fSmrg return false; 390301e04c3fSmrg elem_location += stride; 390401e04c3fSmrg } 390501e04c3fSmrg return true; 390601e04c3fSmrg } 39077ec681f3Smrg FALLTHROUGH; 390801e04c3fSmrg } 390901e04c3fSmrg 391001e04c3fSmrg default: { 391101e04c3fSmrg /* The ARB_program_interface_query spec says: 391201e04c3fSmrg * 391301e04c3fSmrg * "For an active variable declared as a single instance of a basic 391401e04c3fSmrg * type, a single entry will be generated, using the variable name 391501e04c3fSmrg * from the shader source." 391601e04c3fSmrg */ 391701e04c3fSmrg gl_shader_variable *sha_v = 391801e04c3fSmrg create_shader_variable(shProg, var, name, type, interface_type, 391901e04c3fSmrg use_implicit_location, location, 392001e04c3fSmrg outermost_struct_type); 392101e04c3fSmrg if (!sha_v) 392201e04c3fSmrg return false; 392301e04c3fSmrg 392401e04c3fSmrg return link_util_add_program_resource(shProg, resource_set, 392501e04c3fSmrg programInterface, sha_v, stage_mask); 392601e04c3fSmrg } 392701e04c3fSmrg } 392801e04c3fSmrg} 392901e04c3fSmrg 393001e04c3fSmrgstatic bool 393101e04c3fSmrginout_has_same_location(const ir_variable *var, unsigned stage) 393201e04c3fSmrg{ 393301e04c3fSmrg if (!var->data.patch && 393401e04c3fSmrg ((var->data.mode == ir_var_shader_out && 393501e04c3fSmrg stage == MESA_SHADER_TESS_CTRL) || 393601e04c3fSmrg (var->data.mode == ir_var_shader_in && 393701e04c3fSmrg (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_TESS_EVAL || 393801e04c3fSmrg stage == MESA_SHADER_GEOMETRY)))) 393901e04c3fSmrg return true; 394001e04c3fSmrg else 394101e04c3fSmrg return false; 394201e04c3fSmrg} 394301e04c3fSmrg 394401e04c3fSmrgstatic bool 394501e04c3fSmrgadd_interface_variables(const struct gl_context *ctx, 394601e04c3fSmrg struct gl_shader_program *shProg, 394701e04c3fSmrg struct set *resource_set, 394801e04c3fSmrg unsigned stage, GLenum programInterface) 394901e04c3fSmrg{ 395001e04c3fSmrg exec_list *ir = shProg->_LinkedShaders[stage]->ir; 395101e04c3fSmrg 395201e04c3fSmrg foreach_in_list(ir_instruction, node, ir) { 395301e04c3fSmrg ir_variable *var = node->as_variable(); 395401e04c3fSmrg 395501e04c3fSmrg if (!var || var->data.how_declared == ir_var_hidden) 395601e04c3fSmrg continue; 395701e04c3fSmrg 395801e04c3fSmrg int loc_bias; 395901e04c3fSmrg 396001e04c3fSmrg switch (var->data.mode) { 396101e04c3fSmrg case ir_var_system_value: 396201e04c3fSmrg case ir_var_shader_in: 396301e04c3fSmrg if (programInterface != GL_PROGRAM_INPUT) 396401e04c3fSmrg continue; 396501e04c3fSmrg loc_bias = (stage == MESA_SHADER_VERTEX) ? int(VERT_ATTRIB_GENERIC0) 396601e04c3fSmrg : int(VARYING_SLOT_VAR0); 396701e04c3fSmrg break; 396801e04c3fSmrg case ir_var_shader_out: 396901e04c3fSmrg if (programInterface != GL_PROGRAM_OUTPUT) 397001e04c3fSmrg continue; 397101e04c3fSmrg loc_bias = (stage == MESA_SHADER_FRAGMENT) ? int(FRAG_RESULT_DATA0) 397201e04c3fSmrg : int(VARYING_SLOT_VAR0); 397301e04c3fSmrg break; 397401e04c3fSmrg default: 397501e04c3fSmrg continue; 397601e04c3fSmrg }; 397701e04c3fSmrg 397801e04c3fSmrg if (var->data.patch) 397901e04c3fSmrg loc_bias = int(VARYING_SLOT_PATCH0); 398001e04c3fSmrg 398101e04c3fSmrg /* Skip packed varyings, packed varyings are handled separately 398201e04c3fSmrg * by add_packed_varyings. 398301e04c3fSmrg */ 398401e04c3fSmrg if (strncmp(var->name, "packed:", 7) == 0) 398501e04c3fSmrg continue; 398601e04c3fSmrg 398701e04c3fSmrg /* Skip fragdata arrays, these are handled separately 398801e04c3fSmrg * by add_fragdata_arrays. 398901e04c3fSmrg */ 399001e04c3fSmrg if (strncmp(var->name, "gl_out_FragData", 15) == 0) 399101e04c3fSmrg continue; 399201e04c3fSmrg 399301e04c3fSmrg const bool vs_input_or_fs_output = 399401e04c3fSmrg (stage == MESA_SHADER_VERTEX && var->data.mode == ir_var_shader_in) || 399501e04c3fSmrg (stage == MESA_SHADER_FRAGMENT && var->data.mode == ir_var_shader_out); 399601e04c3fSmrg 399701e04c3fSmrg if (!add_shader_variable(ctx, shProg, resource_set, 399801e04c3fSmrg 1 << stage, programInterface, 399901e04c3fSmrg var, var->name, var->type, vs_input_or_fs_output, 400001e04c3fSmrg var->data.location - loc_bias, 400101e04c3fSmrg inout_has_same_location(var, stage))) 400201e04c3fSmrg return false; 400301e04c3fSmrg } 400401e04c3fSmrg return true; 400501e04c3fSmrg} 400601e04c3fSmrg 400701e04c3fSmrgstatic bool 400801e04c3fSmrgadd_packed_varyings(const struct gl_context *ctx, 400901e04c3fSmrg struct gl_shader_program *shProg, 401001e04c3fSmrg struct set *resource_set, 401101e04c3fSmrg int stage, GLenum type) 401201e04c3fSmrg{ 401301e04c3fSmrg struct gl_linked_shader *sh = shProg->_LinkedShaders[stage]; 401401e04c3fSmrg GLenum iface; 401501e04c3fSmrg 401601e04c3fSmrg if (!sh || !sh->packed_varyings) 401701e04c3fSmrg return true; 401801e04c3fSmrg 401901e04c3fSmrg foreach_in_list(ir_instruction, node, sh->packed_varyings) { 402001e04c3fSmrg ir_variable *var = node->as_variable(); 402101e04c3fSmrg if (var) { 402201e04c3fSmrg switch (var->data.mode) { 402301e04c3fSmrg case ir_var_shader_in: 402401e04c3fSmrg iface = GL_PROGRAM_INPUT; 402501e04c3fSmrg break; 402601e04c3fSmrg case ir_var_shader_out: 402701e04c3fSmrg iface = GL_PROGRAM_OUTPUT; 402801e04c3fSmrg break; 402901e04c3fSmrg default: 403001e04c3fSmrg unreachable("unexpected type"); 403101e04c3fSmrg } 403201e04c3fSmrg 403301e04c3fSmrg if (type == iface) { 403401e04c3fSmrg const int stage_mask = 403501e04c3fSmrg build_stageref(shProg, var->name, var->data.mode); 403601e04c3fSmrg if (!add_shader_variable(ctx, shProg, resource_set, 403701e04c3fSmrg stage_mask, 403801e04c3fSmrg iface, var, var->name, var->type, false, 403901e04c3fSmrg var->data.location - VARYING_SLOT_VAR0, 404001e04c3fSmrg inout_has_same_location(var, stage))) 404101e04c3fSmrg return false; 404201e04c3fSmrg } 404301e04c3fSmrg } 404401e04c3fSmrg } 404501e04c3fSmrg return true; 404601e04c3fSmrg} 404701e04c3fSmrg 404801e04c3fSmrgstatic bool 404901e04c3fSmrgadd_fragdata_arrays(const struct gl_context *ctx, 405001e04c3fSmrg struct gl_shader_program *shProg, 405101e04c3fSmrg struct set *resource_set) 405201e04c3fSmrg{ 405301e04c3fSmrg struct gl_linked_shader *sh = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]; 405401e04c3fSmrg 405501e04c3fSmrg if (!sh || !sh->fragdata_arrays) 405601e04c3fSmrg return true; 405701e04c3fSmrg 405801e04c3fSmrg foreach_in_list(ir_instruction, node, sh->fragdata_arrays) { 405901e04c3fSmrg ir_variable *var = node->as_variable(); 406001e04c3fSmrg if (var) { 406101e04c3fSmrg assert(var->data.mode == ir_var_shader_out); 406201e04c3fSmrg 406301e04c3fSmrg if (!add_shader_variable(ctx, shProg, resource_set, 406401e04c3fSmrg 1 << MESA_SHADER_FRAGMENT, 406501e04c3fSmrg GL_PROGRAM_OUTPUT, var, var->name, var->type, 406601e04c3fSmrg true, var->data.location - FRAG_RESULT_DATA0, 406701e04c3fSmrg false)) 406801e04c3fSmrg return false; 406901e04c3fSmrg } 407001e04c3fSmrg } 407101e04c3fSmrg return true; 407201e04c3fSmrg} 407301e04c3fSmrg 407401e04c3fSmrg/** 407501e04c3fSmrg * Builds up a list of program resources that point to existing 407601e04c3fSmrg * resource data. 407701e04c3fSmrg */ 407801e04c3fSmrgvoid 407901e04c3fSmrgbuild_program_resource_list(struct gl_context *ctx, 40807ec681f3Smrg struct gl_shader_program *shProg, 40817ec681f3Smrg bool add_packed_varyings_only) 408201e04c3fSmrg{ 408301e04c3fSmrg /* Rebuild resource list. */ 408401e04c3fSmrg if (shProg->data->ProgramResourceList) { 408501e04c3fSmrg ralloc_free(shProg->data->ProgramResourceList); 408601e04c3fSmrg shProg->data->ProgramResourceList = NULL; 408701e04c3fSmrg shProg->data->NumProgramResourceList = 0; 408801e04c3fSmrg } 408901e04c3fSmrg 409001e04c3fSmrg int input_stage = MESA_SHADER_STAGES, output_stage = 0; 409101e04c3fSmrg 409201e04c3fSmrg /* Determine first input and final output stage. These are used to 409301e04c3fSmrg * detect which variables should be enumerated in the resource list 409401e04c3fSmrg * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT. 409501e04c3fSmrg */ 409601e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 409701e04c3fSmrg if (!shProg->_LinkedShaders[i]) 409801e04c3fSmrg continue; 409901e04c3fSmrg if (input_stage == MESA_SHADER_STAGES) 410001e04c3fSmrg input_stage = i; 410101e04c3fSmrg output_stage = i; 410201e04c3fSmrg } 410301e04c3fSmrg 410401e04c3fSmrg /* Empty shader, no resources. */ 410501e04c3fSmrg if (input_stage == MESA_SHADER_STAGES && output_stage == 0) 410601e04c3fSmrg return; 410701e04c3fSmrg 41087e102996Smaya struct set *resource_set = _mesa_pointer_set_create(NULL); 410901e04c3fSmrg 411001e04c3fSmrg /* Program interface needs to expose varyings in case of SSO. */ 411101e04c3fSmrg if (shProg->SeparateShader) { 411201e04c3fSmrg if (!add_packed_varyings(ctx, shProg, resource_set, 411301e04c3fSmrg input_stage, GL_PROGRAM_INPUT)) 411401e04c3fSmrg return; 411501e04c3fSmrg 411601e04c3fSmrg if (!add_packed_varyings(ctx, shProg, resource_set, 411701e04c3fSmrg output_stage, GL_PROGRAM_OUTPUT)) 411801e04c3fSmrg return; 411901e04c3fSmrg } 412001e04c3fSmrg 41217ec681f3Smrg if (add_packed_varyings_only) { 41227ec681f3Smrg _mesa_set_destroy(resource_set, NULL); 41237ec681f3Smrg return; 41247ec681f3Smrg } 41257ec681f3Smrg 412601e04c3fSmrg if (!add_fragdata_arrays(ctx, shProg, resource_set)) 412701e04c3fSmrg return; 412801e04c3fSmrg 412901e04c3fSmrg /* Add inputs and outputs to the resource list. */ 413001e04c3fSmrg if (!add_interface_variables(ctx, shProg, resource_set, 413101e04c3fSmrg input_stage, GL_PROGRAM_INPUT)) 413201e04c3fSmrg return; 413301e04c3fSmrg 413401e04c3fSmrg if (!add_interface_variables(ctx, shProg, resource_set, 413501e04c3fSmrg output_stage, GL_PROGRAM_OUTPUT)) 413601e04c3fSmrg return; 413701e04c3fSmrg 413801e04c3fSmrg if (shProg->last_vert_prog) { 413901e04c3fSmrg struct gl_transform_feedback_info *linked_xfb = 414001e04c3fSmrg shProg->last_vert_prog->sh.LinkedTransformFeedback; 414101e04c3fSmrg 414201e04c3fSmrg /* Add transform feedback varyings. */ 414301e04c3fSmrg if (linked_xfb->NumVarying > 0) { 414401e04c3fSmrg for (int i = 0; i < linked_xfb->NumVarying; i++) { 414501e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, 414601e04c3fSmrg GL_TRANSFORM_FEEDBACK_VARYING, 414701e04c3fSmrg &linked_xfb->Varyings[i], 0)) 414801e04c3fSmrg return; 414901e04c3fSmrg } 415001e04c3fSmrg } 415101e04c3fSmrg 415201e04c3fSmrg /* Add transform feedback buffers. */ 415301e04c3fSmrg for (unsigned i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) { 415401e04c3fSmrg if ((linked_xfb->ActiveBuffers >> i) & 1) { 415501e04c3fSmrg linked_xfb->Buffers[i].Binding = i; 415601e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, 415701e04c3fSmrg GL_TRANSFORM_FEEDBACK_BUFFER, 415801e04c3fSmrg &linked_xfb->Buffers[i], 0)) 415901e04c3fSmrg return; 416001e04c3fSmrg } 416101e04c3fSmrg } 416201e04c3fSmrg } 416301e04c3fSmrg 41647ec681f3Smrg int top_level_array_base_offset = -1; 41657ec681f3Smrg int top_level_array_size_in_bytes = -1; 41667ec681f3Smrg int second_element_offset = -1; 41677ec681f3Smrg int buffer_block_index = -1; 41687ec681f3Smrg 416901e04c3fSmrg /* Add uniforms from uniform storage. */ 417001e04c3fSmrg for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) { 417101e04c3fSmrg /* Do not add uniforms internally used by Mesa. */ 417201e04c3fSmrg if (shProg->data->UniformStorage[i].hidden) 417301e04c3fSmrg continue; 417401e04c3fSmrg 417501e04c3fSmrg bool is_shader_storage = 417601e04c3fSmrg shProg->data->UniformStorage[i].is_shader_storage; 417701e04c3fSmrg GLenum type = is_shader_storage ? GL_BUFFER_VARIABLE : GL_UNIFORM; 41787ec681f3Smrg if (!link_util_should_add_buffer_variable(shProg, 41797ec681f3Smrg &shProg->data->UniformStorage[i], 41807ec681f3Smrg top_level_array_base_offset, 41817ec681f3Smrg top_level_array_size_in_bytes, 41827ec681f3Smrg second_element_offset, 41837ec681f3Smrg buffer_block_index)) 418401e04c3fSmrg continue; 418501e04c3fSmrg 418601e04c3fSmrg if (is_shader_storage) { 41877ec681f3Smrg /* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources: 41887ec681f3Smrg * 41897ec681f3Smrg * "For an active shader storage block member declared as an array 41907ec681f3Smrg * of an aggregate type, an entry will be generated only for the 41917ec681f3Smrg * first array element, regardless of its type. Such block members 41927ec681f3Smrg * are referred to as top-level arrays. If the block member is an 41937ec681f3Smrg * aggregate type, the enumeration rules are then applied 41947ec681f3Smrg * recursively." 41957ec681f3Smrg * 41967ec681f3Smrg * Below we update our tracking values used by 41977ec681f3Smrg * link_util_should_add_buffer_variable(). We only want to reset the 41987ec681f3Smrg * offsets once we have moved past the first element. 41997ec681f3Smrg */ 42007ec681f3Smrg if (shProg->data->UniformStorage[i].offset >= second_element_offset) { 42017ec681f3Smrg top_level_array_base_offset = 42027ec681f3Smrg shProg->data->UniformStorage[i].offset; 42037ec681f3Smrg 42047ec681f3Smrg top_level_array_size_in_bytes = 42057ec681f3Smrg shProg->data->UniformStorage[i].top_level_array_size * 42067ec681f3Smrg shProg->data->UniformStorage[i].top_level_array_stride; 42077ec681f3Smrg 42087ec681f3Smrg /* Set or reset the second element offset. For non arrays this 42097ec681f3Smrg * will be set to -1. 42107ec681f3Smrg */ 42117ec681f3Smrg second_element_offset = top_level_array_size_in_bytes ? 42127ec681f3Smrg top_level_array_base_offset + 42137ec681f3Smrg shProg->data->UniformStorage[i].top_level_array_stride : -1; 42147ec681f3Smrg } 42157ec681f3Smrg 42167ec681f3Smrg buffer_block_index = shProg->data->UniformStorage[i].block_index; 421701e04c3fSmrg } 421801e04c3fSmrg 42197ec681f3Smrg uint8_t stageref = shProg->data->UniformStorage[i].active_shader_mask; 422001e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, type, 422101e04c3fSmrg &shProg->data->UniformStorage[i], stageref)) 422201e04c3fSmrg return; 422301e04c3fSmrg } 422401e04c3fSmrg 422501e04c3fSmrg /* Add program uniform blocks. */ 422601e04c3fSmrg for (unsigned i = 0; i < shProg->data->NumUniformBlocks; i++) { 422701e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, GL_UNIFORM_BLOCK, 422801e04c3fSmrg &shProg->data->UniformBlocks[i], 0)) 422901e04c3fSmrg return; 423001e04c3fSmrg } 423101e04c3fSmrg 423201e04c3fSmrg /* Add program shader storage blocks. */ 423301e04c3fSmrg for (unsigned i = 0; i < shProg->data->NumShaderStorageBlocks; i++) { 423401e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, GL_SHADER_STORAGE_BLOCK, 423501e04c3fSmrg &shProg->data->ShaderStorageBlocks[i], 0)) 423601e04c3fSmrg return; 423701e04c3fSmrg } 423801e04c3fSmrg 423901e04c3fSmrg /* Add atomic counter buffers. */ 424001e04c3fSmrg for (unsigned i = 0; i < shProg->data->NumAtomicBuffers; i++) { 424101e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, GL_ATOMIC_COUNTER_BUFFER, 424201e04c3fSmrg &shProg->data->AtomicBuffers[i], 0)) 424301e04c3fSmrg return; 424401e04c3fSmrg } 424501e04c3fSmrg 424601e04c3fSmrg for (unsigned i = 0; i < shProg->data->NumUniformStorage; i++) { 424701e04c3fSmrg GLenum type; 424801e04c3fSmrg if (!shProg->data->UniformStorage[i].hidden) 424901e04c3fSmrg continue; 425001e04c3fSmrg 425101e04c3fSmrg for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) { 425201e04c3fSmrg if (!shProg->data->UniformStorage[i].opaque[j].active || 425301e04c3fSmrg !shProg->data->UniformStorage[i].type->is_subroutine()) 425401e04c3fSmrg continue; 425501e04c3fSmrg 425601e04c3fSmrg type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j); 425701e04c3fSmrg /* add shader subroutines */ 425801e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, 425901e04c3fSmrg type, &shProg->data->UniformStorage[i], 0)) 426001e04c3fSmrg return; 426101e04c3fSmrg } 426201e04c3fSmrg } 426301e04c3fSmrg 426401e04c3fSmrg unsigned mask = shProg->data->linked_stages; 426501e04c3fSmrg while (mask) { 426601e04c3fSmrg const int i = u_bit_scan(&mask); 426701e04c3fSmrg struct gl_program *p = shProg->_LinkedShaders[i]->Program; 426801e04c3fSmrg 426901e04c3fSmrg GLuint type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i); 427001e04c3fSmrg for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { 427101e04c3fSmrg if (!link_util_add_program_resource(shProg, resource_set, 427201e04c3fSmrg type, &p->sh.SubroutineFunctions[j], 0)) 427301e04c3fSmrg return; 427401e04c3fSmrg } 427501e04c3fSmrg } 427601e04c3fSmrg 427701e04c3fSmrg _mesa_set_destroy(resource_set, NULL); 427801e04c3fSmrg} 427901e04c3fSmrg 428001e04c3fSmrg/** 428101e04c3fSmrg * This check is done to make sure we allow only constant expression 428201e04c3fSmrg * indexing and "constant-index-expression" (indexing with an expression 428301e04c3fSmrg * that includes loop induction variable). 428401e04c3fSmrg */ 428501e04c3fSmrgstatic bool 428601e04c3fSmrgvalidate_sampler_array_indexing(struct gl_context *ctx, 428701e04c3fSmrg struct gl_shader_program *prog) 428801e04c3fSmrg{ 428901e04c3fSmrg dynamic_sampler_array_indexing_visitor v; 429001e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 429101e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 429201e04c3fSmrg continue; 429301e04c3fSmrg 429401e04c3fSmrg bool no_dynamic_indexing = 429501e04c3fSmrg ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler; 429601e04c3fSmrg 429701e04c3fSmrg /* Search for array derefs in shader. */ 429801e04c3fSmrg v.run(prog->_LinkedShaders[i]->ir); 429901e04c3fSmrg if (v.uses_dynamic_sampler_array_indexing()) { 430001e04c3fSmrg const char *msg = "sampler arrays indexed with non-constant " 430101e04c3fSmrg "expressions is forbidden in GLSL %s %u"; 430201e04c3fSmrg /* Backend has indicated that it has no dynamic indexing support. */ 430301e04c3fSmrg if (no_dynamic_indexing) { 430401e04c3fSmrg linker_error(prog, msg, prog->IsES ? "ES" : "", 430501e04c3fSmrg prog->data->Version); 430601e04c3fSmrg return false; 430701e04c3fSmrg } else { 430801e04c3fSmrg linker_warning(prog, msg, prog->IsES ? "ES" : "", 430901e04c3fSmrg prog->data->Version); 431001e04c3fSmrg } 431101e04c3fSmrg } 431201e04c3fSmrg } 431301e04c3fSmrg return true; 431401e04c3fSmrg} 431501e04c3fSmrg 431601e04c3fSmrgstatic void 431701e04c3fSmrglink_assign_subroutine_types(struct gl_shader_program *prog) 431801e04c3fSmrg{ 431901e04c3fSmrg unsigned mask = prog->data->linked_stages; 432001e04c3fSmrg while (mask) { 432101e04c3fSmrg const int i = u_bit_scan(&mask); 432201e04c3fSmrg gl_program *p = prog->_LinkedShaders[i]->Program; 432301e04c3fSmrg 432401e04c3fSmrg p->sh.MaxSubroutineFunctionIndex = 0; 432501e04c3fSmrg foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) { 432601e04c3fSmrg ir_function *fn = node->as_function(); 432701e04c3fSmrg if (!fn) 432801e04c3fSmrg continue; 432901e04c3fSmrg 433001e04c3fSmrg if (fn->is_subroutine) 433101e04c3fSmrg p->sh.NumSubroutineUniformTypes++; 433201e04c3fSmrg 433301e04c3fSmrg if (!fn->num_subroutine_types) 433401e04c3fSmrg continue; 433501e04c3fSmrg 433601e04c3fSmrg /* these should have been calculated earlier. */ 433701e04c3fSmrg assert(fn->subroutine_index != -1); 433801e04c3fSmrg if (p->sh.NumSubroutineFunctions + 1 > MAX_SUBROUTINES) { 433901e04c3fSmrg linker_error(prog, "Too many subroutine functions declared.\n"); 434001e04c3fSmrg return; 434101e04c3fSmrg } 434201e04c3fSmrg p->sh.SubroutineFunctions = reralloc(p, p->sh.SubroutineFunctions, 434301e04c3fSmrg struct gl_subroutine_function, 434401e04c3fSmrg p->sh.NumSubroutineFunctions + 1); 434501e04c3fSmrg p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].name = ralloc_strdup(p, fn->name); 434601e04c3fSmrg p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types; 434701e04c3fSmrg p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types = 434801e04c3fSmrg ralloc_array(p, const struct glsl_type *, 434901e04c3fSmrg fn->num_subroutine_types); 435001e04c3fSmrg 435101e04c3fSmrg /* From Section 4.4.4(Subroutine Function Layout Qualifiers) of the 435201e04c3fSmrg * GLSL 4.5 spec: 435301e04c3fSmrg * 435401e04c3fSmrg * "Each subroutine with an index qualifier in the shader must be 435501e04c3fSmrg * given a unique index, otherwise a compile or link error will be 435601e04c3fSmrg * generated." 435701e04c3fSmrg */ 435801e04c3fSmrg for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { 435901e04c3fSmrg if (p->sh.SubroutineFunctions[j].index != -1 && 436001e04c3fSmrg p->sh.SubroutineFunctions[j].index == fn->subroutine_index) { 436101e04c3fSmrg linker_error(prog, "each subroutine index qualifier in the " 436201e04c3fSmrg "shader must be unique\n"); 436301e04c3fSmrg return; 436401e04c3fSmrg } 436501e04c3fSmrg } 436601e04c3fSmrg p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].index = 436701e04c3fSmrg fn->subroutine_index; 436801e04c3fSmrg 436901e04c3fSmrg if (fn->subroutine_index > (int)p->sh.MaxSubroutineFunctionIndex) 437001e04c3fSmrg p->sh.MaxSubroutineFunctionIndex = fn->subroutine_index; 437101e04c3fSmrg 437201e04c3fSmrg for (int j = 0; j < fn->num_subroutine_types; j++) 437301e04c3fSmrg p->sh.SubroutineFunctions[p->sh.NumSubroutineFunctions].types[j] = fn->subroutine_types[j]; 437401e04c3fSmrg p->sh.NumSubroutineFunctions++; 437501e04c3fSmrg } 437601e04c3fSmrg } 437701e04c3fSmrg} 437801e04c3fSmrg 437901e04c3fSmrgstatic void 438001e04c3fSmrgverify_subroutine_associated_funcs(struct gl_shader_program *prog) 438101e04c3fSmrg{ 438201e04c3fSmrg unsigned mask = prog->data->linked_stages; 438301e04c3fSmrg while (mask) { 438401e04c3fSmrg const int i = u_bit_scan(&mask); 438501e04c3fSmrg gl_program *p = prog->_LinkedShaders[i]->Program; 438601e04c3fSmrg glsl_symbol_table *symbols = prog->_LinkedShaders[i]->symbols; 438701e04c3fSmrg 438801e04c3fSmrg /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says: 438901e04c3fSmrg * 439001e04c3fSmrg * "A program will fail to compile or link if any shader 439101e04c3fSmrg * or stage contains two or more functions with the same 439201e04c3fSmrg * name if the name is associated with a subroutine type." 439301e04c3fSmrg */ 439401e04c3fSmrg for (unsigned j = 0; j < p->sh.NumSubroutineFunctions; j++) { 439501e04c3fSmrg unsigned definitions = 0; 439601e04c3fSmrg char *name = p->sh.SubroutineFunctions[j].name; 439701e04c3fSmrg ir_function *fn = symbols->get_function(name); 439801e04c3fSmrg 439901e04c3fSmrg /* Calculate number of function definitions with the same name */ 440001e04c3fSmrg foreach_in_list(ir_function_signature, sig, &fn->signatures) { 440101e04c3fSmrg if (sig->is_defined) { 440201e04c3fSmrg if (++definitions > 1) { 440301e04c3fSmrg linker_error(prog, "%s shader contains two or more function " 440401e04c3fSmrg "definitions with name `%s', which is " 440501e04c3fSmrg "associated with a subroutine type.\n", 440601e04c3fSmrg _mesa_shader_stage_to_string(i), 440701e04c3fSmrg fn->name); 440801e04c3fSmrg return; 440901e04c3fSmrg } 441001e04c3fSmrg } 441101e04c3fSmrg } 441201e04c3fSmrg } 441301e04c3fSmrg } 441401e04c3fSmrg} 441501e04c3fSmrg 441601e04c3fSmrg 441701e04c3fSmrgstatic void 441801e04c3fSmrgset_always_active_io(exec_list *ir, ir_variable_mode io_mode) 441901e04c3fSmrg{ 442001e04c3fSmrg assert(io_mode == ir_var_shader_in || io_mode == ir_var_shader_out); 442101e04c3fSmrg 442201e04c3fSmrg foreach_in_list(ir_instruction, node, ir) { 442301e04c3fSmrg ir_variable *const var = node->as_variable(); 442401e04c3fSmrg 442501e04c3fSmrg if (var == NULL || var->data.mode != io_mode) 442601e04c3fSmrg continue; 442701e04c3fSmrg 442801e04c3fSmrg /* Don't set always active on builtins that haven't been redeclared */ 442901e04c3fSmrg if (var->data.how_declared == ir_var_declared_implicitly) 443001e04c3fSmrg continue; 443101e04c3fSmrg 443201e04c3fSmrg var->data.always_active_io = true; 443301e04c3fSmrg } 443401e04c3fSmrg} 443501e04c3fSmrg 443601e04c3fSmrg/** 443701e04c3fSmrg * When separate shader programs are enabled, only input/outputs between 443801e04c3fSmrg * the stages of a multi-stage separate program can be safely removed 443901e04c3fSmrg * from the shader interface. Other inputs/outputs must remain active. 444001e04c3fSmrg */ 444101e04c3fSmrgstatic void 444201e04c3fSmrgdisable_varying_optimizations_for_sso(struct gl_shader_program *prog) 444301e04c3fSmrg{ 444401e04c3fSmrg unsigned first, last; 444501e04c3fSmrg assert(prog->SeparateShader); 444601e04c3fSmrg 444701e04c3fSmrg first = MESA_SHADER_STAGES; 444801e04c3fSmrg last = 0; 444901e04c3fSmrg 445001e04c3fSmrg /* Determine first and last stage. Excluding the compute stage */ 445101e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) { 445201e04c3fSmrg if (!prog->_LinkedShaders[i]) 445301e04c3fSmrg continue; 445401e04c3fSmrg if (first == MESA_SHADER_STAGES) 445501e04c3fSmrg first = i; 445601e04c3fSmrg last = i; 445701e04c3fSmrg } 445801e04c3fSmrg 445901e04c3fSmrg if (first == MESA_SHADER_STAGES) 446001e04c3fSmrg return; 446101e04c3fSmrg 446201e04c3fSmrg for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) { 446301e04c3fSmrg gl_linked_shader *sh = prog->_LinkedShaders[stage]; 446401e04c3fSmrg if (!sh) 446501e04c3fSmrg continue; 446601e04c3fSmrg 446701e04c3fSmrg /* Prevent the removal of inputs to the first and outputs from the last 446801e04c3fSmrg * stage, unless they are the initial pipeline inputs or final pipeline 446901e04c3fSmrg * outputs, respectively. 447001e04c3fSmrg * 447101e04c3fSmrg * The removal of IO between shaders in the same program is always 447201e04c3fSmrg * allowed. 447301e04c3fSmrg */ 447401e04c3fSmrg if (stage == first && stage != MESA_SHADER_VERTEX) 447501e04c3fSmrg set_always_active_io(sh->ir, ir_var_shader_in); 447601e04c3fSmrg if (stage == last && stage != MESA_SHADER_FRAGMENT) 447701e04c3fSmrg set_always_active_io(sh->ir, ir_var_shader_out); 447801e04c3fSmrg } 447901e04c3fSmrg} 448001e04c3fSmrg 448101e04c3fSmrgstatic void 448201e04c3fSmrglink_and_validate_uniforms(struct gl_context *ctx, 448301e04c3fSmrg struct gl_shader_program *prog) 448401e04c3fSmrg{ 44857ec681f3Smrg assert(!ctx->Const.UseNIRGLSLLinker); 44867ec681f3Smrg 448701e04c3fSmrg update_array_sizes(prog); 448801e04c3fSmrg link_assign_uniform_locations(prog, ctx); 448901e04c3fSmrg 44907ec681f3Smrg if (prog->data->LinkStatus == LINKING_FAILURE) 44917ec681f3Smrg return; 44927ec681f3Smrg 44937ec681f3Smrg link_util_calculate_subroutine_compat(prog); 44947ec681f3Smrg link_util_check_uniform_resources(ctx, prog); 44957ec681f3Smrg link_util_check_subroutine_resources(prog); 449601e04c3fSmrg check_image_resources(ctx, prog); 44977ec681f3Smrg link_assign_atomic_counter_resources(ctx, prog); 449801e04c3fSmrg link_check_atomic_counter_resources(ctx, prog); 449901e04c3fSmrg} 450001e04c3fSmrg 450101e04c3fSmrgstatic bool 450201e04c3fSmrglink_varyings_and_uniforms(unsigned first, unsigned last, 450301e04c3fSmrg struct gl_context *ctx, 450401e04c3fSmrg struct gl_shader_program *prog, void *mem_ctx) 450501e04c3fSmrg{ 450601e04c3fSmrg /* Mark all generic shader inputs and outputs as unpaired. */ 450701e04c3fSmrg for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) { 450801e04c3fSmrg if (prog->_LinkedShaders[i] != NULL) { 450901e04c3fSmrg link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir); 451001e04c3fSmrg } 451101e04c3fSmrg } 451201e04c3fSmrg 451301e04c3fSmrg unsigned prev = first; 451401e04c3fSmrg for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) { 451501e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 451601e04c3fSmrg continue; 451701e04c3fSmrg 451801e04c3fSmrg match_explicit_outputs_to_inputs(prog->_LinkedShaders[prev], 451901e04c3fSmrg prog->_LinkedShaders[i]); 452001e04c3fSmrg prev = i; 452101e04c3fSmrg } 452201e04c3fSmrg 452301e04c3fSmrg if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, 452401e04c3fSmrg MESA_SHADER_VERTEX, true)) { 452501e04c3fSmrg return false; 452601e04c3fSmrg } 452701e04c3fSmrg 452801e04c3fSmrg if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, 452901e04c3fSmrg MESA_SHADER_FRAGMENT, true)) { 453001e04c3fSmrg return false; 453101e04c3fSmrg } 453201e04c3fSmrg 453301e04c3fSmrg prog->last_vert_prog = NULL; 453401e04c3fSmrg for (int i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) { 453501e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 453601e04c3fSmrg continue; 453701e04c3fSmrg 453801e04c3fSmrg prog->last_vert_prog = prog->_LinkedShaders[i]->Program; 453901e04c3fSmrg break; 454001e04c3fSmrg } 454101e04c3fSmrg 454201e04c3fSmrg if (!link_varyings(prog, first, last, ctx, mem_ctx)) 454301e04c3fSmrg return false; 454401e04c3fSmrg 45457ec681f3Smrg if (!ctx->Const.UseNIRGLSLLinker) 45467ec681f3Smrg link_and_validate_uniforms(ctx, prog); 454701e04c3fSmrg 454801e04c3fSmrg if (!prog->data->LinkStatus) 454901e04c3fSmrg return false; 455001e04c3fSmrg 455101e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 455201e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 455301e04c3fSmrg continue; 455401e04c3fSmrg 455501e04c3fSmrg const struct gl_shader_compiler_options *options = 455601e04c3fSmrg &ctx->Const.ShaderCompilerOptions[i]; 455701e04c3fSmrg 455801e04c3fSmrg if (options->LowerBufferInterfaceBlocks) 455901e04c3fSmrg lower_ubo_reference(prog->_LinkedShaders[i], 456001e04c3fSmrg options->ClampBlockIndicesToArrayBounds, 456101e04c3fSmrg ctx->Const.UseSTD430AsDefaultPacking); 456201e04c3fSmrg 456301e04c3fSmrg if (i == MESA_SHADER_COMPUTE) 456401e04c3fSmrg lower_shared_reference(ctx, prog, prog->_LinkedShaders[i]); 456501e04c3fSmrg 456601e04c3fSmrg lower_vector_derefs(prog->_LinkedShaders[i]); 456701e04c3fSmrg do_vec_index_to_swizzle(prog->_LinkedShaders[i]->ir); 456801e04c3fSmrg } 456901e04c3fSmrg 457001e04c3fSmrg return true; 457101e04c3fSmrg} 457201e04c3fSmrg 457301e04c3fSmrgstatic void 457401e04c3fSmrglinker_optimisation_loop(struct gl_context *ctx, exec_list *ir, 457501e04c3fSmrg unsigned stage) 457601e04c3fSmrg{ 457701e04c3fSmrg if (ctx->Const.GLSLOptimizeConservatively) { 457801e04c3fSmrg /* Run it just once. */ 457901e04c3fSmrg do_common_optimization(ir, true, false, 458001e04c3fSmrg &ctx->Const.ShaderCompilerOptions[stage], 458101e04c3fSmrg ctx->Const.NativeIntegers); 458201e04c3fSmrg } else { 458301e04c3fSmrg /* Repeat it until it stops making changes. */ 458401e04c3fSmrg while (do_common_optimization(ir, true, false, 458501e04c3fSmrg &ctx->Const.ShaderCompilerOptions[stage], 458601e04c3fSmrg ctx->Const.NativeIntegers)) 458701e04c3fSmrg ; 458801e04c3fSmrg } 458901e04c3fSmrg} 459001e04c3fSmrg 459101e04c3fSmrgvoid 459201e04c3fSmrglink_shaders(struct gl_context *ctx, struct gl_shader_program *prog) 459301e04c3fSmrg{ 459401e04c3fSmrg prog->data->LinkStatus = LINKING_SUCCESS; /* All error paths will set this to false */ 459501e04c3fSmrg prog->data->Validated = false; 459601e04c3fSmrg 459701e04c3fSmrg /* Section 7.3 (Program Objects) of the OpenGL 4.5 Core Profile spec says: 459801e04c3fSmrg * 459901e04c3fSmrg * "Linking can fail for a variety of reasons as specified in the 460001e04c3fSmrg * OpenGL Shading Language Specification, as well as any of the 460101e04c3fSmrg * following reasons: 460201e04c3fSmrg * 460301e04c3fSmrg * - No shader objects are attached to program." 460401e04c3fSmrg * 460501e04c3fSmrg * The Compatibility Profile specification does not list the error. In 460601e04c3fSmrg * Compatibility Profile missing shader stages are replaced by 460701e04c3fSmrg * fixed-function. This applies to the case where all stages are 460801e04c3fSmrg * missing. 460901e04c3fSmrg */ 461001e04c3fSmrg if (prog->NumShaders == 0) { 461101e04c3fSmrg if (ctx->API != API_OPENGL_COMPAT) 461201e04c3fSmrg linker_error(prog, "no shaders attached to the program\n"); 461301e04c3fSmrg return; 461401e04c3fSmrg } 461501e04c3fSmrg 461601e04c3fSmrg#ifdef ENABLE_SHADER_CACHE 461701e04c3fSmrg if (shader_cache_read_program_metadata(ctx, prog)) 461801e04c3fSmrg return; 461901e04c3fSmrg#endif 462001e04c3fSmrg 462101e04c3fSmrg void *mem_ctx = ralloc_context(NULL); // temporary linker context 462201e04c3fSmrg 462301e04c3fSmrg prog->ARB_fragment_coord_conventions_enable = false; 462401e04c3fSmrg 462501e04c3fSmrg /* Separate the shaders into groups based on their type. 462601e04c3fSmrg */ 462701e04c3fSmrg struct gl_shader **shader_list[MESA_SHADER_STAGES]; 462801e04c3fSmrg unsigned num_shaders[MESA_SHADER_STAGES]; 462901e04c3fSmrg 463001e04c3fSmrg for (int i = 0; i < MESA_SHADER_STAGES; i++) { 463101e04c3fSmrg shader_list[i] = (struct gl_shader **) 463201e04c3fSmrg calloc(prog->NumShaders, sizeof(struct gl_shader *)); 463301e04c3fSmrg num_shaders[i] = 0; 463401e04c3fSmrg } 463501e04c3fSmrg 463601e04c3fSmrg unsigned min_version = UINT_MAX; 463701e04c3fSmrg unsigned max_version = 0; 463801e04c3fSmrg for (unsigned i = 0; i < prog->NumShaders; i++) { 463901e04c3fSmrg min_version = MIN2(min_version, prog->Shaders[i]->Version); 464001e04c3fSmrg max_version = MAX2(max_version, prog->Shaders[i]->Version); 464101e04c3fSmrg 464201e04c3fSmrg if (!ctx->Const.AllowGLSLRelaxedES && 464301e04c3fSmrg prog->Shaders[i]->IsES != prog->Shaders[0]->IsES) { 464401e04c3fSmrg linker_error(prog, "all shaders must use same shading " 464501e04c3fSmrg "language version\n"); 464601e04c3fSmrg goto done; 464701e04c3fSmrg } 464801e04c3fSmrg 464901e04c3fSmrg if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) { 465001e04c3fSmrg prog->ARB_fragment_coord_conventions_enable = true; 465101e04c3fSmrg } 465201e04c3fSmrg 465301e04c3fSmrg gl_shader_stage shader_type = prog->Shaders[i]->Stage; 465401e04c3fSmrg shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i]; 465501e04c3fSmrg num_shaders[shader_type]++; 465601e04c3fSmrg } 465701e04c3fSmrg 465801e04c3fSmrg /* In desktop GLSL, different shader versions may be linked together. In 465901e04c3fSmrg * GLSL ES, all shader versions must be the same. 466001e04c3fSmrg */ 466101e04c3fSmrg if (!ctx->Const.AllowGLSLRelaxedES && prog->Shaders[0]->IsES && 466201e04c3fSmrg min_version != max_version) { 466301e04c3fSmrg linker_error(prog, "all shaders must use same shading " 466401e04c3fSmrg "language version\n"); 466501e04c3fSmrg goto done; 466601e04c3fSmrg } 466701e04c3fSmrg 466801e04c3fSmrg prog->data->Version = max_version; 466901e04c3fSmrg prog->IsES = prog->Shaders[0]->IsES; 467001e04c3fSmrg 467101e04c3fSmrg /* Some shaders have to be linked with some other shaders present. 467201e04c3fSmrg */ 467301e04c3fSmrg if (!prog->SeparateShader) { 467401e04c3fSmrg if (num_shaders[MESA_SHADER_GEOMETRY] > 0 && 467501e04c3fSmrg num_shaders[MESA_SHADER_VERTEX] == 0) { 467601e04c3fSmrg linker_error(prog, "Geometry shader must be linked with " 467701e04c3fSmrg "vertex shader\n"); 467801e04c3fSmrg goto done; 467901e04c3fSmrg } 468001e04c3fSmrg if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 && 468101e04c3fSmrg num_shaders[MESA_SHADER_VERTEX] == 0) { 468201e04c3fSmrg linker_error(prog, "Tessellation evaluation shader must be linked " 468301e04c3fSmrg "with vertex shader\n"); 468401e04c3fSmrg goto done; 468501e04c3fSmrg } 468601e04c3fSmrg if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && 468701e04c3fSmrg num_shaders[MESA_SHADER_VERTEX] == 0) { 468801e04c3fSmrg linker_error(prog, "Tessellation control shader must be linked with " 468901e04c3fSmrg "vertex shader\n"); 469001e04c3fSmrg goto done; 469101e04c3fSmrg } 469201e04c3fSmrg 469301e04c3fSmrg /* Section 7.3 of the OpenGL ES 3.2 specification says: 469401e04c3fSmrg * 469501e04c3fSmrg * "Linking can fail for [...] any of the following reasons: 469601e04c3fSmrg * 469701e04c3fSmrg * * program contains an object to form a tessellation control 469801e04c3fSmrg * shader [...] and [...] the program is not separable and 469901e04c3fSmrg * contains no object to form a tessellation evaluation shader" 470001e04c3fSmrg * 470101e04c3fSmrg * The OpenGL spec is contradictory. It allows linking without a tess 470201e04c3fSmrg * eval shader, but that can only be used with transform feedback and 470301e04c3fSmrg * rasterization disabled. However, transform feedback isn't allowed 470401e04c3fSmrg * with GL_PATCHES, so it can't be used. 470501e04c3fSmrg * 470601e04c3fSmrg * More investigation showed that the idea of transform feedback after 470701e04c3fSmrg * a tess control shader was dropped, because some hw vendors couldn't 470801e04c3fSmrg * support tessellation without a tess eval shader, but the linker 470901e04c3fSmrg * section wasn't updated to reflect that. 471001e04c3fSmrg * 471101e04c3fSmrg * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this 471201e04c3fSmrg * spec bug. 471301e04c3fSmrg * 471401e04c3fSmrg * Do what's reasonable and always require a tess eval shader if a tess 471501e04c3fSmrg * control shader is present. 471601e04c3fSmrg */ 471701e04c3fSmrg if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 && 471801e04c3fSmrg num_shaders[MESA_SHADER_TESS_EVAL] == 0) { 471901e04c3fSmrg linker_error(prog, "Tessellation control shader must be linked with " 472001e04c3fSmrg "tessellation evaluation shader\n"); 472101e04c3fSmrg goto done; 472201e04c3fSmrg } 472301e04c3fSmrg 472401e04c3fSmrg if (prog->IsES) { 472501e04c3fSmrg if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 && 472601e04c3fSmrg num_shaders[MESA_SHADER_TESS_CTRL] == 0) { 472701e04c3fSmrg linker_error(prog, "GLSL ES requires non-separable programs " 472801e04c3fSmrg "containing a tessellation evaluation shader to also " 472901e04c3fSmrg "be linked with a tessellation control shader\n"); 473001e04c3fSmrg goto done; 473101e04c3fSmrg } 473201e04c3fSmrg } 473301e04c3fSmrg } 473401e04c3fSmrg 473501e04c3fSmrg /* Compute shaders have additional restrictions. */ 473601e04c3fSmrg if (num_shaders[MESA_SHADER_COMPUTE] > 0 && 473701e04c3fSmrg num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) { 473801e04c3fSmrg linker_error(prog, "Compute shaders may not be linked with any other " 473901e04c3fSmrg "type of shader\n"); 474001e04c3fSmrg } 474101e04c3fSmrg 474201e04c3fSmrg /* Link all shaders for a particular stage and validate the result. 474301e04c3fSmrg */ 474401e04c3fSmrg for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) { 474501e04c3fSmrg if (num_shaders[stage] > 0) { 474601e04c3fSmrg gl_linked_shader *const sh = 474701e04c3fSmrg link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage], 474801e04c3fSmrg num_shaders[stage], false); 474901e04c3fSmrg 475001e04c3fSmrg if (!prog->data->LinkStatus) { 475101e04c3fSmrg if (sh) 475201e04c3fSmrg _mesa_delete_linked_shader(ctx, sh); 475301e04c3fSmrg goto done; 475401e04c3fSmrg } 475501e04c3fSmrg 475601e04c3fSmrg switch (stage) { 475701e04c3fSmrg case MESA_SHADER_VERTEX: 475801e04c3fSmrg validate_vertex_shader_executable(prog, sh, ctx); 475901e04c3fSmrg break; 476001e04c3fSmrg case MESA_SHADER_TESS_CTRL: 476101e04c3fSmrg /* nothing to be done */ 476201e04c3fSmrg break; 476301e04c3fSmrg case MESA_SHADER_TESS_EVAL: 476401e04c3fSmrg validate_tess_eval_shader_executable(prog, sh, ctx); 476501e04c3fSmrg break; 476601e04c3fSmrg case MESA_SHADER_GEOMETRY: 476701e04c3fSmrg validate_geometry_shader_executable(prog, sh, ctx); 476801e04c3fSmrg break; 476901e04c3fSmrg case MESA_SHADER_FRAGMENT: 477001e04c3fSmrg validate_fragment_shader_executable(prog, sh); 477101e04c3fSmrg break; 477201e04c3fSmrg } 477301e04c3fSmrg if (!prog->data->LinkStatus) { 477401e04c3fSmrg if (sh) 477501e04c3fSmrg _mesa_delete_linked_shader(ctx, sh); 477601e04c3fSmrg goto done; 477701e04c3fSmrg } 477801e04c3fSmrg 477901e04c3fSmrg prog->_LinkedShaders[stage] = sh; 478001e04c3fSmrg prog->data->linked_stages |= 1 << stage; 478101e04c3fSmrg } 478201e04c3fSmrg } 478301e04c3fSmrg 478401e04c3fSmrg /* Here begins the inter-stage linking phase. Some initial validation is 478501e04c3fSmrg * performed, then locations are assigned for uniforms, attributes, and 478601e04c3fSmrg * varyings. 478701e04c3fSmrg */ 478801e04c3fSmrg cross_validate_uniforms(ctx, prog); 478901e04c3fSmrg if (!prog->data->LinkStatus) 479001e04c3fSmrg goto done; 479101e04c3fSmrg 479201e04c3fSmrg unsigned first, last, prev; 479301e04c3fSmrg 479401e04c3fSmrg first = MESA_SHADER_STAGES; 479501e04c3fSmrg last = 0; 479601e04c3fSmrg 479701e04c3fSmrg /* Determine first and last stage. */ 479801e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 479901e04c3fSmrg if (!prog->_LinkedShaders[i]) 480001e04c3fSmrg continue; 480101e04c3fSmrg if (first == MESA_SHADER_STAGES) 480201e04c3fSmrg first = i; 480301e04c3fSmrg last = i; 480401e04c3fSmrg } 480501e04c3fSmrg 480601e04c3fSmrg check_explicit_uniform_locations(ctx, prog); 480701e04c3fSmrg link_assign_subroutine_types(prog); 480801e04c3fSmrg verify_subroutine_associated_funcs(prog); 480901e04c3fSmrg 481001e04c3fSmrg if (!prog->data->LinkStatus) 481101e04c3fSmrg goto done; 481201e04c3fSmrg 481301e04c3fSmrg resize_tes_inputs(ctx, prog); 481401e04c3fSmrg 481501e04c3fSmrg /* Validate the inputs of each stage with the output of the preceding 481601e04c3fSmrg * stage. 481701e04c3fSmrg */ 481801e04c3fSmrg prev = first; 481901e04c3fSmrg for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) { 482001e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 482101e04c3fSmrg continue; 482201e04c3fSmrg 482301e04c3fSmrg validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev], 482401e04c3fSmrg prog->_LinkedShaders[i]); 482501e04c3fSmrg if (!prog->data->LinkStatus) 482601e04c3fSmrg goto done; 482701e04c3fSmrg 482801e04c3fSmrg cross_validate_outputs_to_inputs(ctx, prog, 482901e04c3fSmrg prog->_LinkedShaders[prev], 483001e04c3fSmrg prog->_LinkedShaders[i]); 483101e04c3fSmrg if (!prog->data->LinkStatus) 483201e04c3fSmrg goto done; 483301e04c3fSmrg 483401e04c3fSmrg prev = i; 483501e04c3fSmrg } 483601e04c3fSmrg 48377e102996Smaya /* The cross validation of outputs/inputs above validates interstage 48387e102996Smaya * explicit locations. We need to do this also for the inputs in the first 48397e102996Smaya * stage and outputs of the last stage included in the program, since there 48407e102996Smaya * is no cross validation for these. 484101e04c3fSmrg */ 48427e102996Smaya validate_first_and_last_interface_explicit_locations(ctx, prog, 48437e102996Smaya (gl_shader_stage) first, 48447e102996Smaya (gl_shader_stage) last); 484501e04c3fSmrg 484601e04c3fSmrg /* Cross-validate uniform blocks between shader stages */ 484701e04c3fSmrg validate_interstage_uniform_blocks(prog, prog->_LinkedShaders); 484801e04c3fSmrg if (!prog->data->LinkStatus) 484901e04c3fSmrg goto done; 485001e04c3fSmrg 485101e04c3fSmrg for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) { 485201e04c3fSmrg if (prog->_LinkedShaders[i] != NULL) 485301e04c3fSmrg lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]); 485401e04c3fSmrg } 485501e04c3fSmrg 485601e04c3fSmrg if (prog->IsES && prog->data->Version == 100) 485701e04c3fSmrg if (!validate_invariant_builtins(prog, 485801e04c3fSmrg prog->_LinkedShaders[MESA_SHADER_VERTEX], 485901e04c3fSmrg prog->_LinkedShaders[MESA_SHADER_FRAGMENT])) 486001e04c3fSmrg goto done; 486101e04c3fSmrg 486201e04c3fSmrg /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do 486301e04c3fSmrg * it before optimization because we want most of the checks to get 486401e04c3fSmrg * dropped thanks to constant propagation. 486501e04c3fSmrg * 486601e04c3fSmrg * This rule also applies to GLSL ES 3.00. 486701e04c3fSmrg */ 486801e04c3fSmrg if (max_version >= (prog->IsES ? 300 : 130)) { 486901e04c3fSmrg struct gl_linked_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]; 487001e04c3fSmrg if (sh) { 487101e04c3fSmrg lower_discard_flow(sh->ir); 487201e04c3fSmrg } 487301e04c3fSmrg } 487401e04c3fSmrg 487501e04c3fSmrg if (prog->SeparateShader) 487601e04c3fSmrg disable_varying_optimizations_for_sso(prog); 487701e04c3fSmrg 487801e04c3fSmrg /* Process UBOs */ 487901e04c3fSmrg if (!interstage_cross_validate_uniform_blocks(prog, false)) 488001e04c3fSmrg goto done; 488101e04c3fSmrg 488201e04c3fSmrg /* Process SSBOs */ 488301e04c3fSmrg if (!interstage_cross_validate_uniform_blocks(prog, true)) 488401e04c3fSmrg goto done; 488501e04c3fSmrg 488601e04c3fSmrg /* Do common optimization before assigning storage for attributes, 488701e04c3fSmrg * uniforms, and varyings. Later optimization could possibly make 488801e04c3fSmrg * some of that unused. 488901e04c3fSmrg */ 489001e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 489101e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 489201e04c3fSmrg continue; 489301e04c3fSmrg 489401e04c3fSmrg detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir); 489501e04c3fSmrg if (!prog->data->LinkStatus) 489601e04c3fSmrg goto done; 489701e04c3fSmrg 489801e04c3fSmrg if (ctx->Const.ShaderCompilerOptions[i].LowerCombinedClipCullDistance) { 489901e04c3fSmrg lower_clip_cull_distance(prog, prog->_LinkedShaders[i]); 490001e04c3fSmrg } 490101e04c3fSmrg 490201e04c3fSmrg if (ctx->Const.LowerTessLevel) { 490301e04c3fSmrg lower_tess_level(prog->_LinkedShaders[i]); 490401e04c3fSmrg } 490501e04c3fSmrg 490601e04c3fSmrg /* Section 13.46 (Vertex Attribute Aliasing) of the OpenGL ES 3.2 490701e04c3fSmrg * specification says: 490801e04c3fSmrg * 490901e04c3fSmrg * "In general, the behavior of GLSL ES should not depend on compiler 491001e04c3fSmrg * optimizations which might be implementation-dependent. Name matching 491101e04c3fSmrg * rules in most languages, including C++ from which GLSL ES is derived, 491201e04c3fSmrg * are based on declarations rather than use. 491301e04c3fSmrg * 491401e04c3fSmrg * RESOLUTION: The existence of aliasing is determined by declarations 491501e04c3fSmrg * present after preprocessing." 491601e04c3fSmrg * 491701e04c3fSmrg * Because of this rule, we do a 'dry-run' of attribute assignment for 491801e04c3fSmrg * vertex shader inputs here. 491901e04c3fSmrg */ 492001e04c3fSmrg if (prog->IsES && i == MESA_SHADER_VERTEX) { 492101e04c3fSmrg if (!assign_attribute_or_color_locations(mem_ctx, prog, &ctx->Const, 492201e04c3fSmrg MESA_SHADER_VERTEX, false)) { 492301e04c3fSmrg goto done; 492401e04c3fSmrg } 492501e04c3fSmrg } 492601e04c3fSmrg 492701e04c3fSmrg /* Call opts before lowering const arrays to uniforms so we can const 492801e04c3fSmrg * propagate any elements accessed directly. 492901e04c3fSmrg */ 493001e04c3fSmrg linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i); 493101e04c3fSmrg 493201e04c3fSmrg /* Call opts after lowering const arrays to copy propagate things. */ 49337ec681f3Smrg if (ctx->Const.GLSLLowerConstArrays && 49347ec681f3Smrg lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir, i, 49357ec681f3Smrg ctx->Const.Program[i].MaxUniformComponents)) 493601e04c3fSmrg linker_optimisation_loop(ctx, prog->_LinkedShaders[i]->ir, i); 493701e04c3fSmrg 493801e04c3fSmrg } 493901e04c3fSmrg 494001e04c3fSmrg /* Validation for special cases where we allow sampler array indexing 494101e04c3fSmrg * with loop induction variable. This check emits a warning or error 494201e04c3fSmrg * depending if backend can handle dynamic indexing. 494301e04c3fSmrg */ 494401e04c3fSmrg if ((!prog->IsES && prog->data->Version < 130) || 494501e04c3fSmrg (prog->IsES && prog->data->Version < 300)) { 494601e04c3fSmrg if (!validate_sampler_array_indexing(ctx, prog)) 494701e04c3fSmrg goto done; 494801e04c3fSmrg } 494901e04c3fSmrg 495001e04c3fSmrg /* Check and validate stream emissions in geometry shaders */ 495101e04c3fSmrg validate_geometry_shader_emissions(ctx, prog); 495201e04c3fSmrg 495301e04c3fSmrg store_fragdepth_layout(prog); 495401e04c3fSmrg 495501e04c3fSmrg if(!link_varyings_and_uniforms(first, last, ctx, prog, mem_ctx)) 495601e04c3fSmrg goto done; 495701e04c3fSmrg 495801e04c3fSmrg /* Linking varyings can cause some extra, useless swizzles to be generated 495901e04c3fSmrg * due to packing and unpacking. 496001e04c3fSmrg */ 496101e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 496201e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 496301e04c3fSmrg continue; 496401e04c3fSmrg 496501e04c3fSmrg optimize_swizzles(prog->_LinkedShaders[i]->ir); 496601e04c3fSmrg } 496701e04c3fSmrg 496801e04c3fSmrg /* OpenGL ES < 3.1 requires that a vertex shader and a fragment shader both 496901e04c3fSmrg * be present in a linked program. GL_ARB_ES2_compatibility doesn't say 497001e04c3fSmrg * anything about shader linking when one of the shaders (vertex or 497101e04c3fSmrg * fragment shader) is absent. So, the extension shouldn't change the 497201e04c3fSmrg * behavior specified in GLSL specification. 497301e04c3fSmrg * 497401e04c3fSmrg * From OpenGL ES 3.1 specification (7.3 Program Objects): 497501e04c3fSmrg * "Linking can fail for a variety of reasons as specified in the 497601e04c3fSmrg * OpenGL ES Shading Language Specification, as well as any of the 497701e04c3fSmrg * following reasons: 497801e04c3fSmrg * 497901e04c3fSmrg * ... 498001e04c3fSmrg * 498101e04c3fSmrg * * program contains objects to form either a vertex shader or 498201e04c3fSmrg * fragment shader, and program is not separable, and does not 498301e04c3fSmrg * contain objects to form both a vertex shader and fragment 498401e04c3fSmrg * shader." 498501e04c3fSmrg * 498601e04c3fSmrg * However, the only scenario in 3.1+ where we don't require them both is 498701e04c3fSmrg * when we have a compute shader. For example: 498801e04c3fSmrg * 498901e04c3fSmrg * - No shaders is a link error. 499001e04c3fSmrg * - Geom or Tess without a Vertex shader is a link error which means we 499101e04c3fSmrg * always require a Vertex shader and hence a Fragment shader. 499201e04c3fSmrg * - Finally a Compute shader linked with any other stage is a link error. 499301e04c3fSmrg */ 499401e04c3fSmrg if (!prog->SeparateShader && ctx->API == API_OPENGLES2 && 499501e04c3fSmrg num_shaders[MESA_SHADER_COMPUTE] == 0) { 499601e04c3fSmrg if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) { 499701e04c3fSmrg linker_error(prog, "program lacks a vertex shader\n"); 499801e04c3fSmrg } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) { 499901e04c3fSmrg linker_error(prog, "program lacks a fragment shader\n"); 500001e04c3fSmrg } 500101e04c3fSmrg } 500201e04c3fSmrg 500301e04c3fSmrgdone: 500401e04c3fSmrg for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { 500501e04c3fSmrg free(shader_list[i]); 500601e04c3fSmrg if (prog->_LinkedShaders[i] == NULL) 500701e04c3fSmrg continue; 500801e04c3fSmrg 500901e04c3fSmrg /* Do a final validation step to make sure that the IR wasn't 501001e04c3fSmrg * invalidated by any modifications performed after intrastage linking. 501101e04c3fSmrg */ 501201e04c3fSmrg validate_ir_tree(prog->_LinkedShaders[i]->ir); 501301e04c3fSmrg 501401e04c3fSmrg /* Retain any live IR, but trash the rest. */ 501501e04c3fSmrg reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir); 501601e04c3fSmrg 501701e04c3fSmrg /* The symbol table in the linked shaders may contain references to 501801e04c3fSmrg * variables that were removed (e.g., unused uniforms). Since it may 501901e04c3fSmrg * contain junk, there is no possible valid use. Delete it and set the 502001e04c3fSmrg * pointer to NULL. 502101e04c3fSmrg */ 502201e04c3fSmrg delete prog->_LinkedShaders[i]->symbols; 502301e04c3fSmrg prog->_LinkedShaders[i]->symbols = NULL; 502401e04c3fSmrg } 502501e04c3fSmrg 502601e04c3fSmrg ralloc_free(mem_ctx); 502701e04c3fSmrg} 5028