Path: blob/master/thirdparty/glslang/glslang/MachineIndependent/ParseHelper.cpp
10889 views
//1// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.2// Copyright (C) 2012-2015 LunarG, Inc.3// Copyright (C) 2015-2018 Google, Inc.4// Copyright (C) 2017, 2019 ARM Limited.5// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.6//7// All rights reserved.8//9// Redistribution and use in source and binary forms, with or without10// modification, are permitted provided that the following conditions11// are met:12//13// Redistributions of source code must retain the above copyright14// notice, this list of conditions and the following disclaimer.15//16// Redistributions in binary form must reproduce the above17// copyright notice, this list of conditions and the following18// disclaimer in the documentation and/or other materials provided19// with the distribution.20//21// Neither the name of 3Dlabs Inc. Ltd. nor the names of its22// contributors may be used to endorse or promote products derived23// from this software without specific prior written permission.24//25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS26// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT27// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS28// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE29// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,30// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,31// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;32// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER33// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT34// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN35// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36// POSSIBILITY OF SUCH DAMAGE.37//3839#include "ParseHelper.h"40#include "Initialize.h"41#include "Scan.h"4243#include "../OSDependent/osinclude.h"44#include <algorithm>4546#include "preprocessor/PpContext.h"4748extern int yyparse(glslang::TParseContext*);4950namespace glslang {5152TParseContext::TParseContext(TSymbolTable& symbolTable, TIntermediate& interm, bool parsingBuiltins,53int version, EProfile profile, const SpvVersion& spvVersion, EShLanguage language,54TInfoSink& infoSink, bool forwardCompatible, EShMessages messages,55const TString* entryPoint) :56TParseContextBase(symbolTable, interm, parsingBuiltins, version, profile, spvVersion, language,57infoSink, forwardCompatible, messages, entryPoint),58inMain(false),59blockName(nullptr),60limits(resources.limits),61atomicUintOffsets(nullptr), anyIndexLimits(false)62{63// decide whether precision qualifiers should be ignored or respected64if (isEsProfile() || spvVersion.vulkan > 0) {65precisionManager.respectPrecisionQualifiers();66if (! parsingBuiltins && language == EShLangFragment && !isEsProfile() && spvVersion.vulkan > 0)67precisionManager.warnAboutDefaults();68}6970setPrecisionDefaults();7172globalUniformDefaults.clear();73globalUniformDefaults.layoutMatrix = ElmColumnMajor;74globalUniformDefaults.layoutPacking = spvVersion.spv != 0 ? ElpStd140 : ElpShared;7576globalBufferDefaults.clear();77globalBufferDefaults.layoutMatrix = ElmColumnMajor;78globalBufferDefaults.layoutPacking = spvVersion.spv != 0 ? ElpStd430 : ElpShared;7980globalInputDefaults.clear();81globalOutputDefaults.clear();8283globalSharedDefaults.clear();84globalSharedDefaults.layoutMatrix = ElmColumnMajor;85globalSharedDefaults.layoutPacking = ElpStd430;8687// "Shaders in the transform88// feedback capturing mode have an initial global default of89// layout(xfb_buffer = 0) out;"90if (language == EShLangVertex ||91language == EShLangTessControl ||92language == EShLangTessEvaluation ||93language == EShLangGeometry)94globalOutputDefaults.layoutXfbBuffer = 0;9596if (language == EShLangGeometry)97globalOutputDefaults.layoutStream = 0;9899if (entryPoint != nullptr && entryPoint->size() > 0 && *entryPoint != "main")100infoSink.info.message(EPrefixError, "Source entry point must be \"main\"");101}102103TParseContext::~TParseContext()104{105delete [] atomicUintOffsets;106}107108// Set up all default precisions as needed by the current environment.109// Intended just as a TParseContext constructor helper.110void TParseContext::setPrecisionDefaults()111{112// Set all precision defaults to EpqNone, which is correct for all types113// when not obeying precision qualifiers, and correct for types that don't114// have defaults (thus getting an error on use) when obeying precision115// qualifiers.116117for (int type = 0; type < EbtNumTypes; ++type)118defaultPrecision[type] = EpqNone;119120for (int type = 0; type < maxSamplerIndex; ++type)121defaultSamplerPrecision[type] = EpqNone;122123// replace with real precision defaults for those that have them124if (obeyPrecisionQualifiers()) {125if (isEsProfile()) {126// Most don't have defaults, a few default to lowp.127TSampler sampler;128sampler.set(EbtFloat, Esd2D);129defaultSamplerPrecision[computeSamplerTypeIndex(sampler)] = EpqLow;130sampler.set(EbtFloat, EsdCube);131defaultSamplerPrecision[computeSamplerTypeIndex(sampler)] = EpqLow;132sampler.set(EbtFloat, Esd2D);133sampler.setExternal(true);134defaultSamplerPrecision[computeSamplerTypeIndex(sampler)] = EpqLow;135}136137// If we are parsing built-in computational variables/functions, it is meaningful to record138// whether the built-in has no precision qualifier, as that ambiguity139// is used to resolve the precision from the supplied arguments/operands instead.140// So, we don't actually want to replace EpqNone with a default precision for built-ins.141if (! parsingBuiltins) {142if (isEsProfile() && language == EShLangFragment) {143defaultPrecision[EbtInt] = EpqMedium;144defaultPrecision[EbtUint] = EpqMedium;145} else {146defaultPrecision[EbtInt] = EpqHigh;147defaultPrecision[EbtUint] = EpqHigh;148defaultPrecision[EbtFloat] = EpqHigh;149}150151if (!isEsProfile()) {152// Non-ES profile153// All sampler precisions default to highp.154for (int type = 0; type < maxSamplerIndex; ++type)155defaultSamplerPrecision[type] = EpqHigh;156}157}158159defaultPrecision[EbtSampler] = EpqLow;160defaultPrecision[EbtAtomicUint] = EpqHigh;161}162}163164void TParseContext::setLimits(const TBuiltInResource& r)165{166resources = r;167intermediate.setLimits(r);168169anyIndexLimits = ! limits.generalAttributeMatrixVectorIndexing ||170! limits.generalConstantMatrixVectorIndexing ||171! limits.generalSamplerIndexing ||172! limits.generalUniformIndexing ||173! limits.generalVariableIndexing ||174! limits.generalVaryingIndexing;175176177// "Each binding point tracks its own current default offset for178// inheritance of subsequent variables using the same binding. The initial state of compilation is that all179// binding points have an offset of 0."180atomicUintOffsets = new int[resources.maxAtomicCounterBindings];181for (int b = 0; b < resources.maxAtomicCounterBindings; ++b)182atomicUintOffsets[b] = 0;183}184185//186// Parse an array of strings using yyparse, going through the187// preprocessor to tokenize the shader strings, then through188// the GLSL scanner.189//190// Returns true for successful acceptance of the shader, false if any errors.191//192bool TParseContext::parseShaderStrings(TPpContext& ppContext, TInputScanner& input, bool versionWillBeError)193{194currentScanner = &input;195ppContext.setInput(input, versionWillBeError);196yyparse(this);197198finish();199200return numErrors == 0;201}202203// This is called from bison when it has a parse (syntax) error204// Note though that to stop cascading errors, we set EOF, which205// will usually cause a syntax error, so be more accurate that206// compilation is terminating.207void TParseContext::parserError(const char* s)208{209if (! getScanner()->atEndOfInput() || numErrors == 0)210error(getCurrentLoc(), "", "", s, "");211else212error(getCurrentLoc(), "compilation terminated", "", "");213}214215void TParseContext::growGlobalUniformBlock(const TSourceLoc& loc, TType& memberType, const TString& memberName, TTypeList* typeList)216{217bool createBlock = globalUniformBlock == nullptr;218219if (createBlock) {220globalUniformBinding = intermediate.getGlobalUniformBinding();221globalUniformSet = intermediate.getGlobalUniformSet();222}223224// use base class function to create/expand block225TParseContextBase::growGlobalUniformBlock(loc, memberType, memberName, typeList);226227if (spvVersion.vulkan > 0 && spvVersion.vulkanRelaxed) {228// check for a block storage override229TBlockStorageClass storageOverride = intermediate.getBlockStorageOverride(getGlobalUniformBlockName());230TQualifier& qualifier = globalUniformBlock->getWritableType().getQualifier();231qualifier.defaultBlock = true;232233if (storageOverride != EbsNone) {234if (createBlock) {235// Remap block storage236qualifier.setBlockStorage(storageOverride);237238// check that the change didn't create errors239blockQualifierCheck(loc, qualifier, false);240}241242// remap meber storage as well243memberType.getQualifier().setBlockStorage(storageOverride);244}245}246}247248void TParseContext::growAtomicCounterBlock(int binding, const TSourceLoc& loc, TType& memberType, const TString& memberName, TTypeList* typeList)249{250bool createBlock = atomicCounterBuffers.find(binding) == atomicCounterBuffers.end();251252if (createBlock) {253atomicCounterBlockSet = intermediate.getAtomicCounterBlockSet();254}255256// use base class function to create/expand block257TParseContextBase::growAtomicCounterBlock(binding, loc, memberType, memberName, typeList);258TQualifier& qualifier = atomicCounterBuffers[binding]->getWritableType().getQualifier();259qualifier.defaultBlock = true;260261if (spvVersion.vulkan > 0 && spvVersion.vulkanRelaxed) {262// check for a Block storage override263TBlockStorageClass storageOverride = intermediate.getBlockStorageOverride(getAtomicCounterBlockName());264265if (storageOverride != EbsNone) {266if (createBlock) {267// Remap block storage268269qualifier.setBlockStorage(storageOverride);270271// check that the change didn't create errors272blockQualifierCheck(loc, qualifier, false);273}274275// remap meber storage as well276memberType.getQualifier().setBlockStorage(storageOverride);277}278}279}280281const char* TParseContext::getGlobalUniformBlockName() const282{283const char* name = intermediate.getGlobalUniformBlockName();284if (std::string(name) == "")285return "gl_DefaultUniformBlock";286else287return name;288}289void TParseContext::finalizeGlobalUniformBlockLayout(TVariable&)290{291}292void TParseContext::setUniformBlockDefaults(TType& block) const293{294block.getQualifier().layoutPacking = ElpStd140;295block.getQualifier().layoutMatrix = ElmColumnMajor;296}297298299const char* TParseContext::getAtomicCounterBlockName() const300{301const char* name = intermediate.getAtomicCounterBlockName();302if (std::string(name) == "")303return "gl_AtomicCounterBlock";304else305return name;306}307void TParseContext::finalizeAtomicCounterBlockLayout(TVariable&)308{309}310311void TParseContext::setAtomicCounterBlockDefaults(TType& block) const312{313block.getQualifier().layoutPacking = ElpStd430;314block.getQualifier().layoutMatrix = ElmRowMajor;315}316317void TParseContext::setInvariant(const TSourceLoc& loc, const char* builtin) {318TSymbol* symbol = symbolTable.find(builtin);319if (symbol && symbol->getType().getQualifier().isPipeOutput()) {320if (intermediate.inIoAccessed(builtin))321warn(loc, "changing qualification after use", "invariant", builtin);322TSymbol* csymbol = symbolTable.copyUp(symbol);323csymbol->getWritableType().getQualifier().invariant = true;324}325}326327void TParseContext::handlePragma(const TSourceLoc& loc, const TVector<TString>& tokens)328{329if (pragmaCallback)330pragmaCallback(loc.line, tokens);331332if (tokens.size() == 0)333return;334335if (tokens[0].compare("optimize") == 0) {336if (tokens.size() != 4) {337error(loc, "optimize pragma syntax is incorrect", "#pragma", "");338return;339}340341if (tokens[1].compare("(") != 0) {342error(loc, "\"(\" expected after 'optimize' keyword", "#pragma", "");343return;344}345346if (tokens[2].compare("on") == 0)347contextPragma.optimize = true;348else if (tokens[2].compare("off") == 0)349contextPragma.optimize = false;350else {351if(relaxedErrors())352// If an implementation does not recognize the tokens following #pragma, then it will ignore that pragma.353warn(loc, "\"on\" or \"off\" expected after '(' for 'optimize' pragma", "#pragma", "");354return;355}356357if (tokens[3].compare(")") != 0) {358error(loc, "\")\" expected to end 'optimize' pragma", "#pragma", "");359return;360}361} else if (tokens[0].compare("debug") == 0) {362if (tokens.size() != 4) {363error(loc, "debug pragma syntax is incorrect", "#pragma", "");364return;365}366367if (tokens[1].compare("(") != 0) {368error(loc, "\"(\" expected after 'debug' keyword", "#pragma", "");369return;370}371372if (tokens[2].compare("on") == 0)373contextPragma.debug = true;374else if (tokens[2].compare("off") == 0)375contextPragma.debug = false;376else {377if(relaxedErrors())378// If an implementation does not recognize the tokens following #pragma, then it will ignore that pragma.379warn(loc, "\"on\" or \"off\" expected after '(' for 'debug' pragma", "#pragma", "");380return;381}382383if (tokens[3].compare(")") != 0) {384error(loc, "\")\" expected to end 'debug' pragma", "#pragma", "");385return;386}387} else if (spvVersion.spv > 0 && tokens[0].compare("use_storage_buffer") == 0) {388if (tokens.size() != 1)389error(loc, "extra tokens", "#pragma", "");390intermediate.setUseStorageBuffer();391} else if (spvVersion.spv > 0 && tokens[0].compare("use_vulkan_memory_model") == 0) {392if (tokens.size() != 1)393error(loc, "extra tokens", "#pragma", "");394intermediate.setUseVulkanMemoryModel();395} else if (spvVersion.spv > 0 && tokens[0].compare("use_variable_pointers") == 0) {396if (tokens.size() != 1)397error(loc, "extra tokens", "#pragma", "");398if (spvVersion.spv < glslang::EShTargetSpv_1_3)399error(loc, "requires SPIR-V 1.3", "#pragma use_variable_pointers", "");400intermediate.setUseVariablePointers();401} else if (tokens[0].compare("once") == 0) {402warn(loc, "not implemented", "#pragma once", "");403} else if (tokens[0].compare("glslang_binary_double_output") == 0) {404intermediate.setBinaryDoubleOutput();405} else if (spvVersion.spv > 0 && tokens[0].compare("STDGL") == 0 &&406tokens[1].compare("invariant") == 0 && tokens[3].compare("all") == 0) {407intermediate.setInvariantAll();408// Set all builtin out variables invariant if declared409setInvariant(loc, "gl_Position");410setInvariant(loc, "gl_PointSize");411setInvariant(loc, "gl_ClipDistance");412setInvariant(loc, "gl_CullDistance");413setInvariant(loc, "gl_TessLevelOuter");414setInvariant(loc, "gl_TessLevelInner");415setInvariant(loc, "gl_PrimitiveID");416setInvariant(loc, "gl_Layer");417setInvariant(loc, "gl_ViewportIndex");418setInvariant(loc, "gl_FragDepth");419setInvariant(loc, "gl_SampleMask");420setInvariant(loc, "gl_ClipVertex");421setInvariant(loc, "gl_FrontColor");422setInvariant(loc, "gl_BackColor");423setInvariant(loc, "gl_FrontSecondaryColor");424setInvariant(loc, "gl_BackSecondaryColor");425setInvariant(loc, "gl_TexCoord");426setInvariant(loc, "gl_FogFragCoord");427setInvariant(loc, "gl_FragColor");428setInvariant(loc, "gl_FragData");429}430}431432//433// Handle seeing a variable identifier in the grammar.434//435TIntermTyped* TParseContext::handleVariable(const TSourceLoc& loc, TSymbol* symbol, const TString* string)436{437TIntermTyped* node = nullptr;438439// Error check for requiring specific extensions present.440if (symbol && symbol->getNumExtensions())441requireExtensions(loc, symbol->getNumExtensions(), symbol->getExtensions(), symbol->getName().c_str());442443if (symbol && symbol->isReadOnly()) {444// All shared things containing an unsized array must be copied up445// on first use, so that all future references will share its array structure,446// so that editing the implicit size will effect all nodes consuming it,447// and so that editing the implicit size won't change the shared one.448//449// If this is a variable or a block, check it and all it contains, but if this450// is a member of an anonymous block, check the whole block, as the whole block451// will need to be copied up if it contains an unsized array.452//453// This check is being done before the block-name check further down, so guard454// for that too.455if (!symbol->getType().isUnusableName()) {456if (symbol->getType().containsUnsizedArray() ||457(symbol->getAsAnonMember() &&458symbol->getAsAnonMember()->getAnonContainer().getType().containsUnsizedArray()))459makeEditable(symbol);460}461}462463const TVariable* variable;464const TAnonMember* anon = symbol ? symbol->getAsAnonMember() : nullptr;465if (anon) {466// It was a member of an anonymous container.467468// Create a subtree for its dereference.469variable = anon->getAnonContainer().getAsVariable();470TIntermTyped* container = intermediate.addSymbol(*variable, loc);471TIntermTyped* constNode = intermediate.addConstantUnion(anon->getMemberNumber(), loc);472node = intermediate.addIndex(EOpIndexDirectStruct, container, constNode, loc);473474node->setType(*(*variable->getType().getStruct())[anon->getMemberNumber()].type);475if (node->getType().hiddenMember())476error(loc, "member of nameless block was not redeclared", string->c_str(), "");477} else {478// Not a member of an anonymous container.479480// The symbol table search was done in the lexical phase.481// See if it was a variable.482variable = symbol ? symbol->getAsVariable() : nullptr;483if (variable) {484if (variable->getType().isUnusableName()) {485error(loc, "cannot be used (maybe an instance name is needed)", string->c_str(), "");486variable = nullptr;487}488489if (language == EShLangMesh && variable) {490TLayoutGeometry primitiveType = intermediate.getOutputPrimitive();491if ((variable->getMangledName() == "gl_PrimitiveTriangleIndicesEXT" && primitiveType != ElgTriangles) ||492(variable->getMangledName() == "gl_PrimitiveLineIndicesEXT" && primitiveType != ElgLines) ||493(variable->getMangledName() == "gl_PrimitivePointIndicesEXT" && primitiveType != ElgPoints)) {494error(loc, "cannot be used (ouput primitive type mismatch)", string->c_str(), "");495variable = nullptr;496}497}498} else {499if (symbol)500error(loc, "variable name expected", string->c_str(), "");501}502503// Recovery, if it wasn't found or was not a variable.504if (! variable)505variable = new TVariable(string, TType(EbtVoid));506507if (variable->getType().getQualifier().isFrontEndConstant())508node = intermediate.addConstantUnion(variable->getConstArray(), variable->getType(), loc);509else510node = intermediate.addSymbol(*variable, loc);511}512513if (variable->getType().getQualifier().isIo())514intermediate.addIoAccessed(*string);515516if (variable->getType().isReference() &&517variable->getType().getQualifier().bufferReferenceNeedsVulkanMemoryModel()) {518intermediate.setUseVulkanMemoryModel();519}520521return node;522}523524//525// Handle seeing a base[index] dereference in the grammar.526//527TIntermTyped* TParseContext::handleBracketDereference(const TSourceLoc& loc, TIntermTyped* base, TIntermTyped* index)528{529int indexValue = 0;530if (index->getQualifier().isFrontEndConstant())531indexValue = index->getAsConstantUnion()->getConstArray()[0].getIConst();532533// basic type checks...534variableCheck(base);535536if (! base->isArray() && ! base->isMatrix() && ! base->isVector() && ! base->getType().isCoopMat() &&537! base->isReference()) {538if (base->getAsSymbolNode())539error(loc, " left of '[' is not of type array, matrix, or vector ", base->getAsSymbolNode()->getName().c_str(), "");540else541error(loc, " left of '[' is not of type array, matrix, or vector ", "expression", "");542543// Insert dummy error-recovery result544return intermediate.addConstantUnion(0.0, EbtFloat, loc);545}546547if (!base->isArray() && base->isVector()) {548if (base->getType().contains16BitFloat())549requireFloat16Arithmetic(loc, "[", "does not operate on types containing float16");550if (base->getType().contains16BitInt())551requireInt16Arithmetic(loc, "[", "does not operate on types containing (u)int16");552if (base->getType().contains8BitInt())553requireInt8Arithmetic(loc, "[", "does not operate on types containing (u)int8");554}555556// check for constant folding557if (base->getType().getQualifier().isFrontEndConstant() && index->getQualifier().isFrontEndConstant()) {558// both base and index are front-end constants559checkIndex(loc, base->getType(), indexValue);560return intermediate.foldDereference(base, indexValue, loc);561}562563// at least one of base and index is not a front-end constant variable...564TIntermTyped* result = nullptr;565566if (base->isReference() && ! base->isArray()) {567requireExtensions(loc, 1, &E_GL_EXT_buffer_reference2, "buffer reference indexing");568if (base->getType().getReferentType()->containsUnsizedArray()) {569error(loc, "cannot index reference to buffer containing an unsized array", "", "");570result = nullptr;571} else {572result = intermediate.addBinaryMath(EOpAdd, base, index, loc);573if (result != nullptr)574result->setType(base->getType());575}576if (result == nullptr) {577error(loc, "cannot index buffer reference", "", "");578result = intermediate.addConstantUnion(0.0, EbtFloat, loc);579}580return result;581}582if (base->getAsSymbolNode() && isIoResizeArray(base->getType()))583handleIoResizeArrayAccess(loc, base);584585if (index->getQualifier().isFrontEndConstant())586checkIndex(loc, base->getType(), indexValue);587588if (index->getQualifier().isFrontEndConstant()) {589if (base->getType().isUnsizedArray()) {590base->getWritableType().updateImplicitArraySize(indexValue + 1);591base->getWritableType().setImplicitlySized(true);592if (base->getQualifier().builtIn == EbvClipDistance &&593indexValue >= resources.maxClipDistances) {594error(loc, "gl_ClipDistance", "[", "array index out of range '%d'", indexValue);595}596else if (base->getQualifier().builtIn == EbvCullDistance &&597indexValue >= resources.maxCullDistances) {598error(loc, "gl_CullDistance", "[", "array index out of range '%d'", indexValue);599}600else if (base->getQualifier().builtIn == EbvSampleMask &&601indexValue >= (resources.maxSamples + 31) / 32) {602error(loc, "gl_SampleMask", "[", "array index out of range '%d'", indexValue);603}604// For 2D per-view builtin arrays, update the inner dimension size in parent type605if (base->getQualifier().isPerView() && base->getQualifier().builtIn != EbvNone) {606TIntermBinary* binaryNode = base->getAsBinaryNode();607if (binaryNode) {608TType& leftType = binaryNode->getLeft()->getWritableType();609TArraySizes& arraySizes = *leftType.getArraySizes();610assert(arraySizes.getNumDims() == 2);611arraySizes.setDimSize(1, std::max(arraySizes.getDimSize(1), indexValue + 1));612}613}614} else615checkIndex(loc, base->getType(), indexValue);616result = intermediate.addIndex(EOpIndexDirect, base, index, loc);617} else {618if (base->getType().isUnsizedArray()) {619// we have a variable index into an unsized array, which is okay,620// depending on the situation621if (base->getAsSymbolNode() && isIoResizeArray(base->getType()))622error(loc, "", "[", "array must be sized by a redeclaration or layout qualifier before being indexed with a variable");623else {624// it is okay for a run-time sized array625checkRuntimeSizable(loc, *base);626}627base->getWritableType().setArrayVariablyIndexed();628}629if (base->getBasicType() == EbtBlock) {630if (base->getQualifier().storage == EvqBuffer)631requireProfile(base->getLoc(), ~EEsProfile, "variable indexing buffer block array");632else if (base->getQualifier().storage == EvqUniform)633profileRequires(base->getLoc(), EEsProfile, 320, Num_AEP_gpu_shader5, AEP_gpu_shader5,634"variable indexing uniform block array");635else {636// input/output blocks either don't exist or can't be variably indexed637}638} else if (language == EShLangFragment && base->getQualifier().isPipeOutput() && base->getQualifier().builtIn != EbvSampleMask)639requireProfile(base->getLoc(), ~EEsProfile, "variable indexing fragment shader output array");640else if (base->getBasicType() == EbtSampler && version >= 130) {641const char* explanation = "variable indexing sampler array";642requireProfile(base->getLoc(), EEsProfile | ECoreProfile | ECompatibilityProfile, explanation);643profileRequires(base->getLoc(), EEsProfile, 320, Num_AEP_gpu_shader5, AEP_gpu_shader5, explanation);644profileRequires(base->getLoc(), ECoreProfile | ECompatibilityProfile, 400, nullptr, explanation);645}646647result = intermediate.addIndex(EOpIndexIndirect, base, index, loc);648}649650// Insert valid dereferenced result type651TType newType(base->getType(), 0);652if (base->getType().getQualifier().isConstant() && index->getQualifier().isConstant()) {653newType.getQualifier().storage = EvqConst;654// If base or index is a specialization constant, the result should also be a specialization constant.655if (base->getType().getQualifier().isSpecConstant() || index->getQualifier().isSpecConstant()) {656newType.getQualifier().makeSpecConstant();657}658} else {659newType.getQualifier().storage = EvqTemporary;660newType.getQualifier().specConstant = false;661}662result->setType(newType);663664inheritMemoryQualifiers(base->getQualifier(), result->getWritableType().getQualifier());665666// Propagate nonuniform667if (base->getQualifier().isNonUniform() || index->getQualifier().isNonUniform())668result->getWritableType().getQualifier().nonUniform = true;669670if (anyIndexLimits)671handleIndexLimits(loc, base, index);672673return result;674}675676// for ES 2.0 (version 100) limitations for almost all index operations except vertex-shader uniforms677void TParseContext::handleIndexLimits(const TSourceLoc& /*loc*/, TIntermTyped* base, TIntermTyped* index)678{679if ((! limits.generalSamplerIndexing && base->getBasicType() == EbtSampler) ||680(! limits.generalUniformIndexing && base->getQualifier().isUniformOrBuffer() && language != EShLangVertex) ||681(! limits.generalAttributeMatrixVectorIndexing && base->getQualifier().isPipeInput() && language == EShLangVertex && (base->getType().isMatrix() || base->getType().isVector())) ||682(! limits.generalConstantMatrixVectorIndexing && base->getAsConstantUnion()) ||683(! limits.generalVariableIndexing && ! base->getType().getQualifier().isUniformOrBuffer() &&684! base->getType().getQualifier().isPipeInput() &&685! base->getType().getQualifier().isPipeOutput() &&686! base->getType().getQualifier().isConstant()) ||687(! limits.generalVaryingIndexing && (base->getType().getQualifier().isPipeInput() ||688base->getType().getQualifier().isPipeOutput()))) {689// it's too early to know what the inductive variables are, save it for post processing690needsIndexLimitationChecking.push_back(index);691}692}693694// Make a shared symbol have a non-shared version that can be edited by the current695// compile, such that editing its type will not change the shared version and will696// effect all nodes sharing it.697void TParseContext::makeEditable(TSymbol*& symbol)698{699TParseContextBase::makeEditable(symbol);700701// See if it's tied to IO resizing702if (isIoResizeArray(symbol->getType()))703ioArraySymbolResizeList.push_back(symbol);704}705706// Return true if this is a geometry shader input array or tessellation control output array707// or mesh shader output array.708bool TParseContext::isIoResizeArray(const TType& type) const709{710return type.isArray() &&711((language == EShLangGeometry && type.getQualifier().storage == EvqVaryingIn) ||712(language == EShLangTessControl && type.getQualifier().storage == EvqVaryingOut &&713! type.getQualifier().patch) ||714(language == EShLangFragment && type.getQualifier().storage == EvqVaryingIn &&715(type.getQualifier().pervertexNV || type.getQualifier().pervertexEXT)) ||716(language == EShLangMesh && type.getQualifier().storage == EvqVaryingOut &&717!type.getQualifier().perTaskNV));718}719720// If an array is not isIoResizeArray() but is an io array, make sure it has the right size721void TParseContext::fixIoArraySize(const TSourceLoc& loc, TType& type)722{723if (! type.isArray() || type.getQualifier().patch || symbolTable.atBuiltInLevel())724return;725726assert(! isIoResizeArray(type));727728if (type.getQualifier().storage != EvqVaryingIn || type.getQualifier().patch)729return;730731if (language == EShLangTessControl || language == EShLangTessEvaluation) {732if (type.getOuterArraySize() != resources.maxPatchVertices) {733if (type.isSizedArray())734error(loc, "tessellation input array size must be gl_MaxPatchVertices or implicitly sized", "[]", "");735type.changeOuterArraySize(resources.maxPatchVertices);736}737}738}739740// Issue any errors if the non-array object is missing arrayness WRT741// shader I/O that has array requirements.742// All arrayness checking is handled in array paths, this is for743void TParseContext::ioArrayCheck(const TSourceLoc& loc, const TType& type, const TString& identifier)744{745if (! type.isArray() && ! symbolTable.atBuiltInLevel()) {746if (type.getQualifier().isArrayedIo(language) && !type.getQualifier().layoutPassthrough)747error(loc, "type must be an array:", type.getStorageQualifierString(), identifier.c_str());748}749}750751// Handle a dereference of a geometry shader input array or tessellation control output array.752// See ioArraySymbolResizeList comment in ParseHelper.h.753//754void TParseContext::handleIoResizeArrayAccess(const TSourceLoc& /*loc*/, TIntermTyped* base)755{756TIntermSymbol* symbolNode = base->getAsSymbolNode();757assert(symbolNode);758if (! symbolNode)759return;760761// fix array size, if it can be fixed and needs to be fixed (will allow variable indexing)762if (symbolNode->getType().isUnsizedArray()) {763int newSize = getIoArrayImplicitSize(symbolNode->getType().getQualifier());764if (newSize > 0)765symbolNode->getWritableType().changeOuterArraySize(newSize);766}767}768769// If there has been an input primitive declaration (geometry shader) or an output770// number of vertices declaration(tessellation shader), make sure all input array types771// match it in size. Types come either from nodes in the AST or symbols in the772// symbol table.773//774// Types without an array size will be given one.775// Types already having a size that is wrong will get an error.776//777void TParseContext::checkIoArraysConsistency(const TSourceLoc &loc, bool tailOnly)778{779int requiredSize = 0;780TString featureString;781size_t listSize = ioArraySymbolResizeList.size();782size_t i = 0;783784// If tailOnly = true, only check the last array symbol in the list.785if (tailOnly) {786i = listSize - 1;787}788for (bool firstIteration = true; i < listSize; ++i) {789TType &type = ioArraySymbolResizeList[i]->getWritableType();790791// As I/O array sizes don't change, fetch requiredSize only once,792// except for mesh shaders which could have different I/O array sizes based on type qualifiers.793if (firstIteration || (language == EShLangMesh)) {794requiredSize = getIoArrayImplicitSize(type.getQualifier(), &featureString);795if (requiredSize == 0)796break;797firstIteration = false;798}799800checkIoArrayConsistency(loc, requiredSize, featureString.c_str(), type,801ioArraySymbolResizeList[i]->getName());802}803}804805int TParseContext::getIoArrayImplicitSize(const TQualifier &qualifier, TString *featureString) const806{807int expectedSize = 0;808TString str = "unknown";809unsigned int maxVertices = intermediate.getVertices() != TQualifier::layoutNotSet ? intermediate.getVertices() : 0;810811if (language == EShLangGeometry) {812expectedSize = TQualifier::mapGeometryToSize(intermediate.getInputPrimitive());813str = TQualifier::getGeometryString(intermediate.getInputPrimitive());814}815else if (language == EShLangTessControl) {816expectedSize = maxVertices;817str = "vertices";818} else if (language == EShLangFragment) {819// Number of vertices for Fragment shader is always three.820expectedSize = 3;821str = "vertices";822} else if (language == EShLangMesh) {823unsigned int maxPrimitives =824intermediate.getPrimitives() != TQualifier::layoutNotSet ? intermediate.getPrimitives() : 0;825if (qualifier.builtIn == EbvPrimitiveIndicesNV) {826expectedSize = maxPrimitives * TQualifier::mapGeometryToSize(intermediate.getOutputPrimitive());827str = "max_primitives*";828str += TQualifier::getGeometryString(intermediate.getOutputPrimitive());829}830else if (qualifier.builtIn == EbvPrimitiveTriangleIndicesEXT || qualifier.builtIn == EbvPrimitiveLineIndicesEXT ||831qualifier.builtIn == EbvPrimitivePointIndicesEXT) {832expectedSize = maxPrimitives;833str = "max_primitives";834}835else if (qualifier.isPerPrimitive()) {836expectedSize = maxPrimitives;837str = "max_primitives";838}839else {840expectedSize = maxVertices;841str = "max_vertices";842}843}844if (featureString)845*featureString = str;846return expectedSize;847}848849void TParseContext::checkIoArrayConsistency(const TSourceLoc& loc, int requiredSize, const char* feature, TType& type, const TString& name)850{851if (type.isUnsizedArray())852type.changeOuterArraySize(requiredSize);853else if (type.getOuterArraySize() != requiredSize) {854if (language == EShLangGeometry)855error(loc, "inconsistent input primitive for array size of", feature, name.c_str());856else if (language == EShLangTessControl)857error(loc, "inconsistent output number of vertices for array size of", feature, name.c_str());858else if (language == EShLangFragment) {859if (type.getOuterArraySize() > requiredSize)860error(loc, " cannot be greater than 3 for pervertexEXT", feature, name.c_str());861}862else if (language == EShLangMesh)863error(loc, "inconsistent output array size of", feature, name.c_str());864else865assert(0);866}867}868869// Handle seeing a binary node with a math operation.870// Returns nullptr if not semantically allowed.871TIntermTyped* TParseContext::handleBinaryMath(const TSourceLoc& loc, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right)872{873rValueErrorCheck(loc, str, left->getAsTyped());874rValueErrorCheck(loc, str, right->getAsTyped());875876bool allowed = true;877switch (op) {878// TODO: Bring more source language-specific checks up from intermediate.cpp879// to the specific parse helpers for that source language.880case EOpLessThan:881case EOpGreaterThan:882case EOpLessThanEqual:883case EOpGreaterThanEqual:884if (! left->isScalar() || ! right->isScalar())885allowed = false;886break;887default:888break;889}890891if (((left->getType().contains16BitFloat() || right->getType().contains16BitFloat()) && !float16Arithmetic()) ||892((left->getType().contains16BitInt() || right->getType().contains16BitInt()) && !int16Arithmetic()) ||893((left->getType().contains8BitInt() || right->getType().contains8BitInt()) && !int8Arithmetic())) {894allowed = false;895}896897TIntermTyped* result = nullptr;898if (allowed) {899if ((left->isReference() || right->isReference()))900requireExtensions(loc, 1, &E_GL_EXT_buffer_reference2, "buffer reference math");901result = intermediate.addBinaryMath(op, left, right, loc);902}903904if (result == nullptr) {905bool enhanced = intermediate.getEnhancedMsgs();906binaryOpError(loc, str, left->getCompleteString(enhanced), right->getCompleteString(enhanced));907}908909return result;910}911912// Handle seeing a unary node with a math operation.913TIntermTyped* TParseContext::handleUnaryMath(const TSourceLoc& loc, const char* str, TOperator op, TIntermTyped* childNode)914{915rValueErrorCheck(loc, str, childNode);916917bool allowed = true;918if ((childNode->getType().contains16BitFloat() && !float16Arithmetic()) ||919(childNode->getType().contains16BitInt() && !int16Arithmetic()) ||920(childNode->getType().contains8BitInt() && !int8Arithmetic())) {921allowed = false;922}923924TIntermTyped* result = nullptr;925if (allowed)926result = intermediate.addUnaryMath(op, childNode, loc);927928if (result)929return result;930else {931bool enhanced = intermediate.getEnhancedMsgs();932unaryOpError(loc, str, childNode->getCompleteString(enhanced));933}934935return childNode;936}937938//939// Handle seeing a base.field dereference in the grammar.940//941TIntermTyped* TParseContext::handleDotDereference(const TSourceLoc& loc, TIntermTyped* base, const TString& field)942{943variableCheck(base);944945//946// .length() can't be resolved until we later see the function-calling syntax.947// Save away the name in the AST for now. Processing is completed in948// handleLengthMethod().949//950if (field == "length") {951if (base->isArray()) {952profileRequires(loc, ENoProfile, 120, E_GL_3DL_array_objects, ".length");953profileRequires(loc, EEsProfile, 300, nullptr, ".length");954} else if (base->isVector() || base->isMatrix()) {955const char* feature = ".length() on vectors and matrices";956requireProfile(loc, ~EEsProfile, feature);957profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, feature);958} else if (!base->getType().isCoopMat()) {959bool enhanced = intermediate.getEnhancedMsgs();960error(loc, "does not operate on this type:", field.c_str(), base->getType().getCompleteString(enhanced).c_str());961return base;962}963964return intermediate.addMethod(base, TType(EbtInt), &field, loc);965}966967// It's not .length() if we get to here.968969if (base->isArray()) {970error(loc, "cannot apply to an array:", ".", field.c_str());971972return base;973}974975if (base->getType().isCoopMat()) {976error(loc, "cannot apply to a cooperative matrix type:", ".", field.c_str());977return base;978}979980// It's neither an array nor .length() if we get here,981// leaving swizzles and struct/block dereferences.982983TIntermTyped* result = base;984if ((base->isVector() || base->isScalar()) &&985(base->isFloatingDomain() || base->isIntegerDomain() || base->getBasicType() == EbtBool)) {986result = handleDotSwizzle(loc, base, field);987} else if (base->isStruct() || base->isReference()) {988const TTypeList* fields = base->isReference() ?989base->getType().getReferentType()->getStruct() :990base->getType().getStruct();991bool fieldFound = false;992int member;993for (member = 0; member < (int)fields->size(); ++member) {994if ((*fields)[member].type->getFieldName() == field) {995fieldFound = true;996break;997}998}9991000if (fieldFound) {1001if (spvVersion.vulkan != 0 && spvVersion.vulkanRelaxed)1002result = vkRelaxedRemapDotDereference(loc, *base, *(*fields)[member].type, field);10031004if (result == base)1005{1006if (base->getType().getQualifier().isFrontEndConstant())1007result = intermediate.foldDereference(base, member, loc);1008else {1009blockMemberExtensionCheck(loc, base, member, field);1010TIntermTyped* index = intermediate.addConstantUnion(member, loc);1011result = intermediate.addIndex(EOpIndexDirectStruct, base, index, loc);1012result->setType(*(*fields)[member].type);1013if ((*fields)[member].type->getQualifier().isIo())1014intermediate.addIoAccessed(field);1015}1016}10171018inheritMemoryQualifiers(base->getQualifier(), result->getWritableType().getQualifier());1019} else {1020auto baseSymbol = base;1021while (baseSymbol->getAsSymbolNode() == nullptr) {1022auto binaryNode = baseSymbol->getAsBinaryNode();1023if (binaryNode == nullptr) break;1024baseSymbol = binaryNode->getLeft();1025}1026if (baseSymbol->getAsSymbolNode() != nullptr) {1027TString structName;1028structName.append("\'").append(baseSymbol->getAsSymbolNode()->getName().c_str()).append("\'");1029error(loc, "no such field in structure", field.c_str(), structName.c_str());1030} else {1031error(loc, "no such field in structure", field.c_str(), "");1032}1033}1034} else1035error(loc, "does not apply to this type:", field.c_str(),1036base->getType().getCompleteString(intermediate.getEnhancedMsgs()).c_str());10371038// Propagate noContraction up the dereference chain1039if (base->getQualifier().isNoContraction())1040result->getWritableType().getQualifier().setNoContraction();10411042// Propagate nonuniform1043if (base->getQualifier().isNonUniform())1044result->getWritableType().getQualifier().nonUniform = true;10451046return result;1047}10481049//1050// Handle seeing a base.swizzle, a subset of base.identifier in the grammar.1051//1052TIntermTyped* TParseContext::handleDotSwizzle(const TSourceLoc& loc, TIntermTyped* base, const TString& field)1053{1054TIntermTyped* result = base;1055if (base->isScalar()) {1056const char* dotFeature = "scalar swizzle";1057requireProfile(loc, ~EEsProfile, dotFeature);1058profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, dotFeature);1059}10601061TSwizzleSelectors<TVectorSelector> selectors;1062parseSwizzleSelector(loc, field, base->getVectorSize(), selectors);10631064if (base->isVector() && selectors.size() != 1 && base->getType().contains16BitFloat())1065requireFloat16Arithmetic(loc, ".", "can't swizzle types containing float16");1066if (base->isVector() && selectors.size() != 1 && base->getType().contains16BitInt())1067requireInt16Arithmetic(loc, ".", "can't swizzle types containing (u)int16");1068if (base->isVector() && selectors.size() != 1 && base->getType().contains8BitInt())1069requireInt8Arithmetic(loc, ".", "can't swizzle types containing (u)int8");10701071if (base->isScalar()) {1072if (selectors.size() == 1)1073return result;1074else {1075TType type(base->getBasicType(), EvqTemporary, selectors.size());1076// Swizzle operations propagate specialization-constantness1077if (base->getQualifier().isSpecConstant())1078type.getQualifier().makeSpecConstant();1079return addConstructor(loc, base, type);1080}1081}10821083if (base->getType().getQualifier().isFrontEndConstant())1084result = intermediate.foldSwizzle(base, selectors, loc);1085else {1086if (selectors.size() == 1) {1087TIntermTyped* index = intermediate.addConstantUnion(selectors[0], loc);1088result = intermediate.addIndex(EOpIndexDirect, base, index, loc);1089result->setType(TType(base->getBasicType(), EvqTemporary, base->getType().getQualifier().precision));1090} else {1091TIntermTyped* index = intermediate.addSwizzle(selectors, loc);1092result = intermediate.addIndex(EOpVectorSwizzle, base, index, loc);1093result->setType(TType(base->getBasicType(), EvqTemporary, base->getType().getQualifier().precision, selectors.size()));1094}1095// Swizzle operations propagate specialization-constantness1096if (base->getType().getQualifier().isSpecConstant())1097result->getWritableType().getQualifier().makeSpecConstant();1098}10991100return result;1101}11021103void TParseContext::blockMemberExtensionCheck(const TSourceLoc& loc, const TIntermTyped* base, int member, const TString& memberName)1104{1105// a block that needs extension checking is either 'base', or if arrayed,1106// one level removed to the left1107const TIntermSymbol* baseSymbol = nullptr;1108if (base->getAsBinaryNode() == nullptr)1109baseSymbol = base->getAsSymbolNode();1110else1111baseSymbol = base->getAsBinaryNode()->getLeft()->getAsSymbolNode();1112if (baseSymbol == nullptr)1113return;1114const TSymbol* symbol = symbolTable.find(baseSymbol->getName());1115if (symbol == nullptr)1116return;1117const TVariable* variable = symbol->getAsVariable();1118if (variable == nullptr)1119return;1120if (!variable->hasMemberExtensions())1121return;11221123// We now have a variable that is the base of a dot reference1124// with members that need extension checking.1125if (variable->getNumMemberExtensions(member) > 0)1126requireExtensions(loc, variable->getNumMemberExtensions(member), variable->getMemberExtensions(member), memberName.c_str());1127}11281129//1130// Handle seeing a function declarator in the grammar. This is the precursor1131// to recognizing a function prototype or function definition.1132//1133TFunction* TParseContext::handleFunctionDeclarator(const TSourceLoc& loc, TFunction& function, bool prototype)1134{1135// ES can't declare prototypes inside functions1136if (! symbolTable.atGlobalLevel())1137requireProfile(loc, ~EEsProfile, "local function declaration");11381139//1140// Multiple declarations of the same function name are allowed.1141//1142// If this is a definition, the definition production code will check for redefinitions1143// (we don't know at this point if it's a definition or not).1144//1145// Redeclarations (full signature match) are allowed. But, return types and parameter qualifiers must also match.1146// - except ES 100, which only allows a single prototype1147//1148// ES 100 does not allow redefining, but does allow overloading of built-in functions.1149// ES 300 does not allow redefining or overloading of built-in functions.1150//1151bool builtIn;1152TSymbol* symbol = symbolTable.find(function.getMangledName(), &builtIn);1153if (symbol && symbol->getAsFunction() && builtIn)1154requireProfile(loc, ~EEsProfile, "redefinition of built-in function");1155// Check the validity of using spirv_literal qualifier1156for (int i = 0; i < function.getParamCount(); ++i) {1157if (function[i].type->getQualifier().isSpirvLiteral() && function.getBuiltInOp() != EOpSpirvInst)1158error(loc, "'spirv_literal' can only be used on functions defined with 'spirv_instruction' for argument",1159function.getName().c_str(), "%d", i + 1);1160}11611162// For function declaration with SPIR-V instruction qualifier, always ignore the built-in function and1163// respect this redeclared one.1164if (symbol && builtIn && function.getBuiltInOp() == EOpSpirvInst)1165symbol = nullptr;1166const TFunction* prevDec = symbol ? symbol->getAsFunction() : nullptr;1167if (prevDec) {1168if (prevDec->isPrototyped() && prototype)1169profileRequires(loc, EEsProfile, 300, nullptr, "multiple prototypes for same function");1170if (prevDec->getType() != function.getType())1171error(loc, "overloaded functions must have the same return type", function.getName().c_str(), "");1172if (prevDec->getSpirvInstruction() != function.getSpirvInstruction()) {1173error(loc, "overloaded functions must have the same qualifiers", function.getName().c_str(),1174"spirv_instruction");1175}1176for (int i = 0; i < prevDec->getParamCount(); ++i) {1177if ((*prevDec)[i].type->getQualifier().storage != function[i].type->getQualifier().storage)1178error(loc, "overloaded functions must have the same parameter storage qualifiers for argument", function[i].type->getStorageQualifierString(), "%d", i+1);11791180if ((*prevDec)[i].type->getQualifier().precision != function[i].type->getQualifier().precision)1181error(loc, "overloaded functions must have the same parameter precision qualifiers for argument", function[i].type->getPrecisionQualifierString(), "%d", i+1);1182}1183}11841185arrayObjectCheck(loc, function.getType(), "array in function return type");11861187if (prototype) {1188// All built-in functions are defined, even though they don't have a body.1189// Count their prototype as a definition instead.1190if (symbolTable.atBuiltInLevel())1191function.setDefined();1192else {1193if (prevDec && ! builtIn)1194symbol->getAsFunction()->setPrototyped(); // need a writable one, but like having prevDec as a const1195function.setPrototyped();1196}1197}11981199// This insert won't actually insert it if it's a duplicate signature, but it will still check for1200// other forms of name collisions.1201if (! symbolTable.insert(function))1202error(loc, "function name is redeclaration of existing name", function.getName().c_str(), "");12031204//1205// If this is a redeclaration, it could also be a definition,1206// in which case, we need to use the parameter names from this one, and not the one that's1207// being redeclared. So, pass back this declaration, not the one in the symbol table.1208//1209return &function;1210}12111212//1213// Handle seeing the function prototype in front of a function definition in the grammar.1214// The body is handled after this function returns.1215//1216TIntermAggregate* TParseContext::handleFunctionDefinition(const TSourceLoc& loc, TFunction& function)1217{1218currentCaller = function.getMangledName();1219TSymbol* symbol = symbolTable.find(function.getMangledName());1220TFunction* prevDec = symbol ? symbol->getAsFunction() : nullptr;12211222if (! prevDec)1223error(loc, "can't find function", function.getName().c_str(), "");1224// Note: 'prevDec' could be 'function' if this is the first time we've seen function1225// as it would have just been put in the symbol table. Otherwise, we're looking up1226// an earlier occurrence.12271228if (prevDec && prevDec->isDefined()) {1229// Then this function already has a body.1230error(loc, "function already has a body", function.getName().c_str(), "");1231}1232if (prevDec && ! prevDec->isDefined()) {1233prevDec->setDefined();12341235// Remember the return type for later checking for RETURN statements.1236currentFunctionType = &(prevDec->getType());1237} else1238currentFunctionType = new TType(EbtVoid);1239functionReturnsValue = false;12401241// Check for entry point1242if (function.getName().compare(intermediate.getEntryPointName().c_str()) == 0) {1243intermediate.setEntryPointMangledName(function.getMangledName().c_str());1244intermediate.incrementEntryPointCount();1245inMain = true;1246} else1247inMain = false;12481249//1250// Raise error message if main function takes any parameters or returns anything other than void1251//1252if (inMain) {1253if (function.getParamCount() > 0)1254error(loc, "function cannot take any parameter(s)", function.getName().c_str(), "");1255if (function.getType().getBasicType() != EbtVoid)1256error(loc, "", function.getType().getBasicTypeString().c_str(), "entry point cannot return a value");1257if (function.getLinkType() != ELinkNone)1258error(loc, "main function cannot be exported", "", "");1259}12601261//1262// New symbol table scope for body of function plus its arguments1263//1264symbolTable.push();12651266//1267// Insert parameters into the symbol table.1268// If the parameter has no name, it's not an error, just don't insert it1269// (could be used for unused args).1270//1271// Also, accumulate the list of parameters into the HIL, so lower level code1272// knows where to find parameters.1273//1274TIntermAggregate* paramNodes = new TIntermAggregate;1275for (int i = 0; i < function.getParamCount(); i++) {1276TParameter& param = function[i];1277if (param.name != nullptr) {1278TVariable *variable = new TVariable(param.name, *param.type);12791280// Insert the parameters with name in the symbol table.1281if (! symbolTable.insert(*variable))1282error(loc, "redefinition", variable->getName().c_str(), "");1283else {1284// Transfer ownership of name pointer to symbol table.1285param.name = nullptr;12861287// Add the parameter to the HIL1288paramNodes = intermediate.growAggregate(paramNodes,1289intermediate.addSymbol(*variable, loc),1290loc);1291}1292} else1293paramNodes = intermediate.growAggregate(paramNodes, intermediate.addSymbol(*param.type, loc), loc);1294}1295paramNodes->setLinkType(function.getLinkType());1296intermediate.setAggregateOperator(paramNodes, EOpParameters, TType(EbtVoid), loc);1297loopNestingLevel = 0;1298statementNestingLevel = 0;1299controlFlowNestingLevel = 0;1300postEntryPointReturn = false;13011302return paramNodes;1303}13041305//1306// Handle seeing function call syntax in the grammar, which could be any of1307// - .length() method1308// - constructor1309// - a call to a built-in function mapped to an operator1310// - a call to a built-in function that will remain a function call (e.g., texturing)1311// - user function1312// - subroutine call (not implemented yet)1313//1314TIntermTyped* TParseContext::handleFunctionCall(const TSourceLoc& loc, TFunction* function, TIntermNode* arguments)1315{1316TIntermTyped* result = nullptr;13171318if (spvVersion.vulkan != 0 && spvVersion.vulkanRelaxed) {1319// allow calls that are invalid in Vulkan Semantics to be invisibily1320// remapped to equivalent valid functions1321result = vkRelaxedRemapFunctionCall(loc, function, arguments);1322if (result)1323return result;1324}13251326if (function->getBuiltInOp() == EOpArrayLength)1327result = handleLengthMethod(loc, function, arguments);1328else if (function->getBuiltInOp() != EOpNull) {1329//1330// Then this should be a constructor.1331// Don't go through the symbol table for constructors.1332// Their parameters will be verified algorithmically.1333//1334TType type(EbtVoid); // use this to get the type back1335if (! constructorError(loc, arguments, *function, function->getBuiltInOp(), type)) {1336//1337// It's a constructor, of type 'type'.1338//1339result = addConstructor(loc, arguments, type);1340if (result == nullptr)1341error(loc, "cannot construct with these arguments", type.getCompleteString(intermediate.getEnhancedMsgs()).c_str(), "");1342}1343} else {1344//1345// Find it in the symbol table.1346//1347const TFunction* fnCandidate;1348bool builtIn {false};1349fnCandidate = findFunction(loc, *function, builtIn);1350if (fnCandidate) {1351// This is a declared function that might map to1352// - a built-in operator,1353// - a built-in function not mapped to an operator, or1354// - a user function.13551356// Error check for a function requiring specific extensions present.1357if (builtIn &&1358(fnCandidate->getBuiltInOp() == EOpSubgroupQuadAll || fnCandidate->getBuiltInOp() == EOpSubgroupQuadAny))1359requireExtensions(loc, 1, &E_GL_EXT_shader_quad_control, fnCandidate->getName().c_str());13601361if (builtIn && fnCandidate->getNumExtensions())1362requireExtensions(loc, fnCandidate->getNumExtensions(), fnCandidate->getExtensions(), fnCandidate->getName().c_str());13631364if (builtIn && fnCandidate->getType().contains16BitFloat())1365requireFloat16Arithmetic(loc, "built-in function", "float16 types can only be in uniform block or buffer storage");1366if (builtIn && fnCandidate->getType().contains16BitInt())1367requireInt16Arithmetic(loc, "built-in function", "(u)int16 types can only be in uniform block or buffer storage");1368if (builtIn && fnCandidate->getType().contains8BitInt())1369requireInt8Arithmetic(loc, "built-in function", "(u)int8 types can only be in uniform block or buffer storage");1370if (builtIn && (fnCandidate->getBuiltInOp() == EOpTextureFetch || fnCandidate->getBuiltInOp() == EOpTextureQuerySize)) {1371if ((*fnCandidate)[0].type->getSampler().isMultiSample() && version <= 140)1372requireExtensions(loc, 1, &E_GL_ARB_texture_multisample, fnCandidate->getName().c_str());1373}1374if (arguments != nullptr) {1375// Make sure qualifications work for these arguments.1376TIntermAggregate* aggregate = arguments->getAsAggregate();1377for (int i = 0; i < fnCandidate->getParamCount(); ++i) {1378// At this early point there is a slight ambiguity between whether an aggregate 'arguments'1379// is the single argument itself or its children are the arguments. Only one argument1380// means take 'arguments' itself as the one argument.1381TIntermNode* arg = fnCandidate->getParamCount() == 1 ? arguments : (aggregate ? aggregate->getSequence()[i] : arguments);1382TQualifier& formalQualifier = (*fnCandidate)[i].type->getQualifier();1383if (formalQualifier.isParamOutput()) {1384if (lValueErrorCheck(arguments->getLoc(), "assign", arg->getAsTyped()))1385error(arguments->getLoc(), "Non-L-value cannot be passed for 'out' or 'inout' parameters.", "out", "");1386}1387if (formalQualifier.isSpirvLiteral()) {1388if (!arg->getAsTyped()->getQualifier().isFrontEndConstant()) {1389error(arguments->getLoc(),1390"Non front-end constant expressions cannot be passed for 'spirv_literal' parameters.",1391"spirv_literal", "");1392}1393}1394const TType& argType = arg->getAsTyped()->getType();1395const TQualifier& argQualifier = argType.getQualifier();1396bool containsBindlessSampler = intermediate.getBindlessMode() && argType.containsSampler();1397if (argQualifier.isMemory() && !containsBindlessSampler && (argType.containsOpaque() || argType.isReference())) {1398const char* message = "argument cannot drop memory qualifier when passed to formal parameter";1399if (argQualifier.volatil && ! formalQualifier.volatil)1400error(arguments->getLoc(), message, "volatile", "");1401if (argQualifier.coherent && ! (formalQualifier.devicecoherent || formalQualifier.coherent))1402error(arguments->getLoc(), message, "coherent", "");1403if (argQualifier.devicecoherent && ! (formalQualifier.devicecoherent || formalQualifier.coherent))1404error(arguments->getLoc(), message, "devicecoherent", "");1405if (argQualifier.queuefamilycoherent && ! (formalQualifier.queuefamilycoherent || formalQualifier.devicecoherent || formalQualifier.coherent))1406error(arguments->getLoc(), message, "queuefamilycoherent", "");1407if (argQualifier.workgroupcoherent && ! (formalQualifier.workgroupcoherent || formalQualifier.queuefamilycoherent || formalQualifier.devicecoherent || formalQualifier.coherent))1408error(arguments->getLoc(), message, "workgroupcoherent", "");1409if (argQualifier.subgroupcoherent && ! (formalQualifier.subgroupcoherent || formalQualifier.workgroupcoherent || formalQualifier.queuefamilycoherent || formalQualifier.devicecoherent || formalQualifier.coherent))1410error(arguments->getLoc(), message, "subgroupcoherent", "");1411if (argQualifier.readonly && ! formalQualifier.readonly)1412error(arguments->getLoc(), message, "readonly", "");1413if (argQualifier.writeonly && ! formalQualifier.writeonly)1414error(arguments->getLoc(), message, "writeonly", "");1415// Don't check 'restrict', it is different than the rest:1416// "...but only restrict can be taken away from a calling argument, by a formal parameter that1417// lacks the restrict qualifier..."1418}1419if (!builtIn && argQualifier.getFormat() != formalQualifier.getFormat()) {1420// we have mismatched formats, which should only be allowed if writeonly1421// and at least one format is unknown1422if (!formalQualifier.isWriteOnly() || (formalQualifier.getFormat() != ElfNone &&1423argQualifier.getFormat() != ElfNone))1424error(arguments->getLoc(), "image formats must match", "format", "");1425}1426if (builtIn && arg->getAsTyped()->getType().contains16BitFloat())1427requireFloat16Arithmetic(arguments->getLoc(), "built-in function", "float16 types can only be in uniform block or buffer storage");1428if (builtIn && arg->getAsTyped()->getType().contains16BitInt())1429requireInt16Arithmetic(arguments->getLoc(), "built-in function", "(u)int16 types can only be in uniform block or buffer storage");1430if (builtIn && arg->getAsTyped()->getType().contains8BitInt())1431requireInt8Arithmetic(arguments->getLoc(), "built-in function", "(u)int8 types can only be in uniform block or buffer storage");14321433// TODO 4.5 functionality: A shader will fail to compile1434// if the value passed to the memargument of an atomic memory function does not correspond to a buffer or1435// shared variable. It is acceptable to pass an element of an array or a single component of a vector to the1436// memargument of an atomic memory function, as long as the underlying array or vector is a buffer or1437// shared variable.1438}14391440// Convert 'in' arguments1441addInputArgumentConversions(*fnCandidate, arguments); // arguments may be modified if it's just a single argument node1442}14431444if (builtIn && fnCandidate->getBuiltInOp() != EOpNull) {1445// A function call mapped to a built-in operation.1446result = handleBuiltInFunctionCall(loc, arguments, *fnCandidate);1447} else if (fnCandidate->getBuiltInOp() == EOpSpirvInst) {1448// When SPIR-V instruction qualifier is specified, the function call is still mapped to a built-in operation.1449result = handleBuiltInFunctionCall(loc, arguments, *fnCandidate);1450} else {1451// This is a function call not mapped to built-in operator.1452// It could still be a built-in function, but only if PureOperatorBuiltins == false.1453result = intermediate.setAggregateOperator(arguments, EOpFunctionCall, fnCandidate->getType(), loc);1454TIntermAggregate* call = result->getAsAggregate();1455call->setName(fnCandidate->getMangledName());14561457// this is how we know whether the given function is a built-in function or a user-defined function1458// if builtIn == false, it's a userDefined -> could be an overloaded built-in function also1459// if builtIn == true, it's definitely a built-in function with EOpNull1460if (! builtIn) {1461call->setUserDefined();1462if (symbolTable.atGlobalLevel()) {1463requireProfile(loc, ~EEsProfile, "calling user function from global scope");1464intermediate.addToCallGraph(infoSink, "main(", fnCandidate->getMangledName());1465} else1466intermediate.addToCallGraph(infoSink, currentCaller, fnCandidate->getMangledName());1467}14681469if (builtIn)1470nonOpBuiltInCheck(loc, *fnCandidate, *call);1471else1472userFunctionCallCheck(loc, *call);1473}14741475// Convert 'out' arguments. If it was a constant folded built-in, it won't be an aggregate anymore.1476// Built-ins with a single argument aren't called with an aggregate, but they also don't have an output.1477// Also, build the qualifier list for user function calls, which are always called with an aggregate.1478if (result->getAsAggregate()) {1479TQualifierList& qualifierList = result->getAsAggregate()->getQualifierList();1480for (int i = 0; i < fnCandidate->getParamCount(); ++i) {1481TStorageQualifier qual = (*fnCandidate)[i].type->getQualifier().storage;1482qualifierList.push_back(qual);1483}1484result = addOutputArgumentConversions(*fnCandidate, *result->getAsAggregate());1485}14861487if (result->getAsTyped()->getType().isCoopMat() &&1488!result->getAsTyped()->getType().isParameterized()) {1489assert(fnCandidate->getBuiltInOp() == EOpCooperativeMatrixMulAdd ||1490fnCandidate->getBuiltInOp() == EOpCooperativeMatrixMulAddNV);14911492result->setType(result->getAsAggregate()->getSequence()[2]->getAsTyped()->getType());1493}1494}1495}14961497// generic error recovery1498// TODO: simplification: localize all the error recoveries that look like this, and taking type into account to reduce cascades1499if (result == nullptr)1500result = intermediate.addConstantUnion(0.0, EbtFloat, loc);15011502return result;1503}15041505TIntermTyped* TParseContext::handleBuiltInFunctionCall(TSourceLoc loc, TIntermNode* arguments,1506const TFunction& function)1507{1508checkLocation(loc, function.getBuiltInOp());1509TIntermTyped *result = intermediate.addBuiltInFunctionCall(loc, function.getBuiltInOp(),1510function.getParamCount() == 1,1511arguments, function.getType());1512if (result != nullptr && obeyPrecisionQualifiers())1513computeBuiltinPrecisions(*result, function);15141515if (result == nullptr) {1516if (arguments == nullptr)1517error(loc, " wrong operand type", "Internal Error",1518"built in unary operator function. Type: %s", "");1519else1520error(arguments->getLoc(), " wrong operand type", "Internal Error",1521"built in unary operator function. Type: %s",1522static_cast<TIntermTyped*>(arguments)->getCompleteString(intermediate.getEnhancedMsgs()).c_str());1523} else if (result->getAsOperator())1524builtInOpCheck(loc, function, *result->getAsOperator());15251526// Special handling for function call with SPIR-V instruction qualifier specified1527if (function.getBuiltInOp() == EOpSpirvInst) {1528if (auto agg = result->getAsAggregate()) {1529// Propogate spirv_by_reference/spirv_literal from parameters to arguments1530auto& sequence = agg->getSequence();1531for (unsigned i = 0; i < sequence.size(); ++i) {1532if (function[i].type->getQualifier().isSpirvByReference())1533sequence[i]->getAsTyped()->getQualifier().setSpirvByReference();1534if (function[i].type->getQualifier().isSpirvLiteral())1535sequence[i]->getAsTyped()->getQualifier().setSpirvLiteral();1536}15371538// Attach the function call to SPIR-V intruction1539agg->setSpirvInstruction(function.getSpirvInstruction());1540} else if (auto unaryNode = result->getAsUnaryNode()) {1541// Propogate spirv_by_reference/spirv_literal from parameters to arguments1542if (function[0].type->getQualifier().isSpirvByReference())1543unaryNode->getOperand()->getQualifier().setSpirvByReference();1544if (function[0].type->getQualifier().isSpirvLiteral())1545unaryNode->getOperand()->getQualifier().setSpirvLiteral();15461547// Attach the function call to SPIR-V intruction1548unaryNode->setSpirvInstruction(function.getSpirvInstruction());1549} else1550assert(0);1551}15521553return result;1554}15551556// "The operation of a built-in function can have a different precision1557// qualification than the precision qualification of the resulting value.1558// These two precision qualifications are established as follows.1559//1560// The precision qualification of the operation of a built-in function is1561// based on the precision qualification of its input arguments and formal1562// parameters: When a formal parameter specifies a precision qualifier,1563// that is used, otherwise, the precision qualification of the calling1564// argument is used. The highest precision of these will be the precision1565// qualification of the operation of the built-in function. Generally,1566// this is applied across all arguments to a built-in function, with the1567// exceptions being:1568// - bitfieldExtract and bitfieldInsert ignore the 'offset' and 'bits'1569// arguments.1570// - interpolateAt* functions only look at the 'interpolant' argument.1571//1572// The precision qualification of the result of a built-in function is1573// determined in one of the following ways:1574//1575// - For the texture sampling, image load, and image store functions,1576// the precision of the return type matches the precision of the1577// sampler type1578//1579// Otherwise:1580//1581// - For prototypes that do not specify a resulting precision qualifier,1582// the precision will be the same as the precision of the operation.1583//1584// - For prototypes that do specify a resulting precision qualifier,1585// the specified precision qualifier is the precision qualification of1586// the result."1587//1588void TParseContext::computeBuiltinPrecisions(TIntermTyped& node, const TFunction& function)1589{1590TPrecisionQualifier operationPrecision = EpqNone;1591TPrecisionQualifier resultPrecision = EpqNone;15921593TIntermOperator* opNode = node.getAsOperator();1594if (opNode == nullptr)1595return;15961597if (TIntermUnary* unaryNode = node.getAsUnaryNode()) {1598operationPrecision = std::max(function[0].type->getQualifier().precision,1599unaryNode->getOperand()->getType().getQualifier().precision);1600if (function.getType().getBasicType() != EbtBool)1601resultPrecision = function.getType().getQualifier().precision == EpqNone ?1602operationPrecision :1603function.getType().getQualifier().precision;1604} else if (TIntermAggregate* agg = node.getAsAggregate()) {1605TIntermSequence& sequence = agg->getSequence();1606unsigned int numArgs = (unsigned int)sequence.size();1607switch (agg->getOp()) {1608case EOpBitfieldExtract:1609numArgs = 1;1610break;1611case EOpBitfieldInsert:1612numArgs = 2;1613break;1614case EOpInterpolateAtCentroid:1615case EOpInterpolateAtOffset:1616case EOpInterpolateAtSample:1617numArgs = 1;1618break;1619case EOpDebugPrintf:1620numArgs = 0;1621break;1622default:1623break;1624}1625// find the maximum precision from the arguments and parameters1626for (unsigned int arg = 0; arg < numArgs; ++arg) {1627operationPrecision = std::max(operationPrecision, sequence[arg]->getAsTyped()->getQualifier().precision);1628operationPrecision = std::max(operationPrecision, function[arg].type->getQualifier().precision);1629}1630// compute the result precision1631if (agg->isSampling() ||1632agg->getOp() == EOpImageLoad || agg->getOp() == EOpImageStore ||1633agg->getOp() == EOpImageLoadLod || agg->getOp() == EOpImageStoreLod)1634resultPrecision = sequence[0]->getAsTyped()->getQualifier().precision;1635else if (function.getType().getBasicType() != EbtBool)1636resultPrecision = function.getType().getQualifier().precision == EpqNone ?1637operationPrecision :1638function.getType().getQualifier().precision;1639}16401641// Propagate precision through this node and its children. That algorithm stops1642// when a precision is found, so start by clearing this subroot precision1643opNode->getQualifier().precision = EpqNone;1644if (operationPrecision != EpqNone) {1645opNode->propagatePrecision(operationPrecision);1646opNode->setOperationPrecision(operationPrecision);1647}1648// Now, set the result precision, which might not match1649opNode->getQualifier().precision = resultPrecision;1650}16511652TIntermNode* TParseContext::handleReturnValue(const TSourceLoc& loc, TIntermTyped* value)1653{1654storage16BitAssignmentCheck(loc, value->getType(), "return");16551656functionReturnsValue = true;1657TIntermBranch* branch = nullptr;1658if (currentFunctionType->getBasicType() == EbtVoid) {1659error(loc, "void function cannot return a value", "return", "");1660branch = intermediate.addBranch(EOpReturn, loc);1661} else if (*currentFunctionType != value->getType()) {1662TIntermTyped* converted = intermediate.addConversion(EOpReturn, *currentFunctionType, value);1663if (converted) {1664if (*currentFunctionType != converted->getType())1665error(loc, "cannot convert return value to function return type", "return", "");1666if (version < 420)1667warn(loc, "type conversion on return values was not explicitly allowed until version 420",1668"return", "");1669branch = intermediate.addBranch(EOpReturn, converted, loc);1670} else {1671error(loc, "type does not match, or is not convertible to, the function's return type", "return", "");1672branch = intermediate.addBranch(EOpReturn, value, loc);1673}1674} else {1675if (value->getType().isTexture() || value->getType().isImage()) {1676if (spvVersion.spv != 0)1677error(loc, "sampler or image cannot be used as return type when generating SPIR-V", "return", "");1678else if (!extensionTurnedOn(E_GL_ARB_bindless_texture))1679error(loc, "sampler or image can be used as return type only when the extension GL_ARB_bindless_texture enabled", "return", "");1680}1681branch = intermediate.addBranch(EOpReturn, value, loc);1682}1683branch->updatePrecision(currentFunctionType->getQualifier().precision);1684return branch;1685}16861687// See if the operation is being done in an illegal location.1688void TParseContext::checkLocation(const TSourceLoc& loc, TOperator op)1689{1690switch (op) {1691case EOpBarrier:1692if (language == EShLangTessControl) {1693if (controlFlowNestingLevel > 0)1694error(loc, "tessellation control barrier() cannot be placed within flow control", "", "");1695if (! inMain)1696error(loc, "tessellation control barrier() must be in main()", "", "");1697else if (postEntryPointReturn)1698error(loc, "tessellation control barrier() cannot be placed after a return from main()", "", "");1699}1700break;1701case EOpBeginInvocationInterlock:1702if (language != EShLangFragment)1703error(loc, "beginInvocationInterlockARB() must be in a fragment shader", "", "");1704if (! inMain)1705error(loc, "beginInvocationInterlockARB() must be in main()", "", "");1706else if (postEntryPointReturn)1707error(loc, "beginInvocationInterlockARB() cannot be placed after a return from main()", "", "");1708if (controlFlowNestingLevel > 0)1709error(loc, "beginInvocationInterlockARB() cannot be placed within flow control", "", "");17101711if (beginInvocationInterlockCount > 0)1712error(loc, "beginInvocationInterlockARB() must only be called once", "", "");1713if (endInvocationInterlockCount > 0)1714error(loc, "beginInvocationInterlockARB() must be called before endInvocationInterlockARB()", "", "");17151716beginInvocationInterlockCount++;17171718// default to pixel_interlock_ordered1719if (intermediate.getInterlockOrdering() == EioNone)1720intermediate.setInterlockOrdering(EioPixelInterlockOrdered);1721break;1722case EOpEndInvocationInterlock:1723if (language != EShLangFragment)1724error(loc, "endInvocationInterlockARB() must be in a fragment shader", "", "");1725if (! inMain)1726error(loc, "endInvocationInterlockARB() must be in main()", "", "");1727else if (postEntryPointReturn)1728error(loc, "endInvocationInterlockARB() cannot be placed after a return from main()", "", "");1729if (controlFlowNestingLevel > 0)1730error(loc, "endInvocationInterlockARB() cannot be placed within flow control", "", "");17311732if (endInvocationInterlockCount > 0)1733error(loc, "endInvocationInterlockARB() must only be called once", "", "");1734if (beginInvocationInterlockCount == 0)1735error(loc, "beginInvocationInterlockARB() must be called before endInvocationInterlockARB()", "", "");17361737endInvocationInterlockCount++;1738break;1739default:1740break;1741}1742}17431744// Finish processing object.length(). This started earlier in handleDotDereference(), where1745// the ".length" part was recognized and semantically checked, and finished here where the1746// function syntax "()" is recognized.1747//1748// Return resulting tree node.1749TIntermTyped* TParseContext::handleLengthMethod(const TSourceLoc& loc, TFunction* function, TIntermNode* intermNode)1750{1751int length = 0;17521753if (function->getParamCount() > 0)1754error(loc, "method does not accept any arguments", function->getName().c_str(), "");1755else {1756const TType& type = intermNode->getAsTyped()->getType();1757if (type.isArray()) {1758if (type.isUnsizedArray()) {1759if (intermNode->getAsSymbolNode() && isIoResizeArray(type)) {1760// We could be between a layout declaration that gives a built-in io array implicit size and1761// a user redeclaration of that array, meaning we have to substitute its implicit size here1762// without actually redeclaring the array. (It is an error to use a member before the1763// redeclaration, but not an error to use the array name itself.)1764const TString& name = intermNode->getAsSymbolNode()->getName();1765if (name == "gl_in" || name == "gl_out" || name == "gl_MeshVerticesNV" ||1766name == "gl_MeshPrimitivesNV") {1767length = getIoArrayImplicitSize(type.getQualifier());1768}1769} else if (const auto typed = intermNode->getAsTyped()) {1770if (typed->getQualifier().builtIn == EbvSampleMask) {1771requireProfile(loc, EEsProfile, "the array size of gl_SampleMask and gl_SampleMaskIn is ceil(gl_MaxSamples/32)");1772length = (resources.maxSamples + 31) / 32;1773}1774}1775if (length == 0) {1776if (intermNode->getAsSymbolNode() && isIoResizeArray(type))1777error(loc, "", function->getName().c_str(), "array must first be sized by a redeclaration or layout qualifier");1778else if (isRuntimeLength(*intermNode->getAsTyped())) {1779// Create a unary op and let the back end handle it1780return intermediate.addBuiltInFunctionCall(loc, EOpArrayLength, true, intermNode, TType(EbtInt));1781} else1782error(loc, "", function->getName().c_str(), "array must be declared with a size before using this method");1783}1784} else if (type.getOuterArrayNode()) {1785// If the array's outer size is specified by an intermediate node, it means the array's length1786// was specified by a specialization constant. In such a case, we should return the node of the1787// specialization constants to represent the length.1788return type.getOuterArrayNode();1789} else1790length = type.getOuterArraySize();1791} else if (type.isMatrix())1792length = type.getMatrixCols();1793else if (type.isVector())1794length = type.getVectorSize();1795else if (type.isCoopMat())1796return intermediate.addBuiltInFunctionCall(loc, EOpArrayLength, true, intermNode, TType(EbtInt));1797else {1798// we should not get here, because earlier semantic checking should have prevented this path1799error(loc, ".length()", "unexpected use of .length()", "");1800}1801}18021803if (length == 0)1804length = 1;18051806return intermediate.addConstantUnion(length, loc);1807}18081809//1810// Add any needed implicit conversions for function-call arguments to input parameters.1811//1812void TParseContext::addInputArgumentConversions(const TFunction& function, TIntermNode*& arguments) const1813{1814TIntermAggregate* aggregate = arguments->getAsAggregate();18151816// Process each argument's conversion1817for (int i = 0; i < function.getParamCount(); ++i) {1818// At this early point there is a slight ambiguity between whether an aggregate 'arguments'1819// is the single argument itself or its children are the arguments. Only one argument1820// means take 'arguments' itself as the one argument.1821TIntermTyped* arg = function.getParamCount() == 1 ? arguments->getAsTyped() : (aggregate ? aggregate->getSequence()[i]->getAsTyped() : arguments->getAsTyped());1822if (*function[i].type != arg->getType()) {1823if (function[i].type->getQualifier().isParamInput() &&1824!function[i].type->isCoopMat()) {1825// In-qualified arguments just need an extra node added above the argument to1826// convert to the correct type.1827arg = intermediate.addConversion(EOpFunctionCall, *function[i].type, arg);1828if (arg) {1829if (function.getParamCount() == 1)1830arguments = arg;1831else {1832if (aggregate)1833aggregate->getSequence()[i] = arg;1834else1835arguments = arg;1836}1837}1838}1839}1840}1841}18421843//1844// Add any needed implicit output conversions for function-call arguments. This1845// can require a new tree topology, complicated further by whether the function1846// has a return value.1847//1848// Returns a node of a subtree that evaluates to the return value of the function.1849//1850TIntermTyped* TParseContext::addOutputArgumentConversions(const TFunction& function, TIntermAggregate& intermNode) const1851{1852TIntermSequence& arguments = intermNode.getSequence();18531854// Will there be any output conversions?1855bool outputConversions = false;1856for (int i = 0; i < function.getParamCount(); ++i) {1857if (*function[i].type != arguments[i]->getAsTyped()->getType() && function[i].type->getQualifier().isParamOutput()) {1858outputConversions = true;1859break;1860}1861}18621863if (! outputConversions)1864return &intermNode;18651866// Setup for the new tree, if needed:1867//1868// Output conversions need a different tree topology.1869// Out-qualified arguments need a temporary of the correct type, with the call1870// followed by an assignment of the temporary to the original argument:1871// void: function(arg, ...) -> ( function(tempArg, ...), arg = tempArg, ...)1872// ret = function(arg, ...) -> ret = (tempRet = function(tempArg, ...), arg = tempArg, ..., tempRet)1873// Where the "tempArg" type needs no conversion as an argument, but will convert on assignment.1874TIntermTyped* conversionTree = nullptr;1875TVariable* tempRet = nullptr;1876if (intermNode.getBasicType() != EbtVoid) {1877// do the "tempRet = function(...), " bit from above1878tempRet = makeInternalVariable("tempReturn", intermNode.getType());1879TIntermSymbol* tempRetNode = intermediate.addSymbol(*tempRet, intermNode.getLoc());1880conversionTree = intermediate.addAssign(EOpAssign, tempRetNode, &intermNode, intermNode.getLoc());1881} else1882conversionTree = &intermNode;18831884conversionTree = intermediate.makeAggregate(conversionTree);18851886// Process each argument's conversion1887for (int i = 0; i < function.getParamCount(); ++i) {1888if (*function[i].type != arguments[i]->getAsTyped()->getType()) {1889if (function[i].type->getQualifier().isParamOutput()) {1890// Out-qualified arguments need to use the topology set up above.1891// do the " ...(tempArg, ...), arg = tempArg" bit from above1892TType paramType;1893paramType.shallowCopy(*function[i].type);1894if (arguments[i]->getAsTyped()->getType().isParameterized() &&1895!paramType.isParameterized()) {1896paramType.shallowCopy(arguments[i]->getAsTyped()->getType());1897paramType.copyTypeParameters(*arguments[i]->getAsTyped()->getType().getTypeParameters());1898}1899TVariable* tempArg = makeInternalVariable("tempArg", paramType);1900tempArg->getWritableType().getQualifier().makeTemporary();1901TIntermSymbol* tempArgNode = intermediate.addSymbol(*tempArg, intermNode.getLoc());1902TIntermTyped* tempAssign = intermediate.addAssign(EOpAssign, arguments[i]->getAsTyped(), tempArgNode, arguments[i]->getLoc());1903conversionTree = intermediate.growAggregate(conversionTree, tempAssign, arguments[i]->getLoc());1904// replace the argument with another node for the same tempArg variable1905arguments[i] = intermediate.addSymbol(*tempArg, intermNode.getLoc());1906}1907}1908}19091910// Finalize the tree topology (see bigger comment above).1911if (tempRet) {1912// do the "..., tempRet" bit from above1913TIntermSymbol* tempRetNode = intermediate.addSymbol(*tempRet, intermNode.getLoc());1914conversionTree = intermediate.growAggregate(conversionTree, tempRetNode, intermNode.getLoc());1915}1916conversionTree = intermediate.setAggregateOperator(conversionTree, EOpComma, intermNode.getType(), intermNode.getLoc());19171918return conversionTree;1919}19201921TIntermTyped* TParseContext::addAssign(const TSourceLoc& loc, TOperator op, TIntermTyped* left, TIntermTyped* right)1922{1923if ((op == EOpAddAssign || op == EOpSubAssign) && left->isReference())1924requireExtensions(loc, 1, &E_GL_EXT_buffer_reference2, "+= and -= on a buffer reference");19251926if (op == EOpAssign && left->getBasicType() == EbtSampler && right->getBasicType() == EbtSampler)1927requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "sampler assignment for bindless texture");19281929return intermediate.addAssign(op, left, right, loc);1930}19311932void TParseContext::memorySemanticsCheck(const TSourceLoc& loc, const TFunction& fnCandidate, const TIntermOperator& callNode)1933{1934const TIntermSequence* argp = &callNode.getAsAggregate()->getSequence();19351936//const int gl_SemanticsRelaxed = 0x0;1937const int gl_SemanticsAcquire = 0x2;1938const int gl_SemanticsRelease = 0x4;1939const int gl_SemanticsAcquireRelease = 0x8;1940const int gl_SemanticsMakeAvailable = 0x2000;1941const int gl_SemanticsMakeVisible = 0x4000;1942const int gl_SemanticsVolatile = 0x8000;19431944//const int gl_StorageSemanticsNone = 0x0;1945const int gl_StorageSemanticsBuffer = 0x40;1946const int gl_StorageSemanticsShared = 0x100;1947const int gl_StorageSemanticsImage = 0x800;1948const int gl_StorageSemanticsOutput = 0x1000;194919501951unsigned int semantics = 0, storageClassSemantics = 0;1952unsigned int semantics2 = 0, storageClassSemantics2 = 0;19531954const TIntermTyped* arg0 = (*argp)[0]->getAsTyped();1955const bool isMS = arg0->getBasicType() == EbtSampler && arg0->getType().getSampler().isMultiSample();19561957// Grab the semantics and storage class semantics from the operands, based on opcode1958switch (callNode.getOp()) {1959case EOpAtomicAdd:1960case EOpAtomicSubtract:1961case EOpAtomicMin:1962case EOpAtomicMax:1963case EOpAtomicAnd:1964case EOpAtomicOr:1965case EOpAtomicXor:1966case EOpAtomicExchange:1967case EOpAtomicStore:1968storageClassSemantics = (*argp)[3]->getAsConstantUnion()->getConstArray()[0].getIConst();1969semantics = (*argp)[4]->getAsConstantUnion()->getConstArray()[0].getIConst();1970break;1971case EOpAtomicLoad:1972storageClassSemantics = (*argp)[2]->getAsConstantUnion()->getConstArray()[0].getIConst();1973semantics = (*argp)[3]->getAsConstantUnion()->getConstArray()[0].getIConst();1974break;1975case EOpAtomicCompSwap:1976storageClassSemantics = (*argp)[4]->getAsConstantUnion()->getConstArray()[0].getIConst();1977semantics = (*argp)[5]->getAsConstantUnion()->getConstArray()[0].getIConst();1978storageClassSemantics2 = (*argp)[6]->getAsConstantUnion()->getConstArray()[0].getIConst();1979semantics2 = (*argp)[7]->getAsConstantUnion()->getConstArray()[0].getIConst();1980break;19811982case EOpImageAtomicAdd:1983case EOpImageAtomicMin:1984case EOpImageAtomicMax:1985case EOpImageAtomicAnd:1986case EOpImageAtomicOr:1987case EOpImageAtomicXor:1988case EOpImageAtomicExchange:1989case EOpImageAtomicStore:1990storageClassSemantics = (*argp)[isMS ? 5 : 4]->getAsConstantUnion()->getConstArray()[0].getIConst();1991semantics = (*argp)[isMS ? 6 : 5]->getAsConstantUnion()->getConstArray()[0].getIConst();1992break;1993case EOpImageAtomicLoad:1994storageClassSemantics = (*argp)[isMS ? 4 : 3]->getAsConstantUnion()->getConstArray()[0].getIConst();1995semantics = (*argp)[isMS ? 5 : 4]->getAsConstantUnion()->getConstArray()[0].getIConst();1996break;1997case EOpImageAtomicCompSwap:1998storageClassSemantics = (*argp)[isMS ? 6 : 5]->getAsConstantUnion()->getConstArray()[0].getIConst();1999semantics = (*argp)[isMS ? 7 : 6]->getAsConstantUnion()->getConstArray()[0].getIConst();2000storageClassSemantics2 = (*argp)[isMS ? 8 : 7]->getAsConstantUnion()->getConstArray()[0].getIConst();2001semantics2 = (*argp)[isMS ? 9 : 8]->getAsConstantUnion()->getConstArray()[0].getIConst();2002break;20032004case EOpBarrier:2005storageClassSemantics = (*argp)[2]->getAsConstantUnion()->getConstArray()[0].getIConst();2006semantics = (*argp)[3]->getAsConstantUnion()->getConstArray()[0].getIConst();2007break;2008case EOpMemoryBarrier:2009storageClassSemantics = (*argp)[1]->getAsConstantUnion()->getConstArray()[0].getIConst();2010semantics = (*argp)[2]->getAsConstantUnion()->getConstArray()[0].getIConst();2011break;2012default:2013break;2014}20152016if ((semantics & gl_SemanticsAcquire) &&2017(callNode.getOp() == EOpAtomicStore || callNode.getOp() == EOpImageAtomicStore)) {2018error(loc, "gl_SemanticsAcquire must not be used with (image) atomic store",2019fnCandidate.getName().c_str(), "");2020}2021if ((semantics & gl_SemanticsRelease) &&2022(callNode.getOp() == EOpAtomicLoad || callNode.getOp() == EOpImageAtomicLoad)) {2023error(loc, "gl_SemanticsRelease must not be used with (image) atomic load",2024fnCandidate.getName().c_str(), "");2025}2026if ((semantics & gl_SemanticsAcquireRelease) &&2027(callNode.getOp() == EOpAtomicStore || callNode.getOp() == EOpImageAtomicStore ||2028callNode.getOp() == EOpAtomicLoad || callNode.getOp() == EOpImageAtomicLoad)) {2029error(loc, "gl_SemanticsAcquireRelease must not be used with (image) atomic load/store",2030fnCandidate.getName().c_str(), "");2031}2032if (((semantics | semantics2) & ~(gl_SemanticsAcquire |2033gl_SemanticsRelease |2034gl_SemanticsAcquireRelease |2035gl_SemanticsMakeAvailable |2036gl_SemanticsMakeVisible |2037gl_SemanticsVolatile))) {2038error(loc, "Invalid semantics value", fnCandidate.getName().c_str(), "");2039}2040if (((storageClassSemantics | storageClassSemantics2) & ~(gl_StorageSemanticsBuffer |2041gl_StorageSemanticsShared |2042gl_StorageSemanticsImage |2043gl_StorageSemanticsOutput))) {2044error(loc, "Invalid storage class semantics value", fnCandidate.getName().c_str(), "");2045}20462047if (callNode.getOp() == EOpMemoryBarrier) {2048if (!IsPow2(semantics & (gl_SemanticsAcquire | gl_SemanticsRelease | gl_SemanticsAcquireRelease))) {2049error(loc, "Semantics must include exactly one of gl_SemanticsRelease, gl_SemanticsAcquire, or "2050"gl_SemanticsAcquireRelease", fnCandidate.getName().c_str(), "");2051}2052} else {2053if (semantics & (gl_SemanticsAcquire | gl_SemanticsRelease | gl_SemanticsAcquireRelease)) {2054if (!IsPow2(semantics & (gl_SemanticsAcquire | gl_SemanticsRelease | gl_SemanticsAcquireRelease))) {2055error(loc, "Semantics must not include multiple of gl_SemanticsRelease, gl_SemanticsAcquire, or "2056"gl_SemanticsAcquireRelease", fnCandidate.getName().c_str(), "");2057}2058}2059if (semantics2 & (gl_SemanticsAcquire | gl_SemanticsRelease | gl_SemanticsAcquireRelease)) {2060if (!IsPow2(semantics2 & (gl_SemanticsAcquire | gl_SemanticsRelease | gl_SemanticsAcquireRelease))) {2061error(loc, "semUnequal must not include multiple of gl_SemanticsRelease, gl_SemanticsAcquire, or "2062"gl_SemanticsAcquireRelease", fnCandidate.getName().c_str(), "");2063}2064}2065}2066if (callNode.getOp() == EOpMemoryBarrier) {2067if (storageClassSemantics == 0) {2068error(loc, "Storage class semantics must not be zero", fnCandidate.getName().c_str(), "");2069}2070}2071if (callNode.getOp() == EOpBarrier && semantics != 0 && storageClassSemantics == 0) {2072error(loc, "Storage class semantics must not be zero", fnCandidate.getName().c_str(), "");2073}2074if ((callNode.getOp() == EOpAtomicCompSwap || callNode.getOp() == EOpImageAtomicCompSwap) &&2075(semantics2 & (gl_SemanticsRelease | gl_SemanticsAcquireRelease))) {2076error(loc, "semUnequal must not be gl_SemanticsRelease or gl_SemanticsAcquireRelease",2077fnCandidate.getName().c_str(), "");2078}2079if ((semantics & gl_SemanticsMakeAvailable) &&2080!(semantics & (gl_SemanticsRelease | gl_SemanticsAcquireRelease))) {2081error(loc, "gl_SemanticsMakeAvailable requires gl_SemanticsRelease or gl_SemanticsAcquireRelease",2082fnCandidate.getName().c_str(), "");2083}2084if ((semantics & gl_SemanticsMakeVisible) &&2085!(semantics & (gl_SemanticsAcquire | gl_SemanticsAcquireRelease))) {2086error(loc, "gl_SemanticsMakeVisible requires gl_SemanticsAcquire or gl_SemanticsAcquireRelease",2087fnCandidate.getName().c_str(), "");2088}2089if ((semantics & gl_SemanticsVolatile) &&2090(callNode.getOp() == EOpMemoryBarrier || callNode.getOp() == EOpBarrier)) {2091error(loc, "gl_SemanticsVolatile must not be used with memoryBarrier or controlBarrier",2092fnCandidate.getName().c_str(), "");2093}2094if ((callNode.getOp() == EOpAtomicCompSwap || callNode.getOp() == EOpImageAtomicCompSwap) &&2095((semantics ^ semantics2) & gl_SemanticsVolatile)) {2096error(loc, "semEqual and semUnequal must either both include gl_SemanticsVolatile or neither",2097fnCandidate.getName().c_str(), "");2098}2099}21002101//2102// Do additional checking of built-in function calls that is not caught2103// by normal semantic checks on argument type, extension tagging, etc.2104//2105// Assumes there has been a semantically correct match to a built-in function prototype.2106//2107void TParseContext::builtInOpCheck(const TSourceLoc& loc, const TFunction& fnCandidate, TIntermOperator& callNode)2108{2109// Set up convenience accessors to the argument(s). There is almost always2110// multiple arguments for the cases below, but when there might be one,2111// check the unaryArg first.2112const TIntermSequence* argp = nullptr; // confusing to use [] syntax on a pointer, so this is to help get a reference2113const TIntermTyped* unaryArg = nullptr;2114const TIntermTyped* arg0 = nullptr;2115if (callNode.getAsAggregate()) {2116argp = &callNode.getAsAggregate()->getSequence();2117if (argp->size() > 0)2118arg0 = (*argp)[0]->getAsTyped();2119} else {2120assert(callNode.getAsUnaryNode());2121unaryArg = callNode.getAsUnaryNode()->getOperand();2122arg0 = unaryArg;2123}21242125TString featureString;2126const char* feature = nullptr;2127switch (callNode.getOp()) {2128case EOpTextureGather:2129case EOpTextureGatherOffset:2130case EOpTextureGatherOffsets:2131{2132// Figure out which variants are allowed by what extensions,2133// and what arguments must be constant for which situations.21342135featureString = fnCandidate.getName();2136featureString += "(...)";2137feature = featureString.c_str();2138profileRequires(loc, EEsProfile, 310, nullptr, feature);2139int compArg = -1; // track which argument, if any, is the constant component argument2140switch (callNode.getOp()) {2141case EOpTextureGather:2142// More than two arguments needs gpu_shader5, and rectangular or shadow needs gpu_shader5,2143// otherwise, need GL_ARB_texture_gather.2144if (fnCandidate.getParamCount() > 2 || fnCandidate[0].type->getSampler().dim == EsdRect || fnCandidate[0].type->getSampler().shadow) {2145profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2146if (! fnCandidate[0].type->getSampler().shadow)2147compArg = 2;2148} else2149profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_texture_gather, feature);2150break;2151case EOpTextureGatherOffset:2152// GL_ARB_texture_gather is good enough for 2D non-shadow textures with no component argument2153if (fnCandidate[0].type->getSampler().dim == Esd2D && ! fnCandidate[0].type->getSampler().shadow && fnCandidate.getParamCount() == 3)2154profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_texture_gather, feature);2155else2156profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2157if (! (*argp)[fnCandidate[0].type->getSampler().shadow ? 3 : 2]->getAsConstantUnion())2158profileRequires(loc, EEsProfile, 320, Num_AEP_gpu_shader5, AEP_gpu_shader5,2159"non-constant offset argument");2160if (! fnCandidate[0].type->getSampler().shadow)2161compArg = 3;2162break;2163case EOpTextureGatherOffsets:2164profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2165if (! fnCandidate[0].type->getSampler().shadow)2166compArg = 3;2167// check for constant offsets2168if (! (*argp)[fnCandidate[0].type->getSampler().shadow ? 3 : 2]->getAsConstantUnion())2169error(loc, "must be a compile-time constant:", feature, "offsets argument");2170break;2171default:2172break;2173}21742175if (compArg > 0 && compArg < fnCandidate.getParamCount()) {2176if ((*argp)[compArg]->getAsConstantUnion()) {2177int value = (*argp)[compArg]->getAsConstantUnion()->getConstArray()[0].getIConst();2178if (value < 0 || value > 3)2179error(loc, "must be 0, 1, 2, or 3:", feature, "component argument");2180} else2181error(loc, "must be a compile-time constant:", feature, "component argument");2182}21832184bool bias = false;2185if (callNode.getOp() == EOpTextureGather)2186bias = fnCandidate.getParamCount() > 3;2187else if (callNode.getOp() == EOpTextureGatherOffset ||2188callNode.getOp() == EOpTextureGatherOffsets)2189bias = fnCandidate.getParamCount() > 4;21902191if (bias) {2192featureString = fnCandidate.getName();2193featureString += "with bias argument";2194feature = featureString.c_str();2195profileRequires(loc, ~EEsProfile, 450, nullptr, feature);2196requireExtensions(loc, 1, &E_GL_AMD_texture_gather_bias_lod, feature);2197}2198break;2199}22002201case EOpTexture:2202case EOpTextureLod:2203{2204if ((fnCandidate.getParamCount() > 2) && ((*argp)[1]->getAsTyped()->getType().getBasicType() == EbtFloat) &&2205((*argp)[1]->getAsTyped()->getType().getVectorSize() == 4) && fnCandidate[0].type->getSampler().shadow) {2206featureString = fnCandidate.getName();2207if (callNode.getOp() == EOpTexture)2208featureString += "(..., float bias)";2209else2210featureString += "(..., float lod)";2211feature = featureString.c_str();22122213if ((fnCandidate[0].type->getSampler().dim == Esd2D && fnCandidate[0].type->getSampler().arrayed) || //2D Array Shadow2214(fnCandidate[0].type->getSampler().dim == EsdCube && fnCandidate[0].type->getSampler().arrayed && fnCandidate.getParamCount() > 3) || // Cube Array Shadow2215(fnCandidate[0].type->getSampler().dim == EsdCube && callNode.getOp() == EOpTextureLod)) { // Cube Shadow2216requireExtensions(loc, 1, &E_GL_EXT_texture_shadow_lod, feature);2217if (isEsProfile()) {2218if (version < 320 &&2219!extensionsTurnedOn(Num_AEP_texture_cube_map_array, AEP_texture_cube_map_array))2220error(loc, "GL_EXT_texture_shadow_lod not supported for this ES version", feature, "");2221else2222profileRequires(loc, EEsProfile, 320, nullptr, feature);2223} else { // Desktop2224profileRequires(loc, ~EEsProfile, 130, nullptr, feature);2225}2226}2227}2228break;2229}22302231case EOpSparseTextureGather:2232case EOpSparseTextureGatherOffset:2233case EOpSparseTextureGatherOffsets:2234{2235bool bias = false;2236if (callNode.getOp() == EOpSparseTextureGather)2237bias = fnCandidate.getParamCount() > 4;2238else if (callNode.getOp() == EOpSparseTextureGatherOffset ||2239callNode.getOp() == EOpSparseTextureGatherOffsets)2240bias = fnCandidate.getParamCount() > 5;22412242if (bias) {2243featureString = fnCandidate.getName();2244featureString += "with bias argument";2245feature = featureString.c_str();2246profileRequires(loc, ~EEsProfile, 450, nullptr, feature);2247requireExtensions(loc, 1, &E_GL_AMD_texture_gather_bias_lod, feature);2248}2249// As per GL_ARB_sparse_texture2 extension "Offsets" parameter must be constant integral expression2250// for sparseTextureGatherOffsetsARB just as textureGatherOffsets2251if (callNode.getOp() == EOpSparseTextureGatherOffsets) {2252int offsetsArg = arg0->getType().getSampler().shadow ? 3 : 2;2253if (!(*argp)[offsetsArg]->getAsConstantUnion())2254error(loc, "argument must be compile-time constant", "offsets", "");2255}2256break;2257}22582259case EOpSparseTextureGatherLod:2260case EOpSparseTextureGatherLodOffset:2261case EOpSparseTextureGatherLodOffsets:2262{2263requireExtensions(loc, 1, &E_GL_ARB_sparse_texture2, fnCandidate.getName().c_str());2264break;2265}22662267case EOpSwizzleInvocations:2268{2269if (! (*argp)[1]->getAsConstantUnion())2270error(loc, "argument must be compile-time constant", "offset", "");2271else {2272unsigned offset[4] = {};2273offset[0] = (*argp)[1]->getAsConstantUnion()->getConstArray()[0].getUConst();2274offset[1] = (*argp)[1]->getAsConstantUnion()->getConstArray()[1].getUConst();2275offset[2] = (*argp)[1]->getAsConstantUnion()->getConstArray()[2].getUConst();2276offset[3] = (*argp)[1]->getAsConstantUnion()->getConstArray()[3].getUConst();2277if (offset[0] > 3 || offset[1] > 3 || offset[2] > 3 || offset[3] > 3)2278error(loc, "components must be in the range [0, 3]", "offset", "");2279}22802281break;2282}22832284case EOpSwizzleInvocationsMasked:2285{2286if (! (*argp)[1]->getAsConstantUnion())2287error(loc, "argument must be compile-time constant", "mask", "");2288else {2289unsigned mask[3] = {};2290mask[0] = (*argp)[1]->getAsConstantUnion()->getConstArray()[0].getUConst();2291mask[1] = (*argp)[1]->getAsConstantUnion()->getConstArray()[1].getUConst();2292mask[2] = (*argp)[1]->getAsConstantUnion()->getConstArray()[2].getUConst();2293if (mask[0] > 31 || mask[1] > 31 || mask[2] > 31)2294error(loc, "components must be in the range [0, 31]", "mask", "");2295}22962297break;2298}22992300case EOpTextureOffset:2301case EOpTextureFetchOffset:2302case EOpTextureProjOffset:2303case EOpTextureLodOffset:2304case EOpTextureProjLodOffset:2305case EOpTextureGradOffset:2306case EOpTextureProjGradOffset:2307{2308// Handle texture-offset limits checking2309// Pick which argument has to hold constant offsets2310int arg = -1;2311switch (callNode.getOp()) {2312case EOpTextureOffset: arg = 2; break;2313case EOpTextureFetchOffset: arg = (arg0->getType().getSampler().isRect()) ? 2 : 3; break;2314case EOpTextureProjOffset: arg = 2; break;2315case EOpTextureLodOffset: arg = 3; break;2316case EOpTextureProjLodOffset: arg = 3; break;2317case EOpTextureGradOffset: arg = 4; break;2318case EOpTextureProjGradOffset: arg = 4; break;2319default:2320assert(0);2321break;2322}23232324if (arg > 0) {23252326bool f16ShadowCompare = (*argp)[1]->getAsTyped()->getBasicType() == EbtFloat16 &&2327arg0->getType().getSampler().shadow;2328if (f16ShadowCompare)2329++arg;2330if (! (*argp)[arg]->getAsTyped()->getQualifier().isConstant())2331error(loc, "argument must be compile-time constant", "texel offset", "");2332else if ((*argp)[arg]->getAsConstantUnion()) {2333const TType& type = (*argp)[arg]->getAsTyped()->getType();2334for (int c = 0; c < type.getVectorSize(); ++c) {2335int offset = (*argp)[arg]->getAsConstantUnion()->getConstArray()[c].getIConst();2336if (offset > resources.maxProgramTexelOffset || offset < resources.minProgramTexelOffset)2337error(loc, "value is out of range:", "texel offset",2338"[gl_MinProgramTexelOffset, gl_MaxProgramTexelOffset]");2339}2340}23412342if (callNode.getOp() == EOpTextureOffset) {2343TSampler s = arg0->getType().getSampler();2344if (s.is2D() && s.isArrayed() && s.isShadow()) {2345if (2346((*argp)[1]->getAsTyped()->getType().getBasicType() == EbtFloat) &&2347((*argp)[1]->getAsTyped()->getType().getVectorSize() == 4) &&2348(fnCandidate.getParamCount() == 4)) {2349featureString = fnCandidate.getName() + " for sampler2DArrayShadow";2350feature = featureString.c_str();2351requireExtensions(loc, 1, &E_GL_EXT_texture_shadow_lod, feature);2352profileRequires(loc, EEsProfile, 300, nullptr, feature);2353profileRequires(loc, ~EEsProfile, 130, nullptr, feature);2354}2355else if (isEsProfile())2356error(loc, "TextureOffset does not support sampler2DArrayShadow : ", "sampler", "ES Profile");2357else if (version <= 420)2358error(loc, "TextureOffset does not support sampler2DArrayShadow : ", "sampler", "version <= 420");2359}2360}23612362if (callNode.getOp() == EOpTextureLodOffset) {2363TSampler s = arg0->getType().getSampler();2364if (s.is2D() && s.isArrayed() && s.isShadow() &&2365((*argp)[1]->getAsTyped()->getType().getBasicType() == EbtFloat) &&2366((*argp)[1]->getAsTyped()->getType().getVectorSize() == 4) &&2367(fnCandidate.getParamCount() == 4)) {2368featureString = fnCandidate.getName() + " for sampler2DArrayShadow";2369feature = featureString.c_str();2370profileRequires(loc, EEsProfile, 300, nullptr, feature);2371profileRequires(loc, ~EEsProfile, 130, nullptr, feature);2372requireExtensions(loc, 1, &E_GL_EXT_texture_shadow_lod, feature);2373}2374}2375}23762377break;2378}23792380case EOpTraceNV:2381if (!(*argp)[10]->getAsConstantUnion())2382error(loc, "argument must be compile-time constant", "payload number", "a");2383break;2384case EOpTraceRayMotionNV:2385if (!(*argp)[11]->getAsConstantUnion())2386error(loc, "argument must be compile-time constant", "payload number", "a");2387break;2388case EOpTraceKHR:2389if (!(*argp)[10]->getAsConstantUnion())2390error(loc, "argument must be compile-time constant", "payload number", "a");2391else {2392unsigned int location = (*argp)[10]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2393if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(0, location) < 0)2394error(loc, "with layout(location =", "no rayPayloadEXT/rayPayloadInEXT declared", "%d)", location);2395}2396break;2397case EOpExecuteCallableNV:2398if (!(*argp)[1]->getAsConstantUnion())2399error(loc, "argument must be compile-time constant", "callable data number", "");2400break;2401case EOpExecuteCallableKHR:2402if (!(*argp)[1]->getAsConstantUnion())2403error(loc, "argument must be compile-time constant", "callable data number", "");2404else {2405unsigned int location = (*argp)[1]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2406if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(1, location) < 0)2407error(loc, "with layout(location =", "no callableDataEXT/callableDataInEXT declared", "%d)", location);2408}2409break;24102411case EOpHitObjectTraceRayNV:2412if (!(*argp)[11]->getAsConstantUnion())2413error(loc, "argument must be compile-time constant", "payload number", "");2414else {2415unsigned int location = (*argp)[11]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2416if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(0, location) < 0)2417error(loc, "with layout(location =", "no rayPayloadEXT/rayPayloadInEXT declared", "%d)", location);2418}2419break;2420case EOpHitObjectTraceRayMotionNV:2421if (!(*argp)[12]->getAsConstantUnion())2422error(loc, "argument must be compile-time constant", "payload number", "");2423else {2424unsigned int location = (*argp)[12]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2425if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(0, location) < 0)2426error(loc, "with layout(location =", "no rayPayloadEXT/rayPayloadInEXT declared", "%d)", location);2427}2428break;2429case EOpHitObjectExecuteShaderNV:2430if (!(*argp)[1]->getAsConstantUnion())2431error(loc, "argument must be compile-time constant", "payload number", "");2432else {2433unsigned int location = (*argp)[1]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2434if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(0, location) < 0)2435error(loc, "with layout(location =", "no rayPayloadEXT/rayPayloadInEXT declared", "%d)", location);2436}2437break;2438case EOpHitObjectRecordHitNV:2439if (!(*argp)[12]->getAsConstantUnion())2440error(loc, "argument must be compile-time constant", "hitobjectattribute number", "");2441else {2442unsigned int location = (*argp)[12]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2443if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(2, location) < 0)2444error(loc, "with layout(location =", "no hitObjectAttributeNV declared", "%d)", location);2445}2446break;2447case EOpHitObjectRecordHitMotionNV:2448if (!(*argp)[13]->getAsConstantUnion())2449error(loc, "argument must be compile-time constant", "hitobjectattribute number", "");2450else {2451unsigned int location = (*argp)[13]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2452if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(2, location) < 0)2453error(loc, "with layout(location =", "no hitObjectAttributeNV declared", "%d)", location);2454}2455break;2456case EOpHitObjectRecordHitWithIndexNV:2457if (!(*argp)[11]->getAsConstantUnion())2458error(loc, "argument must be compile-time constant", "hitobjectattribute number", "");2459else {2460unsigned int location = (*argp)[11]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2461if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(2, location) < 0)2462error(loc, "with layout(location =", "no hitObjectAttributeNV declared", "%d)", location);2463}2464break;2465case EOpHitObjectRecordHitWithIndexMotionNV:2466if (!(*argp)[12]->getAsConstantUnion())2467error(loc, "argument must be compile-time constant", "hitobjectattribute number", "");2468else {2469unsigned int location = (*argp)[12]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2470if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(2, location) < 0)2471error(loc, "with layout(location =", "no hitObjectAttributeNV declared", "%d)", location);2472}2473break;2474case EOpHitObjectGetAttributesNV:2475if (!(*argp)[1]->getAsConstantUnion())2476error(loc, "argument must be compile-time constant", "hitobjectattribute number", "");2477else {2478unsigned int location = (*argp)[1]->getAsConstantUnion()->getAsConstantUnion()->getConstArray()[0].getUConst();2479if (!extensionTurnedOn(E_GL_EXT_spirv_intrinsics) && intermediate.checkLocationRT(2, location) < 0)2480error(loc, "with layout(location =", "no hitObjectAttributeNV declared", "%d)", location);2481}2482break;24832484case EOpRayQueryGetIntersectionType:2485case EOpRayQueryGetIntersectionT:2486case EOpRayQueryGetIntersectionInstanceCustomIndex:2487case EOpRayQueryGetIntersectionInstanceId:2488case EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset:2489case EOpRayQueryGetIntersectionGeometryIndex:2490case EOpRayQueryGetIntersectionPrimitiveIndex:2491case EOpRayQueryGetIntersectionBarycentrics:2492case EOpRayQueryGetIntersectionFrontFace:2493case EOpRayQueryGetIntersectionObjectRayDirection:2494case EOpRayQueryGetIntersectionObjectRayOrigin:2495case EOpRayQueryGetIntersectionObjectToWorld:2496case EOpRayQueryGetIntersectionWorldToObject:2497case EOpRayQueryGetIntersectionTriangleVertexPositionsEXT:2498if (!(*argp)[1]->getAsConstantUnion())2499error(loc, "argument must be compile-time constant", "committed", "");2500break;25012502case EOpTextureQuerySamples:2503case EOpImageQuerySamples:2504// GL_ARB_shader_texture_image_samples2505profileRequires(loc, ~EEsProfile, 450, E_GL_ARB_shader_texture_image_samples, "textureSamples and imageSamples");2506break;25072508case EOpImageAtomicAdd:2509case EOpImageAtomicMin:2510case EOpImageAtomicMax:2511case EOpImageAtomicAnd:2512case EOpImageAtomicOr:2513case EOpImageAtomicXor:2514case EOpImageAtomicExchange:2515case EOpImageAtomicCompSwap:2516case EOpImageAtomicLoad:2517case EOpImageAtomicStore:2518{2519// Make sure the image types have the correct layout() format and correct argument types2520const TType& imageType = arg0->getType();2521if (imageType.getSampler().type == EbtInt || imageType.getSampler().type == EbtUint ||2522imageType.getSampler().type == EbtInt64 || imageType.getSampler().type == EbtUint64) {2523if (imageType.getQualifier().getFormat() != ElfR32i && imageType.getQualifier().getFormat() != ElfR32ui &&2524imageType.getQualifier().getFormat() != ElfR64i && imageType.getQualifier().getFormat() != ElfR64ui)2525error(loc, "only supported on image with format r32i or r32ui", fnCandidate.getName().c_str(), "");2526if (callNode.getType().getBasicType() == EbtInt64 && imageType.getQualifier().getFormat() != ElfR64i)2527error(loc, "only supported on image with format r64i", fnCandidate.getName().c_str(), "");2528else if (callNode.getType().getBasicType() == EbtUint64 && imageType.getQualifier().getFormat() != ElfR64ui)2529error(loc, "only supported on image with format r64ui", fnCandidate.getName().c_str(), "");2530} else if(callNode.getType().getBasicType() == EbtFloat16 &&2531((callNode.getType().getVectorSize() == 2 && arg0->getType().getQualifier().getFormat() == ElfRg16f) ||2532(callNode.getType().getVectorSize() == 4 && arg0->getType().getQualifier().getFormat() == ElfRgba16f))) {2533if (StartsWith(fnCandidate.getName(), "imageAtomicAdd") ||2534StartsWith(fnCandidate.getName(), "imageAtomicExchange") ||2535StartsWith(fnCandidate.getName(), "imageAtomicMin") ||2536StartsWith(fnCandidate.getName(), "imageAtomicMax")) {2537requireExtensions(loc, 1, &E_GL_NV_shader_atomic_fp16_vector, fnCandidate.getName().c_str());2538} else {2539error(loc, "f16vec2/4 operation not supported on: ", fnCandidate.getName().c_str(), "");2540}2541} else if (imageType.getSampler().type == EbtFloat) {2542if (StartsWith(fnCandidate.getName(), "imageAtomicExchange")) {2543// imageAtomicExchange doesn't require an extension2544} else if (StartsWith(fnCandidate.getName(), "imageAtomicAdd") ||2545StartsWith(fnCandidate.getName(), "imageAtomicLoad") ||2546StartsWith(fnCandidate.getName(), "imageAtomicStore")) {2547requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float, fnCandidate.getName().c_str());2548} else if (StartsWith(fnCandidate.getName(), "imageAtomicMin") ||2549StartsWith(fnCandidate.getName(), "imageAtomicMax")) {2550requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float2, fnCandidate.getName().c_str());2551} else {2552error(loc, "only supported on integer images", fnCandidate.getName().c_str(), "");2553}2554if (imageType.getQualifier().getFormat() != ElfR32f && isEsProfile())2555error(loc, "only supported on image with format r32f", fnCandidate.getName().c_str(), "");2556} else {2557error(loc, "not supported on this image type", fnCandidate.getName().c_str(), "");2558}25592560const size_t maxArgs = imageType.getSampler().isMultiSample() ? 5 : 4;2561if (argp->size() > maxArgs) {2562requireExtensions(loc, 1, &E_GL_KHR_memory_scope_semantics, fnCandidate.getName().c_str());2563memorySemanticsCheck(loc, fnCandidate, callNode);2564}25652566break;2567}25682569case EOpAtomicAdd:2570case EOpAtomicSubtract:2571case EOpAtomicMin:2572case EOpAtomicMax:2573case EOpAtomicAnd:2574case EOpAtomicOr:2575case EOpAtomicXor:2576case EOpAtomicExchange:2577case EOpAtomicCompSwap:2578case EOpAtomicLoad:2579case EOpAtomicStore:2580{2581if (argp->size() > 3) {2582requireExtensions(loc, 1, &E_GL_KHR_memory_scope_semantics, fnCandidate.getName().c_str());2583memorySemanticsCheck(loc, fnCandidate, callNode);2584if ((callNode.getOp() == EOpAtomicAdd || callNode.getOp() == EOpAtomicExchange ||2585callNode.getOp() == EOpAtomicLoad || callNode.getOp() == EOpAtomicStore) &&2586(arg0->getType().getBasicType() == EbtFloat ||2587arg0->getType().getBasicType() == EbtDouble)) {2588requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float, fnCandidate.getName().c_str());2589} else if ((callNode.getOp() == EOpAtomicAdd || callNode.getOp() == EOpAtomicExchange ||2590callNode.getOp() == EOpAtomicLoad || callNode.getOp() == EOpAtomicStore ||2591callNode.getOp() == EOpAtomicMin || callNode.getOp() == EOpAtomicMax) &&2592arg0->getType().isFloatingDomain()) {2593requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float2, fnCandidate.getName().c_str());2594}2595} else if (arg0->getType().getBasicType() == EbtInt64 || arg0->getType().getBasicType() == EbtUint64) {2596const char* const extensions[2] = { E_GL_NV_shader_atomic_int64,2597E_GL_EXT_shader_atomic_int64 };2598requireExtensions(loc, 2, extensions, fnCandidate.getName().c_str());2599} else if ((callNode.getOp() == EOpAtomicAdd || callNode.getOp() == EOpAtomicExchange ||2600callNode.getOp() == EOpAtomicMin || callNode.getOp() == EOpAtomicMax) &&2601arg0->getType().getBasicType() == EbtFloat16 &&2602(arg0->getType().getVectorSize() == 2 || arg0->getType().getVectorSize() == 4 )) {2603requireExtensions(loc, 1, &E_GL_NV_shader_atomic_fp16_vector, fnCandidate.getName().c_str());2604} else if ((callNode.getOp() == EOpAtomicAdd || callNode.getOp() == EOpAtomicExchange) &&2605(arg0->getType().getBasicType() == EbtFloat ||2606arg0->getType().getBasicType() == EbtDouble)) {2607requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float, fnCandidate.getName().c_str());2608} else if ((callNode.getOp() == EOpAtomicAdd || callNode.getOp() == EOpAtomicExchange ||2609callNode.getOp() == EOpAtomicLoad || callNode.getOp() == EOpAtomicStore ||2610callNode.getOp() == EOpAtomicMin || callNode.getOp() == EOpAtomicMax) &&2611arg0->getType().isFloatingDomain()) {2612requireExtensions(loc, 1, &E_GL_EXT_shader_atomic_float2, fnCandidate.getName().c_str());2613}26142615const TIntermTyped* base = TIntermediate::traverseLValueBase(arg0, true, true);2616const char* errMsg = "Only l-values corresponding to shader block storage or shared variables can be used with "2617"atomic memory functions.";2618if (base) {2619const TType* refType = (base->getType().isReference()) ? base->getType().getReferentType() : nullptr;2620const TQualifier& qualifier =2621(refType != nullptr) ? refType->getQualifier() : base->getType().getQualifier();2622if (qualifier.storage != EvqShared && qualifier.storage != EvqBuffer &&2623qualifier.storage != EvqtaskPayloadSharedEXT)2624error(loc, errMsg, fnCandidate.getName().c_str(), "");2625} else {2626error(loc, errMsg, fnCandidate.getName().c_str(), "");2627}26282629break;2630}26312632case EOpInterpolateAtCentroid:2633case EOpInterpolateAtSample:2634case EOpInterpolateAtOffset:2635case EOpInterpolateAtVertex: {2636if (arg0->getType().getQualifier().storage != EvqVaryingIn) {2637// Traverse down the left branch of arg0 to ensure this argument is a valid interpolant.2638//2639// For desktop GL >4.3 we effectively only need to ensure that arg0 represents an l-value from an2640// input declaration.2641//2642// For desktop GL <= 4.3 and ES, we must also ensure that swizzling is not used2643//2644// For ES, we must also ensure that a field selection operator (i.e., '.') is not used on a named2645// struct.26462647const bool esProfile = isEsProfile();2648const bool swizzleOkay = !esProfile && (version >= 440);26492650std::string interpolantErrorMsg = "first argument must be an interpolant, or interpolant-array element";2651bool isValid = true; // Assume that the interpolant is valid until we find a condition making it invalid2652bool isIn = false; // Checks whether or not the interpolant is a shader input2653bool structAccessOp = false; // Whether or not the previous node in the chain is a struct accessor2654TIntermediate::traverseLValueBase(2655arg0, swizzleOkay, false,2656[&isValid, &isIn, &interpolantErrorMsg, esProfile, &structAccessOp](const TIntermNode& n) -> bool {2657auto* type = n.getAsTyped();2658if (type) {2659if (type->getType().getQualifier().storage == EvqVaryingIn) {2660isIn = true;2661}2662// If a field accessor was used, it can only be used to access a field with an input block, not a struct.2663if (structAccessOp && (type->getType().getBasicType() != EbtBlock)) {2664interpolantErrorMsg +=2665". Using the field of a named struct as an interpolant argument is not "2666"allowed (ES-only).";2667isValid = false;2668}2669}26702671// ES has different requirements for interpolants than GL2672if (esProfile) {2673// Swizzling will be taken care of by the 'swizzleOkay' argument passsed to traverseLValueBase,2674// so we only ned to check whether or not a field accessor has been used with a named struct.2675auto* binary = n.getAsBinaryNode();2676if (binary && (binary->getOp() == EOpIndexDirectStruct)) {2677structAccessOp = true;2678}2679}2680// Don't continue traversing if we know we have an invalid interpolant at this point.2681return isValid;2682});2683if (!isIn || !isValid) {2684error(loc, interpolantErrorMsg.c_str(), fnCandidate.getName().c_str(), "");2685}2686}26872688if (callNode.getOp() == EOpInterpolateAtVertex) {2689if (!arg0->getType().getQualifier().isExplicitInterpolation())2690error(loc, "argument must be qualified as __explicitInterpAMD in", "interpolant", "");2691else {2692if (! (*argp)[1]->getAsConstantUnion())2693error(loc, "argument must be compile-time constant", "vertex index", "");2694else {2695unsigned vertexIdx = (*argp)[1]->getAsConstantUnion()->getConstArray()[0].getUConst();2696if (vertexIdx > 2)2697error(loc, "must be in the range [0, 2]", "vertex index", "");2698}2699}2700}2701} break;27022703case EOpEmitStreamVertex:2704case EOpEndStreamPrimitive:2705if (version == 150)2706requireExtensions(loc, 1, &E_GL_ARB_gpu_shader5, "if the verison is 150 , the EmitStreamVertex and EndStreamPrimitive only support at extension GL_ARB_gpu_shader5");2707intermediate.setMultiStream();2708break;27092710case EOpSubgroupClusteredAdd:2711case EOpSubgroupClusteredMul:2712case EOpSubgroupClusteredMin:2713case EOpSubgroupClusteredMax:2714case EOpSubgroupClusteredAnd:2715case EOpSubgroupClusteredOr:2716case EOpSubgroupClusteredXor:2717// The <clusterSize> as used in the subgroupClustered<op>() operations must be:2718// - An integral constant expression.2719// - At least 1.2720// - A power of 2.2721if ((*argp)[1]->getAsConstantUnion() == nullptr)2722error(loc, "argument must be compile-time constant", "cluster size", "");2723else {2724int size = (*argp)[1]->getAsConstantUnion()->getConstArray()[0].getIConst();2725if (size < 1)2726error(loc, "argument must be at least 1", "cluster size", "");2727else if (!IsPow2(size))2728error(loc, "argument must be a power of 2", "cluster size", "");2729}2730break;27312732case EOpSubgroupBroadcast:2733case EOpSubgroupQuadBroadcast:2734if (spvVersion.spv < EShTargetSpv_1_5) {2735// <id> must be an integral constant expression.2736if ((*argp)[1]->getAsConstantUnion() == nullptr)2737error(loc, "argument must be compile-time constant", "id", "");2738}2739break;27402741case EOpBarrier:2742case EOpMemoryBarrier:2743if (argp->size() > 0) {2744requireExtensions(loc, 1, &E_GL_KHR_memory_scope_semantics, fnCandidate.getName().c_str());2745memorySemanticsCheck(loc, fnCandidate, callNode);2746}2747break;27482749case EOpMix:2750if (profile == EEsProfile && version < 310) {2751// Look for specific signatures2752if ((*argp)[0]->getAsTyped()->getBasicType() != EbtFloat &&2753(*argp)[1]->getAsTyped()->getBasicType() != EbtFloat &&2754(*argp)[2]->getAsTyped()->getBasicType() == EbtBool) {2755requireExtensions(loc, 1, &E_GL_EXT_shader_integer_mix, "specific signature of builtin mix");2756}2757}27582759if (profile != EEsProfile && version < 450) {2760if ((*argp)[0]->getAsTyped()->getBasicType() != EbtFloat &&2761(*argp)[0]->getAsTyped()->getBasicType() != EbtDouble &&2762(*argp)[1]->getAsTyped()->getBasicType() != EbtFloat &&2763(*argp)[1]->getAsTyped()->getBasicType() != EbtDouble &&2764(*argp)[2]->getAsTyped()->getBasicType() == EbtBool) {2765requireExtensions(loc, 1, &E_GL_EXT_shader_integer_mix, fnCandidate.getName().c_str());2766}2767}27682769break;27702771default:2772break;2773}27742775// Texture operations on texture objects (aside from texelFetch on a2776// textureBuffer) require EXT_samplerless_texture_functions.2777switch (callNode.getOp()) {2778case EOpTextureQuerySize:2779case EOpTextureQueryLevels:2780case EOpTextureQuerySamples:2781case EOpTextureFetch:2782case EOpTextureFetchOffset:2783{2784const TSampler& sampler = fnCandidate[0].type->getSampler();27852786const bool isTexture = sampler.isTexture() && !sampler.isCombined();2787const bool isBuffer = sampler.isBuffer();2788const bool isFetch = callNode.getOp() == EOpTextureFetch || callNode.getOp() == EOpTextureFetchOffset;27892790if (isTexture && (!isBuffer || !isFetch))2791requireExtensions(loc, 1, &E_GL_EXT_samplerless_texture_functions, fnCandidate.getName().c_str());27922793break;2794}27952796default:2797break;2798}27992800if (callNode.isSubgroup()) {2801// these require SPIR-V 1.32802if (spvVersion.spv > 0 && spvVersion.spv < EShTargetSpv_1_3)2803error(loc, "requires SPIR-V 1.3", "subgroup op", "");28042805// Check that if extended types are being used that the correct extensions are enabled.2806if (arg0 != nullptr) {2807const TType& type = arg0->getType();2808bool enhanced = intermediate.getEnhancedMsgs();2809switch (type.getBasicType()) {2810default:2811break;2812case EbtInt8:2813case EbtUint8:2814requireExtensions(loc, 1, &E_GL_EXT_shader_subgroup_extended_types_int8, type.getCompleteString(enhanced).c_str());2815break;2816case EbtInt16:2817case EbtUint16:2818requireExtensions(loc, 1, &E_GL_EXT_shader_subgroup_extended_types_int16, type.getCompleteString(enhanced).c_str());2819break;2820case EbtInt64:2821case EbtUint64:2822requireExtensions(loc, 1, &E_GL_EXT_shader_subgroup_extended_types_int64, type.getCompleteString(enhanced).c_str());2823break;2824case EbtFloat16:2825requireExtensions(loc, 1, &E_GL_EXT_shader_subgroup_extended_types_float16, type.getCompleteString(enhanced).c_str());2826break;2827}2828}2829}2830}283128322833// Deprecated! Use PureOperatorBuiltins == true instead, in which case this2834// functionality is handled in builtInOpCheck() instead of here.2835//2836// Do additional checking of built-in function calls that were not mapped2837// to built-in operations (e.g., texturing functions).2838//2839// Assumes there has been a semantically correct match to a built-in function.2840//2841void TParseContext::nonOpBuiltInCheck(const TSourceLoc& loc, const TFunction& fnCandidate, TIntermAggregate& callNode)2842{2843// Further maintenance of this function is deprecated, because the "correct"2844// future-oriented design is to not have to do string compares on function names.28452846// If PureOperatorBuiltins == true, then all built-ins should be mapped2847// to a TOperator, and this function would then never get called.28482849assert(PureOperatorBuiltins == false);28502851// built-in texturing functions get their return value precision from the precision of the sampler2852if (fnCandidate.getType().getQualifier().precision == EpqNone &&2853fnCandidate.getParamCount() > 0 && fnCandidate[0].type->getBasicType() == EbtSampler)2854callNode.getQualifier().precision = callNode.getSequence()[0]->getAsTyped()->getQualifier().precision;28552856if (fnCandidate.getName().compare(0, 7, "texture") == 0) {2857if (fnCandidate.getName().compare(0, 13, "textureGather") == 0) {2858TString featureString = fnCandidate.getName() + "(...)";2859const char* feature = featureString.c_str();2860profileRequires(loc, EEsProfile, 310, nullptr, feature);28612862int compArg = -1; // track which argument, if any, is the constant component argument2863if (fnCandidate.getName().compare("textureGatherOffset") == 0) {2864// GL_ARB_texture_gather is good enough for 2D non-shadow textures with no component argument2865if (fnCandidate[0].type->getSampler().dim == Esd2D && ! fnCandidate[0].type->getSampler().shadow && fnCandidate.getParamCount() == 3)2866profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_texture_gather, feature);2867else2868profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2869int offsetArg = fnCandidate[0].type->getSampler().shadow ? 3 : 2;2870if (! callNode.getSequence()[offsetArg]->getAsConstantUnion())2871profileRequires(loc, EEsProfile, 320, Num_AEP_gpu_shader5, AEP_gpu_shader5,2872"non-constant offset argument");2873if (! fnCandidate[0].type->getSampler().shadow)2874compArg = 3;2875} else if (fnCandidate.getName().compare("textureGatherOffsets") == 0) {2876profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2877if (! fnCandidate[0].type->getSampler().shadow)2878compArg = 3;2879// check for constant offsets2880int offsetArg = fnCandidate[0].type->getSampler().shadow ? 3 : 2;2881if (! callNode.getSequence()[offsetArg]->getAsConstantUnion())2882error(loc, "must be a compile-time constant:", feature, "offsets argument");2883} else if (fnCandidate.getName().compare("textureGather") == 0) {2884// More than two arguments needs gpu_shader5, and rectangular or shadow needs gpu_shader5,2885// otherwise, need GL_ARB_texture_gather.2886if (fnCandidate.getParamCount() > 2 || fnCandidate[0].type->getSampler().dim == EsdRect || fnCandidate[0].type->getSampler().shadow) {2887profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_gpu_shader5, feature);2888if (! fnCandidate[0].type->getSampler().shadow)2889compArg = 2;2890} else2891profileRequires(loc, ~EEsProfile, 400, E_GL_ARB_texture_gather, feature);2892}28932894if (compArg > 0 && compArg < fnCandidate.getParamCount()) {2895if (callNode.getSequence()[compArg]->getAsConstantUnion()) {2896int value = callNode.getSequence()[compArg]->getAsConstantUnion()->getConstArray()[0].getIConst();2897if (value < 0 || value > 3)2898error(loc, "must be 0, 1, 2, or 3:", feature, "component argument");2899} else2900error(loc, "must be a compile-time constant:", feature, "component argument");2901}2902} else {2903// this is only for functions not starting "textureGather"...2904if (fnCandidate.getName().find("Offset") != TString::npos) {29052906// Handle texture-offset limits checking2907int arg = -1;2908if (fnCandidate.getName().compare("textureOffset") == 0)2909arg = 2;2910else if (fnCandidate.getName().compare("texelFetchOffset") == 0)2911arg = 3;2912else if (fnCandidate.getName().compare("textureProjOffset") == 0)2913arg = 2;2914else if (fnCandidate.getName().compare("textureLodOffset") == 0)2915arg = 3;2916else if (fnCandidate.getName().compare("textureProjLodOffset") == 0)2917arg = 3;2918else if (fnCandidate.getName().compare("textureGradOffset") == 0)2919arg = 4;2920else if (fnCandidate.getName().compare("textureProjGradOffset") == 0)2921arg = 4;29222923if (arg > 0) {2924if (! callNode.getSequence()[arg]->getAsConstantUnion())2925error(loc, "argument must be compile-time constant", "texel offset", "");2926else {2927const TType& type = callNode.getSequence()[arg]->getAsTyped()->getType();2928for (int c = 0; c < type.getVectorSize(); ++c) {2929int offset = callNode.getSequence()[arg]->getAsConstantUnion()->getConstArray()[c].getIConst();2930if (offset > resources.maxProgramTexelOffset || offset < resources.minProgramTexelOffset)2931error(loc, "value is out of range:", "texel offset", "[gl_MinProgramTexelOffset, gl_MaxProgramTexelOffset]");2932}2933}2934}2935}2936}2937}29382939// GL_ARB_shader_texture_image_samples2940if (fnCandidate.getName().compare(0, 14, "textureSamples") == 0 || fnCandidate.getName().compare(0, 12, "imageSamples") == 0)2941profileRequires(loc, ~EEsProfile, 450, E_GL_ARB_shader_texture_image_samples, "textureSamples and imageSamples");29422943if (fnCandidate.getName().compare(0, 11, "imageAtomic") == 0) {2944const TType& imageType = callNode.getSequence()[0]->getAsTyped()->getType();2945if (imageType.getSampler().type == EbtInt || imageType.getSampler().type == EbtUint) {2946if (imageType.getQualifier().getFormat() != ElfR32i && imageType.getQualifier().getFormat() != ElfR32ui)2947error(loc, "only supported on image with format r32i or r32ui", fnCandidate.getName().c_str(), "");2948} else {2949if (fnCandidate.getName().compare(0, 19, "imageAtomicExchange") != 0)2950error(loc, "only supported on integer images", fnCandidate.getName().c_str(), "");2951else if (imageType.getQualifier().getFormat() != ElfR32f && isEsProfile())2952error(loc, "only supported on image with format r32f", fnCandidate.getName().c_str(), "");2953}2954}2955}29562957//2958// Do any extra checking for a user function call.2959//2960void TParseContext::userFunctionCallCheck(const TSourceLoc& loc, TIntermAggregate& callNode)2961{2962TIntermSequence& arguments = callNode.getSequence();29632964for (int i = 0; i < (int)arguments.size(); ++i)2965samplerConstructorLocationCheck(loc, "call argument", arguments[i]);2966}29672968//2969// Emit an error if this is a sampler constructor2970//2971void TParseContext::samplerConstructorLocationCheck(const TSourceLoc& loc, const char* token, TIntermNode* node)2972{2973if (node->getAsOperator() && node->getAsOperator()->getOp() == EOpConstructTextureSampler)2974error(loc, "sampler constructor must appear at point of use", token, "");2975}29762977//2978// Handle seeing a built-in constructor in a grammar production.2979//2980TFunction* TParseContext::handleConstructorCall(const TSourceLoc& loc, const TPublicType& publicType)2981{2982TType type(publicType);2983type.getQualifier().precision = EpqNone;29842985if (type.isArray()) {2986profileRequires(loc, ENoProfile, 120, E_GL_3DL_array_objects, "arrayed constructor");2987profileRequires(loc, EEsProfile, 300, nullptr, "arrayed constructor");2988}29892990// Reuse EOpConstructTextureSampler for bindless image constructor2991// uvec2 imgHandle;2992// imageLoad(image1D(imgHandle), 0);2993if (type.isImage() && extensionTurnedOn(E_GL_ARB_bindless_texture))2994{2995intermediate.setBindlessImageMode(currentCaller, AstRefTypeFunc);2996}29972998TOperator op = intermediate.mapTypeToConstructorOp(type);29993000if (op == EOpNull) {3001if (intermediate.getEnhancedMsgs() && type.getBasicType() == EbtSampler)3002error(loc, "function not supported in this version; use texture() instead", "texture*D*", "");3003else3004error(loc, "cannot construct this type", type.getBasicString(), "");3005op = EOpConstructFloat;3006TType errorType(EbtFloat);3007type.shallowCopy(errorType);3008}30093010TString empty("");30113012return new TFunction(&empty, type, op);3013}30143015// Handle seeing a precision qualifier in the grammar.3016void TParseContext::handlePrecisionQualifier(const TSourceLoc& /*loc*/, TQualifier& qualifier, TPrecisionQualifier precision)3017{3018if (obeyPrecisionQualifiers())3019qualifier.precision = precision;3020}30213022// Check for messages to give on seeing a precision qualifier used in a3023// declaration in the grammar.3024void TParseContext::checkPrecisionQualifier(const TSourceLoc& loc, TPrecisionQualifier)3025{3026if (precisionManager.shouldWarnAboutDefaults()) {3027warn(loc, "all default precisions are highp; use precision statements to quiet warning, e.g.:\n"3028" \"precision mediump int; precision highp float;\"", "", "");3029precisionManager.defaultWarningGiven();3030}3031}30323033//3034// Same error message for all places assignments don't work.3035//3036void TParseContext::assignError(const TSourceLoc& loc, const char* op, TString left, TString right)3037{3038error(loc, "", op, "cannot convert from '%s' to '%s'",3039right.c_str(), left.c_str());3040}30413042//3043// Same error message for all places unary operations don't work.3044//3045void TParseContext::unaryOpError(const TSourceLoc& loc, const char* op, TString operand)3046{3047error(loc, " wrong operand type", op,3048"no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",3049op, operand.c_str());3050}30513052//3053// Same error message for all binary operations don't work.3054//3055void TParseContext::binaryOpError(const TSourceLoc& loc, const char* op, TString left, TString right)3056{3057error(loc, " wrong operand types:", op,3058"no operation '%s' exists that takes a left-hand operand of type '%s' and "3059"a right operand of type '%s' (or there is no acceptable conversion)",3060op, left.c_str(), right.c_str());3061}30623063//3064// A basic type of EbtVoid is a key that the name string was seen in the source, but3065// it was not found as a variable in the symbol table. If so, give the error3066// message and insert a dummy variable in the symbol table to prevent future errors.3067//3068void TParseContext::variableCheck(TIntermTyped*& nodePtr)3069{3070TIntermSymbol* symbol = nodePtr->getAsSymbolNode();3071if (! symbol)3072return;30733074if (symbol->getType().getBasicType() == EbtVoid) {3075const char *extraInfoFormat = "";3076if (spvVersion.vulkan != 0 && symbol->getName() == "gl_VertexID") {3077extraInfoFormat = "(Did you mean gl_VertexIndex?)";3078} else if (spvVersion.vulkan != 0 && symbol->getName() == "gl_InstanceID") {3079extraInfoFormat = "(Did you mean gl_InstanceIndex?)";3080}3081error(symbol->getLoc(), "undeclared identifier", symbol->getName().c_str(), extraInfoFormat);30823083// Add to symbol table to prevent future error messages on the same name3084if (symbol->getName().size() > 0) {3085TVariable* fakeVariable = new TVariable(&symbol->getName(), TType(EbtFloat));3086symbolTable.insert(*fakeVariable);30873088// substitute a symbol node for this new variable3089nodePtr = intermediate.addSymbol(*fakeVariable, symbol->getLoc());3090}3091} else {3092switch (symbol->getQualifier().storage) {3093case EvqPointCoord:3094profileRequires(symbol->getLoc(), ENoProfile, 120, nullptr, "gl_PointCoord");3095break;3096default: break; // some compilers want this3097}3098}3099}31003101//3102// Both test and if necessary, spit out an error, to see if the node is really3103// an l-value that can be operated on this way.3104//3105// Returns true if there was an error.3106//3107bool TParseContext::lValueErrorCheck(const TSourceLoc& loc, const char* op, TIntermTyped* node)3108{3109TIntermBinary* binaryNode = node->getAsBinaryNode();31103111if (binaryNode) {3112bool errorReturn = false;31133114switch(binaryNode->getOp()) {3115case EOpIndexDirect:3116case EOpIndexIndirect:3117// ... tessellation control shader ...3118// If a per-vertex output variable is used as an l-value, it is a3119// compile-time or link-time error if the expression indicating the3120// vertex index is not the identifier gl_InvocationID.3121if (language == EShLangTessControl) {3122const TType& leftType = binaryNode->getLeft()->getType();3123if (leftType.getQualifier().storage == EvqVaryingOut && ! leftType.getQualifier().patch && binaryNode->getLeft()->getAsSymbolNode()) {3124// we have a per-vertex output3125const TIntermSymbol* rightSymbol = binaryNode->getRight()->getAsSymbolNode();3126if (! rightSymbol || rightSymbol->getQualifier().builtIn != EbvInvocationId)3127error(loc, "tessellation-control per-vertex output l-value must be indexed with gl_InvocationID", "[]", "");3128}3129}3130break; // left node is checked by base class3131case EOpVectorSwizzle:3132errorReturn = lValueErrorCheck(loc, op, binaryNode->getLeft());3133if (!errorReturn) {3134int offset[4] = {0,0,0,0};31353136TIntermTyped* rightNode = binaryNode->getRight();3137TIntermAggregate *aggrNode = rightNode->getAsAggregate();31383139for (TIntermSequence::iterator p = aggrNode->getSequence().begin();3140p != aggrNode->getSequence().end(); p++) {3141int value = (*p)->getAsTyped()->getAsConstantUnion()->getConstArray()[0].getIConst();3142offset[value]++;3143if (offset[value] > 1) {3144error(loc, " l-value of swizzle cannot have duplicate components", op, "", "");31453146return true;3147}3148}3149}31503151return errorReturn;3152default:3153break;3154}31553156if (errorReturn) {3157error(loc, " l-value required", op, "", "");3158return true;3159}3160}31613162if (binaryNode && binaryNode->getOp() == EOpIndexDirectStruct && binaryNode->getLeft()->isReference())3163return false;31643165// Let the base class check errors3166if (TParseContextBase::lValueErrorCheck(loc, op, node))3167return true;31683169const char* symbol = nullptr;3170TIntermSymbol* symNode = node->getAsSymbolNode();3171if (symNode != nullptr)3172symbol = symNode->getName().c_str();31733174const char* message = nullptr;3175switch (node->getQualifier().storage) {3176case EvqVaryingIn: message = "can't modify shader input"; break;3177case EvqInstanceId: message = "can't modify gl_InstanceID"; break;3178case EvqVertexId: message = "can't modify gl_VertexID"; break;3179case EvqFace: message = "can't modify gl_FrontFace"; break;3180case EvqFragCoord: message = "can't modify gl_FragCoord"; break;3181case EvqPointCoord: message = "can't modify gl_PointCoord"; break;3182case EvqFragDepth:3183intermediate.setDepthReplacing();3184// "In addition, it is an error to statically write to gl_FragDepth in the fragment shader."3185if (isEsProfile() && intermediate.getEarlyFragmentTests())3186message = "can't modify gl_FragDepth if using early_fragment_tests";3187break;3188case EvqFragStencil:3189intermediate.setStencilReplacing();3190// "In addition, it is an error to statically write to gl_FragDepth in the fragment shader."3191if (isEsProfile() && intermediate.getEarlyFragmentTests())3192message = "can't modify EvqFragStencil if using early_fragment_tests";3193break;31943195case EvqtaskPayloadSharedEXT:3196if (language == EShLangMesh)3197message = "can't modify variable with storage qualifier taskPayloadSharedEXT in mesh shaders";3198break;3199default:3200break;3201}32023203if (message == nullptr && binaryNode == nullptr && symNode == nullptr) {3204error(loc, " l-value required", op, "", "");32053206return true;3207}32083209//3210// Everything else is okay, no error.3211//3212if (message == nullptr)3213return false;32143215//3216// If we get here, we have an error and a message.3217//3218if (symNode)3219error(loc, " l-value required", op, "\"%s\" (%s)", symbol, message);3220else3221error(loc, " l-value required", op, "(%s)", message);32223223return true;3224}32253226// Test for and give an error if the node can't be read from.3227void TParseContext::rValueErrorCheck(const TSourceLoc& loc, const char* op, TIntermTyped* node)3228{3229// Let the base class check errors3230TParseContextBase::rValueErrorCheck(loc, op, node);32313232TIntermSymbol* symNode = node->getAsSymbolNode();3233if (!(symNode && symNode->getQualifier().isWriteOnly())) // base class checks3234if (symNode && symNode->getQualifier().isExplicitInterpolation())3235error(loc, "can't read from explicitly-interpolated object: ", op, symNode->getName().c_str());32363237// local_size_{xyz} must be assigned or specialized before gl_WorkGroupSize can be assigned.3238if(node->getQualifier().builtIn == EbvWorkGroupSize &&3239!(intermediate.isLocalSizeSet() || intermediate.isLocalSizeSpecialized()))3240error(loc, "can't read from gl_WorkGroupSize before a fixed workgroup size has been declared", op, "");3241}32423243//3244// Both test, and if necessary spit out an error, to see if the node is really3245// a constant.3246//3247void TParseContext::constantValueCheck(TIntermTyped* node, const char* token)3248{3249if (! node->getQualifier().isConstant())3250error(node->getLoc(), "constant expression required", token, "");3251}32523253//3254// Both test, and if necessary spit out an error, to see if the node is really3255// a 32-bit integer or can implicitly convert to one.3256//3257void TParseContext::integerCheck(const TIntermTyped* node, const char* token)3258{3259auto from_type = node->getBasicType();3260if ((from_type == EbtInt || from_type == EbtUint ||3261intermediate.canImplicitlyPromote(from_type, EbtInt, EOpNull) ||3262intermediate.canImplicitlyPromote(from_type, EbtUint, EOpNull)) && node->isScalar())3263return;32643265error(node->getLoc(), "scalar integer expression required", token, "");3266}32673268//3269// Both test, and if necessary spit out an error, to see if we are currently3270// globally scoped.3271//3272void TParseContext::globalCheck(const TSourceLoc& loc, const char* token)3273{3274if (! symbolTable.atGlobalLevel())3275error(loc, "not allowed in nested scope", token, "");3276}32773278//3279// Reserved errors for GLSL.3280//3281void TParseContext::reservedErrorCheck(const TSourceLoc& loc, const TString& identifier)3282{3283// "Identifiers starting with "gl_" are reserved for use by OpenGL, and may not be3284// declared in a shader; this results in a compile-time error."3285if (! symbolTable.atBuiltInLevel()) {3286if (builtInName(identifier) && !extensionTurnedOn(E_GL_EXT_spirv_intrinsics))3287// The extension GL_EXT_spirv_intrinsics allows us to declare identifiers starting with "gl_".3288error(loc, "identifiers starting with \"gl_\" are reserved", identifier.c_str(), "");32893290// "__" are not supposed to be an error. ES 300 (and desktop) added the clarification:3291// "In addition, all identifiers containing two consecutive underscores (__) are3292// reserved; using such a name does not itself result in an error, but may result3293// in undefined behavior."3294// however, before that, ES tests required an error.3295if (identifier.find("__") != TString::npos && !extensionTurnedOn(E_GL_EXT_spirv_intrinsics)) {3296// The extension GL_EXT_spirv_intrinsics allows us to declare identifiers starting with "__".3297if (isEsProfile() && version < 300)3298error(loc, "identifiers containing consecutive underscores (\"__\") are reserved, and an error if version < 300", identifier.c_str(), "");3299else3300warn(loc, "identifiers containing consecutive underscores (\"__\") are reserved", identifier.c_str(), "");3301}3302}3303}33043305//3306// Reserved errors for the preprocessor.3307//3308void TParseContext::reservedPpErrorCheck(const TSourceLoc& loc, const char* identifier, const char* op)3309{3310// "__" are not supposed to be an error. ES 300 (and desktop) added the clarification:3311// "All macro names containing two consecutive underscores ( __ ) are reserved;3312// defining such a name does not itself result in an error, but may result in3313// undefined behavior. All macro names prefixed with "GL_" ("GL" followed by a3314// single underscore) are also reserved, and defining such a name results in a3315// compile-time error."3316// however, before that, ES tests required an error.3317if (strncmp(identifier, "GL_", 3) == 0 && !extensionTurnedOn(E_GL_EXT_spirv_intrinsics))3318// The extension GL_EXT_spirv_intrinsics allows us to declare macros prefixed with "GL_".3319ppError(loc, "names beginning with \"GL_\" can't be (un)defined:", op, identifier);3320else if (strncmp(identifier, "defined", 8) == 0)3321if (relaxedErrors())3322ppWarn(loc, "\"defined\" is (un)defined:", op, identifier);3323else3324ppError(loc, "\"defined\" can't be (un)defined:", op, identifier);3325else if (strstr(identifier, "__") != nullptr && !extensionTurnedOn(E_GL_EXT_spirv_intrinsics)) {3326// The extension GL_EXT_spirv_intrinsics allows us to declare macros prefixed with "__".3327if (isEsProfile() && version >= 300 &&3328(strcmp(identifier, "__LINE__") == 0 ||3329strcmp(identifier, "__FILE__") == 0 ||3330strcmp(identifier, "__VERSION__") == 0))3331ppError(loc, "predefined names can't be (un)defined:", op, identifier);3332else {3333if (isEsProfile() && version < 300 && !relaxedErrors())3334ppError(loc, "names containing consecutive underscores are reserved, and an error if version < 300:", op, identifier);3335else3336ppWarn(loc, "names containing consecutive underscores are reserved:", op, identifier);3337}3338}3339}33403341//3342// See if this version/profile allows use of the line-continuation character '\'.3343//3344// Returns true if a line continuation should be done.3345//3346bool TParseContext::lineContinuationCheck(const TSourceLoc& loc, bool endOfComment)3347{3348const char* message = "line continuation";33493350bool lineContinuationAllowed = (isEsProfile() && version >= 300) ||3351(!isEsProfile() && (version >= 420 || extensionTurnedOn(E_GL_ARB_shading_language_420pack)));33523353if (endOfComment) {3354if (lineContinuationAllowed)3355warn(loc, "used at end of comment; the following line is still part of the comment", message, "");3356else3357warn(loc, "used at end of comment, but this version does not provide line continuation", message, "");33583359return lineContinuationAllowed;3360}33613362if (relaxedErrors()) {3363if (! lineContinuationAllowed)3364warn(loc, "not allowed in this version", message, "");3365return true;3366} else {3367profileRequires(loc, EEsProfile, 300, nullptr, message);3368profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, message);3369}33703371return lineContinuationAllowed;3372}33733374bool TParseContext::builtInName(const TString& identifier)3375{3376return identifier.compare(0, 3, "gl_") == 0;3377}33783379//3380// Make sure there is enough data and not too many arguments provided to the3381// constructor to build something of the type of the constructor. Also returns3382// the type of the constructor.3383//3384// Part of establishing type is establishing specialization-constness.3385// We don't yet know "top down" whether type is a specialization constant,3386// but a const constructor can becomes a specialization constant if any of3387// its children are, subject to KHR_vulkan_glsl rules:3388//3389// - int(), uint(), and bool() constructors for type conversions3390// from any of the following types to any of the following types:3391// * int3392// * uint3393// * bool3394// - vector versions of the above conversion constructors3395//3396// Returns true if there was an error in construction.3397//3398bool TParseContext::constructorError(const TSourceLoc& loc, TIntermNode* node, TFunction& function, TOperator op, TType& type)3399{3400// See if the constructor does not establish the main type, only requalifies3401// it, in which case the type comes from the argument instead of from the3402// constructor function.3403switch (op) {3404case EOpConstructNonuniform:3405if (node != nullptr && node->getAsTyped() != nullptr) {3406type.shallowCopy(node->getAsTyped()->getType());3407type.getQualifier().makeTemporary();3408type.getQualifier().nonUniform = true;3409}3410break;3411default:3412type.shallowCopy(function.getType());3413break;3414}34153416TString constructorString;3417if (intermediate.getEnhancedMsgs())3418constructorString.append(type.getCompleteString(true, false, false, true)).append(" constructor");3419else3420constructorString.append("constructor");34213422// See if it's a matrix3423bool constructingMatrix = false;3424switch (op) {3425case EOpConstructTextureSampler:3426return constructorTextureSamplerError(loc, function);3427case EOpConstructMat2x2:3428case EOpConstructMat2x3:3429case EOpConstructMat2x4:3430case EOpConstructMat3x2:3431case EOpConstructMat3x3:3432case EOpConstructMat3x4:3433case EOpConstructMat4x2:3434case EOpConstructMat4x3:3435case EOpConstructMat4x4:3436case EOpConstructDMat2x2:3437case EOpConstructDMat2x3:3438case EOpConstructDMat2x4:3439case EOpConstructDMat3x2:3440case EOpConstructDMat3x3:3441case EOpConstructDMat3x4:3442case EOpConstructDMat4x2:3443case EOpConstructDMat4x3:3444case EOpConstructDMat4x4:3445case EOpConstructF16Mat2x2:3446case EOpConstructF16Mat2x3:3447case EOpConstructF16Mat2x4:3448case EOpConstructF16Mat3x2:3449case EOpConstructF16Mat3x3:3450case EOpConstructF16Mat3x4:3451case EOpConstructF16Mat4x2:3452case EOpConstructF16Mat4x3:3453case EOpConstructF16Mat4x4:3454constructingMatrix = true;3455break;3456default:3457break;3458}34593460//3461// Walk the arguments for first-pass checks and collection of information.3462//34633464int size = 0;3465bool constType = true;3466bool specConstType = false; // value is only valid if constType is true3467bool full = false;3468bool overFull = false;3469bool matrixInMatrix = false;3470bool arrayArg = false;3471bool floatArgument = false;3472bool intArgument = false;3473for (int arg = 0; arg < function.getParamCount(); ++arg) {3474if (function[arg].type->isArray()) {3475if (function[arg].type->isUnsizedArray()) {3476// Can't construct from an unsized array.3477error(loc, "array argument must be sized", constructorString.c_str(), "");3478return true;3479}3480arrayArg = true;3481}3482if (constructingMatrix && function[arg].type->isMatrix())3483matrixInMatrix = true;34843485// 'full' will go to true when enough args have been seen. If we loop3486// again, there is an extra argument.3487if (full) {3488// For vectors and matrices, it's okay to have too many components3489// available, but not okay to have unused arguments.3490overFull = true;3491}34923493size += function[arg].type->computeNumComponents();3494if (op != EOpConstructStruct && ! type.isArray() && size >= type.computeNumComponents())3495full = true;34963497if (! function[arg].type->getQualifier().isConstant())3498constType = false;3499if (function[arg].type->getQualifier().isSpecConstant())3500specConstType = true;3501if (function[arg].type->isFloatingDomain())3502floatArgument = true;3503if (function[arg].type->isIntegerDomain())3504intArgument = true;3505if (type.isStruct()) {3506if (function[arg].type->contains16BitFloat()) {3507requireFloat16Arithmetic(loc, constructorString.c_str(), "can't construct structure containing 16-bit type");3508}3509if (function[arg].type->contains16BitInt()) {3510requireInt16Arithmetic(loc, constructorString.c_str(), "can't construct structure containing 16-bit type");3511}3512if (function[arg].type->contains8BitInt()) {3513requireInt8Arithmetic(loc, constructorString.c_str(), "can't construct structure containing 8-bit type");3514}3515}3516}3517if (op == EOpConstructNonuniform)3518constType = false;35193520switch (op) {3521case EOpConstructFloat16:3522case EOpConstructF16Vec2:3523case EOpConstructF16Vec3:3524case EOpConstructF16Vec4:3525if (type.isArray())3526requireFloat16Arithmetic(loc, constructorString.c_str(), "16-bit arrays not supported");3527if (type.isVector() && function.getParamCount() != 1)3528requireFloat16Arithmetic(loc, constructorString.c_str(), "16-bit vectors only take vector types");3529break;3530case EOpConstructUint16:3531case EOpConstructU16Vec2:3532case EOpConstructU16Vec3:3533case EOpConstructU16Vec4:3534case EOpConstructInt16:3535case EOpConstructI16Vec2:3536case EOpConstructI16Vec3:3537case EOpConstructI16Vec4:3538if (type.isArray())3539requireInt16Arithmetic(loc, constructorString.c_str(), "16-bit arrays not supported");3540if (type.isVector() && function.getParamCount() != 1)3541requireInt16Arithmetic(loc, constructorString.c_str(), "16-bit vectors only take vector types");3542break;3543case EOpConstructUint8:3544case EOpConstructU8Vec2:3545case EOpConstructU8Vec3:3546case EOpConstructU8Vec4:3547case EOpConstructInt8:3548case EOpConstructI8Vec2:3549case EOpConstructI8Vec3:3550case EOpConstructI8Vec4:3551if (type.isArray())3552requireInt8Arithmetic(loc, constructorString.c_str(), "8-bit arrays not supported");3553if (type.isVector() && function.getParamCount() != 1)3554requireInt8Arithmetic(loc, constructorString.c_str(), "8-bit vectors only take vector types");3555break;3556default:3557break;3558}35593560// inherit constness from children3561if (constType) {3562bool makeSpecConst;3563// Finish pinning down spec-const semantics3564if (specConstType) {3565switch (op) {3566case EOpConstructInt8:3567case EOpConstructInt:3568case EOpConstructUint:3569case EOpConstructBool:3570case EOpConstructBVec2:3571case EOpConstructBVec3:3572case EOpConstructBVec4:3573case EOpConstructIVec2:3574case EOpConstructIVec3:3575case EOpConstructIVec4:3576case EOpConstructUVec2:3577case EOpConstructUVec3:3578case EOpConstructUVec4:3579case EOpConstructUint8:3580case EOpConstructInt16:3581case EOpConstructUint16:3582case EOpConstructInt64:3583case EOpConstructUint64:3584case EOpConstructI8Vec2:3585case EOpConstructI8Vec3:3586case EOpConstructI8Vec4:3587case EOpConstructU8Vec2:3588case EOpConstructU8Vec3:3589case EOpConstructU8Vec4:3590case EOpConstructI16Vec2:3591case EOpConstructI16Vec3:3592case EOpConstructI16Vec4:3593case EOpConstructU16Vec2:3594case EOpConstructU16Vec3:3595case EOpConstructU16Vec4:3596case EOpConstructI64Vec2:3597case EOpConstructI64Vec3:3598case EOpConstructI64Vec4:3599case EOpConstructU64Vec2:3600case EOpConstructU64Vec3:3601case EOpConstructU64Vec4:3602// This was the list of valid ones, if they aren't converting from float3603// and aren't making an array.3604makeSpecConst = ! floatArgument && ! type.isArray();3605break;36063607case EOpConstructVec2:3608case EOpConstructVec3:3609case EOpConstructVec4:3610// This was the list of valid ones, if they aren't converting from int3611// and aren't making an array.3612makeSpecConst = ! intArgument && !type.isArray();3613break;36143615default:3616// anything else wasn't white-listed in the spec as a conversion3617makeSpecConst = false;3618break;3619}3620} else3621makeSpecConst = false;36223623if (makeSpecConst)3624type.getQualifier().makeSpecConstant();3625else if (specConstType)3626type.getQualifier().makeTemporary();3627else3628type.getQualifier().storage = EvqConst;3629}36303631if (type.isArray()) {3632if (function.getParamCount() == 0) {3633error(loc, "array constructor must have at least one argument", constructorString.c_str(), "");3634return true;3635}36363637if (type.isUnsizedArray()) {3638// auto adapt the constructor type to the number of arguments3639type.changeOuterArraySize(function.getParamCount());3640} else if (type.getOuterArraySize() != function.getParamCount()) {3641error(loc, "array constructor needs one argument per array element", constructorString.c_str(), "");3642return true;3643}36443645if (type.isArrayOfArrays()) {3646// Types have to match, but we're still making the type.3647// Finish making the type, and the comparison is done later3648// when checking for conversion.3649TArraySizes& arraySizes = *type.getArraySizes();36503651// At least the dimensionalities have to match.3652if (! function[0].type->isArray() ||3653arraySizes.getNumDims() != function[0].type->getArraySizes()->getNumDims() + 1) {3654error(loc, "array constructor argument not correct type to construct array element", constructorString.c_str(), "");3655return true;3656}36573658if (arraySizes.isInnerUnsized()) {3659// "Arrays of arrays ..., and the size for any dimension is optional"3660// That means we need to adopt (from the first argument) the other array sizes into the type.3661for (int d = 1; d < arraySizes.getNumDims(); ++d) {3662if (arraySizes.getDimSize(d) == UnsizedArraySize) {3663arraySizes.setDimSize(d, function[0].type->getArraySizes()->getDimSize(d - 1));3664}3665}3666}3667}3668}36693670if (arrayArg && op != EOpConstructStruct && ! type.isArrayOfArrays()) {3671error(loc, "constructing non-array constituent from array argument", constructorString.c_str(), "");3672return true;3673}36743675if (matrixInMatrix && ! type.isArray()) {3676profileRequires(loc, ENoProfile, 120, nullptr, "constructing matrix from matrix");36773678// "If a matrix argument is given to a matrix constructor,3679// it is a compile-time error to have any other arguments."3680if (function.getParamCount() != 1)3681error(loc, "matrix constructed from matrix can only have one argument", constructorString.c_str(), "");3682return false;3683}36843685if (overFull) {3686error(loc, "too many arguments", constructorString.c_str(), "");3687return true;3688}36893690if (op == EOpConstructStruct && ! type.isArray() && (int)type.getStruct()->size() != function.getParamCount()) {3691error(loc, "Number of constructor parameters does not match the number of structure fields", constructorString.c_str(), "");3692return true;3693}36943695if ((op != EOpConstructStruct && size != 1 && size < type.computeNumComponents()) ||3696(op == EOpConstructStruct && size < type.computeNumComponents())) {3697error(loc, "not enough data provided for construction", constructorString.c_str(), "");3698return true;3699}37003701if (type.isCoopMat() && function.getParamCount() != 1) {3702error(loc, "wrong number of arguments", constructorString.c_str(), "");3703return true;3704}3705if (type.isCoopMat() &&3706!(function[0].type->isScalar() || function[0].type->isCoopMat())) {3707error(loc, "Cooperative matrix constructor argument must be scalar or cooperative matrix", constructorString.c_str(), "");3708return true;3709}37103711TIntermTyped* typed = node->getAsTyped();3712if (type.isCoopMat() && typed->getType().isCoopMat() &&3713!type.sameCoopMatShapeAndUse(typed->getType())) {3714error(loc, "Cooperative matrix type parameters mismatch", constructorString.c_str(), "");3715return true;3716}37173718if (typed == nullptr) {3719error(loc, "constructor argument does not have a type", constructorString.c_str(), "");3720return true;3721}3722if (op != EOpConstructStruct && op != EOpConstructNonuniform && typed->getBasicType() == EbtSampler) {3723if (op == EOpConstructUVec2 && extensionTurnedOn(E_GL_ARB_bindless_texture)) {3724intermediate.setBindlessTextureMode(currentCaller, AstRefTypeFunc);3725}3726else {3727error(loc, "cannot convert a sampler", constructorString.c_str(), "");3728return true;3729}3730}3731if (op != EOpConstructStruct && typed->isAtomic()) {3732error(loc, "cannot convert an atomic_uint", constructorString.c_str(), "");3733return true;3734}3735if (typed->getBasicType() == EbtVoid) {3736error(loc, "cannot convert a void", constructorString.c_str(), "");3737return true;3738}37393740return false;3741}37423743// Verify all the correct semantics for constructing a combined texture/sampler.3744// Return true if the semantics are incorrect.3745bool TParseContext::constructorTextureSamplerError(const TSourceLoc& loc, const TFunction& function)3746{3747TString constructorName = function.getType().getBasicTypeString(); // TODO: performance: should not be making copy; interface needs to change3748const char* token = constructorName.c_str();3749// verify the constructor for bindless texture, the input must be ivec2 or uvec23750if (function.getParamCount() == 1) {3751TType* pType = function[0].type;3752TBasicType basicType = pType->getBasicType();3753bool isIntegerVec2 = ((basicType == EbtUint || basicType == EbtInt) && pType->getVectorSize() == 2);3754bool bindlessMode = extensionTurnedOn(E_GL_ARB_bindless_texture);3755if (isIntegerVec2 && bindlessMode) {3756if (pType->getSampler().isImage())3757intermediate.setBindlessImageMode(currentCaller, AstRefTypeFunc);3758else3759intermediate.setBindlessTextureMode(currentCaller, AstRefTypeFunc);3760return false;3761} else {3762if (!bindlessMode)3763error(loc, "sampler-constructor requires the extension GL_ARB_bindless_texture enabled", token, "");3764else3765error(loc, "sampler-constructor requires the input to be ivec2 or uvec2", token, "");3766return true;3767}3768}37693770// exactly two arguments needed3771if (function.getParamCount() != 2) {3772error(loc, "sampler-constructor requires two arguments", token, "");3773return true;3774}37753776// For now, not allowing arrayed constructors, the rest of this function3777// is set up to allow them, if this test is removed:3778if (function.getType().isArray()) {3779error(loc, "sampler-constructor cannot make an array of samplers", token, "");3780return true;3781}37823783// first argument3784// * the constructor's first argument must be a texture type3785// * the dimensionality (1D, 2D, 3D, Cube, Rect, Buffer, MS, and Array)3786// of the texture type must match that of the constructed sampler type3787// (that is, the suffixes of the type of the first argument and the3788// type of the constructor will be spelled the same way)3789if (function[0].type->getBasicType() != EbtSampler ||3790! function[0].type->getSampler().isTexture() ||3791function[0].type->isArray()) {3792error(loc, "sampler-constructor first argument must be a scalar *texture* type", token, "");3793return true;3794}3795// simulate the first argument's impact on the result type, so it can be compared with the encapsulated operator!=()3796TSampler texture = function.getType().getSampler();3797texture.setCombined(false);3798texture.shadow = false;3799if (texture != function[0].type->getSampler()) {3800error(loc, "sampler-constructor first argument must be a *texture* type"3801" matching the dimensionality and sampled type of the constructor", token, "");3802return true;3803}38043805// second argument3806// * the constructor's second argument must be a scalar of type3807// *sampler* or *samplerShadow*3808if ( function[1].type->getBasicType() != EbtSampler ||3809! function[1].type->getSampler().isPureSampler() ||3810function[1].type->isArray()) {3811error(loc, "sampler-constructor second argument must be a scalar sampler or samplerShadow", token, "");3812return true;3813}38143815return false;3816}38173818// Checks to see if a void variable has been declared and raise an error message for such a case3819//3820// returns true in case of an error3821//3822bool TParseContext::voidErrorCheck(const TSourceLoc& loc, const TString& identifier, const TBasicType basicType)3823{3824if (basicType == EbtVoid) {3825error(loc, "illegal use of type 'void'", identifier.c_str(), "");3826return true;3827}38283829return false;3830}38313832// Checks to see if the node (for the expression) contains a scalar boolean expression or not3833void TParseContext::boolCheck(const TSourceLoc& loc, const TIntermTyped* type)3834{3835if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())3836error(loc, "boolean expression expected", "", "");3837}38383839// This function checks to see if the node (for the expression) contains a scalar boolean expression or not3840void TParseContext::boolCheck(const TSourceLoc& loc, const TPublicType& pType)3841{3842if (pType.basicType != EbtBool || pType.arraySizes || pType.matrixCols > 1 || (pType.vectorSize > 1))3843error(loc, "boolean expression expected", "", "");3844}38453846void TParseContext::samplerCheck(const TSourceLoc& loc, const TType& type, const TString& identifier, TIntermTyped* /*initializer*/)3847{3848// Check that the appropriate extension is enabled if external sampler is used.3849// There are two extensions. The correct one must be used based on GLSL version.3850if (type.getBasicType() == EbtSampler && type.getSampler().isExternal()) {3851if (version < 300) {3852requireExtensions(loc, 1, &E_GL_OES_EGL_image_external, "samplerExternalOES");3853} else {3854requireExtensions(loc, 1, &E_GL_OES_EGL_image_external_essl3, "samplerExternalOES");3855}3856}3857if (type.getSampler().isYuv()) {3858requireExtensions(loc, 1, &E_GL_EXT_YUV_target, "__samplerExternal2DY2YEXT");3859}38603861if (type.getQualifier().storage == EvqUniform)3862return;38633864if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtSampler)) {3865// For bindless texture, sampler can be declared as an struct member3866if (extensionTurnedOn(E_GL_ARB_bindless_texture)) {3867if (type.getSampler().isImage())3868intermediate.setBindlessImageMode(currentCaller, AstRefTypeVar);3869else3870intermediate.setBindlessTextureMode(currentCaller, AstRefTypeVar);3871}3872else {3873error(loc, "non-uniform struct contains a sampler or image:", type.getBasicTypeString().c_str(), identifier.c_str());3874}3875}3876else if (type.getBasicType() == EbtSampler && type.getQualifier().storage != EvqUniform) {3877// For bindless texture, sampler can be declared as an input/output/block member3878if (extensionTurnedOn(E_GL_ARB_bindless_texture)) {3879if (type.getSampler().isImage())3880intermediate.setBindlessImageMode(currentCaller, AstRefTypeVar);3881else3882intermediate.setBindlessTextureMode(currentCaller, AstRefTypeVar);3883}3884else {3885// non-uniform sampler3886// not yet: okay if it has an initializer3887// if (! initializer)3888if (type.getSampler().isAttachmentEXT() && type.getQualifier().storage != EvqTileImageEXT)3889error(loc, "can only be used in tileImageEXT variables or function parameters:", type.getBasicTypeString().c_str(), identifier.c_str());3890else if (type.getQualifier().storage != EvqTileImageEXT)3891error(loc, "sampler/image types can only be used in uniform variables or function parameters:", type.getBasicTypeString().c_str(), identifier.c_str());3892}3893}3894}38953896void TParseContext::atomicUintCheck(const TSourceLoc& loc, const TType& type, const TString& identifier)3897{3898if (type.getQualifier().storage == EvqUniform)3899return;39003901if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtAtomicUint))3902error(loc, "non-uniform struct contains an atomic_uint:", type.getBasicTypeString().c_str(), identifier.c_str());3903else if (type.getBasicType() == EbtAtomicUint && type.getQualifier().storage != EvqUniform)3904error(loc, "atomic_uints can only be used in uniform variables or function parameters:", type.getBasicTypeString().c_str(), identifier.c_str());3905}39063907void TParseContext::accStructCheck(const TSourceLoc& loc, const TType& type, const TString& identifier)3908{3909if (type.getQualifier().storage == EvqUniform)3910return;39113912if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtAccStruct))3913error(loc, "non-uniform struct contains an accelerationStructureNV:", type.getBasicTypeString().c_str(), identifier.c_str());3914else if (type.getBasicType() == EbtAccStruct && type.getQualifier().storage != EvqUniform)3915error(loc, "accelerationStructureNV can only be used in uniform variables or function parameters:",3916type.getBasicTypeString().c_str(), identifier.c_str());39173918}39193920void TParseContext::transparentOpaqueCheck(const TSourceLoc& loc, const TType& type, const TString& identifier)3921{3922if (parsingBuiltins)3923return;39243925if (type.getQualifier().storage != EvqUniform)3926return;39273928if (type.containsNonOpaque()) {3929// Vulkan doesn't allow transparent uniforms outside of blocks3930if (spvVersion.vulkan > 0 && !spvVersion.vulkanRelaxed)3931vulkanRemoved(loc, "non-opaque uniforms outside a block");3932// OpenGL wants locations on these (unless they are getting automapped)3933if (spvVersion.openGl > 0 && !type.getQualifier().hasLocation() && !intermediate.getAutoMapLocations())3934error(loc, "non-opaque uniform variables need a layout(location=L)", identifier.c_str(), "");3935}3936}39373938//3939// Qualifier checks knowing the qualifier and that it is a member of a struct/block.3940//3941void TParseContext::memberQualifierCheck(glslang::TPublicType& publicType)3942{3943globalQualifierFixCheck(publicType.loc, publicType.qualifier, true);3944checkNoShaderLayouts(publicType.loc, publicType.shaderQualifiers);3945if (publicType.qualifier.isNonUniform()) {3946error(publicType.loc, "not allowed on block or structure members", "nonuniformEXT", "");3947publicType.qualifier.nonUniform = false;3948}3949}39503951//3952// Check/fix just a full qualifier (no variables or types yet, but qualifier is complete) at global level.3953//3954void TParseContext::globalQualifierFixCheck(const TSourceLoc& loc, TQualifier& qualifier, bool isMemberCheck, const TPublicType* publicType)3955{3956bool nonuniformOkay = false;39573958// move from parameter/unknown qualifiers to pipeline in/out qualifiers3959switch (qualifier.storage) {3960case EvqIn:3961profileRequires(loc, ENoProfile, 130, nullptr, "in for stage inputs");3962profileRequires(loc, EEsProfile, 300, nullptr, "in for stage inputs");3963qualifier.storage = EvqVaryingIn;3964nonuniformOkay = true;3965break;3966case EvqOut:3967profileRequires(loc, ENoProfile, 130, nullptr, "out for stage outputs");3968profileRequires(loc, EEsProfile, 300, nullptr, "out for stage outputs");3969qualifier.storage = EvqVaryingOut;3970if (intermediate.isInvariantAll())3971qualifier.invariant = true;3972break;3973case EvqInOut:3974qualifier.storage = EvqVaryingIn;3975error(loc, "cannot use 'inout' at global scope", "", "");3976break;3977case EvqGlobal:3978case EvqTemporary:3979nonuniformOkay = true;3980break;3981case EvqUniform:3982// According to GLSL spec: The std430 qualifier is supported only for shader storage blocks; a shader using3983// the std430 qualifier on a uniform block will fail to compile.3984// Only check the global declaration: layout(std430) uniform;3985if (blockName == nullptr &&3986qualifier.layoutPacking == ElpStd430)3987{3988requireExtensions(loc, 1, &E_GL_EXT_scalar_block_layout, "default std430 layout for uniform");3989}39903991if (publicType != nullptr && publicType->isImage() &&3992(qualifier.layoutFormat > ElfExtSizeGuard && qualifier.layoutFormat < ElfCount))3993qualifier.layoutFormat = mapLegacyLayoutFormat(qualifier.layoutFormat, publicType->sampler.getBasicType());39943995break;3996default:3997break;3998}39994000if (!nonuniformOkay && qualifier.isNonUniform())4001error(loc, "for non-parameter, can only apply to 'in' or no storage qualifier", "nonuniformEXT", "");40024003if (qualifier.isSpirvByReference())4004error(loc, "can only apply to parameter", "spirv_by_reference", "");40054006if (qualifier.isSpirvLiteral())4007error(loc, "can only apply to parameter", "spirv_literal", "");40084009// Storage qualifier isn't ready for memberQualifierCheck, we should skip invariantCheck for it.4010if (!isMemberCheck || structNestingLevel > 0)4011invariantCheck(loc, qualifier);40124013if (qualifier.isFullQuads()) {4014if (qualifier.storage != EvqVaryingIn)4015error(loc, "can only apply to input layout", "full_quads ", "");4016intermediate.setReqFullQuadsMode();4017}40184019if (qualifier.isQuadDeriv()) {4020if (qualifier.storage != EvqVaryingIn)4021error(loc, "can only apply to input layout", "quad_derivatives", "");4022intermediate.setQuadDerivMode();4023}4024}40254026//4027// Check a full qualifier and type (no variable yet) at global level.4028//4029void TParseContext::globalQualifierTypeCheck(const TSourceLoc& loc, const TQualifier& qualifier, const TPublicType& publicType)4030{4031if (! symbolTable.atGlobalLevel())4032return;40334034if (!(publicType.userDef && publicType.userDef->isReference()) && !parsingBuiltins) {4035if (qualifier.isMemoryQualifierImageAndSSBOOnly() && ! publicType.isImage() && publicType.qualifier.storage != EvqBuffer) {4036error(loc, "memory qualifiers cannot be used on this type", "", "");4037} else if (qualifier.isMemory() && (publicType.basicType != EbtSampler) && !publicType.qualifier.isUniformOrBuffer()) {4038error(loc, "memory qualifiers cannot be used on this type", "", "");4039}4040}40414042if (qualifier.storage == EvqBuffer &&4043publicType.basicType != EbtBlock &&4044!qualifier.hasBufferReference())4045error(loc, "buffers can be declared only as blocks", "buffer", "");40464047if (qualifier.storage != EvqVaryingIn && publicType.basicType == EbtDouble &&4048extensionTurnedOn(E_GL_ARB_vertex_attrib_64bit) && language == EShLangVertex &&4049version < 400) {4050profileRequires(loc, ECoreProfile | ECompatibilityProfile, 410, E_GL_ARB_gpu_shader_fp64, "vertex-shader `double` type");4051}4052if (qualifier.storage != EvqVaryingIn && qualifier.storage != EvqVaryingOut)4053return;40544055if (publicType.shaderQualifiers.hasBlendEquation())4056error(loc, "can only be applied to a standalone 'out'", "blend equation", "");40574058// now, knowing it is a shader in/out, do all the in/out semantic checks40594060if (publicType.basicType == EbtBool && !parsingBuiltins) {4061error(loc, "cannot be bool", GetStorageQualifierString(qualifier.storage), "");4062return;4063}40644065if (isTypeInt(publicType.basicType) || publicType.basicType == EbtDouble) {4066profileRequires(loc, EEsProfile, 300, nullptr, "non-float shader input/output");4067profileRequires(loc, ~EEsProfile, 130, nullptr, "non-float shader input/output");4068}40694070if (!qualifier.flat && !qualifier.isExplicitInterpolation() && !qualifier.isPervertexNV() && !qualifier.isPervertexEXT()) {4071if (isTypeInt(publicType.basicType) ||4072publicType.basicType == EbtDouble ||4073(publicType.userDef && ( publicType.userDef->containsBasicType(EbtInt)4074|| publicType.userDef->containsBasicType(EbtUint)4075|| publicType.userDef->contains16BitInt()4076|| publicType.userDef->contains8BitInt()4077|| publicType.userDef->contains64BitInt()4078|| publicType.userDef->containsDouble()))) {4079if (qualifier.storage == EvqVaryingIn && language == EShLangFragment)4080error(loc, "must be qualified as flat", TType::getBasicString(publicType.basicType), GetStorageQualifierString(qualifier.storage));4081else if (qualifier.storage == EvqVaryingOut && language == EShLangVertex && version == 300)4082error(loc, "must be qualified as flat", TType::getBasicString(publicType.basicType), GetStorageQualifierString(qualifier.storage));4083}4084}40854086if (qualifier.isPatch() && qualifier.isInterpolation())4087error(loc, "cannot use interpolation qualifiers with patch", "patch", "");40884089if (qualifier.isTaskPayload() && publicType.basicType == EbtBlock)4090error(loc, "taskPayloadSharedEXT variables should not be declared as interface blocks", "taskPayloadSharedEXT", "");40914092if (qualifier.isTaskMemory() && publicType.basicType != EbtBlock)4093error(loc, "taskNV variables can be declared only as blocks", "taskNV", "");40944095if (qualifier.storage == EvqVaryingIn) {4096switch (language) {4097case EShLangVertex:4098if (publicType.basicType == EbtStruct) {4099error(loc, "cannot be a structure", GetStorageQualifierString(qualifier.storage), "");4100return;4101}4102if (publicType.arraySizes) {4103requireProfile(loc, ~EEsProfile, "vertex input arrays");4104profileRequires(loc, ENoProfile, 150, nullptr, "vertex input arrays");4105}4106if (publicType.basicType == EbtDouble)4107profileRequires(loc, ~EEsProfile, 410, E_GL_ARB_vertex_attrib_64bit, "vertex-shader `double` type input");4108if (qualifier.isAuxiliary() || qualifier.isInterpolation() || qualifier.isMemory() || qualifier.invariant)4109error(loc, "vertex input cannot be further qualified", "", "");4110break;4111case EShLangFragment:4112if (publicType.userDef) {4113profileRequires(loc, EEsProfile, 300, nullptr, "fragment-shader struct input");4114profileRequires(loc, ~EEsProfile, 150, nullptr, "fragment-shader struct input");4115if (publicType.userDef->containsStructure())4116requireProfile(loc, ~EEsProfile, "fragment-shader struct input containing structure");4117if (publicType.userDef->containsArray())4118requireProfile(loc, ~EEsProfile, "fragment-shader struct input containing an array");4119}4120break;4121case EShLangCompute:4122if (! symbolTable.atBuiltInLevel())4123error(loc, "global storage input qualifier cannot be used in a compute shader", "in", "");4124break;4125case EShLangTessControl:4126if (qualifier.patch)4127error(loc, "can only use on output in tessellation-control shader", "patch", "");4128break;4129default:4130break;4131}4132} else {4133// qualifier.storage == EvqVaryingOut4134switch (language) {4135case EShLangVertex:4136if (publicType.userDef) {4137profileRequires(loc, EEsProfile, 300, nullptr, "vertex-shader struct output");4138profileRequires(loc, ~EEsProfile, 150, nullptr, "vertex-shader struct output");4139if (publicType.userDef->containsStructure())4140requireProfile(loc, ~EEsProfile, "vertex-shader struct output containing structure");4141if (publicType.userDef->containsArray())4142requireProfile(loc, ~EEsProfile, "vertex-shader struct output containing an array");4143}41444145break;4146case EShLangFragment:4147profileRequires(loc, EEsProfile, 300, nullptr, "fragment shader output");4148if (publicType.basicType == EbtStruct) {4149error(loc, "cannot be a structure", GetStorageQualifierString(qualifier.storage), "");4150return;4151}4152if (publicType.matrixRows > 0) {4153error(loc, "cannot be a matrix", GetStorageQualifierString(qualifier.storage), "");4154return;4155}4156if (qualifier.isAuxiliary())4157error(loc, "can't use auxiliary qualifier on a fragment output", "centroid/sample/patch", "");4158if (qualifier.isInterpolation())4159error(loc, "can't use interpolation qualifier on a fragment output", "flat/smooth/noperspective", "");4160if (publicType.basicType == EbtDouble || publicType.basicType == EbtInt64 || publicType.basicType == EbtUint64)4161error(loc, "cannot contain a double, int64, or uint64", GetStorageQualifierString(qualifier.storage), "");4162break;41634164case EShLangCompute:4165error(loc, "global storage output qualifier cannot be used in a compute shader", "out", "");4166break;4167case EShLangTessEvaluation:4168if (qualifier.patch)4169error(loc, "can only use on input in tessellation-evaluation shader", "patch", "");4170break;4171default:4172break;4173}4174}4175}41764177//4178// Merge characteristics of the 'src' qualifier into the 'dst'.4179// If there is duplication, issue error messages, unless 'force'4180// is specified, which means to just override default settings.4181//4182// Also, when force is false, it will be assumed that 'src' follows4183// 'dst', for the purpose of error checking order for versions4184// that require specific orderings of qualifiers.4185//4186void TParseContext::mergeQualifiers(const TSourceLoc& loc, TQualifier& dst, const TQualifier& src, bool force)4187{4188// Multiple auxiliary qualifiers (mostly done later by 'individual qualifiers')4189if (src.isAuxiliary() && dst.isAuxiliary())4190error(loc, "can only have one auxiliary qualifier (centroid, patch, and sample)", "", "");41914192// Multiple interpolation qualifiers (mostly done later by 'individual qualifiers')4193if (src.isInterpolation() && dst.isInterpolation())4194error(loc, "can only have one interpolation qualifier (flat, smooth, noperspective, __explicitInterpAMD)", "", "");41954196// Ordering4197if (! force && ((!isEsProfile() && version < 420) ||4198(isEsProfile() && version < 310))4199&& ! extensionTurnedOn(E_GL_ARB_shading_language_420pack)) {4200// non-function parameters4201if (src.isNoContraction() && (dst.invariant || dst.isInterpolation() || dst.isAuxiliary() || dst.storage != EvqTemporary || dst.precision != EpqNone))4202error(loc, "precise qualifier must appear first", "", "");4203if (src.invariant && (dst.isInterpolation() || dst.isAuxiliary() || dst.storage != EvqTemporary || dst.precision != EpqNone))4204error(loc, "invariant qualifier must appear before interpolation, storage, and precision qualifiers ", "", "");4205else if (src.isInterpolation() && (dst.isAuxiliary() || dst.storage != EvqTemporary || dst.precision != EpqNone))4206error(loc, "interpolation qualifiers must appear before storage and precision qualifiers", "", "");4207else if (src.isAuxiliary() && (dst.storage != EvqTemporary || dst.precision != EpqNone))4208error(loc, "Auxiliary qualifiers (centroid, patch, and sample) must appear before storage and precision qualifiers", "", "");4209else if (src.storage != EvqTemporary && (dst.precision != EpqNone))4210error(loc, "precision qualifier must appear as last qualifier", "", "");42114212// function parameters4213if (src.isNoContraction() && (dst.storage == EvqConst || dst.storage == EvqIn || dst.storage == EvqOut))4214error(loc, "precise qualifier must appear first", "", "");4215if (src.storage == EvqConst && (dst.storage == EvqIn || dst.storage == EvqOut))4216error(loc, "in/out must appear before const", "", "");4217}42184219// Storage qualification4220if (dst.storage == EvqTemporary || dst.storage == EvqGlobal)4221dst.storage = src.storage;4222else if ((dst.storage == EvqIn && src.storage == EvqOut) ||4223(dst.storage == EvqOut && src.storage == EvqIn))4224dst.storage = EvqInOut;4225else if ((dst.storage == EvqIn && src.storage == EvqConst) ||4226(dst.storage == EvqConst && src.storage == EvqIn))4227dst.storage = EvqConstReadOnly;4228else if (src.storage != EvqTemporary &&4229src.storage != EvqGlobal)4230error(loc, "too many storage qualifiers", GetStorageQualifierString(src.storage), "");42314232// Precision qualifiers4233if (! force && src.precision != EpqNone && dst.precision != EpqNone)4234error(loc, "only one precision qualifier allowed", GetPrecisionQualifierString(src.precision), "");4235if (dst.precision == EpqNone || (force && src.precision != EpqNone))4236dst.precision = src.precision;42374238if (!force && ((src.coherent && (dst.devicecoherent || dst.queuefamilycoherent || dst.workgroupcoherent || dst.subgroupcoherent || dst.shadercallcoherent)) ||4239(src.devicecoherent && (dst.coherent || dst.queuefamilycoherent || dst.workgroupcoherent || dst.subgroupcoherent || dst.shadercallcoherent)) ||4240(src.queuefamilycoherent && (dst.coherent || dst.devicecoherent || dst.workgroupcoherent || dst.subgroupcoherent || dst.shadercallcoherent)) ||4241(src.workgroupcoherent && (dst.coherent || dst.devicecoherent || dst.queuefamilycoherent || dst.subgroupcoherent || dst.shadercallcoherent)) ||4242(src.subgroupcoherent && (dst.coherent || dst.devicecoherent || dst.queuefamilycoherent || dst.workgroupcoherent || dst.shadercallcoherent)) ||4243(src.shadercallcoherent && (dst.coherent || dst.devicecoherent || dst.queuefamilycoherent || dst.workgroupcoherent || dst.subgroupcoherent)))) {4244error(loc, "only one coherent/devicecoherent/queuefamilycoherent/workgroupcoherent/subgroupcoherent/shadercallcoherent qualifier allowed",4245GetPrecisionQualifierString(src.precision), "");4246}42474248// Layout qualifiers4249mergeObjectLayoutQualifiers(dst, src, false);42504251// individual qualifiers4252bool repeated = false;4253#define MERGE_SINGLETON(field) repeated |= dst.field && src.field; dst.field |= src.field;4254MERGE_SINGLETON(invariant);4255MERGE_SINGLETON(centroid);4256MERGE_SINGLETON(smooth);4257MERGE_SINGLETON(flat);4258MERGE_SINGLETON(specConstant);4259MERGE_SINGLETON(noContraction);4260MERGE_SINGLETON(nopersp);4261MERGE_SINGLETON(explicitInterp);4262MERGE_SINGLETON(perPrimitiveNV);4263MERGE_SINGLETON(perViewNV);4264MERGE_SINGLETON(perTaskNV);4265MERGE_SINGLETON(patch);4266MERGE_SINGLETON(sample);4267MERGE_SINGLETON(coherent);4268MERGE_SINGLETON(devicecoherent);4269MERGE_SINGLETON(queuefamilycoherent);4270MERGE_SINGLETON(workgroupcoherent);4271MERGE_SINGLETON(subgroupcoherent);4272MERGE_SINGLETON(shadercallcoherent);4273MERGE_SINGLETON(nonprivate);4274MERGE_SINGLETON(volatil);4275MERGE_SINGLETON(restrict);4276MERGE_SINGLETON(readonly);4277MERGE_SINGLETON(writeonly);4278MERGE_SINGLETON(nonUniform);42794280// SPIR-V storage class qualifier (GL_EXT_spirv_intrinsics)4281dst.spirvStorageClass = src.spirvStorageClass;42824283// SPIR-V decorate qualifiers (GL_EXT_spirv_intrinsics)4284if (src.hasSpirvDecorate()) {4285if (dst.hasSpirvDecorate()) {4286const TSpirvDecorate& srcSpirvDecorate = src.getSpirvDecorate();4287TSpirvDecorate& dstSpirvDecorate = dst.getSpirvDecorate();4288for (auto& decorate : srcSpirvDecorate.decorates) {4289if (dstSpirvDecorate.decorates.find(decorate.first) != dstSpirvDecorate.decorates.end())4290error(loc, "too many SPIR-V decorate qualifiers", "spirv_decorate", "(decoration=%u)", decorate.first);4291else4292dstSpirvDecorate.decorates.insert(decorate);4293}42944295for (auto& decorateId : srcSpirvDecorate.decorateIds) {4296if (dstSpirvDecorate.decorateIds.find(decorateId.first) != dstSpirvDecorate.decorateIds.end())4297error(loc, "too many SPIR-V decorate qualifiers", "spirv_decorate_id", "(decoration=%u)", decorateId.first);4298else4299dstSpirvDecorate.decorateIds.insert(decorateId);4300}43014302for (auto& decorateString : srcSpirvDecorate.decorateStrings) {4303if (dstSpirvDecorate.decorates.find(decorateString.first) != dstSpirvDecorate.decorates.end())4304error(loc, "too many SPIR-V decorate qualifiers", "spirv_decorate_string", "(decoration=%u)", decorateString.first);4305else4306dstSpirvDecorate.decorateStrings.insert(decorateString);4307}4308} else {4309dst.spirvDecorate = src.spirvDecorate;4310}4311}43124313if (repeated)4314error(loc, "replicated qualifiers", "", "");4315}43164317void TParseContext::setDefaultPrecision(const TSourceLoc& loc, TPublicType& publicType, TPrecisionQualifier qualifier)4318{4319TBasicType basicType = publicType.basicType;43204321if (basicType == EbtSampler) {4322defaultSamplerPrecision[computeSamplerTypeIndex(publicType.sampler)] = qualifier;43234324return; // all is well4325}43264327if (basicType == EbtInt || basicType == EbtFloat) {4328if (publicType.isScalar()) {4329defaultPrecision[basicType] = qualifier;4330if (basicType == EbtInt) {4331defaultPrecision[EbtUint] = qualifier;4332precisionManager.explicitIntDefaultSeen();4333} else4334precisionManager.explicitFloatDefaultSeen();43354336return; // all is well4337}4338}43394340if (basicType == EbtAtomicUint) {4341if (qualifier != EpqHigh)4342error(loc, "can only apply highp to atomic_uint", "precision", "");43434344return;4345}43464347error(loc, "cannot apply precision statement to this type; use 'float', 'int' or a sampler type", TType::getBasicString(basicType), "");4348}43494350// used to flatten the sampler type space into a single dimension4351// correlates with the declaration of defaultSamplerPrecision[]4352int TParseContext::computeSamplerTypeIndex(TSampler& sampler)4353{4354int arrayIndex = sampler.arrayed ? 1 : 0;4355int shadowIndex = sampler.shadow ? 1 : 0;4356int externalIndex = sampler.isExternal() ? 1 : 0;4357int imageIndex = sampler.isImageClass() ? 1 : 0;4358int msIndex = sampler.isMultiSample() ? 1 : 0;43594360int flattened = EsdNumDims * (EbtNumTypes * (2 * (2 * (2 * (2 * arrayIndex + msIndex) + imageIndex) + shadowIndex) +4361externalIndex) + sampler.type) + sampler.dim;4362assert(flattened < maxSamplerIndex);43634364return flattened;4365}43664367TPrecisionQualifier TParseContext::getDefaultPrecision(TPublicType& publicType)4368{4369if (publicType.basicType == EbtSampler)4370return defaultSamplerPrecision[computeSamplerTypeIndex(publicType.sampler)];4371else4372return defaultPrecision[publicType.basicType];4373}43744375void TParseContext::precisionQualifierCheck(const TSourceLoc& loc, TBasicType baseType, TQualifier& qualifier, bool isCoopMat)4376{4377// Built-in symbols are allowed some ambiguous precisions, to be pinned down4378// later by context.4379if (! obeyPrecisionQualifiers() || parsingBuiltins)4380return;43814382if (baseType == EbtAtomicUint && qualifier.precision != EpqNone && qualifier.precision != EpqHigh)4383error(loc, "atomic counters can only be highp", "atomic_uint", "");43844385if (isCoopMat)4386return;43874388if (baseType == EbtFloat || baseType == EbtUint || baseType == EbtInt || baseType == EbtSampler || baseType == EbtAtomicUint) {4389if (qualifier.precision == EpqNone) {4390if (relaxedErrors())4391warn(loc, "type requires declaration of default precision qualifier", TType::getBasicString(baseType), "substituting 'mediump'");4392else4393error(loc, "type requires declaration of default precision qualifier", TType::getBasicString(baseType), "");4394qualifier.precision = EpqMedium;4395defaultPrecision[baseType] = EpqMedium;4396}4397} else if (qualifier.precision != EpqNone)4398error(loc, "type cannot have precision qualifier", TType::getBasicString(baseType), "");4399}44004401void TParseContext::parameterTypeCheck(const TSourceLoc& loc, TStorageQualifier qualifier, const TType& type)4402{4403if ((qualifier == EvqOut || qualifier == EvqInOut) && type.isOpaque() && !intermediate.getBindlessMode())4404error(loc, "samplers and atomic_uints cannot be output parameters", type.getBasicTypeString().c_str(), "");4405if (!parsingBuiltins && type.contains16BitFloat())4406requireFloat16Arithmetic(loc, type.getBasicTypeString().c_str(), "float16 types can only be in uniform block or buffer storage");4407if (!parsingBuiltins && type.contains16BitInt())4408requireInt16Arithmetic(loc, type.getBasicTypeString().c_str(), "(u)int16 types can only be in uniform block or buffer storage");4409if (!parsingBuiltins && type.contains8BitInt())4410requireInt8Arithmetic(loc, type.getBasicTypeString().c_str(), "(u)int8 types can only be in uniform block or buffer storage");4411}44124413bool TParseContext::containsFieldWithBasicType(const TType& type, TBasicType basicType)4414{4415if (type.getBasicType() == basicType)4416return true;44174418if (type.getBasicType() == EbtStruct) {4419const TTypeList& structure = *type.getStruct();4420for (unsigned int i = 0; i < structure.size(); ++i) {4421if (containsFieldWithBasicType(*structure[i].type, basicType))4422return true;4423}4424}44254426return false;4427}44284429//4430// Do size checking for an array type's size.4431//4432void TParseContext::arraySizeCheck(const TSourceLoc& loc, TIntermTyped* expr, TArraySize& sizePair,4433const char* sizeType, const bool allowZero)4434{4435bool isConst = false;4436sizePair.node = nullptr;44374438int size = 1;44394440TIntermConstantUnion* constant = expr->getAsConstantUnion();4441if (constant) {4442// handle true (non-specialization) constant4443size = constant->getConstArray()[0].getIConst();4444isConst = true;4445} else {4446// see if it's a specialization constant instead4447if (expr->getQualifier().isSpecConstant()) {4448isConst = true;4449sizePair.node = expr;4450TIntermSymbol* symbol = expr->getAsSymbolNode();4451if (symbol && symbol->getConstArray().size() > 0)4452size = symbol->getConstArray()[0].getIConst();4453} else if (expr->getAsUnaryNode() && expr->getAsUnaryNode()->getOp() == glslang::EOpArrayLength &&4454expr->getAsUnaryNode()->getOperand()->getType().isCoopMatNV()) {4455isConst = true;4456size = 1;4457sizePair.node = expr->getAsUnaryNode();4458}4459}44604461sizePair.size = size;44624463if (! isConst || (expr->getBasicType() != EbtInt && expr->getBasicType() != EbtUint)) {4464error(loc, sizeType, "", "must be a constant integer expression");4465return;4466}44674468if (allowZero) {4469if (size < 0) {4470error(loc, sizeType, "", "must be a non-negative integer");4471return;4472}4473} else {4474if (size <= 0) {4475error(loc, sizeType, "", "must be a positive integer");4476return;4477}4478}4479}44804481//4482// See if this qualifier can be an array.4483//4484// Returns true if there is an error.4485//4486bool TParseContext::arrayQualifierError(const TSourceLoc& loc, const TQualifier& qualifier)4487{4488if (qualifier.storage == EvqConst) {4489profileRequires(loc, ENoProfile, 120, E_GL_3DL_array_objects, "const array");4490profileRequires(loc, EEsProfile, 300, nullptr, "const array");4491}44924493if (qualifier.storage == EvqVaryingIn && language == EShLangVertex) {4494requireProfile(loc, ~EEsProfile, "vertex input arrays");4495profileRequires(loc, ENoProfile, 150, nullptr, "vertex input arrays");4496}44974498return false;4499}45004501//4502// See if this qualifier and type combination can be an array.4503// Assumes arrayQualifierError() was also called to catch the type-invariant tests.4504//4505// Returns true if there is an error.4506//4507bool TParseContext::arrayError(const TSourceLoc& loc, const TType& type)4508{4509if (type.getQualifier().storage == EvqVaryingOut && language == EShLangVertex) {4510if (type.isArrayOfArrays())4511requireProfile(loc, ~EEsProfile, "vertex-shader array-of-array output");4512else if (type.isStruct())4513requireProfile(loc, ~EEsProfile, "vertex-shader array-of-struct output");4514}4515if (type.getQualifier().storage == EvqVaryingIn && language == EShLangFragment) {4516if (type.isArrayOfArrays())4517requireProfile(loc, ~EEsProfile, "fragment-shader array-of-array input");4518else if (type.isStruct())4519requireProfile(loc, ~EEsProfile, "fragment-shader array-of-struct input");4520}4521if (type.getQualifier().storage == EvqVaryingOut && language == EShLangFragment) {4522if (type.isArrayOfArrays())4523requireProfile(loc, ~EEsProfile, "fragment-shader array-of-array output");4524}45254526return false;4527}45284529//4530// Require array to be completely sized4531//4532void TParseContext::arraySizeRequiredCheck(const TSourceLoc& loc, const TArraySizes& arraySizes)4533{4534if (!parsingBuiltins && arraySizes.hasUnsized())4535error(loc, "array size required", "", "");4536}45374538void TParseContext::structArrayCheck(const TSourceLoc& /*loc*/, const TType& type)4539{4540const TTypeList& structure = *type.getStruct();4541for (int m = 0; m < (int)structure.size(); ++m) {4542const TType& member = *structure[m].type;4543if (member.isArray())4544arraySizeRequiredCheck(structure[m].loc, *member.getArraySizes());4545}4546}45474548void TParseContext::arraySizesCheck(const TSourceLoc& loc, const TQualifier& qualifier, TArraySizes* arraySizes,4549const TIntermTyped* initializer, bool lastMember)4550{4551assert(arraySizes);45524553// always allow special built-in ins/outs sized to topologies4554if (parsingBuiltins)4555return;45564557// initializer must be a sized array, in which case4558// allow the initializer to set any unknown array sizes4559if (initializer != nullptr) {4560if (initializer->getType().isUnsizedArray())4561error(loc, "array initializer must be sized", "[]", "");4562return;4563}45644565// No environment allows any non-outer-dimension to be implicitly sized4566if (arraySizes->isInnerUnsized()) {4567error(loc, "only outermost dimension of an array of arrays can be implicitly sized", "[]", "");4568arraySizes->clearInnerUnsized();4569}45704571if (arraySizes->isInnerSpecialization() &&4572(qualifier.storage != EvqTemporary && qualifier.storage != EvqGlobal && qualifier.storage != EvqShared && qualifier.storage != EvqConst))4573error(loc, "only outermost dimension of an array of arrays can be a specialization constant", "[]", "");45744575// desktop always allows outer-dimension-unsized variable arrays,4576if (!isEsProfile())4577return;45784579// for ES, if size isn't coming from an initializer, it has to be explicitly declared now,4580// with very few exceptions45814582// implicitly-sized io exceptions:4583switch (language) {4584case EShLangGeometry:4585if (qualifier.storage == EvqVaryingIn)4586if ((isEsProfile() && version >= 320) ||4587extensionsTurnedOn(Num_AEP_geometry_shader, AEP_geometry_shader))4588return;4589break;4590case EShLangTessControl:4591if ( qualifier.storage == EvqVaryingIn ||4592(qualifier.storage == EvqVaryingOut && ! qualifier.isPatch()))4593if ((isEsProfile() && version >= 320) ||4594extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader))4595return;4596break;4597case EShLangTessEvaluation:4598if ((qualifier.storage == EvqVaryingIn && ! qualifier.isPatch()) ||4599qualifier.storage == EvqVaryingOut)4600if ((isEsProfile() && version >= 320) ||4601extensionsTurnedOn(Num_AEP_tessellation_shader, AEP_tessellation_shader))4602return;4603break;4604case EShLangMesh:4605if (qualifier.storage == EvqVaryingOut)4606if ((isEsProfile() && version >= 320) ||4607extensionsTurnedOn(Num_AEP_mesh_shader, AEP_mesh_shader))4608return;4609break;4610default:4611break;4612}46134614// last member of ssbo block exception:4615if (qualifier.storage == EvqBuffer && lastMember)4616return;46174618arraySizeRequiredCheck(loc, *arraySizes);4619}46204621void TParseContext::arrayOfArrayVersionCheck(const TSourceLoc& loc, const TArraySizes* sizes)4622{4623if (sizes == nullptr || sizes->getNumDims() == 1)4624return;46254626const char* feature = "arrays of arrays";46274628requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, feature);4629profileRequires(loc, EEsProfile, 310, nullptr, feature);4630profileRequires(loc, ECoreProfile | ECompatibilityProfile, 430, nullptr, feature);4631}46324633//4634// Do all the semantic checking for declaring or redeclaring an array, with and4635// without a size, and make the right changes to the symbol table.4636//4637void TParseContext::declareArray(const TSourceLoc& loc, const TString& identifier, const TType& type, TSymbol*& symbol)4638{4639if (symbol == nullptr) {4640bool currentScope;4641symbol = symbolTable.find(identifier, nullptr, ¤tScope);46424643if (symbol && builtInName(identifier) && ! symbolTable.atBuiltInLevel()) {4644// bad shader (errors already reported) trying to redeclare a built-in name as an array4645symbol = nullptr;4646return;4647}4648if (symbol == nullptr || ! currentScope) {4649//4650// Successfully process a new definition.4651// (Redeclarations have to take place at the same scope; otherwise they are hiding declarations)4652//4653symbol = new TVariable(&identifier, type);4654symbolTable.insert(*symbol);4655if (symbolTable.atGlobalLevel())4656trackLinkage(*symbol);46574658if (! symbolTable.atBuiltInLevel()) {4659if (isIoResizeArray(type)) {4660ioArraySymbolResizeList.push_back(symbol);4661checkIoArraysConsistency(loc, true);4662} else4663fixIoArraySize(loc, symbol->getWritableType());4664}46654666return;4667}4668if (symbol->getAsAnonMember()) {4669error(loc, "cannot redeclare a user-block member array", identifier.c_str(), "");4670symbol = nullptr;4671return;4672}4673}46744675//4676// Process a redeclaration.4677//46784679if (symbol == nullptr) {4680error(loc, "array variable name expected", identifier.c_str(), "");4681return;4682}46834684// redeclareBuiltinVariable() should have already done the copyUp()4685TType& existingType = symbol->getWritableType();46864687if (! existingType.isArray()) {4688error(loc, "redeclaring non-array as array", identifier.c_str(), "");4689return;4690}46914692if (! existingType.sameElementType(type)) {4693error(loc, "redeclaration of array with a different element type", identifier.c_str(), "");4694return;4695}46964697if (! existingType.sameInnerArrayness(type)) {4698error(loc, "redeclaration of array with a different array dimensions or sizes", identifier.c_str(), "");4699return;4700}47014702if (existingType.isSizedArray()) {4703// be more leniant for input arrays to geometry shaders and tessellation control outputs, where the redeclaration is the same size4704if (! (isIoResizeArray(type) && existingType.getOuterArraySize() == type.getOuterArraySize()))4705error(loc, "redeclaration of array with size", identifier.c_str(), "");4706return;4707}47084709arrayLimitCheck(loc, identifier, type.getOuterArraySize());47104711existingType.updateArraySizes(type);47124713if (isIoResizeArray(type))4714checkIoArraysConsistency(loc);4715}47164717// Policy and error check for needing a runtime sized array.4718void TParseContext::checkRuntimeSizable(const TSourceLoc& loc, const TIntermTyped& base)4719{4720// runtime length implies runtime sizeable, so no problem4721if (isRuntimeLength(base))4722return;47234724if (base.getType().getQualifier().builtIn == EbvSampleMask)4725return;47264727// Check for last member of a bufferreference type, which is runtime sizeable4728// but doesn't support runtime length4729if (base.getType().getQualifier().storage == EvqBuffer) {4730const TIntermBinary* binary = base.getAsBinaryNode();4731if (binary != nullptr &&4732binary->getOp() == EOpIndexDirectStruct &&4733binary->getLeft()->isReference()) {47344735const int index = binary->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();4736const int memberCount = (int)binary->getLeft()->getType().getReferentType()->getStruct()->size();4737if (index == memberCount - 1)4738return;4739}4740}47414742// check for additional things allowed by GL_EXT_nonuniform_qualifier4743if (base.getBasicType() == EbtSampler || base.getBasicType() == EbtAccStruct || base.getBasicType() == EbtRayQuery ||4744base.getBasicType() == EbtHitObjectNV || (base.getBasicType() == EbtBlock && base.getType().getQualifier().isUniformOrBuffer()))4745requireExtensions(loc, 1, &E_GL_EXT_nonuniform_qualifier, "variable index");4746else4747error(loc, "", "[", "array must be redeclared with a size before being indexed with a variable");4748}47494750// Policy decision for whether a run-time .length() is allowed.4751bool TParseContext::isRuntimeLength(const TIntermTyped& base) const4752{4753if (base.getType().getQualifier().storage == EvqBuffer) {4754// in a buffer block4755const TIntermBinary* binary = base.getAsBinaryNode();4756if (binary != nullptr && binary->getOp() == EOpIndexDirectStruct) {4757// is it the last member?4758const int index = binary->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst();47594760if (binary->getLeft()->isReference())4761return false;47624763const int memberCount = (int)binary->getLeft()->getType().getStruct()->size();4764if (index == memberCount - 1)4765return true;4766}4767}47684769return false;4770}47714772// Check if mesh perviewNV attributes have a view dimension4773// and resize it to gl_MaxMeshViewCountNV when implicitly sized.4774void TParseContext::checkAndResizeMeshViewDim(const TSourceLoc& loc, TType& type, bool isBlockMember)4775{4776// see if member is a per-view attribute4777if (!type.getQualifier().isPerView())4778return;47794780if ((isBlockMember && type.isArray()) || (!isBlockMember && type.isArrayOfArrays())) {4781// since we don't have the maxMeshViewCountNV set during parsing builtins, we hardcode the value.4782int maxViewCount = parsingBuiltins ? 4 : resources.maxMeshViewCountNV;4783// For block members, outermost array dimension is the view dimension.4784// For non-block members, outermost array dimension is the vertex/primitive dimension4785// and 2nd outermost is the view dimension.4786int viewDim = isBlockMember ? 0 : 1;4787int viewDimSize = type.getArraySizes()->getDimSize(viewDim);47884789if (viewDimSize != UnsizedArraySize && viewDimSize != maxViewCount)4790error(loc, "mesh view output array size must be gl_MaxMeshViewCountNV or implicitly sized", "[]", "");4791else if (viewDimSize == UnsizedArraySize)4792type.getArraySizes()->setDimSize(viewDim, maxViewCount);4793}4794else {4795error(loc, "requires a view array dimension", "perviewNV", "");4796}4797}47984799// Returns true if the first argument to the #line directive is the line number for the next line.4800//4801// Desktop, pre-version 3.30: "After processing this directive4802// (including its new-line), the implementation will behave as if it is compiling at line number line+1 and4803// source string number source-string-number."4804//4805// Desktop, version 3.30 and later, and ES: "After processing this directive4806// (including its new-line), the implementation will behave as if it is compiling at line number line and4807// source string number source-string-number.4808bool TParseContext::lineDirectiveShouldSetNextLine() const4809{4810return isEsProfile() || version >= 330;4811}48124813//4814// Enforce non-initializer type/qualifier rules.4815//4816void TParseContext::nonInitConstCheck(const TSourceLoc& loc, TString& identifier, TType& type)4817{4818//4819// Make the qualifier make sense, given that there is not an initializer.4820//4821if (type.getQualifier().storage == EvqConst ||4822type.getQualifier().storage == EvqConstReadOnly) {4823type.getQualifier().makeTemporary();4824error(loc, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");4825}4826}48274828//4829// See if the identifier is a built-in symbol that can be redeclared, and if so,4830// copy the symbol table's read-only built-in variable to the current4831// global level, where it can be modified based on the passed in type.4832//4833// Returns nullptr if no redeclaration took place; meaning a normal declaration still4834// needs to occur for it, not necessarily an error.4835//4836// Returns a redeclared and type-modified variable if a redeclarated occurred.4837//4838TSymbol* TParseContext::redeclareBuiltinVariable(const TSourceLoc& loc, const TString& identifier,4839const TQualifier& qualifier, const TShaderQualifiers& publicType)4840{4841if (! builtInName(identifier) || symbolTable.atBuiltInLevel() || ! symbolTable.atGlobalLevel())4842return nullptr;48434844bool nonEsRedecls = (!isEsProfile() && (version >= 130 || identifier == "gl_TexCoord"));4845bool esRedecls = (isEsProfile() &&4846(version >= 320 || extensionsTurnedOn(Num_AEP_shader_io_blocks, AEP_shader_io_blocks)));4847if (! esRedecls && ! nonEsRedecls)4848return nullptr;48494850// Special case when using GL_ARB_separate_shader_objects4851bool ssoPre150 = false; // means the only reason this variable is redeclared is due to this combination4852if (!isEsProfile() && version <= 140 && extensionTurnedOn(E_GL_ARB_separate_shader_objects)) {4853if (identifier == "gl_Position" ||4854identifier == "gl_PointSize" ||4855identifier == "gl_ClipVertex" ||4856identifier == "gl_FogFragCoord")4857ssoPre150 = true;4858}48594860// Potentially redeclaring a built-in variable...48614862if (ssoPre150 ||4863(identifier == "gl_FragDepth" && ((nonEsRedecls && version >= 420) || esRedecls)) ||4864(identifier == "gl_FragCoord" && ((nonEsRedecls && version >= 140) || esRedecls)) ||4865identifier == "gl_ClipDistance" ||4866identifier == "gl_CullDistance" ||4867identifier == "gl_ShadingRateEXT" ||4868identifier == "gl_PrimitiveShadingRateEXT" ||4869identifier == "gl_FrontColor" ||4870identifier == "gl_BackColor" ||4871identifier == "gl_FrontSecondaryColor" ||4872identifier == "gl_BackSecondaryColor" ||4873identifier == "gl_SecondaryColor" ||4874(identifier == "gl_Color" && language == EShLangFragment) ||4875(identifier == "gl_FragStencilRefARB" && (nonEsRedecls && version >= 140)4876&& language == EShLangFragment) ||4877identifier == "gl_SampleMask" ||4878identifier == "gl_Layer" ||4879identifier == "gl_PrimitiveIndicesNV" ||4880identifier == "gl_PrimitivePointIndicesEXT" ||4881identifier == "gl_PrimitiveLineIndicesEXT" ||4882identifier == "gl_PrimitiveTriangleIndicesEXT" ||4883identifier == "gl_TexCoord") {48844885// Find the existing symbol, if any.4886bool builtIn;4887TSymbol* symbol = symbolTable.find(identifier, &builtIn);48884889// If the symbol was not found, this must be a version/profile/stage4890// that doesn't have it.4891if (! symbol)4892return nullptr;48934894// If it wasn't at a built-in level, then it's already been redeclared;4895// that is, this is a redeclaration of a redeclaration; reuse that initial4896// redeclaration. Otherwise, make the new one.4897if (builtIn) {4898makeEditable(symbol);4899symbolTable.amendSymbolIdLevel(*symbol);4900}49014902// Now, modify the type of the copy, as per the type of the current redeclaration.49034904TQualifier& symbolQualifier = symbol->getWritableType().getQualifier();4905if (ssoPre150) {4906if (intermediate.inIoAccessed(identifier))4907error(loc, "cannot redeclare after use", identifier.c_str(), "");4908if (qualifier.hasLayout())4909error(loc, "cannot apply layout qualifier to", "redeclaration", symbol->getName().c_str());4910if (qualifier.isMemory() || qualifier.isAuxiliary() || (language == EShLangVertex && qualifier.storage != EvqVaryingOut) ||4911(language == EShLangFragment && qualifier.storage != EvqVaryingIn))4912error(loc, "cannot change storage, memory, or auxiliary qualification of", "redeclaration", symbol->getName().c_str());4913if (! qualifier.smooth)4914error(loc, "cannot change interpolation qualification of", "redeclaration", symbol->getName().c_str());4915} else if (identifier == "gl_FrontColor" ||4916identifier == "gl_BackColor" ||4917identifier == "gl_FrontSecondaryColor" ||4918identifier == "gl_BackSecondaryColor" ||4919identifier == "gl_SecondaryColor" ||4920identifier == "gl_Color") {4921symbolQualifier.flat = qualifier.flat;4922symbolQualifier.smooth = qualifier.smooth;4923symbolQualifier.nopersp = qualifier.nopersp;4924if (qualifier.hasLayout())4925error(loc, "cannot apply layout qualifier to", "redeclaration", symbol->getName().c_str());4926if (qualifier.isMemory() || qualifier.isAuxiliary() || symbol->getType().getQualifier().storage != qualifier.storage)4927error(loc, "cannot change storage, memory, or auxiliary qualification of", "redeclaration", symbol->getName().c_str());4928} else if (identifier == "gl_TexCoord" ||4929identifier == "gl_ClipDistance" ||4930identifier == "gl_CullDistance") {4931if (qualifier.hasLayout() || qualifier.isMemory() || qualifier.isAuxiliary() ||4932qualifier.nopersp != symbolQualifier.nopersp || qualifier.flat != symbolQualifier.flat ||4933symbolQualifier.storage != qualifier.storage)4934error(loc, "cannot change qualification of", "redeclaration", symbol->getName().c_str());4935} else if (identifier == "gl_FragCoord") {4936if (!intermediate.getTexCoordRedeclared() && intermediate.inIoAccessed("gl_FragCoord"))4937error(loc, "cannot redeclare after use", "gl_FragCoord", "");4938if (qualifier.nopersp != symbolQualifier.nopersp || qualifier.flat != symbolQualifier.flat ||4939qualifier.isMemory() || qualifier.isAuxiliary())4940error(loc, "can only change layout qualification of", "redeclaration", symbol->getName().c_str());4941if (qualifier.storage != EvqVaryingIn)4942error(loc, "cannot change input storage qualification of", "redeclaration", symbol->getName().c_str());4943if (! builtIn && (publicType.pixelCenterInteger != intermediate.getPixelCenterInteger() ||4944publicType.originUpperLeft != intermediate.getOriginUpperLeft()))4945error(loc, "cannot redeclare with different qualification:", "redeclaration", symbol->getName().c_str());494649474948intermediate.setTexCoordRedeclared();4949if (publicType.pixelCenterInteger)4950intermediate.setPixelCenterInteger();4951if (publicType.originUpperLeft)4952intermediate.setOriginUpperLeft();4953} else if (identifier == "gl_FragDepth") {4954if (qualifier.nopersp != symbolQualifier.nopersp || qualifier.flat != symbolQualifier.flat ||4955qualifier.isMemory() || qualifier.isAuxiliary())4956error(loc, "can only change layout qualification of", "redeclaration", symbol->getName().c_str());4957if (qualifier.storage != EvqVaryingOut)4958error(loc, "cannot change output storage qualification of", "redeclaration", symbol->getName().c_str());4959if (publicType.layoutDepth != EldNone) {4960if (intermediate.inIoAccessed("gl_FragDepth"))4961error(loc, "cannot redeclare after use", "gl_FragDepth", "");4962if (! intermediate.setDepth(publicType.layoutDepth))4963error(loc, "all redeclarations must use the same depth layout on", "redeclaration", symbol->getName().c_str());4964}4965} else if (identifier == "gl_FragStencilRefARB") {4966if (qualifier.nopersp != symbolQualifier.nopersp || qualifier.flat != symbolQualifier.flat ||4967qualifier.isMemory() || qualifier.isAuxiliary())4968error(loc, "can only change layout qualification of", "redeclaration", symbol->getName().c_str());4969if (qualifier.storage != EvqVaryingOut)4970error(loc, "cannot change output storage qualification of", "redeclaration", symbol->getName().c_str());4971if (publicType.layoutStencil != ElsNone) {4972if (intermediate.inIoAccessed("gl_FragStencilRefARB"))4973error(loc, "cannot redeclare after use", "gl_FragStencilRefARB", "");4974if (!intermediate.setStencil(publicType.layoutStencil))4975error(loc, "all redeclarations must use the same stencil layout on", "redeclaration",4976symbol->getName().c_str());4977}4978}4979else if (4980identifier == "gl_PrimitiveIndicesNV") {4981if (qualifier.hasLayout())4982error(loc, "cannot apply layout qualifier to", "redeclaration", symbol->getName().c_str());4983if (qualifier.storage != EvqVaryingOut)4984error(loc, "cannot change output storage qualification of", "redeclaration", symbol->getName().c_str());4985}4986else if (identifier == "gl_SampleMask") {4987if (!publicType.layoutOverrideCoverage) {4988error(loc, "redeclaration only allowed for override_coverage layout", "redeclaration", symbol->getName().c_str());4989}4990intermediate.setLayoutOverrideCoverage();4991}4992else if (identifier == "gl_Layer") {4993if (!qualifier.layoutViewportRelative && qualifier.layoutSecondaryViewportRelativeOffset == -2048)4994error(loc, "redeclaration only allowed for viewport_relative or secondary_view_offset layout", "redeclaration", symbol->getName().c_str());4995symbolQualifier.layoutViewportRelative = qualifier.layoutViewportRelative;4996symbolQualifier.layoutSecondaryViewportRelativeOffset = qualifier.layoutSecondaryViewportRelativeOffset;4997}49984999// TODO: semantics quality: separate smooth from nothing declared, then use IsInterpolation for several tests above50005001return symbol;5002}50035004return nullptr;5005}50065007//5008// Either redeclare the requested block, or give an error message why it can't be done.5009//5010// TODO: functionality: explicitly sizing members of redeclared blocks is not giving them an explicit size5011void TParseContext::redeclareBuiltinBlock(const TSourceLoc& loc, TTypeList& newTypeList, const TString& blockName,5012const TString* instanceName, TArraySizes* arraySizes)5013{5014const char* feature = "built-in block redeclaration";5015profileRequires(loc, EEsProfile, 320, Num_AEP_shader_io_blocks, AEP_shader_io_blocks, feature);5016profileRequires(loc, ~EEsProfile, 410, E_GL_ARB_separate_shader_objects, feature);50175018if (blockName != "gl_PerVertex" && blockName != "gl_PerFragment" &&5019blockName != "gl_MeshPerVertexNV" && blockName != "gl_MeshPerPrimitiveNV" &&5020blockName != "gl_MeshPerVertexEXT" && blockName != "gl_MeshPerPrimitiveEXT") {5021error(loc, "cannot redeclare block: ", "block declaration", blockName.c_str());5022return;5023}50245025// Redeclaring a built-in block...50265027if (instanceName && ! builtInName(*instanceName)) {5028error(loc, "cannot redeclare a built-in block with a user name", instanceName->c_str(), "");5029return;5030}50315032// Blocks with instance names are easy to find, lookup the instance name,5033// Anonymous blocks need to be found via a member.5034bool builtIn;5035TSymbol* block;5036if (instanceName)5037block = symbolTable.find(*instanceName, &builtIn);5038else5039block = symbolTable.find(newTypeList.front().type->getFieldName(), &builtIn);50405041// If the block was not found, this must be a version/profile/stage5042// that doesn't have it, or the instance name is wrong.5043const char* errorName = instanceName ? instanceName->c_str() : newTypeList.front().type->getFieldName().c_str();5044if (! block) {5045error(loc, "no declaration found for redeclaration", errorName, "");5046return;5047}5048// Built-in blocks cannot be redeclared more than once, which if happened,5049// we'd be finding the already redeclared one here, rather than the built in.5050if (! builtIn) {5051error(loc, "can only redeclare a built-in block once, and before any use", blockName.c_str(), "");5052return;5053}50545055// Copy the block to make a writable version, to insert into the block table after editing.5056block = symbolTable.copyUpDeferredInsert(block);50575058if (block->getType().getBasicType() != EbtBlock) {5059error(loc, "cannot redeclare a non block as a block", errorName, "");5060return;5061}50625063// Fix XFB stuff up, it applies to the order of the redeclaration, not5064// the order of the original members.5065if (currentBlockQualifier.storage == EvqVaryingOut && globalOutputDefaults.hasXfbBuffer()) {5066if (!currentBlockQualifier.hasXfbBuffer())5067currentBlockQualifier.layoutXfbBuffer = globalOutputDefaults.layoutXfbBuffer;5068if (!currentBlockQualifier.hasStream())5069currentBlockQualifier.layoutStream = globalOutputDefaults.layoutStream;5070fixXfbOffsets(currentBlockQualifier, newTypeList);5071}50725073// Edit and error check the container against the redeclaration5074// - remove unused members5075// - ensure remaining qualifiers/types match50765077TType& type = block->getWritableType();50785079// if gl_PerVertex is redeclared for the purpose of passing through "gl_Position"5080// for passthrough purpose, the redeclared block should have the same qualifers as5081// the current one5082if (currentBlockQualifier.layoutPassthrough) {5083type.getQualifier().layoutPassthrough = currentBlockQualifier.layoutPassthrough;5084type.getQualifier().storage = currentBlockQualifier.storage;5085type.getQualifier().layoutStream = currentBlockQualifier.layoutStream;5086type.getQualifier().layoutXfbBuffer = currentBlockQualifier.layoutXfbBuffer;5087}50885089TTypeList::iterator member = type.getWritableStruct()->begin();5090size_t numOriginalMembersFound = 0;5091while (member != type.getStruct()->end()) {5092// look for match5093bool found = false;5094TTypeList::const_iterator newMember;5095TSourceLoc memberLoc;5096memberLoc.init();5097for (newMember = newTypeList.begin(); newMember != newTypeList.end(); ++newMember) {5098if (member->type->getFieldName() == newMember->type->getFieldName()) {5099found = true;5100memberLoc = newMember->loc;5101break;5102}5103}51045105if (found) {5106++numOriginalMembersFound;5107// - ensure match between redeclared members' types5108// - check for things that can't be changed5109// - update things that can be changed5110TType& oldType = *member->type;5111const TType& newType = *newMember->type;5112if (! newType.sameElementType(oldType))5113error(memberLoc, "cannot redeclare block member with a different type", member->type->getFieldName().c_str(), "");5114if (oldType.isArray() != newType.isArray())5115error(memberLoc, "cannot change arrayness of redeclared block member", member->type->getFieldName().c_str(), "");5116else if (! oldType.getQualifier().isPerView() && ! oldType.sameArrayness(newType) && oldType.isSizedArray())5117error(memberLoc, "cannot change array size of redeclared block member", member->type->getFieldName().c_str(), "");5118else if (! oldType.getQualifier().isPerView() && newType.isArray())5119arrayLimitCheck(loc, member->type->getFieldName(), newType.getOuterArraySize());5120if (oldType.getQualifier().isPerView() && ! newType.getQualifier().isPerView())5121error(memberLoc, "missing perviewNV qualifier to redeclared block member", member->type->getFieldName().c_str(), "");5122else if (! oldType.getQualifier().isPerView() && newType.getQualifier().isPerView())5123error(memberLoc, "cannot add perviewNV qualifier to redeclared block member", member->type->getFieldName().c_str(), "");5124else if (newType.getQualifier().isPerView()) {5125if (oldType.getArraySizes()->getNumDims() != newType.getArraySizes()->getNumDims())5126error(memberLoc, "cannot change arrayness of redeclared block member", member->type->getFieldName().c_str(), "");5127else if (! newType.isUnsizedArray() && newType.getOuterArraySize() != resources.maxMeshViewCountNV)5128error(loc, "mesh view output array size must be gl_MaxMeshViewCountNV or implicitly sized", "[]", "");5129else if (newType.getArraySizes()->getNumDims() == 2) {5130int innerDimSize = newType.getArraySizes()->getDimSize(1);5131arrayLimitCheck(memberLoc, member->type->getFieldName(), innerDimSize);5132oldType.getArraySizes()->setDimSize(1, innerDimSize);5133}5134}5135if (oldType.getQualifier().isPerPrimitive() && ! newType.getQualifier().isPerPrimitive())5136error(memberLoc, "missing perprimitiveNV qualifier to redeclared block member", member->type->getFieldName().c_str(), "");5137else if (! oldType.getQualifier().isPerPrimitive() && newType.getQualifier().isPerPrimitive())5138error(memberLoc, "cannot add perprimitiveNV qualifier to redeclared block member", member->type->getFieldName().c_str(), "");5139if (newType.getQualifier().isMemory())5140error(memberLoc, "cannot add memory qualifier to redeclared block member", member->type->getFieldName().c_str(), "");5141if (newType.getQualifier().hasNonXfbLayout())5142error(memberLoc, "cannot add non-XFB layout to redeclared block member", member->type->getFieldName().c_str(), "");5143if (newType.getQualifier().patch)5144error(memberLoc, "cannot add patch to redeclared block member", member->type->getFieldName().c_str(), "");5145if (newType.getQualifier().hasXfbBuffer() &&5146newType.getQualifier().layoutXfbBuffer != currentBlockQualifier.layoutXfbBuffer)5147error(memberLoc, "member cannot contradict block (or what block inherited from global)", "xfb_buffer", "");5148if (newType.getQualifier().hasStream() &&5149newType.getQualifier().layoutStream != currentBlockQualifier.layoutStream)5150error(memberLoc, "member cannot contradict block (or what block inherited from global)", "xfb_stream", "");5151oldType.getQualifier().centroid = newType.getQualifier().centroid;5152oldType.getQualifier().sample = newType.getQualifier().sample;5153oldType.getQualifier().invariant = newType.getQualifier().invariant;5154oldType.getQualifier().noContraction = newType.getQualifier().noContraction;5155oldType.getQualifier().smooth = newType.getQualifier().smooth;5156oldType.getQualifier().flat = newType.getQualifier().flat;5157oldType.getQualifier().nopersp = newType.getQualifier().nopersp;5158oldType.getQualifier().layoutXfbOffset = newType.getQualifier().layoutXfbOffset;5159oldType.getQualifier().layoutXfbBuffer = newType.getQualifier().layoutXfbBuffer;5160oldType.getQualifier().layoutXfbStride = newType.getQualifier().layoutXfbStride;5161if (oldType.getQualifier().layoutXfbOffset != TQualifier::layoutXfbBufferEnd) {5162// If any member has an xfb_offset, then the block's xfb_buffer inherents current xfb_buffer,5163// and for xfb processing, the member needs it as well, along with xfb_stride.5164type.getQualifier().layoutXfbBuffer = currentBlockQualifier.layoutXfbBuffer;5165oldType.getQualifier().layoutXfbBuffer = currentBlockQualifier.layoutXfbBuffer;5166}5167if (oldType.isUnsizedArray() && newType.isSizedArray())5168oldType.changeOuterArraySize(newType.getOuterArraySize());51695170// check and process the member's type, which will include managing xfb information5171layoutTypeCheck(loc, oldType);51725173// go to next member5174++member;5175} else {5176// For missing members of anonymous blocks that have been redeclared,5177// hide the original (shared) declaration.5178// Instance-named blocks can just have the member removed.5179if (instanceName)5180member = type.getWritableStruct()->erase(member);5181else {5182member->type->hideMember();5183++member;5184}5185}5186}51875188if (spvVersion.vulkan > 0) {5189// ...then streams apply to built-in blocks, instead of them being only on stream 05190type.getQualifier().layoutStream = currentBlockQualifier.layoutStream;5191}51925193if (numOriginalMembersFound < newTypeList.size())5194error(loc, "block redeclaration has extra members", blockName.c_str(), "");5195if (type.isArray() != (arraySizes != nullptr) ||5196(type.isArray() && arraySizes != nullptr && type.getArraySizes()->getNumDims() != arraySizes->getNumDims()))5197error(loc, "cannot change arrayness of redeclared block", blockName.c_str(), "");5198else if (type.isArray()) {5199// At this point, we know both are arrays and both have the same number of dimensions.52005201// It is okay for a built-in block redeclaration to be unsized, and keep the size of the5202// original block declaration.5203if (!arraySizes->isSized() && type.isSizedArray())5204arraySizes->changeOuterSize(type.getOuterArraySize());52055206// And, okay to be giving a size to the array, by the redeclaration5207if (!type.isSizedArray() && arraySizes->isSized())5208type.changeOuterArraySize(arraySizes->getOuterSize());52095210// Now, they must match in all dimensions.5211if (type.isSizedArray() && *type.getArraySizes() != *arraySizes)5212error(loc, "cannot change array size of redeclared block", blockName.c_str(), "");5213}52145215symbolTable.insert(*block);52165217// Check for general layout qualifier errors5218layoutObjectCheck(loc, *block);52195220// Tracking for implicit sizing of array5221if (isIoResizeArray(block->getType())) {5222ioArraySymbolResizeList.push_back(block);5223checkIoArraysConsistency(loc, true);5224} else if (block->getType().isArray())5225fixIoArraySize(loc, block->getWritableType());52265227// Save it in the AST for linker use.5228trackLinkage(*block);5229}52305231void TParseContext::paramCheckFixStorage(const TSourceLoc& loc, const TStorageQualifier& qualifier, TType& type)5232{5233switch (qualifier) {5234case EvqConst:5235case EvqConstReadOnly:5236type.getQualifier().storage = EvqConstReadOnly;5237break;5238case EvqIn:5239case EvqOut:5240case EvqInOut:5241case EvqTileImageEXT:5242type.getQualifier().storage = qualifier;5243break;5244case EvqGlobal:5245case EvqTemporary:5246type.getQualifier().storage = EvqIn;5247break;5248default:5249type.getQualifier().storage = EvqIn;5250error(loc, "storage qualifier not allowed on function parameter", GetStorageQualifierString(qualifier), "");5251break;5252}5253}52545255void TParseContext::paramCheckFix(const TSourceLoc& loc, const TQualifier& qualifier, TType& type)5256{5257if (qualifier.isMemory()) {5258type.getQualifier().volatil = qualifier.volatil;5259type.getQualifier().coherent = qualifier.coherent;5260type.getQualifier().devicecoherent = qualifier.devicecoherent ;5261type.getQualifier().queuefamilycoherent = qualifier.queuefamilycoherent;5262type.getQualifier().workgroupcoherent = qualifier.workgroupcoherent;5263type.getQualifier().subgroupcoherent = qualifier.subgroupcoherent;5264type.getQualifier().shadercallcoherent = qualifier.shadercallcoherent;5265type.getQualifier().nonprivate = qualifier.nonprivate;5266type.getQualifier().readonly = qualifier.readonly;5267type.getQualifier().writeonly = qualifier.writeonly;5268type.getQualifier().restrict = qualifier.restrict;5269}52705271if (qualifier.isAuxiliary() ||5272qualifier.isInterpolation())5273error(loc, "cannot use auxiliary or interpolation qualifiers on a function parameter", "", "");5274if (qualifier.hasLayout())5275error(loc, "cannot use layout qualifiers on a function parameter", "", "");5276if (qualifier.invariant)5277error(loc, "cannot use invariant qualifier on a function parameter", "", "");5278if (qualifier.isNoContraction()) {5279if (qualifier.isParamOutput())5280type.getQualifier().setNoContraction();5281else5282warn(loc, "qualifier has no effect on non-output parameters", "precise", "");5283}5284if (qualifier.isNonUniform())5285type.getQualifier().nonUniform = qualifier.nonUniform;5286if (qualifier.isSpirvByReference())5287type.getQualifier().setSpirvByReference();5288if (qualifier.isSpirvLiteral()) {5289if (type.getBasicType() == EbtFloat || type.getBasicType() == EbtInt || type.getBasicType() == EbtUint ||5290type.getBasicType() == EbtBool)5291type.getQualifier().setSpirvLiteral();5292else5293error(loc, "cannot use spirv_literal qualifier", type.getBasicTypeString().c_str(), "");5294}52955296paramCheckFixStorage(loc, qualifier.storage, type);5297}52985299void TParseContext::nestedBlockCheck(const TSourceLoc& loc)5300{5301if (structNestingLevel > 0 || blockNestingLevel > 0)5302error(loc, "cannot nest a block definition inside a structure or block", "", "");5303++blockNestingLevel;5304}53055306void TParseContext::nestedStructCheck(const TSourceLoc& loc)5307{5308if (structNestingLevel > 0 || blockNestingLevel > 0)5309error(loc, "cannot nest a structure definition inside a structure or block", "", "");5310++structNestingLevel;5311}53125313void TParseContext::arrayObjectCheck(const TSourceLoc& loc, const TType& type, const char* op)5314{5315// Some versions don't allow comparing arrays or structures containing arrays5316if (type.containsArray()) {5317profileRequires(loc, ENoProfile, 120, E_GL_3DL_array_objects, op);5318profileRequires(loc, EEsProfile, 300, nullptr, op);5319}5320}53215322void TParseContext::opaqueCheck(const TSourceLoc& loc, const TType& type, const char* op)5323{5324if (containsFieldWithBasicType(type, EbtSampler) && !extensionTurnedOn(E_GL_ARB_bindless_texture))5325error(loc, "can't use with samplers or structs containing samplers", op, "");5326}53275328void TParseContext::referenceCheck(const TSourceLoc& loc, const TType& type, const char* op)5329{5330if (containsFieldWithBasicType(type, EbtReference))5331error(loc, "can't use with reference types", op, "");5332}53335334void TParseContext::storage16BitAssignmentCheck(const TSourceLoc& loc, const TType& type, const char* op)5335{5336if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtFloat16))5337requireFloat16Arithmetic(loc, op, "can't use with structs containing float16");53385339if (type.isArray() && type.getBasicType() == EbtFloat16)5340requireFloat16Arithmetic(loc, op, "can't use with arrays containing float16");53415342if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtInt16))5343requireInt16Arithmetic(loc, op, "can't use with structs containing int16");53445345if (type.isArray() && type.getBasicType() == EbtInt16)5346requireInt16Arithmetic(loc, op, "can't use with arrays containing int16");53475348if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtUint16))5349requireInt16Arithmetic(loc, op, "can't use with structs containing uint16");53505351if (type.isArray() && type.getBasicType() == EbtUint16)5352requireInt16Arithmetic(loc, op, "can't use with arrays containing uint16");53535354if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtInt8))5355requireInt8Arithmetic(loc, op, "can't use with structs containing int8");53565357if (type.isArray() && type.getBasicType() == EbtInt8)5358requireInt8Arithmetic(loc, op, "can't use with arrays containing int8");53595360if (type.getBasicType() == EbtStruct && containsFieldWithBasicType(type, EbtUint8))5361requireInt8Arithmetic(loc, op, "can't use with structs containing uint8");53625363if (type.isArray() && type.getBasicType() == EbtUint8)5364requireInt8Arithmetic(loc, op, "can't use with arrays containing uint8");5365}53665367void TParseContext::specializationCheck(const TSourceLoc& loc, const TType& type, const char* op)5368{5369if (type.containsSpecializationSize())5370error(loc, "can't use with types containing arrays sized with a specialization constant", op, "");5371}53725373void TParseContext::structTypeCheck(const TSourceLoc& /*loc*/, TPublicType& publicType)5374{5375const TTypeList& typeList = *publicType.userDef->getStruct();53765377// fix and check for member storage qualifiers and types that don't belong within a structure5378for (unsigned int member = 0; member < typeList.size(); ++member) {5379TQualifier& memberQualifier = typeList[member].type->getQualifier();5380const TSourceLoc& memberLoc = typeList[member].loc;5381if (memberQualifier.isAuxiliary() ||5382memberQualifier.isInterpolation() ||5383(memberQualifier.storage != EvqTemporary && memberQualifier.storage != EvqGlobal))5384error(memberLoc, "cannot use storage or interpolation qualifiers on structure members", typeList[member].type->getFieldName().c_str(), "");5385if (memberQualifier.isMemory())5386error(memberLoc, "cannot use memory qualifiers on structure members", typeList[member].type->getFieldName().c_str(), "");5387if (memberQualifier.hasLayout()) {5388error(memberLoc, "cannot use layout qualifiers on structure members", typeList[member].type->getFieldName().c_str(), "");5389memberQualifier.clearLayout();5390}5391if (memberQualifier.invariant)5392error(memberLoc, "cannot use invariant qualifier on structure members", typeList[member].type->getFieldName().c_str(), "");5393}5394}53955396//5397// See if this loop satisfies the limitations for ES 2.0 (version 100) for loops in Appendex A:5398//5399// "The loop index has type int or float.5400//5401// "The for statement has the form:5402// for ( init-declaration ; condition ; expression )5403// init-declaration has the form: type-specifier identifier = constant-expression5404// condition has the form: loop-index relational_operator constant-expression5405// where relational_operator is one of: > >= < <= == or !=5406// expression [sic] has one of the following forms:5407// loop-index++5408// loop-index--5409// loop-index += constant-expression5410// loop-index -= constant-expression5411//5412// The body is handled in an AST traversal.5413//5414void TParseContext::inductiveLoopCheck(const TSourceLoc& loc, TIntermNode* init, TIntermLoop* loop)5415{5416// loop index init must exist and be a declaration, which shows up in the AST as an aggregate of size 1 of the declaration5417bool badInit = false;5418if (! init || ! init->getAsAggregate() || init->getAsAggregate()->getSequence().size() != 1)5419badInit = true;5420TIntermBinary* binaryInit = nullptr;5421if (! badInit) {5422// get the declaration assignment5423binaryInit = init->getAsAggregate()->getSequence()[0]->getAsBinaryNode();5424if (! binaryInit)5425badInit = true;5426}5427if (badInit) {5428error(loc, "inductive-loop init-declaration requires the form \"type-specifier loop-index = constant-expression\"", "limitations", "");5429return;5430}54315432// loop index must be type int or float5433if (! binaryInit->getType().isScalar() || (binaryInit->getBasicType() != EbtInt && binaryInit->getBasicType() != EbtFloat)) {5434error(loc, "inductive loop requires a scalar 'int' or 'float' loop index", "limitations", "");5435return;5436}54375438// init is the form "loop-index = constant"5439if (binaryInit->getOp() != EOpAssign || ! binaryInit->getLeft()->getAsSymbolNode() || ! binaryInit->getRight()->getAsConstantUnion()) {5440error(loc, "inductive-loop init-declaration requires the form \"type-specifier loop-index = constant-expression\"", "limitations", "");5441return;5442}54435444// get the unique id of the loop index5445long long loopIndex = binaryInit->getLeft()->getAsSymbolNode()->getId();5446inductiveLoopIds.insert(loopIndex);54475448// condition's form must be "loop-index relational-operator constant-expression"5449bool badCond = ! loop->getTest();5450if (! badCond) {5451TIntermBinary* binaryCond = loop->getTest()->getAsBinaryNode();5452badCond = ! binaryCond;5453if (! badCond) {5454switch (binaryCond->getOp()) {5455case EOpGreaterThan:5456case EOpGreaterThanEqual:5457case EOpLessThan:5458case EOpLessThanEqual:5459case EOpEqual:5460case EOpNotEqual:5461break;5462default:5463badCond = true;5464}5465}5466if (binaryCond && (! binaryCond->getLeft()->getAsSymbolNode() ||5467binaryCond->getLeft()->getAsSymbolNode()->getId() != loopIndex ||5468! binaryCond->getRight()->getAsConstantUnion()))5469badCond = true;5470}5471if (badCond) {5472error(loc, "inductive-loop condition requires the form \"loop-index <comparison-op> constant-expression\"", "limitations", "");5473return;5474}54755476// loop-index++5477// loop-index--5478// loop-index += constant-expression5479// loop-index -= constant-expression5480bool badTerminal = ! loop->getTerminal();5481if (! badTerminal) {5482TIntermUnary* unaryTerminal = loop->getTerminal()->getAsUnaryNode();5483TIntermBinary* binaryTerminal = loop->getTerminal()->getAsBinaryNode();5484if (unaryTerminal || binaryTerminal) {5485switch(loop->getTerminal()->getAsOperator()->getOp()) {5486case EOpPostDecrement:5487case EOpPostIncrement:5488case EOpAddAssign:5489case EOpSubAssign:5490break;5491default:5492badTerminal = true;5493}5494} else5495badTerminal = true;5496if (binaryTerminal && (! binaryTerminal->getLeft()->getAsSymbolNode() ||5497binaryTerminal->getLeft()->getAsSymbolNode()->getId() != loopIndex ||5498! binaryTerminal->getRight()->getAsConstantUnion()))5499badTerminal = true;5500if (unaryTerminal && (! unaryTerminal->getOperand()->getAsSymbolNode() ||5501unaryTerminal->getOperand()->getAsSymbolNode()->getId() != loopIndex))5502badTerminal = true;5503}5504if (badTerminal) {5505error(loc, "inductive-loop termination requires the form \"loop-index++, loop-index--, loop-index += constant-expression, or loop-index -= constant-expression\"", "limitations", "");5506return;5507}55085509// the body5510inductiveLoopBodyCheck(loop->getBody(), loopIndex, symbolTable);5511}55125513// Do limit checks for built-in arrays.5514void TParseContext::arrayLimitCheck(const TSourceLoc& loc, const TString& identifier, int size)5515{5516if (identifier.compare("gl_TexCoord") == 0)5517limitCheck(loc, size, "gl_MaxTextureCoords", "gl_TexCoord array size");5518else if (identifier.compare("gl_ClipDistance") == 0)5519limitCheck(loc, size, "gl_MaxClipDistances", "gl_ClipDistance array size");5520else if (identifier.compare("gl_CullDistance") == 0)5521limitCheck(loc, size, "gl_MaxCullDistances", "gl_CullDistance array size");5522else if (identifier.compare("gl_ClipDistancePerViewNV") == 0)5523limitCheck(loc, size, "gl_MaxClipDistances", "gl_ClipDistancePerViewNV array size");5524else if (identifier.compare("gl_CullDistancePerViewNV") == 0)5525limitCheck(loc, size, "gl_MaxCullDistances", "gl_CullDistancePerViewNV array size");5526}55275528// See if the provided value is less than or equal to the symbol indicated by limit,5529// which should be a constant in the symbol table.5530void TParseContext::limitCheck(const TSourceLoc& loc, int value, const char* limit, const char* feature)5531{5532TSymbol* symbol = symbolTable.find(limit);5533assert(symbol->getAsVariable());5534const TConstUnionArray& constArray = symbol->getAsVariable()->getConstArray();5535assert(! constArray.empty());5536if (value > constArray[0].getIConst())5537error(loc, "must be less than or equal to", feature, "%s (%d)", limit, constArray[0].getIConst());5538}55395540//5541// Do any additional error checking, etc., once we know the parsing is done.5542//5543void TParseContext::finish()5544{5545TParseContextBase::finish();55465547if (parsingBuiltins)5548return;55495550// Check on array indexes for ES 2.0 (version 100) limitations.5551for (size_t i = 0; i < needsIndexLimitationChecking.size(); ++i)5552constantIndexExpressionCheck(needsIndexLimitationChecking[i]);55535554// Check for stages that are enabled by extension.5555// Can't do this at the beginning, it is chicken and egg to add a stage by5556// extension.5557// Stage-specific features were correctly tested for already, this is just5558// about the stage itself.5559switch (language) {5560case EShLangGeometry:5561if (isEsProfile() && version == 310)5562requireExtensions(getCurrentLoc(), Num_AEP_geometry_shader, AEP_geometry_shader, "geometry shaders");5563break;5564case EShLangTessControl:5565case EShLangTessEvaluation:5566if (isEsProfile() && version == 310)5567requireExtensions(getCurrentLoc(), Num_AEP_tessellation_shader, AEP_tessellation_shader, "tessellation shaders");5568else if (!isEsProfile() && version < 400)5569requireExtensions(getCurrentLoc(), 1, &E_GL_ARB_tessellation_shader, "tessellation shaders");5570break;5571case EShLangCompute:5572if (!isEsProfile() && version < 430)5573requireExtensions(getCurrentLoc(), 1, &E_GL_ARB_compute_shader, "compute shaders");5574break;5575case EShLangTask:5576requireExtensions(getCurrentLoc(), Num_AEP_mesh_shader, AEP_mesh_shader, "task shaders");5577break;5578case EShLangMesh:5579requireExtensions(getCurrentLoc(), Num_AEP_mesh_shader, AEP_mesh_shader, "mesh shaders");5580break;5581default:5582break;5583}55845585// Set default outputs for GL_NV_geometry_shader_passthrough5586if (language == EShLangGeometry && extensionTurnedOn(E_SPV_NV_geometry_shader_passthrough)) {5587if (intermediate.getOutputPrimitive() == ElgNone) {5588switch (intermediate.getInputPrimitive()) {5589case ElgPoints: intermediate.setOutputPrimitive(ElgPoints); break;5590case ElgLines: intermediate.setOutputPrimitive(ElgLineStrip); break;5591case ElgTriangles: intermediate.setOutputPrimitive(ElgTriangleStrip); break;5592default: break;5593}5594}5595if (intermediate.getVertices() == TQualifier::layoutNotSet) {5596switch (intermediate.getInputPrimitive()) {5597case ElgPoints: intermediate.setVertices(1); break;5598case ElgLines: intermediate.setVertices(2); break;5599case ElgTriangles: intermediate.setVertices(3); break;5600default: break;5601}5602}5603}5604}56055606//5607// Layout qualifier stuff.5608//56095610// Put the id's layout qualification into the public type, for qualifiers not having a number set.5611// This is before we know any type information for error checking.5612void TParseContext::setLayoutQualifier(const TSourceLoc& loc, TPublicType& publicType, TString& id)5613{5614std::transform(id.begin(), id.end(), id.begin(), ::tolower);56155616if (id == TQualifier::getLayoutMatrixString(ElmColumnMajor)) {5617publicType.qualifier.layoutMatrix = ElmColumnMajor;5618return;5619}5620if (id == TQualifier::getLayoutMatrixString(ElmRowMajor)) {5621publicType.qualifier.layoutMatrix = ElmRowMajor;5622return;5623}5624if (id == TQualifier::getLayoutPackingString(ElpPacked)) {5625if (spvVersion.spv != 0) {5626if (spvVersion.vulkanRelaxed)5627return; // silently ignore qualifier5628else5629spvRemoved(loc, "packed");5630}5631publicType.qualifier.layoutPacking = ElpPacked;5632return;5633}5634if (id == TQualifier::getLayoutPackingString(ElpShared)) {5635if (spvVersion.spv != 0) {5636if (spvVersion.vulkanRelaxed)5637return; // silently ignore qualifier5638else5639spvRemoved(loc, "shared");5640}5641publicType.qualifier.layoutPacking = ElpShared;5642return;5643}5644if (id == TQualifier::getLayoutPackingString(ElpStd140)) {5645publicType.qualifier.layoutPacking = ElpStd140;5646return;5647}5648if (id == TQualifier::getLayoutPackingString(ElpStd430)) {5649requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, "std430");5650profileRequires(loc, ECoreProfile | ECompatibilityProfile, 430, E_GL_ARB_shader_storage_buffer_object, "std430");5651profileRequires(loc, EEsProfile, 310, nullptr, "std430");5652publicType.qualifier.layoutPacking = ElpStd430;5653return;5654}5655if (id == TQualifier::getLayoutPackingString(ElpScalar)) {5656requireVulkan(loc, "scalar");5657requireExtensions(loc, 1, &E_GL_EXT_scalar_block_layout, "scalar block layout");5658publicType.qualifier.layoutPacking = ElpScalar;5659return;5660}5661// TODO: compile-time performance: may need to stop doing linear searches5662for (TLayoutFormat format = (TLayoutFormat)(ElfNone + 1); format < ElfCount; format = (TLayoutFormat)(format + 1)) {5663if (id == TQualifier::getLayoutFormatString(format)) {5664if ((format > ElfEsFloatGuard && format < ElfFloatGuard) ||5665(format > ElfEsIntGuard && format < ElfIntGuard) ||5666(format > ElfEsUintGuard && format < ElfCount))5667requireProfile(loc, ENoProfile | ECoreProfile | ECompatibilityProfile, "image load-store format");5668profileRequires(loc, ENoProfile | ECoreProfile | ECompatibilityProfile, 420, E_GL_ARB_shader_image_load_store, "image load store");5669profileRequires(loc, EEsProfile, 310, E_GL_ARB_shader_image_load_store, "image load store");5670publicType.qualifier.layoutFormat = format;5671return;5672}5673}5674if (id == "push_constant") {5675requireVulkan(loc, "push_constant");5676publicType.qualifier.layoutPushConstant = true;5677return;5678}5679if (id == "buffer_reference") {5680requireVulkan(loc, "buffer_reference");5681requireExtensions(loc, 1, &E_GL_EXT_buffer_reference, "buffer_reference");5682publicType.qualifier.layoutBufferReference = true;5683intermediate.setUseStorageBuffer();5684intermediate.setUsePhysicalStorageBuffer();5685return;5686}5687if (id == "bindless_sampler") {5688requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "bindless_sampler");5689publicType.qualifier.layoutBindlessSampler = true;5690intermediate.setBindlessTextureMode(currentCaller, AstRefTypeLayout);5691return;5692}5693if (id == "bindless_image") {5694requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "bindless_image");5695publicType.qualifier.layoutBindlessImage = true;5696intermediate.setBindlessImageMode(currentCaller, AstRefTypeLayout);5697return;5698}5699if (id == "bound_sampler") {5700requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "bound_sampler");5701publicType.qualifier.layoutBindlessSampler = false;5702return;5703}5704if (id == "bound_image") {5705requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "bound_image");5706publicType.qualifier.layoutBindlessImage = false;5707return;5708}5709if (language == EShLangGeometry || language == EShLangTessEvaluation || language == EShLangMesh) {5710if (id == TQualifier::getGeometryString(ElgTriangles)) {5711publicType.shaderQualifiers.geometry = ElgTriangles;5712return;5713}5714if (language == EShLangGeometry || language == EShLangMesh) {5715if (id == TQualifier::getGeometryString(ElgPoints)) {5716publicType.shaderQualifiers.geometry = ElgPoints;5717return;5718}5719if (id == TQualifier::getGeometryString(ElgLines)) {5720publicType.shaderQualifiers.geometry = ElgLines;5721return;5722}5723if (language == EShLangGeometry) {5724if (id == TQualifier::getGeometryString(ElgLineStrip)) {5725publicType.shaderQualifiers.geometry = ElgLineStrip;5726return;5727}5728if (id == TQualifier::getGeometryString(ElgLinesAdjacency)) {5729publicType.shaderQualifiers.geometry = ElgLinesAdjacency;5730return;5731}5732if (id == TQualifier::getGeometryString(ElgTrianglesAdjacency)) {5733publicType.shaderQualifiers.geometry = ElgTrianglesAdjacency;5734return;5735}5736if (id == TQualifier::getGeometryString(ElgTriangleStrip)) {5737publicType.shaderQualifiers.geometry = ElgTriangleStrip;5738return;5739}5740if (id == "passthrough") {5741requireExtensions(loc, 1, &E_SPV_NV_geometry_shader_passthrough, "geometry shader passthrough");5742publicType.qualifier.layoutPassthrough = true;5743intermediate.setGeoPassthroughEXT();5744return;5745}5746}5747} else {5748assert(language == EShLangTessEvaluation);57495750// input primitive5751if (id == TQualifier::getGeometryString(ElgTriangles)) {5752publicType.shaderQualifiers.geometry = ElgTriangles;5753return;5754}5755if (id == TQualifier::getGeometryString(ElgQuads)) {5756publicType.shaderQualifiers.geometry = ElgQuads;5757return;5758}5759if (id == TQualifier::getGeometryString(ElgIsolines)) {5760publicType.shaderQualifiers.geometry = ElgIsolines;5761return;5762}57635764// vertex spacing5765if (id == TQualifier::getVertexSpacingString(EvsEqual)) {5766publicType.shaderQualifiers.spacing = EvsEqual;5767return;5768}5769if (id == TQualifier::getVertexSpacingString(EvsFractionalEven)) {5770publicType.shaderQualifiers.spacing = EvsFractionalEven;5771return;5772}5773if (id == TQualifier::getVertexSpacingString(EvsFractionalOdd)) {5774publicType.shaderQualifiers.spacing = EvsFractionalOdd;5775return;5776}57775778// triangle order5779if (id == TQualifier::getVertexOrderString(EvoCw)) {5780publicType.shaderQualifiers.order = EvoCw;5781return;5782}5783if (id == TQualifier::getVertexOrderString(EvoCcw)) {5784publicType.shaderQualifiers.order = EvoCcw;5785return;5786}57875788// point mode5789if (id == "point_mode") {5790publicType.shaderQualifiers.pointMode = true;5791return;5792}5793}5794}5795if (language == EShLangFragment) {5796if (id == "origin_upper_left") {5797requireProfile(loc, ECoreProfile | ECompatibilityProfile | ENoProfile, "origin_upper_left");5798if (profile == ENoProfile) {5799profileRequires(loc,ECoreProfile | ECompatibilityProfile, 140, E_GL_ARB_fragment_coord_conventions, "origin_upper_left");5800}58015802publicType.shaderQualifiers.originUpperLeft = true;5803return;5804}5805if (id == "pixel_center_integer") {5806requireProfile(loc, ECoreProfile | ECompatibilityProfile | ENoProfile, "pixel_center_integer");5807if (profile == ENoProfile) {5808profileRequires(loc,ECoreProfile | ECompatibilityProfile, 140, E_GL_ARB_fragment_coord_conventions, "pixel_center_integer");5809}5810publicType.shaderQualifiers.pixelCenterInteger = true;5811return;5812}5813if (id == "early_fragment_tests") {5814profileRequires(loc, ENoProfile | ECoreProfile | ECompatibilityProfile, 420, E_GL_ARB_shader_image_load_store, "early_fragment_tests");5815profileRequires(loc, EEsProfile, 310, nullptr, "early_fragment_tests");5816publicType.shaderQualifiers.earlyFragmentTests = true;5817return;5818}5819if (id == "early_and_late_fragment_tests_amd") {5820profileRequires(loc, ENoProfile | ECoreProfile | ECompatibilityProfile, 420, E_GL_AMD_shader_early_and_late_fragment_tests, "early_and_late_fragment_tests_amd");5821profileRequires(loc, EEsProfile, 310, nullptr, "early_and_late_fragment_tests_amd");5822publicType.shaderQualifiers.earlyAndLateFragmentTestsAMD = true;5823return;5824}5825if (id == "post_depth_coverage") {5826requireExtensions(loc, Num_post_depth_coverageEXTs, post_depth_coverageEXTs, "post depth coverage");5827if (extensionTurnedOn(E_GL_ARB_post_depth_coverage)) {5828publicType.shaderQualifiers.earlyFragmentTests = true;5829}5830publicType.shaderQualifiers.postDepthCoverage = true;5831return;5832}5833/* id is transformed into lower case in the beginning of this function. */5834if (id == "non_coherent_color_attachment_readext") {5835requireExtensions(loc, 1, &E_GL_EXT_shader_tile_image, "non_coherent_color_attachment_readEXT");5836publicType.shaderQualifiers.nonCoherentColorAttachmentReadEXT = true;5837return;5838}5839if (id == "non_coherent_depth_attachment_readext") {5840requireExtensions(loc, 1, &E_GL_EXT_shader_tile_image, "non_coherent_depth_attachment_readEXT");5841publicType.shaderQualifiers.nonCoherentDepthAttachmentReadEXT = true;5842return;5843}5844if (id == "non_coherent_stencil_attachment_readext") {5845requireExtensions(loc, 1, &E_GL_EXT_shader_tile_image, "non_coherent_stencil_attachment_readEXT");5846publicType.shaderQualifiers.nonCoherentStencilAttachmentReadEXT = true;5847return;5848}5849for (TLayoutDepth depth = (TLayoutDepth)(EldNone + 1); depth < EldCount; depth = (TLayoutDepth)(depth+1)) {5850if (id == TQualifier::getLayoutDepthString(depth)) {5851requireProfile(loc, ECoreProfile | ECompatibilityProfile, "depth layout qualifier");5852profileRequires(loc, ECoreProfile | ECompatibilityProfile, 420, nullptr, "depth layout qualifier");5853publicType.shaderQualifiers.layoutDepth = depth;5854return;5855}5856}5857for (TLayoutStencil stencil = (TLayoutStencil)(ElsNone + 1); stencil < ElsCount; stencil = (TLayoutStencil)(stencil+1)) {5858if (id == TQualifier::getLayoutStencilString(stencil)) {5859requireProfile(loc, ECoreProfile | ECompatibilityProfile, "stencil layout qualifier");5860profileRequires(loc, ECoreProfile | ECompatibilityProfile, 420, nullptr, "stencil layout qualifier");5861publicType.shaderQualifiers.layoutStencil = stencil;5862return;5863}5864}5865for (TInterlockOrdering order = (TInterlockOrdering)(EioNone + 1); order < EioCount; order = (TInterlockOrdering)(order+1)) {5866if (id == TQualifier::getInterlockOrderingString(order)) {5867requireProfile(loc, ECoreProfile | ECompatibilityProfile, "fragment shader interlock layout qualifier");5868profileRequires(loc, ECoreProfile | ECompatibilityProfile, 450, nullptr, "fragment shader interlock layout qualifier");5869requireExtensions(loc, 1, &E_GL_ARB_fragment_shader_interlock, TQualifier::getInterlockOrderingString(order));5870if (order == EioShadingRateInterlockOrdered || order == EioShadingRateInterlockUnordered)5871requireExtensions(loc, 1, &E_GL_NV_shading_rate_image, TQualifier::getInterlockOrderingString(order));5872publicType.shaderQualifiers.interlockOrdering = order;5873return;5874}5875}5876if (id.compare(0, 13, "blend_support") == 0) {5877bool found = false;5878for (TBlendEquationShift be = (TBlendEquationShift)0; be < EBlendCount; be = (TBlendEquationShift)(be + 1)) {5879if (id == TQualifier::getBlendEquationString(be)) {5880profileRequires(loc, EEsProfile, 320, E_GL_KHR_blend_equation_advanced, "blend equation");5881profileRequires(loc, ~EEsProfile, 0, E_GL_KHR_blend_equation_advanced, "blend equation");5882intermediate.addBlendEquation(be);5883publicType.shaderQualifiers.blendEquation = true;5884found = true;5885break;5886}5887}5888if (! found)5889error(loc, "unknown blend equation", "blend_support", "");5890return;5891}5892if (id == "override_coverage") {5893requireExtensions(loc, 1, &E_GL_NV_sample_mask_override_coverage, "sample mask override coverage");5894publicType.shaderQualifiers.layoutOverrideCoverage = true;5895return;5896}5897if (id == "full_quads")5898{5899const char* feature = "full_quads qualifier";5900requireProfile(loc, ECompatibilityProfile | ECoreProfile | EEsProfile, feature);5901profileRequires(loc, ECoreProfile | ECompatibilityProfile, 140, E_GL_EXT_shader_quad_control, feature);5902profileRequires(loc, EEsProfile, 310, E_GL_EXT_shader_quad_control, feature);5903publicType.qualifier.layoutFullQuads = true;5904return;5905}5906}5907if (language == EShLangVertex ||5908language == EShLangTessControl ||5909language == EShLangTessEvaluation ||5910language == EShLangGeometry ) {5911if (id == "viewport_relative") {5912requireExtensions(loc, 1, &E_GL_NV_viewport_array2, "view port array2");5913publicType.qualifier.layoutViewportRelative = true;5914return;5915}5916} else {5917if (language == EShLangRayGen || language == EShLangIntersect ||5918language == EShLangAnyHit || language == EShLangClosestHit ||5919language == EShLangMiss || language == EShLangCallable) {5920if (id == "shaderrecordnv" || id == "shaderrecordext") {5921if (id == "shaderrecordnv") {5922requireExtensions(loc, 1, &E_GL_NV_ray_tracing, "shader record NV");5923} else {5924requireExtensions(loc, 1, &E_GL_EXT_ray_tracing, "shader record EXT");5925}5926publicType.qualifier.layoutShaderRecord = true;5927return;5928} else if (id == "hitobjectshaderrecordnv") {5929requireExtensions(loc, 1, &E_GL_NV_shader_invocation_reorder, "hitobject shader record NV");5930publicType.qualifier.layoutHitObjectShaderRecordNV = true;5931return;5932}59335934}5935}5936if (language == EShLangCompute) {5937if (id.compare(0, 17, "derivative_group_") == 0) {5938requireExtensions(loc, 1, &E_GL_NV_compute_shader_derivatives, "compute shader derivatives");5939if (id == "derivative_group_quadsnv") {5940publicType.shaderQualifiers.layoutDerivativeGroupQuads = true;5941return;5942} else if (id == "derivative_group_linearnv") {5943publicType.shaderQualifiers.layoutDerivativeGroupLinear = true;5944return;5945}5946}5947}59485949if (id == "primitive_culling") {5950requireExtensions(loc, 1, &E_GL_EXT_ray_flags_primitive_culling, "primitive culling");5951publicType.shaderQualifiers.layoutPrimitiveCulling = true;5952return;5953}59545955if (id == "quad_derivatives")5956{5957const char* feature = "quad_derivatives qualifier";5958requireProfile(loc, ECompatibilityProfile | ECoreProfile | EEsProfile, feature);5959profileRequires(loc, ECoreProfile | ECompatibilityProfile, 140, E_GL_EXT_shader_quad_control, feature);5960profileRequires(loc, EEsProfile, 310, E_GL_EXT_shader_quad_control, feature);5961publicType.qualifier.layoutQuadDeriv = true;5962return;5963}59645965error(loc, "unrecognized layout identifier, or qualifier requires assignment (e.g., binding = 4)", id.c_str(), "");5966}59675968// Put the id's layout qualifier value into the public type, for qualifiers having a number set.5969// This is before we know any type information for error checking.5970void TParseContext::setLayoutQualifier(const TSourceLoc& loc, TPublicType& publicType, TString& id, const TIntermTyped* node)5971{5972const char* feature = "layout-id value";5973const char* nonLiteralFeature = "non-literal layout-id value";59745975integerCheck(node, feature);5976const TIntermConstantUnion* constUnion = node->getAsConstantUnion();5977int value;5978bool nonLiteral = false;5979if (constUnion) {5980value = constUnion->getConstArray()[0].getIConst();5981if (! constUnion->isLiteral()) {5982requireProfile(loc, ECoreProfile | ECompatibilityProfile, nonLiteralFeature);5983profileRequires(loc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, nonLiteralFeature);5984}5985} else {5986// grammar should have give out the error message5987value = 0;5988nonLiteral = true;5989}59905991if (value < 0) {5992error(loc, "cannot be negative", feature, "");5993return;5994}59955996std::transform(id.begin(), id.end(), id.begin(), ::tolower);59975998if (id == "offset") {5999// "offset" can be for either6000// - uniform offsets6001// - atomic_uint offsets6002const char* feature = "offset";6003if (spvVersion.spv == 0) {6004requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, feature);6005const char* exts[2] = { E_GL_ARB_enhanced_layouts, E_GL_ARB_shader_atomic_counters };6006profileRequires(loc, ECoreProfile | ECompatibilityProfile, 420, 2, exts, feature);6007profileRequires(loc, EEsProfile, 310, nullptr, feature);6008}6009publicType.qualifier.layoutOffset = value;6010publicType.qualifier.explicitOffset = true;6011if (nonLiteral)6012error(loc, "needs a literal integer", "offset", "");6013return;6014} else if (id == "align") {6015const char* feature = "uniform buffer-member align";6016if (spvVersion.spv == 0) {6017requireProfile(loc, ECoreProfile | ECompatibilityProfile, feature);6018profileRequires(loc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, feature);6019}6020// "The specified alignment must be a power of 2, or a compile-time error results."6021if (! IsPow2(value))6022error(loc, "must be a power of 2", "align", "");6023else6024publicType.qualifier.layoutAlign = value;6025if (nonLiteral)6026error(loc, "needs a literal integer", "align", "");6027return;6028} else if (id == "location") {6029profileRequires(loc, EEsProfile, 300, nullptr, "location");6030const char* exts[2] = { E_GL_ARB_separate_shader_objects, E_GL_ARB_explicit_attrib_location };6031// GL_ARB_explicit_uniform_location requires 330 or GL_ARB_explicit_attrib_location we do not need to add it here6032profileRequires(loc, ~EEsProfile, 330, 2, exts, "location");6033if ((unsigned int)value >= TQualifier::layoutLocationEnd)6034error(loc, "location is too large", id.c_str(), "");6035else6036publicType.qualifier.layoutLocation = value;6037if (nonLiteral)6038error(loc, "needs a literal integer", "location", "");6039return;6040} else if (id == "set") {6041if ((unsigned int)value >= TQualifier::layoutSetEnd)6042error(loc, "set is too large", id.c_str(), "");6043else6044publicType.qualifier.layoutSet = value;6045if (value != 0)6046requireVulkan(loc, "descriptor set");6047if (nonLiteral)6048error(loc, "needs a literal integer", "set", "");6049return;6050} else if (id == "binding") {6051profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, "binding");6052profileRequires(loc, EEsProfile, 310, nullptr, "binding");6053if ((unsigned int)value >= TQualifier::layoutBindingEnd)6054error(loc, "binding is too large", id.c_str(), "");6055else6056publicType.qualifier.layoutBinding = value;6057if (nonLiteral)6058error(loc, "needs a literal integer", "binding", "");6059return;6060}6061if (id == "constant_id") {6062requireSpv(loc, "constant_id");6063if (value >= (int)TQualifier::layoutSpecConstantIdEnd) {6064error(loc, "specialization-constant id is too large", id.c_str(), "");6065} else {6066publicType.qualifier.layoutSpecConstantId = value;6067publicType.qualifier.specConstant = true;6068if (! intermediate.addUsedConstantId(value))6069error(loc, "specialization-constant id already used", id.c_str(), "");6070}6071if (nonLiteral)6072error(loc, "needs a literal integer", "constant_id", "");6073return;6074}6075if (id == "component") {6076requireProfile(loc, ECoreProfile | ECompatibilityProfile, "component");6077profileRequires(loc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, "component");6078if ((unsigned)value >= TQualifier::layoutComponentEnd)6079error(loc, "component is too large", id.c_str(), "");6080else6081publicType.qualifier.layoutComponent = value;6082if (nonLiteral)6083error(loc, "needs a literal integer", "component", "");6084return;6085}6086if (id.compare(0, 4, "xfb_") == 0) {6087// "Any shader making any static use (after preprocessing) of any of these6088// *xfb_* qualifiers will cause the shader to be in a transform feedback6089// capturing mode and hence responsible for describing the transform feedback6090// setup."6091intermediate.setXfbMode();6092const char* feature = "transform feedback qualifier";6093requireStage(loc, (EShLanguageMask)(EShLangVertexMask | EShLangGeometryMask | EShLangTessControlMask | EShLangTessEvaluationMask), feature);6094requireProfile(loc, ECoreProfile | ECompatibilityProfile, feature);6095profileRequires(loc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, feature);6096if (id == "xfb_buffer") {6097// "It is a compile-time error to specify an *xfb_buffer* that is greater than6098// the implementation-dependent constant gl_MaxTransformFeedbackBuffers."6099if (value >= resources.maxTransformFeedbackBuffers)6100error(loc, "buffer is too large:", id.c_str(), "gl_MaxTransformFeedbackBuffers is %d", resources.maxTransformFeedbackBuffers);6101if (value >= (int)TQualifier::layoutXfbBufferEnd)6102error(loc, "buffer is too large:", id.c_str(), "internal max is %d", TQualifier::layoutXfbBufferEnd-1);6103else6104publicType.qualifier.layoutXfbBuffer = value;6105if (nonLiteral)6106error(loc, "needs a literal integer", "xfb_buffer", "");6107return;6108} else if (id == "xfb_offset") {6109if (value >= (int)TQualifier::layoutXfbOffsetEnd)6110error(loc, "offset is too large:", id.c_str(), "internal max is %d", TQualifier::layoutXfbOffsetEnd-1);6111else6112publicType.qualifier.layoutXfbOffset = value;6113if (nonLiteral)6114error(loc, "needs a literal integer", "xfb_offset", "");6115return;6116} else if (id == "xfb_stride") {6117// "The resulting stride (implicit or explicit), when divided by 4, must be less than or equal to the6118// implementation-dependent constant gl_MaxTransformFeedbackInterleavedComponents."6119if (value > 4 * resources.maxTransformFeedbackInterleavedComponents) {6120error(loc, "1/4 stride is too large:", id.c_str(), "gl_MaxTransformFeedbackInterleavedComponents is %d",6121resources.maxTransformFeedbackInterleavedComponents);6122}6123if (value >= (int)TQualifier::layoutXfbStrideEnd)6124error(loc, "stride is too large:", id.c_str(), "internal max is %d", TQualifier::layoutXfbStrideEnd-1);6125else6126publicType.qualifier.layoutXfbStride = value;6127if (nonLiteral)6128error(loc, "needs a literal integer", "xfb_stride", "");6129return;6130}6131}6132if (id == "input_attachment_index") {6133requireVulkan(loc, "input_attachment_index");6134if (value >= (int)TQualifier::layoutAttachmentEnd)6135error(loc, "attachment index is too large", id.c_str(), "");6136else6137publicType.qualifier.layoutAttachment = value;6138if (nonLiteral)6139error(loc, "needs a literal integer", "input_attachment_index", "");6140return;6141}6142if (id == "num_views") {6143requireExtensions(loc, Num_OVR_multiview_EXTs, OVR_multiview_EXTs, "num_views");6144publicType.shaderQualifiers.numViews = value;6145if (nonLiteral)6146error(loc, "needs a literal integer", "num_views", "");6147return;6148}6149if (language == EShLangVertex ||6150language == EShLangTessControl ||6151language == EShLangTessEvaluation ||6152language == EShLangGeometry) {6153if (id == "secondary_view_offset") {6154requireExtensions(loc, 1, &E_GL_NV_stereo_view_rendering, "stereo view rendering");6155publicType.qualifier.layoutSecondaryViewportRelativeOffset = value;6156if (nonLiteral)6157error(loc, "needs a literal integer", "secondary_view_offset", "");6158return;6159}6160}61616162if (id == "buffer_reference_align") {6163requireExtensions(loc, 1, &E_GL_EXT_buffer_reference, "buffer_reference_align");6164if (! IsPow2(value))6165error(loc, "must be a power of 2", "buffer_reference_align", "");6166else6167publicType.qualifier.layoutBufferReferenceAlign = IntLog2(value);6168if (nonLiteral)6169error(loc, "needs a literal integer", "buffer_reference_align", "");6170return;6171}61726173switch (language) {6174case EShLangTessControl:6175if (id == "vertices") {6176if (value == 0)6177error(loc, "must be greater than 0", "vertices", "");6178else6179publicType.shaderQualifiers.vertices = value;6180if (nonLiteral)6181error(loc, "needs a literal integer", "vertices", "");6182return;6183}6184break;61856186case EShLangGeometry:6187if (id == "invocations") {6188profileRequires(loc, ECompatibilityProfile | ECoreProfile, 400, nullptr, "invocations");6189if (value == 0)6190error(loc, "must be at least 1", "invocations", "");6191else6192publicType.shaderQualifiers.invocations = value;6193if (nonLiteral)6194error(loc, "needs a literal integer", "invocations", "");6195return;6196}6197if (id == "max_vertices") {6198publicType.shaderQualifiers.vertices = value;6199if (value > resources.maxGeometryOutputVertices)6200error(loc, "too large, must be less than gl_MaxGeometryOutputVertices", "max_vertices", "");6201if (nonLiteral)6202error(loc, "needs a literal integer", "max_vertices", "");6203return;6204}6205if (id == "stream") {6206requireProfile(loc, ~EEsProfile, "selecting output stream");6207publicType.qualifier.layoutStream = value;6208if (value > 0)6209intermediate.setMultiStream();6210if (nonLiteral)6211error(loc, "needs a literal integer", "stream", "");6212return;6213}6214break;62156216case EShLangFragment:6217if (id == "index") {6218requireProfile(loc, ECompatibilityProfile | ECoreProfile | EEsProfile, "index layout qualifier on fragment output");6219const char* exts[2] = { E_GL_ARB_separate_shader_objects, E_GL_ARB_explicit_attrib_location };6220profileRequires(loc, ECompatibilityProfile | ECoreProfile, 330, 2, exts, "index layout qualifier on fragment output");6221profileRequires(loc, EEsProfile ,310, E_GL_EXT_blend_func_extended, "index layout qualifier on fragment output");6222// "It is also a compile-time error if a fragment shader sets a layout index to less than 0 or greater than 1."6223if (value < 0 || value > 1) {6224value = 0;6225error(loc, "value must be 0 or 1", "index", "");6226}62276228publicType.qualifier.layoutIndex = value;6229if (nonLiteral)6230error(loc, "needs a literal integer", "index", "");6231return;6232}6233break;62346235case EShLangMesh:6236if (id == "max_vertices") {6237requireExtensions(loc, Num_AEP_mesh_shader, AEP_mesh_shader, "max_vertices");6238publicType.shaderQualifiers.vertices = value;6239int max = extensionTurnedOn(E_GL_EXT_mesh_shader) ? resources.maxMeshOutputVerticesEXT6240: resources.maxMeshOutputVerticesNV;6241if (value > max) {6242TString maxsErrtring = "too large, must be less than ";6243maxsErrtring.append(extensionTurnedOn(E_GL_EXT_mesh_shader) ? "gl_MaxMeshOutputVerticesEXT"6244: "gl_MaxMeshOutputVerticesNV");6245error(loc, maxsErrtring.c_str(), "max_vertices", "");6246}6247if (nonLiteral)6248error(loc, "needs a literal integer", "max_vertices", "");6249return;6250}6251if (id == "max_primitives") {6252requireExtensions(loc, Num_AEP_mesh_shader, AEP_mesh_shader, "max_primitives");6253publicType.shaderQualifiers.primitives = value;6254int max = extensionTurnedOn(E_GL_EXT_mesh_shader) ? resources.maxMeshOutputPrimitivesEXT6255: resources.maxMeshOutputPrimitivesNV;6256if (value > max) {6257TString maxsErrtring = "too large, must be less than ";6258maxsErrtring.append(extensionTurnedOn(E_GL_EXT_mesh_shader) ? "gl_MaxMeshOutputPrimitivesEXT"6259: "gl_MaxMeshOutputPrimitivesNV");6260error(loc, maxsErrtring.c_str(), "max_primitives", "");6261}6262if (nonLiteral)6263error(loc, "needs a literal integer", "max_primitives", "");6264return;6265}6266[[fallthrough]];62676268case EShLangTask:6269// Fall through6270case EShLangCompute:6271if (id.compare(0, 11, "local_size_") == 0) {6272if (language == EShLangMesh || language == EShLangTask) {6273requireExtensions(loc, Num_AEP_mesh_shader, AEP_mesh_shader, "gl_WorkGroupSize");6274} else {6275profileRequires(loc, EEsProfile, 310, nullptr, "gl_WorkGroupSize");6276profileRequires(loc, ~EEsProfile, 430, E_GL_ARB_compute_shader, "gl_WorkGroupSize");6277}6278if (nonLiteral)6279error(loc, "needs a literal integer", "local_size", "");6280if (id.size() == 12 && value == 0) {6281error(loc, "must be at least 1", id.c_str(), "");6282return;6283}6284if (id == "local_size_x") {6285publicType.shaderQualifiers.localSize[0] = value;6286publicType.shaderQualifiers.localSizeNotDefault[0] = true;6287return;6288}6289if (id == "local_size_y") {6290publicType.shaderQualifiers.localSize[1] = value;6291publicType.shaderQualifiers.localSizeNotDefault[1] = true;6292return;6293}6294if (id == "local_size_z") {6295publicType.shaderQualifiers.localSize[2] = value;6296publicType.shaderQualifiers.localSizeNotDefault[2] = true;6297return;6298}6299if (spvVersion.spv != 0) {6300if (id == "local_size_x_id") {6301publicType.shaderQualifiers.localSizeSpecId[0] = value;6302return;6303}6304if (id == "local_size_y_id") {6305publicType.shaderQualifiers.localSizeSpecId[1] = value;6306return;6307}6308if (id == "local_size_z_id") {6309publicType.shaderQualifiers.localSizeSpecId[2] = value;6310return;6311}6312}6313}6314break;63156316default:6317break;6318}63196320error(loc, "there is no such layout identifier for this stage taking an assigned value", id.c_str(), "");6321}63226323// Merge any layout qualifier information from src into dst, leaving everything else in dst alone6324//6325// "More than one layout qualifier may appear in a single declaration.6326// Additionally, the same layout-qualifier-name can occur multiple times6327// within a layout qualifier or across multiple layout qualifiers in the6328// same declaration. When the same layout-qualifier-name occurs6329// multiple times, in a single declaration, the last occurrence overrides6330// the former occurrence(s). Further, if such a layout-qualifier-name6331// will effect subsequent declarations or other observable behavior, it6332// is only the last occurrence that will have any effect, behaving as if6333// the earlier occurrence(s) within the declaration are not present.6334// This is also true for overriding layout-qualifier-names, where one6335// overrides the other (e.g., row_major vs. column_major); only the last6336// occurrence has any effect."6337void TParseContext::mergeObjectLayoutQualifiers(TQualifier& dst, const TQualifier& src, bool inheritOnly)6338{6339if (src.hasMatrix())6340dst.layoutMatrix = src.layoutMatrix;6341if (src.hasPacking())6342dst.layoutPacking = src.layoutPacking;63436344if (src.hasStream())6345dst.layoutStream = src.layoutStream;6346if (src.hasFormat())6347dst.layoutFormat = src.layoutFormat;6348if (src.hasXfbBuffer())6349dst.layoutXfbBuffer = src.layoutXfbBuffer;6350if (src.hasBufferReferenceAlign())6351dst.layoutBufferReferenceAlign = src.layoutBufferReferenceAlign;63526353if (src.hasAlign())6354dst.layoutAlign = src.layoutAlign;63556356if (! inheritOnly) {6357if (src.hasLocation())6358dst.layoutLocation = src.layoutLocation;6359if (src.hasOffset())6360dst.layoutOffset = src.layoutOffset;6361if (src.hasSet())6362dst.layoutSet = src.layoutSet;6363if (src.layoutBinding != TQualifier::layoutBindingEnd)6364dst.layoutBinding = src.layoutBinding;63656366if (src.hasSpecConstantId())6367dst.layoutSpecConstantId = src.layoutSpecConstantId;63686369if (src.hasComponent())6370dst.layoutComponent = src.layoutComponent;6371if (src.hasIndex())6372dst.layoutIndex = src.layoutIndex;6373if (src.hasXfbStride())6374dst.layoutXfbStride = src.layoutXfbStride;6375if (src.hasXfbOffset())6376dst.layoutXfbOffset = src.layoutXfbOffset;6377if (src.hasAttachment())6378dst.layoutAttachment = src.layoutAttachment;6379if (src.layoutPushConstant)6380dst.layoutPushConstant = true;63816382if (src.layoutBufferReference)6383dst.layoutBufferReference = true;63846385if (src.layoutPassthrough)6386dst.layoutPassthrough = true;6387if (src.layoutViewportRelative)6388dst.layoutViewportRelative = true;6389if (src.layoutSecondaryViewportRelativeOffset != -2048)6390dst.layoutSecondaryViewportRelativeOffset = src.layoutSecondaryViewportRelativeOffset;6391if (src.layoutShaderRecord)6392dst.layoutShaderRecord = true;6393if (src.layoutFullQuads)6394dst.layoutFullQuads = true;6395if (src.layoutQuadDeriv)6396dst.layoutQuadDeriv = true;6397if (src.layoutBindlessSampler)6398dst.layoutBindlessSampler = true;6399if (src.layoutBindlessImage)6400dst.layoutBindlessImage = true;6401if (src.pervertexNV)6402dst.pervertexNV = true;6403if (src.pervertexEXT)6404dst.pervertexEXT = true;6405if (src.layoutHitObjectShaderRecordNV)6406dst.layoutHitObjectShaderRecordNV = true;6407}6408}64096410// Do error layout error checking given a full variable/block declaration.6411void TParseContext::layoutObjectCheck(const TSourceLoc& loc, const TSymbol& symbol)6412{6413const TType& type = symbol.getType();6414const TQualifier& qualifier = type.getQualifier();64156416// first, cross check WRT to just the type6417layoutTypeCheck(loc, type);64186419// now, any remaining error checking based on the object itself64206421if (qualifier.hasAnyLocation()) {6422switch (qualifier.storage) {6423case EvqUniform:6424case EvqBuffer:6425if (symbol.getAsVariable() == nullptr)6426error(loc, "can only be used on variable declaration", "location", "");6427break;6428default:6429break;6430}6431}64326433// user-variable location check, which are required for SPIR-V in/out:6434// - variables have it directly,6435// - blocks have it on each member (already enforced), so check first one6436if (spvVersion.spv > 0 && !parsingBuiltins && qualifier.builtIn == EbvNone &&6437!qualifier.hasLocation() && !intermediate.getAutoMapLocations()) {64386439switch (qualifier.storage) {6440case EvqVaryingIn:6441case EvqVaryingOut:6442if (!type.getQualifier().isTaskMemory() && !type.getQualifier().hasSpirvDecorate() &&6443(type.getBasicType() != EbtBlock ||6444(!(*type.getStruct())[0].type->getQualifier().hasLocation() &&6445(*type.getStruct())[0].type->getQualifier().builtIn == EbvNone)))6446error(loc, "SPIR-V requires location for user input/output", "location", "");6447break;6448default:6449break;6450}6451}64526453// Check packing and matrix6454if (qualifier.hasUniformLayout()) {6455switch (qualifier.storage) {6456case EvqUniform:6457case EvqBuffer:6458if (type.getBasicType() != EbtBlock) {6459if (qualifier.hasMatrix())6460error(loc, "cannot specify matrix layout on a variable declaration", "layout", "");6461if (qualifier.hasPacking())6462error(loc, "cannot specify packing on a variable declaration", "layout", "");6463// "The offset qualifier can only be used on block members of blocks..."6464if (qualifier.hasOffset() && !type.isAtomic())6465error(loc, "cannot specify on a variable declaration", "offset", "");6466// "The align qualifier can only be used on blocks or block members..."6467if (qualifier.hasAlign())6468error(loc, "cannot specify on a variable declaration", "align", "");6469if (qualifier.isPushConstant())6470error(loc, "can only specify on a uniform block", "push_constant", "");6471if (qualifier.isShaderRecord())6472error(loc, "can only specify on a buffer block", "shaderRecordNV", "");6473if (qualifier.hasLocation() && type.isAtomic())6474error(loc, "cannot specify on atomic counter", "location", "");6475}6476break;6477default:6478// these were already filtered by layoutTypeCheck() (or its callees)6479break;6480}6481}6482}64836484// "For some blocks declared as arrays, the location can only be applied at the block level:6485// When a block is declared as an array where additional locations are needed for each member6486// for each block array element, it is a compile-time error to specify locations on the block6487// members. That is, when locations would be under specified by applying them on block members,6488// they are not allowed on block members. For arrayed interfaces (those generally having an6489// extra level of arrayness due to interface expansion), the outer array is stripped before6490// applying this rule."6491void TParseContext::layoutMemberLocationArrayCheck(const TSourceLoc& loc, bool memberWithLocation,6492TArraySizes* arraySizes)6493{6494if (memberWithLocation && arraySizes != nullptr) {6495if (arraySizes->getNumDims() > (currentBlockQualifier.isArrayedIo(language) ? 1 : 0))6496error(loc, "cannot use in a block array where new locations are needed for each block element",6497"location", "");6498}6499}65006501// Do layout error checking with respect to a type.6502void TParseContext::layoutTypeCheck(const TSourceLoc& loc, const TType& type)6503{6504const TQualifier& qualifier = type.getQualifier();65056506// first, intra-layout qualifier-only error checking6507layoutQualifierCheck(loc, qualifier);65086509// now, error checking combining type and qualifier65106511if (qualifier.hasAnyLocation()) {6512if (qualifier.hasLocation()) {6513if (qualifier.storage == EvqVaryingOut && language == EShLangFragment) {6514if (qualifier.layoutLocation >= (unsigned int)resources.maxDrawBuffers)6515error(loc, "too large for fragment output", "location", "");6516}6517}6518if (qualifier.hasComponent()) {6519// "It is a compile-time error if this sequence of components gets larger than 3."6520if (qualifier.layoutComponent + type.getVectorSize() * (type.getBasicType() == EbtDouble ? 2 : 1) > 4)6521error(loc, "type overflows the available 4 components", "component", "");65226523// "It is a compile-time error to apply the component qualifier to a matrix, a structure, a block, or an array containing any of these."6524if (type.isMatrix() || type.getBasicType() == EbtBlock || type.getBasicType() == EbtStruct)6525error(loc, "cannot apply to a matrix, structure, or block", "component", "");65266527// " It is a compile-time error to use component 1 or 3 as the beginning of a double or dvec2."6528if (type.getBasicType() == EbtDouble)6529if (qualifier.layoutComponent & 1)6530error(loc, "doubles cannot start on an odd-numbered component", "component", "");6531}65326533switch (qualifier.storage) {6534case EvqVaryingIn:6535case EvqVaryingOut:6536if (type.getBasicType() == EbtBlock)6537profileRequires(loc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, "location qualifier on in/out block");6538if (type.getQualifier().isTaskMemory())6539error(loc, "cannot apply to taskNV in/out blocks", "location", "");6540break;6541case EvqUniform:6542case EvqBuffer:6543if (type.getBasicType() == EbtBlock)6544error(loc, "cannot apply to uniform or buffer block", "location", "");6545else if (type.getBasicType() == EbtSampler && type.getSampler().isAttachmentEXT())6546error(loc, "only applies to", "location", "%s with storage tileImageEXT", type.getBasicTypeString().c_str());6547break;6548case EvqtaskPayloadSharedEXT:6549error(loc, "cannot apply to taskPayloadSharedEXT", "location", "");6550break;6551case EvqPayload:6552case EvqPayloadIn:6553case EvqHitAttr:6554case EvqCallableData:6555case EvqCallableDataIn:6556case EvqHitObjectAttrNV:6557case EvqSpirvStorageClass:6558break;6559case EvqTileImageEXT:6560break;6561default:6562error(loc, "can only apply to uniform, buffer, in, or out storage qualifiers", "location", "");6563break;6564}65656566bool typeCollision;6567int repeated = intermediate.addUsedLocation(qualifier, type, typeCollision);6568if (repeated >= 0 && ! typeCollision)6569error(loc, "overlapping use of location", "location", "%d", repeated);6570// When location aliasing, the aliases sharing the location must have the same underlying numerical type and bit width(6571// floating - point or integer, 32 - bit versus 64 - bit,etc.)6572if (typeCollision && (qualifier.isPipeInput() || qualifier.isPipeOutput() || qualifier.storage == EvqTileImageEXT))6573error(loc, "the aliases sharing the location", "location", "%d must be the same basic type and interpolation qualification", repeated);6574}65756576if (qualifier.hasXfbOffset() && qualifier.hasXfbBuffer()) {6577if (type.isUnsizedArray()) {6578error(loc, "unsized array", "xfb_offset", "in buffer %d", qualifier.layoutXfbBuffer);6579} else {6580int repeated = intermediate.addXfbBufferOffset(type);6581if (repeated >= 0)6582error(loc, "overlapping offsets at", "xfb_offset", "offset %d in buffer %d", repeated, qualifier.layoutXfbBuffer);6583}65846585// "The offset must be a multiple of the size of the first component of the first6586// qualified variable or block member, or a compile-time error results. Further, if applied to an aggregate6587// containing a double or 64-bit integer, the offset must also be a multiple of 8..."6588if ((type.containsBasicType(EbtDouble) || type.containsBasicType(EbtInt64) || type.containsBasicType(EbtUint64)) &&6589! IsMultipleOfPow2(qualifier.layoutXfbOffset, 8))6590error(loc, "type contains double or 64-bit integer; xfb_offset must be a multiple of 8", "xfb_offset", "");6591else if ((type.containsBasicType(EbtBool) || type.containsBasicType(EbtFloat) ||6592type.containsBasicType(EbtInt) || type.containsBasicType(EbtUint)) &&6593! IsMultipleOfPow2(qualifier.layoutXfbOffset, 4))6594error(loc, "must be a multiple of size of first component", "xfb_offset", "");6595// ..., if applied to an aggregate containing a half float or 16-bit integer, the offset must also be a multiple of 2..."6596else if ((type.contains16BitFloat() || type.containsBasicType(EbtInt16) || type.containsBasicType(EbtUint16)) &&6597!IsMultipleOfPow2(qualifier.layoutXfbOffset, 2))6598error(loc, "type contains half float or 16-bit integer; xfb_offset must be a multiple of 2", "xfb_offset", "");6599}6600if (qualifier.hasXfbStride() && qualifier.hasXfbBuffer()) {6601if (! intermediate.setXfbBufferStride(qualifier.layoutXfbBuffer, qualifier.layoutXfbStride))6602error(loc, "all stride settings must match for xfb buffer", "xfb_stride", "%d", qualifier.layoutXfbBuffer);6603}66046605if (qualifier.hasBinding()) {6606// Binding checking, from the spec:6607//6608// "If the binding point for any uniform or shader storage block instance is less than zero, or greater than or6609// equal to the implementation-dependent maximum number of uniform buffer bindings, a compile-time6610// error will occur. When the binding identifier is used with a uniform or shader storage block instanced as6611// an array of size N, all elements of the array from binding through binding + N - 1 must be within this6612// range."6613//6614if (!type.isOpaque() && type.getBasicType() != EbtBlock && type.getBasicType() != EbtSpirvType)6615error(loc, "requires block, or sampler/image, or atomic-counter type", "binding", "");6616if (type.getBasicType() == EbtSampler) {6617int lastBinding = qualifier.layoutBinding;6618if (type.isArray()) {6619if (spvVersion.vulkan == 0) {6620if (type.isSizedArray())6621lastBinding += (type.getCumulativeArraySize() - 1);6622else {6623warn(loc, "assuming binding count of one for compile-time checking of binding numbers for unsized array", "[]", "");6624}6625}6626}6627if (spvVersion.vulkan == 0 && lastBinding >= resources.maxCombinedTextureImageUnits)6628error(loc, "sampler binding not less than gl_MaxCombinedTextureImageUnits", "binding", type.isArray() ? "(using array)" : "");6629}6630if (type.isAtomic() && !spvVersion.vulkanRelaxed) {6631if (qualifier.layoutBinding >= (unsigned int)resources.maxAtomicCounterBindings) {6632error(loc, "atomic_uint binding is too large; see gl_MaxAtomicCounterBindings", "binding", "");6633return;6634}6635}6636} else if (!intermediate.getAutoMapBindings()) {6637// some types require bindings66386639// atomic_uint6640if (type.isAtomic())6641error(loc, "layout(binding=X) is required", "atomic_uint", "");66426643// SPIR-V6644if (spvVersion.spv > 0) {6645if (qualifier.isUniformOrBuffer()) {6646if (type.getBasicType() == EbtBlock && !qualifier.isPushConstant() &&6647!qualifier.isShaderRecord() &&6648!qualifier.hasAttachment() &&6649!qualifier.hasBufferReference())6650error(loc, "uniform/buffer blocks require layout(binding=X)", "binding", "");6651else if (spvVersion.vulkan > 0 && type.getBasicType() == EbtSampler && !type.getSampler().isAttachmentEXT())6652error(loc, "sampler/texture/image requires layout(binding=X)", "binding", "");6653}6654}6655}66566657// some things can't have arrays of arrays6658if (type.isArrayOfArrays()) {6659if (spvVersion.vulkan > 0) {6660if (type.isOpaque() || (type.getQualifier().isUniformOrBuffer() && type.getBasicType() == EbtBlock))6661warn(loc, "Generating SPIR-V array-of-arrays, but Vulkan only supports single array level for this resource", "[][]", "");6662}6663}66646665// "The offset qualifier can only be used on block members of blocks..."6666if (qualifier.hasOffset()) {6667if (type.getBasicType() == EbtBlock)6668error(loc, "only applies to block members, not blocks", "offset", "");6669}66706671// Image format6672if (qualifier.hasFormat()) {6673if (! type.isImage() && !intermediate.getBindlessImageMode())6674error(loc, "only apply to images", TQualifier::getLayoutFormatString(qualifier.getFormat()), "");6675else {6676if (type.getSampler().type == EbtFloat && qualifier.getFormat() > ElfFloatGuard)6677error(loc, "does not apply to floating point images", TQualifier::getLayoutFormatString(qualifier.getFormat()), "");6678if (type.getSampler().type == EbtInt && (qualifier.getFormat() < ElfFloatGuard || qualifier.getFormat() > ElfIntGuard))6679error(loc, "does not apply to signed integer images", TQualifier::getLayoutFormatString(qualifier.getFormat()), "");6680if (type.getSampler().type == EbtUint && qualifier.getFormat() < ElfIntGuard)6681error(loc, "does not apply to unsigned integer images", TQualifier::getLayoutFormatString(qualifier.getFormat()), "");66826683if (isEsProfile()) {6684// "Except for image variables qualified with the format qualifiers r32f, r32i, and r32ui, image variables must6685// specify either memory qualifier readonly or the memory qualifier writeonly."6686if (! (qualifier.getFormat() == ElfR32f || qualifier.getFormat() == ElfR32i || qualifier.getFormat() == ElfR32ui)) {6687if (! qualifier.isReadOnly() && ! qualifier.isWriteOnly())6688error(loc, "format requires readonly or writeonly memory qualifier", TQualifier::getLayoutFormatString(qualifier.getFormat()), "");6689}6690}6691}6692} else if (type.isImage() && ! qualifier.isWriteOnly() && !intermediate.getBindlessImageMode()) {6693const char *explanation = "image variables not declared 'writeonly' and without a format layout qualifier";6694requireProfile(loc, ECoreProfile | ECompatibilityProfile, explanation);6695profileRequires(loc, ECoreProfile | ECompatibilityProfile, 0, E_GL_EXT_shader_image_load_formatted, explanation);6696}66976698if (qualifier.isPushConstant()) {6699if (type.getBasicType() != EbtBlock)6700error(loc, "can only be used with a block", "push_constant", "");6701if (type.isArray())6702error(loc, "Push constants blocks can't be an array", "push_constant", "");6703}67046705if (qualifier.hasBufferReference() && type.getBasicType() != EbtBlock)6706error(loc, "can only be used with a block", "buffer_reference", "");67076708if (qualifier.isShaderRecord() && type.getBasicType() != EbtBlock)6709error(loc, "can only be used with a block", "shaderRecordNV", "");67106711// input attachment6712if (type.isSubpass()) {6713if (extensionTurnedOn(E_GL_EXT_shader_tile_image))6714error(loc, "can not be used with GL_EXT_shader_tile_image enabled", type.getSampler().getString().c_str(), "");6715if (! qualifier.hasAttachment())6716error(loc, "requires an input_attachment_index layout qualifier", "subpass", "");6717} else {6718if (qualifier.hasAttachment())6719error(loc, "can only be used with a subpass", "input_attachment_index", "");6720}67216722// specialization-constant id6723if (qualifier.hasSpecConstantId()) {6724if (type.getQualifier().storage != EvqConst)6725error(loc, "can only be applied to 'const'-qualified scalar", "constant_id", "");6726if (! type.isScalar())6727error(loc, "can only be applied to a scalar", "constant_id", "");6728switch (type.getBasicType())6729{6730case EbtInt8:6731case EbtUint8:6732case EbtInt16:6733case EbtUint16:6734case EbtInt:6735case EbtUint:6736case EbtInt64:6737case EbtUint64:6738case EbtBool:6739case EbtFloat:6740case EbtDouble:6741case EbtFloat16:6742break;6743default:6744error(loc, "cannot be applied to this type", "constant_id", "");6745break;6746}6747}6748}67496750static bool storageCanHaveLayoutInBlock(const enum TStorageQualifier storage)6751{6752switch (storage) {6753case EvqUniform:6754case EvqBuffer:6755case EvqShared:6756return true;6757default:6758return false;6759}6760}67616762// Do layout error checking that can be done within a layout qualifier proper, not needing to know6763// if there are blocks, atomic counters, variables, etc.6764void TParseContext::layoutQualifierCheck(const TSourceLoc& loc, const TQualifier& qualifier)6765{6766if (qualifier.storage == EvqShared && qualifier.hasLayout()) {6767if (spvVersion.spv > 0 && spvVersion.spv < EShTargetSpv_1_4) {6768error(loc, "shared block requires at least SPIR-V 1.4", "shared block", "");6769}6770profileRequires(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, 0, E_GL_EXT_shared_memory_block, "shared block");6771}67726773// "It is a compile-time error to use *component* without also specifying the location qualifier (order does not matter)."6774if (qualifier.hasComponent() && ! qualifier.hasLocation())6775error(loc, "must specify 'location' to use 'component'", "component", "");67766777if (qualifier.hasAnyLocation()) {67786779// "As with input layout qualifiers, all shaders except compute shaders6780// allow *location* layout qualifiers on output variable declarations,6781// output block declarations, and output block member declarations."67826783switch (qualifier.storage) {6784case EvqVaryingIn:6785{6786const char* feature = "location qualifier on input";6787if (isEsProfile() && version < 310)6788requireStage(loc, EShLangVertex, feature);6789else6790requireStage(loc, (EShLanguageMask)~EShLangComputeMask, feature);6791if (language == EShLangVertex) {6792const char* exts[2] = { E_GL_ARB_separate_shader_objects, E_GL_ARB_explicit_attrib_location };6793profileRequires(loc, ~EEsProfile, 330, 2, exts, feature);6794profileRequires(loc, EEsProfile, 300, nullptr, feature);6795} else {6796profileRequires(loc, ~EEsProfile, 410, E_GL_ARB_separate_shader_objects, feature);6797profileRequires(loc, EEsProfile, 310, nullptr, feature);6798}6799break;6800}6801case EvqVaryingOut:6802{6803const char* feature = "location qualifier on output";6804if (isEsProfile() && version < 310)6805requireStage(loc, EShLangFragment, feature);6806else6807requireStage(loc, (EShLanguageMask)~EShLangComputeMask, feature);6808if (language == EShLangFragment) {6809const char* exts[2] = { E_GL_ARB_separate_shader_objects, E_GL_ARB_explicit_attrib_location };6810profileRequires(loc, ~EEsProfile, 330, 2, exts, feature);6811profileRequires(loc, EEsProfile, 300, nullptr, feature);6812} else {6813profileRequires(loc, ~EEsProfile, 410, E_GL_ARB_separate_shader_objects, feature);6814profileRequires(loc, EEsProfile, 310, nullptr, feature);6815}6816break;6817}6818case EvqUniform:6819case EvqBuffer:6820{6821const char* feature = "location qualifier on uniform or buffer";6822requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile | ENoProfile, feature);6823profileRequires(loc, ~EEsProfile, 330, E_GL_ARB_explicit_attrib_location, feature);6824profileRequires(loc, ~EEsProfile, 430, E_GL_ARB_explicit_uniform_location, feature);6825profileRequires(loc, EEsProfile, 310, nullptr, feature);6826break;6827}6828default:6829break;6830}6831if (qualifier.hasIndex()) {6832if (qualifier.storage != EvqVaryingOut)6833error(loc, "can only be used on an output", "index", "");6834if (! qualifier.hasLocation())6835error(loc, "can only be used with an explicit location", "index", "");6836}6837}68386839if (qualifier.hasBinding()) {6840if (! qualifier.isUniformOrBuffer() && !qualifier.isTaskMemory())6841error(loc, "requires uniform or buffer storage qualifier", "binding", "");6842}6843if (qualifier.hasStream()) {6844if (!qualifier.isPipeOutput())6845error(loc, "can only be used on an output", "stream", "");6846}6847if (qualifier.hasXfb()) {6848if (!qualifier.isPipeOutput())6849error(loc, "can only be used on an output", "xfb layout qualifier", "");6850}6851if (qualifier.hasUniformLayout()) {6852if (!storageCanHaveLayoutInBlock(qualifier.storage) && !qualifier.isTaskMemory()) {6853if (qualifier.hasMatrix() || qualifier.hasPacking())6854error(loc, "matrix or packing qualifiers can only be used on a uniform or buffer", "layout", "");6855if (qualifier.hasOffset() || qualifier.hasAlign())6856error(loc, "offset/align can only be used on a uniform or buffer", "layout", "");6857}6858}6859if (qualifier.isPushConstant()) {6860if (qualifier.storage != EvqUniform)6861error(loc, "can only be used with a uniform", "push_constant", "");6862if (qualifier.hasSet())6863error(loc, "cannot be used with push_constant", "set", "");6864if (qualifier.hasBinding())6865error(loc, "cannot be used with push_constant", "binding", "");6866}6867if (qualifier.hasBufferReference()) {6868if (qualifier.storage != EvqBuffer)6869error(loc, "can only be used with buffer", "buffer_reference", "");6870}6871if (qualifier.isShaderRecord()) {6872if (qualifier.storage != EvqBuffer)6873error(loc, "can only be used with a buffer", "shaderRecordNV", "");6874if (qualifier.hasBinding())6875error(loc, "cannot be used with shaderRecordNV", "binding", "");6876if (qualifier.hasSet())6877error(loc, "cannot be used with shaderRecordNV", "set", "");68786879}68806881if (qualifier.storage == EvqTileImageEXT) {6882if (qualifier.hasSet())6883error(loc, "cannot be used with tileImageEXT", "set", "");6884if (!qualifier.hasLocation())6885error(loc, "can only be used with an explicit location", "tileImageEXT", "");6886}68876888if (qualifier.storage == EvqHitAttr && qualifier.hasLayout()) {6889error(loc, "cannot apply layout qualifiers to hitAttributeNV variable", "hitAttributeNV", "");6890}6891}68926893// For places that can't have shader-level layout qualifiers6894void TParseContext::checkNoShaderLayouts(const TSourceLoc& loc, const TShaderQualifiers& shaderQualifiers)6895{6896const char* message = "can only apply to a standalone qualifier";68976898if (shaderQualifiers.geometry != ElgNone)6899error(loc, message, TQualifier::getGeometryString(shaderQualifiers.geometry), "");6900if (shaderQualifiers.spacing != EvsNone)6901error(loc, message, TQualifier::getVertexSpacingString(shaderQualifiers.spacing), "");6902if (shaderQualifiers.order != EvoNone)6903error(loc, message, TQualifier::getVertexOrderString(shaderQualifiers.order), "");6904if (shaderQualifiers.pointMode)6905error(loc, message, "point_mode", "");6906if (shaderQualifiers.invocations != TQualifier::layoutNotSet)6907error(loc, message, "invocations", "");6908for (int i = 0; i < 3; ++i) {6909if (shaderQualifiers.localSize[i] > 1)6910error(loc, message, "local_size", "");6911if (shaderQualifiers.localSizeSpecId[i] != TQualifier::layoutNotSet)6912error(loc, message, "local_size id", "");6913}6914if (shaderQualifiers.vertices != TQualifier::layoutNotSet) {6915if (language == EShLangGeometry || language == EShLangMesh)6916error(loc, message, "max_vertices", "");6917else if (language == EShLangTessControl)6918error(loc, message, "vertices", "");6919else6920assert(0);6921}6922if (shaderQualifiers.earlyFragmentTests)6923error(loc, message, "early_fragment_tests", "");6924if (shaderQualifiers.postDepthCoverage)6925error(loc, message, "post_depth_coverage", "");6926if (shaderQualifiers.nonCoherentColorAttachmentReadEXT)6927error(loc, message, "non_coherent_color_attachment_readEXT", "");6928if (shaderQualifiers.nonCoherentDepthAttachmentReadEXT)6929error(loc, message, "non_coherent_depth_attachment_readEXT", "");6930if (shaderQualifiers.nonCoherentStencilAttachmentReadEXT)6931error(loc, message, "non_coherent_stencil_attachment_readEXT", "");6932if (shaderQualifiers.primitives != TQualifier::layoutNotSet) {6933if (language == EShLangMesh)6934error(loc, message, "max_primitives", "");6935else6936assert(0);6937}6938if (shaderQualifiers.hasBlendEquation())6939error(loc, message, "blend equation", "");6940if (shaderQualifiers.numViews != TQualifier::layoutNotSet)6941error(loc, message, "num_views", "");6942if (shaderQualifiers.interlockOrdering != EioNone)6943error(loc, message, TQualifier::getInterlockOrderingString(shaderQualifiers.interlockOrdering), "");6944if (shaderQualifiers.layoutPrimitiveCulling)6945error(loc, "can only be applied as standalone", "primitive_culling", "");6946}69476948// Correct and/or advance an object's offset layout qualifier.6949void TParseContext::fixOffset(const TSourceLoc& loc, TSymbol& symbol)6950{6951const TQualifier& qualifier = symbol.getType().getQualifier();6952if (symbol.getType().isAtomic()) {6953if (qualifier.hasBinding() && (int)qualifier.layoutBinding < resources.maxAtomicCounterBindings) {69546955// Set the offset6956int offset;6957if (qualifier.hasOffset())6958offset = qualifier.layoutOffset;6959else6960offset = atomicUintOffsets[qualifier.layoutBinding];69616962if (offset % 4 != 0)6963error(loc, "atomic counters offset should align based on 4:", "offset", "%d", offset);69646965symbol.getWritableType().getQualifier().layoutOffset = offset;69666967// Check for overlap6968int numOffsets = 4;6969if (symbol.getType().isArray()) {6970if (symbol.getType().isSizedArray() && !symbol.getType().getArraySizes()->isInnerUnsized())6971numOffsets *= symbol.getType().getCumulativeArraySize();6972else {6973// "It is a compile-time error to declare an unsized array of atomic_uint."6974error(loc, "array must be explicitly sized", "atomic_uint", "");6975}6976}6977int repeated = intermediate.addUsedOffsets(qualifier.layoutBinding, offset, numOffsets);6978if (repeated >= 0)6979error(loc, "atomic counters sharing the same offset:", "offset", "%d", repeated);69806981// Bump the default offset6982atomicUintOffsets[qualifier.layoutBinding] = offset + numOffsets;6983}6984}6985}69866987//6988// Look up a function name in the symbol table, and make sure it is a function.6989//6990// Return the function symbol if found, otherwise nullptr.6991//6992const TFunction* TParseContext::findFunction(const TSourceLoc& loc, const TFunction& call, bool& builtIn)6993{6994if (symbolTable.isFunctionNameVariable(call.getName())) {6995error(loc, "can't use function syntax on variable", call.getName().c_str(), "");6996return nullptr;6997}69986999const TFunction* function = nullptr;70007001// debugPrintfEXT has var args and is in the symbol table as "debugPrintfEXT()",7002// mangled to "debugPrintfEXT("7003if (call.getName() == "debugPrintfEXT") {7004TSymbol* symbol = symbolTable.find("debugPrintfEXT(", &builtIn);7005if (symbol)7006return symbol->getAsFunction();7007}70087009bool explicitTypesEnabled = extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types) ||7010extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int8) ||7011extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int16) ||7012extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int32) ||7013extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_int64) ||7014extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float16) ||7015extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float32) ||7016extensionTurnedOn(E_GL_EXT_shader_explicit_arithmetic_types_float64);70177018if (isEsProfile())7019function = (explicitTypesEnabled && version >= 310)7020? findFunctionExplicitTypes(loc, call, builtIn)7021: ((extensionTurnedOn(E_GL_EXT_shader_implicit_conversions) && version >= 310)7022? findFunction120(loc, call, builtIn)7023: findFunctionExact(loc, call, builtIn));7024else if (version < 120)7025function = findFunctionExact(loc, call, builtIn);7026else if (version < 400) {7027bool needfindFunction400 = extensionTurnedOn(E_GL_ARB_gpu_shader_fp64) || extensionTurnedOn(E_GL_ARB_gpu_shader5);7028function = needfindFunction400 ? findFunction400(loc, call, builtIn) : findFunction120(loc, call, builtIn);7029}7030else if (explicitTypesEnabled)7031function = findFunctionExplicitTypes(loc, call, builtIn);7032else7033function = findFunction400(loc, call, builtIn);70347035return function;7036}70377038// Function finding algorithm for ES and desktop 110.7039const TFunction* TParseContext::findFunctionExact(const TSourceLoc& loc, const TFunction& call, bool& builtIn)7040{7041TSymbol* symbol = symbolTable.find(call.getMangledName(), &builtIn);7042if (symbol == nullptr) {7043error(loc, "no matching overloaded function found", call.getName().c_str(), "");70447045return nullptr;7046}70477048return symbol->getAsFunction();7049}70507051// Function finding algorithm for desktop versions 120 through 330.7052const TFunction* TParseContext::findFunction120(const TSourceLoc& loc, const TFunction& call, bool& builtIn)7053{7054// first, look for an exact match7055TSymbol* symbol = symbolTable.find(call.getMangledName(), &builtIn);7056if (symbol)7057return symbol->getAsFunction();70587059// exact match not found, look through a list of overloaded functions of the same name70607061// "If no exact match is found, then [implicit conversions] will be applied to find a match. Mismatched types7062// on input parameters (in or inout or default) must have a conversion from the calling argument type to the7063// formal parameter type. Mismatched types on output parameters (out or inout) must have a conversion7064// from the formal parameter type to the calling argument type. When argument conversions are used to find7065// a match, it is a semantic error if there are multiple ways to apply these conversions to make the call match7066// more than one function."70677068const TFunction* candidate = nullptr;7069TVector<const TFunction*> candidateList;7070symbolTable.findFunctionNameList(call.getMangledName(), candidateList, builtIn);70717072for (auto it = candidateList.begin(); it != candidateList.end(); ++it) {7073const TFunction& function = *(*it);70747075// to even be a potential match, number of arguments has to match7076if (call.getParamCount() != function.getParamCount())7077continue;70787079bool possibleMatch = true;7080for (int i = 0; i < function.getParamCount(); ++i) {7081// same types is easy7082if (*function[i].type == *call[i].type)7083continue;70847085// We have a mismatch in type, see if it is implicitly convertible70867087if (function[i].type->isArray() || call[i].type->isArray() ||7088! function[i].type->sameElementShape(*call[i].type))7089possibleMatch = false;7090else {7091// do direction-specific checks for conversion of basic type7092if (function[i].type->getQualifier().isParamInput()) {7093if (! intermediate.canImplicitlyPromote(call[i].type->getBasicType(), function[i].type->getBasicType()))7094possibleMatch = false;7095}7096if (function[i].type->getQualifier().isParamOutput()) {7097if (! intermediate.canImplicitlyPromote(function[i].type->getBasicType(), call[i].type->getBasicType()))7098possibleMatch = false;7099}7100}7101if (! possibleMatch)7102break;7103}7104if (possibleMatch) {7105if (candidate) {7106// our second match, meaning ambiguity7107error(loc, "ambiguous function signature match: multiple signatures match under implicit type conversion", call.getName().c_str(), "");7108} else7109candidate = &function;7110}7111}71127113if (candidate == nullptr)7114error(loc, "no matching overloaded function found", call.getName().c_str(), "");71157116return candidate;7117}71187119// Function finding algorithm for desktop version 400 and above.7120//7121// "When function calls are resolved, an exact type match for all the arguments7122// is sought. If an exact match is found, all other functions are ignored, and7123// the exact match is used. If no exact match is found, then the implicit7124// conversions in section 4.1.10 Implicit Conversions will be applied to find7125// a match. Mismatched types on input parameters (in or inout or default) must7126// have a conversion from the calling argument type to the formal parameter type.7127// Mismatched types on output parameters (out or inout) must have a conversion7128// from the formal parameter type to the calling argument type.7129//7130// "If implicit conversions can be used to find more than one matching function,7131// a single best-matching function is sought. To determine a best match, the7132// conversions between calling argument and formal parameter types are compared7133// for each function argument and pair of matching functions. After these7134// comparisons are performed, each pair of matching functions are compared.7135// A function declaration A is considered a better match than function7136// declaration B if7137//7138// * for at least one function argument, the conversion for that argument in A7139// is better than the corresponding conversion in B; and7140// * there is no function argument for which the conversion in B is better than7141// the corresponding conversion in A.7142//7143// "If a single function declaration is considered a better match than every7144// other matching function declaration, it will be used. Otherwise, a7145// compile-time semantic error for an ambiguous overloaded function call occurs.7146//7147// "To determine whether the conversion for a single argument in one match is7148// better than that for another match, the following rules are applied, in order:7149//7150// 1. An exact match is better than a match involving any implicit conversion.7151// 2. A match involving an implicit conversion from float to double is better7152// than a match involving any other implicit conversion.7153// 3. A match involving an implicit conversion from either int or uint to float7154// is better than a match involving an implicit conversion from either int7155// or uint to double.7156//7157// "If none of the rules above apply to a particular pair of conversions, neither7158// conversion is considered better than the other."7159//7160const TFunction* TParseContext::findFunction400(const TSourceLoc& loc, const TFunction& call, bool& builtIn)7161{7162// first, look for an exact match7163TSymbol* symbol = symbolTable.find(call.getMangledName(), &builtIn);7164if (symbol)7165return symbol->getAsFunction();71667167// no exact match, use the generic selector, parameterized by the GLSL rules71687169// create list of candidates to send7170TVector<const TFunction*> candidateList;7171symbolTable.findFunctionNameList(call.getMangledName(), candidateList, builtIn);71727173// can 'from' convert to 'to'?7174const auto convertible = [this,builtIn](const TType& from, const TType& to, TOperator, int) -> bool {7175if (from == to)7176return true;7177if (from.coopMatParameterOK(to))7178return true;7179// Allow a sized array to be passed through an unsized array parameter, for coopMatLoad/Store functions7180if (builtIn && from.isArray() && to.isUnsizedArray()) {7181TType fromElementType(from, 0);7182TType toElementType(to, 0);7183if (fromElementType == toElementType)7184return true;7185}7186if (from.isArray() || to.isArray() || ! from.sameElementShape(to))7187return false;7188if (from.isCoopMat() && to.isCoopMat())7189return from.sameCoopMatBaseType(to);7190return intermediate.canImplicitlyPromote(from.getBasicType(), to.getBasicType());7191};71927193// Is 'to2' a better conversion than 'to1'?7194// Ties should not be considered as better.7195// Assumes 'convertible' already said true.7196const auto better = [](const TType& from, const TType& to1, const TType& to2) -> bool {7197// 1. exact match7198if (from == to2)7199return from != to1;7200if (from == to1)7201return false;72027203// 2. float -> double is better7204if (from.getBasicType() == EbtFloat) {7205if (to2.getBasicType() == EbtDouble && to1.getBasicType() != EbtDouble)7206return true;7207}72087209// 3. -> float is better than -> double7210return to2.getBasicType() == EbtFloat && to1.getBasicType() == EbtDouble;7211};72127213// for ambiguity reporting7214bool tie = false;72157216// send to the generic selector7217const TFunction* bestMatch = selectFunction(candidateList, call, convertible, better, tie);72187219if (bestMatch == nullptr)7220error(loc, "no matching overloaded function found", call.getName().c_str(), "");7221else if (tie)7222error(loc, "ambiguous best function under implicit type conversion", call.getName().c_str(), "");72237224return bestMatch;7225}72267227// "To determine whether the conversion for a single argument in one match7228// is better than that for another match, the conversion is assigned of the7229// three ranks ordered from best to worst:7230// 1. Exact match: no conversion.7231// 2. Promotion: integral or floating-point promotion.7232// 3. Conversion: integral conversion, floating-point conversion,7233// floating-integral conversion.7234// A conversion C1 is better than a conversion C2 if the rank of C1 is7235// better than the rank of C2."7236const TFunction* TParseContext::findFunctionExplicitTypes(const TSourceLoc& loc, const TFunction& call, bool& builtIn)7237{7238// first, look for an exact match7239TSymbol* symbol = symbolTable.find(call.getMangledName(), &builtIn);7240if (symbol)7241return symbol->getAsFunction();72427243// no exact match, use the generic selector, parameterized by the GLSL rules72447245// create list of candidates to send7246TVector<const TFunction*> candidateList;7247symbolTable.findFunctionNameList(call.getMangledName(), candidateList, builtIn);72487249// can 'from' convert to 'to'?7250const auto convertible = [this,builtIn](const TType& from, const TType& to, TOperator, int) -> bool {7251if (from == to)7252return true;7253if (from.coopMatParameterOK(to))7254return true;7255// Allow a sized array to be passed through an unsized array parameter, for coopMatLoad/Store functions7256if (builtIn && from.isArray() && to.isUnsizedArray()) {7257TType fromElementType(from, 0);7258TType toElementType(to, 0);7259if (fromElementType == toElementType)7260return true;7261}7262if (from.isArray() || to.isArray() || ! from.sameElementShape(to))7263return false;7264if (from.isCoopMat() && to.isCoopMat())7265return from.sameCoopMatBaseType(to);7266return intermediate.canImplicitlyPromote(from.getBasicType(), to.getBasicType());7267};72687269// Is 'to2' a better conversion than 'to1'?7270// Ties should not be considered as better.7271// Assumes 'convertible' already said true.7272const auto better = [this](const TType& from, const TType& to1, const TType& to2) -> bool {7273// 1. exact match7274if (from == to2)7275return from != to1;7276if (from == to1)7277return false;72787279// 2. Promotion (integral, floating-point) is better7280TBasicType from_type = from.getBasicType();7281TBasicType to1_type = to1.getBasicType();7282TBasicType to2_type = to2.getBasicType();7283bool isPromotion1 = (intermediate.isIntegralPromotion(from_type, to1_type) ||7284intermediate.isFPPromotion(from_type, to1_type));7285bool isPromotion2 = (intermediate.isIntegralPromotion(from_type, to2_type) ||7286intermediate.isFPPromotion(from_type, to2_type));7287if (isPromotion2)7288return !isPromotion1;7289if(isPromotion1)7290return false;72917292// 3. Conversion (integral, floating-point , floating-integral)7293bool isConversion1 = (intermediate.isIntegralConversion(from_type, to1_type) ||7294intermediate.isFPConversion(from_type, to1_type) ||7295intermediate.isFPIntegralConversion(from_type, to1_type));7296bool isConversion2 = (intermediate.isIntegralConversion(from_type, to2_type) ||7297intermediate.isFPConversion(from_type, to2_type) ||7298intermediate.isFPIntegralConversion(from_type, to2_type));72997300return isConversion2 && !isConversion1;7301};73027303// for ambiguity reporting7304bool tie = false;73057306// send to the generic selector7307const TFunction* bestMatch = selectFunction(candidateList, call, convertible, better, tie);73087309if (bestMatch == nullptr)7310error(loc, "no matching overloaded function found", call.getName().c_str(), "");7311else if (tie)7312error(loc, "ambiguous best function under implicit type conversion", call.getName().c_str(), "");73137314return bestMatch;7315}73167317//7318// Adjust function calls that aren't declared in Vulkan to a7319// calls with equivalent effects7320//7321TIntermTyped* TParseContext::vkRelaxedRemapFunctionCall(const TSourceLoc& loc, TFunction* function, TIntermNode* arguments)7322{7323TIntermTyped* result = nullptr;73247325if (function->getBuiltInOp() != EOpNull) {7326return nullptr;7327}73287329if (function->getName() == "atomicCounterIncrement") {7330// change atomicCounterIncrement into an atomicAdd of 17331TString name("atomicAdd");7332TType uintType(EbtUint);73337334TFunction realFunc(&name, function->getType());73357336// Use copyParam to avoid shared ownership of the 'type' field7337// of the parameter.7338for (int i = 0; i < function->getParamCount(); ++i) {7339realFunc.addParameter(TParameter().copyParam((*function)[i]));7340}73417342TParameter tmpP = { nullptr, &uintType, {} };7343realFunc.addParameter(TParameter().copyParam(tmpP));7344arguments = intermediate.growAggregate(arguments, intermediate.addConstantUnion(1, loc, true));73457346result = handleFunctionCall(loc, &realFunc, arguments);7347} else if (function->getName() == "atomicCounterDecrement") {7348// change atomicCounterDecrement into an atomicAdd with -17349// and subtract 1 from result, to return post-decrement value7350TString name("atomicAdd");7351TType uintType(EbtUint);73527353TFunction realFunc(&name, function->getType());73547355for (int i = 0; i < function->getParamCount(); ++i) {7356realFunc.addParameter(TParameter().copyParam((*function)[i]));7357}73587359TParameter tmpP = { nullptr, &uintType, {} };7360realFunc.addParameter(TParameter().copyParam(tmpP));7361arguments = intermediate.growAggregate(arguments, intermediate.addConstantUnion(-1, loc, true));73627363result = handleFunctionCall(loc, &realFunc, arguments);73647365// post decrement, so that it matches AtomicCounterDecrement semantics7366if (result) {7367result = handleBinaryMath(loc, "-", EOpSub, result, intermediate.addConstantUnion(1, loc, true));7368}7369} else if (function->getName() == "atomicCounter") {7370// change atomicCounter into a direct read of the variable7371if (arguments->getAsTyped()) {7372result = arguments->getAsTyped();7373}7374}73757376return result;7377}73787379// When a declaration includes a type, but not a variable name, it can be used7380// to establish defaults.7381void TParseContext::declareTypeDefaults(const TSourceLoc& loc, const TPublicType& publicType)7382{7383if (publicType.basicType == EbtAtomicUint && publicType.qualifier.hasBinding()) {7384if (publicType.qualifier.layoutBinding >= (unsigned int)resources.maxAtomicCounterBindings) {7385error(loc, "atomic_uint binding is too large", "binding", "");7386return;7387}7388if (publicType.qualifier.hasOffset())7389atomicUintOffsets[publicType.qualifier.layoutBinding] = publicType.qualifier.layoutOffset;7390return;7391}73927393if (publicType.arraySizes) {7394error(loc, "expect an array name", "", "");7395}73967397if (publicType.qualifier.hasLayout() && !publicType.qualifier.hasBufferReference())7398warn(loc, "useless application of layout qualifier", "layout", "");7399}74007401void TParseContext::coopMatTypeParametersCheck(const TSourceLoc& loc, const TPublicType& publicType)7402{7403if (parsingBuiltins)7404return;7405if (publicType.isCoopmatKHR()) {7406if (publicType.typeParameters == nullptr) {7407error(loc, "coopmat missing type parameters", "", "");7408return;7409}7410switch (publicType.typeParameters->basicType) {7411case EbtFloat:7412case EbtFloat16:7413case EbtInt:7414case EbtInt8:7415case EbtInt16:7416case EbtUint:7417case EbtUint8:7418case EbtUint16:7419case EbtSpirvType:7420break;7421default:7422error(loc, "coopmat invalid basic type", TType::getBasicString(publicType.typeParameters->basicType), "");7423break;7424}7425if (publicType.typeParameters->arraySizes->getNumDims() != 4) {7426error(loc, "coopmat incorrect number of type parameters", "", "");7427return;7428}7429int use = publicType.typeParameters->arraySizes->getDimSize(3);7430if (use < 0 || use > 2) {7431error(loc, "coopmat invalid matrix Use", "", "");7432return;7433}7434}7435}74367437bool TParseContext::vkRelaxedRemapUniformVariable(const TSourceLoc& loc, TString& identifier, const TPublicType& publicType,7438TArraySizes*, TIntermTyped* initializer, TType& type)7439{7440vkRelaxedRemapUniformMembers(loc, publicType, type, identifier);74417442if (parsingBuiltins || symbolTable.atBuiltInLevel() || !symbolTable.atGlobalLevel() ||7443type.getQualifier().storage != EvqUniform ||7444!(type.containsNonOpaque() || type.getBasicType() == EbtAtomicUint || (type.containsSampler() && type.isStruct()))) {7445return false;7446}74477448if (type.getQualifier().hasLocation()) {7449warn(loc, "ignoring layout qualifier for uniform", identifier.c_str(), "location");7450type.getQualifier().layoutLocation = TQualifier::layoutLocationEnd;7451}74527453if (initializer) {7454warn(loc, "Ignoring initializer for uniform", identifier.c_str(), "");7455initializer = nullptr;7456}74577458if (type.isArray()) {7459// do array size checks here7460arraySizesCheck(loc, type.getQualifier(), type.getArraySizes(), initializer, false);74617462if (arrayQualifierError(loc, type.getQualifier()) || arrayError(loc, type)) {7463error(loc, "array param error", identifier.c_str(), "");7464}7465}74667467// do some checking on the type as it was declared7468layoutTypeCheck(loc, type);74697470int bufferBinding = TQualifier::layoutBindingEnd;7471TVariable* updatedBlock = nullptr;74727473// Convert atomic_uint into members of a buffer block7474if (type.isAtomic()) {7475type.setBasicType(EbtUint);7476type.getQualifier().storage = EvqBuffer;74777478type.getQualifier().volatil = true;7479type.getQualifier().coherent = true;74807481// xxTODO: use logic from fixOffset() to apply explicit member offset7482bufferBinding = type.getQualifier().layoutBinding;7483type.getQualifier().layoutBinding = TQualifier::layoutBindingEnd;7484type.getQualifier().explicitOffset = false;7485growAtomicCounterBlock(bufferBinding, loc, type, identifier, nullptr);7486updatedBlock = atomicCounterBuffers[bufferBinding];7487}74887489if (!updatedBlock) {7490growGlobalUniformBlock(loc, type, identifier, nullptr);7491updatedBlock = globalUniformBlock;7492}74937494//7495// don't assign explicit member offsets here7496// if any are assigned, need to be updated here and in the merge/link step7497// fixBlockUniformOffsets(updatedBlock->getWritableType().getQualifier(), *updatedBlock->getWritableType().getWritableStruct());74987499// checks on update buffer object7500layoutObjectCheck(loc, *updatedBlock);75017502TSymbol* symbol = symbolTable.find(identifier);75037504if (!symbol) {7505if (updatedBlock == globalUniformBlock)7506error(loc, "error adding uniform to default uniform block", identifier.c_str(), "");7507else7508error(loc, "error adding atomic counter to atomic counter block", identifier.c_str(), "");7509return false;7510}75117512// merge qualifiers7513mergeObjectLayoutQualifiers(updatedBlock->getWritableType().getQualifier(), type.getQualifier(), true);75147515return true;7516}75177518template <typename Function>7519static void ForEachOpaque(const TType& type, const TString& path, Function callback)7520{7521auto recursion = [&callback](const TType& type, const TString& path, bool skipArray, auto& recursion) -> void {7522if (!skipArray && type.isArray())7523{7524std::vector<int> indices(type.getArraySizes()->getNumDims());7525for (int flatIndex = 0;7526flatIndex < type.getArraySizes()->getCumulativeSize();7527++flatIndex)7528{7529TString subscriptPath = path;7530for (size_t dimIndex = 0; dimIndex < indices.size(); ++dimIndex)7531{7532int index = indices[dimIndex];7533subscriptPath.append("[");7534subscriptPath.append(String(index));7535subscriptPath.append("]");7536}75377538recursion(type, subscriptPath, true, recursion);75397540for (size_t dimIndex = 0; dimIndex < indices.size(); ++dimIndex)7541{7542++indices[dimIndex];7543if (indices[dimIndex] < type.getArraySizes()->getDimSize(dimIndex))7544break;7545else7546indices[dimIndex] = 0;7547}7548}7549}75507551else if (type.isStruct() && type.containsOpaque())7552{7553const TTypeList& types = *type.getStruct();7554for (const TTypeLoc& typeLoc : types)7555{7556TString nextPath = path;7557nextPath.append(".");7558nextPath.append(typeLoc.type->getFieldName());75597560recursion(*(typeLoc.type), nextPath, false, recursion);7561}7562}75637564else if (type.isOpaque())7565{7566callback(type, path);7567}7568};75697570recursion(type, path, false, recursion);7571}75727573void TParseContext::vkRelaxedRemapUniformMembers(const TSourceLoc& loc, const TPublicType& publicType, const TType& type,7574const TString& identifier)7575{7576if (!type.isStruct() || !type.containsOpaque())7577return;75787579ForEachOpaque(type, identifier,7580[&publicType, &loc, this](const TType& type, const TString& path) {7581TArraySizes arraySizes = {};7582if (type.getArraySizes()) arraySizes = *type.getArraySizes();7583TTypeParameters typeParameters = {};7584if (type.getTypeParameters()) typeParameters = *type.getTypeParameters();75857586TPublicType memberType{};7587memberType.basicType = type.getBasicType();7588memberType.sampler = type.getSampler();7589memberType.qualifier = type.getQualifier();7590memberType.vectorSize = type.getVectorSize();7591memberType.matrixCols = type.getMatrixCols();7592memberType.matrixRows = type.getMatrixRows();7593memberType.coopmatNV = type.isCoopMatNV();7594memberType.coopmatKHR = type.isCoopMatKHR();7595memberType.arraySizes = nullptr;7596memberType.userDef = nullptr;7597memberType.loc = loc;7598memberType.typeParameters = (type.getTypeParameters() ? &typeParameters : nullptr);7599memberType.spirvType = nullptr;76007601memberType.qualifier.storage = publicType.qualifier.storage;7602memberType.shaderQualifiers = publicType.shaderQualifiers;76037604TString& structMemberName = *NewPoolTString(path.c_str()); // A copy is required due to declareVariable() signature.7605declareVariable(loc, structMemberName, memberType, nullptr, nullptr);7606});7607}76087609void TParseContext::vkRelaxedRemapFunctionParameter(TFunction* function, TParameter& param, std::vector<int>* newParams)7610{7611function->addParameter(param);76127613if (!param.type->isStruct() || !param.type->containsOpaque())7614return;76157616ForEachOpaque(*param.type, (param.name ? *param.name : param.type->getFieldName()),7617[function, param, newParams](const TType& type, const TString& path) {7618TString* memberName = NewPoolTString(path.c_str());76197620TType* memberType = new TType();7621memberType->shallowCopy(type);7622memberType->getQualifier().storage = param.type->getQualifier().storage;7623memberType->clearArraySizes();76247625TParameter memberParam = {};7626memberParam.name = memberName;7627memberParam.type = memberType;7628memberParam.defaultValue = nullptr;7629function->addParameter(memberParam);7630if (newParams)7631newParams->push_back(function->getParamCount()-1);7632});7633}76347635//7636// Generates a valid GLSL dereferencing string for the input TIntermNode7637//7638struct AccessChainTraverser : public TIntermTraverser {7639AccessChainTraverser() : TIntermTraverser(false, false, true)7640{}76417642TString path = "";7643TStorageQualifier topLevelStorageQualifier = TStorageQualifier::EvqLast;76447645bool visitBinary(TVisit, TIntermBinary* binary) override {7646if (binary->getOp() == EOpIndexDirectStruct)7647{7648const TTypeList& members = *binary->getLeft()->getType().getStruct();7649const TTypeLoc& member =7650members[binary->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst()];7651TString memberName = member.type->getFieldName();76527653if (path != "")7654path.append(".");76557656path.append(memberName);7657}76587659if (binary->getOp() == EOpIndexDirect)7660{7661const TConstUnionArray& indices = binary->getRight()->getAsConstantUnion()->getConstArray();7662for (int index = 0; index < indices.size(); ++index)7663{7664path.append("[");7665path.append(String(indices[index].getIConst()));7666path.append("]");7667}7668}76697670return true;7671}76727673void visitSymbol(TIntermSymbol* symbol) override {7674if (symbol->getType().isOpaque())7675topLevelStorageQualifier = symbol->getQualifier().storage;7676if (!IsAnonymous(symbol->getName()))7677path.append(symbol->getName());7678}7679};76807681TIntermNode* TParseContext::vkRelaxedRemapFunctionArgument(const TSourceLoc& loc, TFunction* function, TIntermTyped* intermTyped)7682{7683AccessChainTraverser accessChainTraverser{};7684intermTyped->traverse(&accessChainTraverser);76857686if (accessChainTraverser.topLevelStorageQualifier == TStorageQualifier::EvqUniform)7687{7688TParameter param = { 0, new TType, {} };7689param.type->shallowCopy(intermTyped->getType());76907691function->addParameter(param);7692return intermTyped;7693}76947695TParameter param = { NewPoolTString(accessChainTraverser.path.c_str()), new TType, {} };7696param.type->shallowCopy(intermTyped->getType());76977698std::vector<int> newParams = {};7699vkRelaxedRemapFunctionParameter(function, param, &newParams);77007701if (intermTyped->getType().isOpaque())7702{7703TIntermNode* remappedArgument = intermTyped;7704{7705TIntermSymbol* intermSymbol = nullptr;7706TSymbol* symbol = symbolTable.find(*param.name);7707if (symbol && symbol->getAsVariable())7708intermSymbol = intermediate.addSymbol(*symbol->getAsVariable(), loc);7709else7710{7711TVariable* variable = new TVariable(param.name, *param.type);7712intermSymbol = intermediate.addSymbol(*variable, loc);7713}77147715remappedArgument = intermSymbol;7716}77177718return remappedArgument;7719}7720else if (!(intermTyped->isStruct() && intermTyped->getType().containsOpaque()))7721return intermTyped;7722else7723{7724TIntermNode* remappedArgument = intermTyped;7725{7726TSymbol* symbol = symbolTable.find(*param.name);7727if (symbol && symbol->getAsVariable())7728remappedArgument = intermediate.addSymbol(*symbol->getAsVariable(), loc);7729}77307731if (!newParams.empty())7732remappedArgument = intermediate.makeAggregate(remappedArgument, loc);77337734for (int paramIndex : newParams)7735{7736TParameter& newParam = function->operator[](paramIndex);7737TIntermSymbol* intermSymbol = nullptr;7738TSymbol* symbol = symbolTable.find(*newParam.name);7739if (symbol && symbol->getAsVariable())7740intermSymbol = intermediate.addSymbol(*symbol->getAsVariable(), loc);7741else7742{7743TVariable* variable = new TVariable(newParam.name, *newParam.type);7744intermSymbol = intermediate.addSymbol(*variable, loc);7745}77467747remappedArgument = intermediate.growAggregate(remappedArgument, intermSymbol);7748}77497750return remappedArgument;7751}7752}77537754TIntermTyped* TParseContext::vkRelaxedRemapDotDereference(const TSourceLoc&, TIntermTyped& base, const TType& member,7755const TString& identifier)7756{7757if (!member.isOpaque())7758return &base;77597760AccessChainTraverser traverser{};7761base.traverse(&traverser);7762if (!traverser.path.empty())7763traverser.path.append(".");7764traverser.path.append(identifier);77657766const TSymbol* symbol = symbolTable.find(traverser.path);7767if (!symbol)7768return &base;77697770TIntermTyped* result = intermediate.addSymbol(*symbol->getAsVariable());7771result->setType(symbol->getType());7772return result;7773}77747775//7776// Do everything necessary to handle a variable (non-block) declaration.7777// Either redeclaring a variable, or making a new one, updating the symbol7778// table, and all error checking.7779//7780// Returns a subtree node that computes an initializer, if needed.7781// Returns nullptr if there is no code to execute for initialization.7782//7783// 'publicType' is the type part of the declaration (to the left)7784// 'arraySizes' is the arrayness tagged on the identifier (to the right)7785//7786TIntermNode* TParseContext::declareVariable(const TSourceLoc& loc, TString& identifier, const TPublicType& publicType,7787TArraySizes* arraySizes, TIntermTyped* initializer)7788{7789// Make a fresh type that combines the characteristics from the individual7790// identifier syntax and the declaration-type syntax.7791TType type(publicType);7792type.transferArraySizes(arraySizes);7793type.copyArrayInnerSizes(publicType.arraySizes);7794arrayOfArrayVersionCheck(loc, type.getArraySizes());77957796if (initializer) {7797if (type.getBasicType() == EbtRayQuery) {7798error(loc, "ray queries can only be initialized by using the rayQueryInitializeEXT intrinsic:", "=", identifier.c_str());7799} else if (type.getBasicType() == EbtHitObjectNV) {7800error(loc, "hit objects cannot be initialized using initializers", "=", identifier.c_str());7801}78027803}78047805if (type.isCoopMatKHR()) {7806intermediate.setUseVulkanMemoryModel();7807intermediate.setUseStorageBuffer();78087809if (!publicType.typeParameters || !publicType.typeParameters->arraySizes ||7810publicType.typeParameters->arraySizes->getNumDims() != 3) {7811error(loc, "unexpected number type parameters", identifier.c_str(), "");7812}7813if (publicType.typeParameters) {7814if (!isTypeFloat(publicType.typeParameters->basicType) &&7815!isTypeInt(publicType.typeParameters->basicType) && publicType.typeParameters->basicType != EbtSpirvType) {7816error(loc, "expected 8, 16, 32, or 64 bit signed or unsigned integer or 16, 32, or 64 bit float type", identifier.c_str(), "");7817}7818}7819}7820else if (type.isCoopMatNV()) {7821intermediate.setUseVulkanMemoryModel();7822intermediate.setUseStorageBuffer();78237824if (!publicType.typeParameters || publicType.typeParameters->arraySizes->getNumDims() != 4) {7825error(loc, "expected four type parameters", identifier.c_str(), "");7826}7827if (publicType.typeParameters) {7828if (isTypeFloat(publicType.basicType) &&7829publicType.typeParameters->arraySizes->getDimSize(0) != 16 &&7830publicType.typeParameters->arraySizes->getDimSize(0) != 32 &&7831publicType.typeParameters->arraySizes->getDimSize(0) != 64) {7832error(loc, "expected 16, 32, or 64 bits for first type parameter", identifier.c_str(), "");7833}7834if (isTypeInt(publicType.basicType) &&7835publicType.typeParameters->arraySizes->getDimSize(0) != 8 &&7836publicType.typeParameters->arraySizes->getDimSize(0) != 16 &&7837publicType.typeParameters->arraySizes->getDimSize(0) != 32) {7838error(loc, "expected 8, 16, or 32 bits for first type parameter", identifier.c_str(), "");7839}7840}7841} else {7842if (publicType.typeParameters && publicType.typeParameters->arraySizes->getNumDims() != 0) {7843error(loc, "unexpected type parameters", identifier.c_str(), "");7844}7845}78467847if (voidErrorCheck(loc, identifier, type.getBasicType()))7848return nullptr;78497850if (initializer)7851rValueErrorCheck(loc, "initializer", initializer);7852else7853nonInitConstCheck(loc, identifier, type);78547855samplerCheck(loc, type, identifier, initializer);7856transparentOpaqueCheck(loc, type, identifier);7857atomicUintCheck(loc, type, identifier);7858accStructCheck(loc, type, identifier);7859checkAndResizeMeshViewDim(loc, type, /*isBlockMember*/ false);7860if (type.getQualifier().storage == EvqConst && type.containsReference()) {7861error(loc, "variables with reference type can't have qualifier 'const'", "qualifier", "");7862}78637864if (type.getQualifier().storage != EvqUniform && type.getQualifier().storage != EvqBuffer) {7865if (type.contains16BitFloat())7866requireFloat16Arithmetic(loc, "qualifier", "float16 types can only be in uniform block or buffer storage");7867if (type.contains16BitInt())7868requireInt16Arithmetic(loc, "qualifier", "(u)int16 types can only be in uniform block or buffer storage");7869if (type.contains8BitInt())7870requireInt8Arithmetic(loc, "qualifier", "(u)int8 types can only be in uniform block or buffer storage");7871}78727873if (type.getQualifier().storage == EvqtaskPayloadSharedEXT)7874intermediate.addTaskPayloadEXTCount();7875if (type.getQualifier().storage == EvqShared && type.containsCoopMat())7876error(loc, "qualifier", "Cooperative matrix types must not be used in shared memory", "");78777878if (profile == EEsProfile) {7879if (type.getQualifier().isPipeInput() && type.getBasicType() == EbtStruct) {7880if (type.getQualifier().isArrayedIo(language)) {7881TType perVertexType(type, 0);7882if (perVertexType.containsArray() && perVertexType.containsBuiltIn() == false) {7883error(loc, "A per vertex structure containing an array is not allowed as input in ES", type.getTypeName().c_str(), "");7884}7885}7886else if (type.containsArray() && type.containsBuiltIn() == false) {7887error(loc, "A structure containing an array is not allowed as input in ES", type.getTypeName().c_str(), "");7888}7889if (type.containsStructure())7890error(loc, "A structure containing an struct is not allowed as input in ES", type.getTypeName().c_str(), "");7891}7892}78937894if (identifier != "gl_FragCoord" && (publicType.shaderQualifiers.originUpperLeft || publicType.shaderQualifiers.pixelCenterInteger))7895error(loc, "can only apply origin_upper_left and pixel_center_origin to gl_FragCoord", "layout qualifier", "");7896if (identifier != "gl_FragDepth" && publicType.shaderQualifiers.getDepth() != EldNone)7897error(loc, "can only apply depth layout to gl_FragDepth", "layout qualifier", "");7898if (identifier != "gl_FragStencilRefARB" && publicType.shaderQualifiers.getStencil() != ElsNone)7899error(loc, "can only apply depth layout to gl_FragStencilRefARB", "layout qualifier", "");79007901// Check for redeclaration of built-ins and/or attempting to declare a reserved name7902TSymbol* symbol = redeclareBuiltinVariable(loc, identifier, type.getQualifier(), publicType.shaderQualifiers);7903if (symbol == nullptr)7904reservedErrorCheck(loc, identifier);79057906if (symbol == nullptr && spvVersion.vulkan > 0 && spvVersion.vulkanRelaxed) {7907bool remapped = vkRelaxedRemapUniformVariable(loc, identifier, publicType, arraySizes, initializer, type);79087909if (remapped) {7910return nullptr;7911}7912}79137914inheritGlobalDefaults(type.getQualifier());79157916// Declare the variable7917if (type.isArray()) {7918// Check that implicit sizing is only where allowed.7919arraySizesCheck(loc, type.getQualifier(), type.getArraySizes(), initializer, false);79207921if (! arrayQualifierError(loc, type.getQualifier()) && ! arrayError(loc, type))7922declareArray(loc, identifier, type, symbol);79237924if (initializer) {7925profileRequires(loc, ENoProfile, 120, E_GL_3DL_array_objects, "initializer");7926profileRequires(loc, EEsProfile, 300, nullptr, "initializer");7927}7928} else {7929// non-array case7930if (symbol == nullptr)7931symbol = declareNonArray(loc, identifier, type);7932else if (type != symbol->getType())7933error(loc, "cannot change the type of", "redeclaration", symbol->getName().c_str());7934}79357936if (symbol == nullptr)7937return nullptr;79387939// Deal with initializer7940TIntermNode* initNode = nullptr;7941if (symbol != nullptr && initializer) {7942TVariable* variable = symbol->getAsVariable();7943if (! variable) {7944error(loc, "initializer requires a variable, not a member", identifier.c_str(), "");7945return nullptr;7946}7947initNode = executeInitializer(loc, initializer, variable);7948}79497950// look for errors in layout qualifier use7951layoutObjectCheck(loc, *symbol);79527953// fix up7954fixOffset(loc, *symbol);79557956return initNode;7957}79587959// Pick up global defaults from the provide global defaults into dst.7960void TParseContext::inheritGlobalDefaults(TQualifier& dst) const7961{7962if (dst.storage == EvqVaryingOut) {7963if (! dst.hasStream() && language == EShLangGeometry)7964dst.layoutStream = globalOutputDefaults.layoutStream;7965if (! dst.hasXfbBuffer())7966dst.layoutXfbBuffer = globalOutputDefaults.layoutXfbBuffer;7967}7968}79697970//7971// Make an internal-only variable whose name is for debug purposes only7972// and won't be searched for. Callers will only use the return value to use7973// the variable, not the name to look it up. It is okay if the name7974// is the same as other names; there won't be any conflict.7975//7976TVariable* TParseContext::makeInternalVariable(const char* name, const TType& type) const7977{7978TString* nameString = NewPoolTString(name);7979TVariable* variable = new TVariable(nameString, type);7980symbolTable.makeInternalVariable(*variable);79817982return variable;7983}79847985//7986// Declare a non-array variable, the main point being there is no redeclaration7987// for resizing allowed.7988//7989// Return the successfully declared variable.7990//7991TVariable* TParseContext::declareNonArray(const TSourceLoc& loc, const TString& identifier, const TType& type)7992{7993// make a new variable7994TVariable* variable = new TVariable(&identifier, type);79957996ioArrayCheck(loc, type, identifier);79977998// add variable to symbol table7999if (symbolTable.insert(*variable)) {8000if (symbolTable.atGlobalLevel())8001trackLinkage(*variable);8002return variable;8003}80048005error(loc, "redefinition", variable->getName().c_str(), "");8006return nullptr;8007}80088009//8010// Handle all types of initializers from the grammar.8011//8012// Returning nullptr just means there is no code to execute to handle the8013// initializer, which will, for example, be the case for constant initializers.8014//8015TIntermNode* TParseContext::executeInitializer(const TSourceLoc& loc, TIntermTyped* initializer, TVariable* variable)8016{8017// A null initializer is an aggregate that hasn't had an op assigned yet8018// (still EOpNull, no relation to nullInit), and has no children.8019bool nullInit = initializer->getAsAggregate() && initializer->getAsAggregate()->getOp() == EOpNull &&8020initializer->getAsAggregate()->getSequence().size() == 0;80218022//8023// Identifier must be of type constant, a global, or a temporary, and8024// starting at version 120, desktop allows uniforms to have initializers.8025//8026TStorageQualifier qualifier = variable->getType().getQualifier().storage;8027if (! (qualifier == EvqTemporary || qualifier == EvqGlobal || qualifier == EvqConst ||8028(qualifier == EvqUniform && !isEsProfile() && version >= 120))) {8029if (qualifier == EvqShared) {8030// GL_EXT_null_initializer allows this for shared, if it's a null initializer8031if (nullInit) {8032const char* feature = "initialization with shared qualifier";8033profileRequires(loc, EEsProfile, 0, E_GL_EXT_null_initializer, feature);8034profileRequires(loc, ~EEsProfile, 0, E_GL_EXT_null_initializer, feature);8035} else {8036error(loc, "initializer can only be a null initializer ('{}')", "shared", "");8037}8038} else {8039error(loc, " cannot initialize this type of qualifier ",8040variable->getType().getStorageQualifierString(), "");8041return nullptr;8042}8043}80448045if (nullInit) {8046// only some types can be null initialized8047if (variable->getType().containsUnsizedArray()) {8048error(loc, "null initializers can't size unsized arrays", "{}", "");8049return nullptr;8050}8051if (variable->getType().containsOpaque()) {8052error(loc, "null initializers can't be used on opaque values", "{}", "");8053return nullptr;8054}8055variable->getWritableType().getQualifier().setNullInit();8056return nullptr;8057}80588059arrayObjectCheck(loc, variable->getType(), "array initializer");80608061//8062// If the initializer was from braces { ... }, we convert the whole subtree to a8063// constructor-style subtree, allowing the rest of the code to operate8064// identically for both kinds of initializers.8065//8066// Type can't be deduced from the initializer list, so a skeletal type to8067// follow has to be passed in. Constness and specialization-constness8068// should be deduced bottom up, not dictated by the skeletal type.8069//8070TType skeletalType;8071skeletalType.shallowCopy(variable->getType());8072skeletalType.getQualifier().makeTemporary();8073initializer = convertInitializerList(loc, skeletalType, initializer);8074if (! initializer) {8075// error recovery; don't leave const without constant values8076if (qualifier == EvqConst)8077variable->getWritableType().getQualifier().makeTemporary();8078return nullptr;8079}80808081// Fix outer arrayness if variable is unsized, getting size from the initializer8082if (initializer->getType().isSizedArray() && variable->getType().isUnsizedArray())8083variable->getWritableType().changeOuterArraySize(initializer->getType().getOuterArraySize());80848085// Inner arrayness can also get set by an initializer8086if (initializer->getType().isArrayOfArrays() && variable->getType().isArrayOfArrays() &&8087initializer->getType().getArraySizes()->getNumDims() ==8088variable->getType().getArraySizes()->getNumDims()) {8089// adopt unsized sizes from the initializer's sizes8090for (int d = 1; d < variable->getType().getArraySizes()->getNumDims(); ++d) {8091if (variable->getType().getArraySizes()->getDimSize(d) == UnsizedArraySize) {8092variable->getWritableType().getArraySizes()->setDimSize(d,8093initializer->getType().getArraySizes()->getDimSize(d));8094}8095}8096}80978098// Uniforms require a compile-time constant initializer8099if (qualifier == EvqUniform && ! initializer->getType().getQualifier().isFrontEndConstant()) {8100error(loc, "uniform initializers must be constant", "=", "'%s'",8101variable->getType().getCompleteString(intermediate.getEnhancedMsgs()).c_str());8102variable->getWritableType().getQualifier().makeTemporary();8103return nullptr;8104}8105// Global consts require a constant initializer (specialization constant is okay)8106if (qualifier == EvqConst && symbolTable.atGlobalLevel() && ! initializer->getType().getQualifier().isConstant()) {8107error(loc, "global const initializers must be constant", "=", "'%s'",8108variable->getType().getCompleteString(intermediate.getEnhancedMsgs()).c_str());8109variable->getWritableType().getQualifier().makeTemporary();8110return nullptr;8111}81128113// Const variables require a constant initializer, depending on version8114if (qualifier == EvqConst) {8115if (! initializer->getType().getQualifier().isConstant()) {8116const char* initFeature = "non-constant initializer";8117requireProfile(loc, ~EEsProfile, initFeature);8118profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, initFeature);8119variable->getWritableType().getQualifier().storage = EvqConstReadOnly;8120qualifier = EvqConstReadOnly;8121}8122} else {8123// Non-const global variables in ES need a const initializer.8124//8125// "In declarations of global variables with no storage qualifier or with a const8126// qualifier any initializer must be a constant expression."8127if (symbolTable.atGlobalLevel() && ! initializer->getType().getQualifier().isConstant()) {8128const char* initFeature =8129"non-constant global initializer (needs GL_EXT_shader_non_constant_global_initializers)";8130if (isEsProfile()) {8131if (relaxedErrors() && ! extensionTurnedOn(E_GL_EXT_shader_non_constant_global_initializers))8132warn(loc, "not allowed in this version", initFeature, "");8133else8134profileRequires(loc, EEsProfile, 0, E_GL_EXT_shader_non_constant_global_initializers, initFeature);8135}8136}8137}81388139if (qualifier == EvqConst || qualifier == EvqUniform) {8140// Compile-time tagging of the variable with its constant value...81418142initializer = intermediate.addConversion(EOpAssign, variable->getType(), initializer);8143if (! initializer || ! initializer->getType().getQualifier().isConstant() ||8144variable->getType() != initializer->getType()) {8145error(loc, "non-matching or non-convertible constant type for const initializer",8146variable->getType().getStorageQualifierString(), "");8147variable->getWritableType().getQualifier().makeTemporary();8148return nullptr;8149}81508151// We either have a folded constant in getAsConstantUnion, or we have to use8152// the initializer's subtree in the AST to represent the computation of a8153// specialization constant.8154assert(initializer->getAsConstantUnion() || initializer->getType().getQualifier().isSpecConstant());8155if (initializer->getAsConstantUnion())8156variable->setConstArray(initializer->getAsConstantUnion()->getConstArray());8157else {8158// It's a specialization constant.8159variable->getWritableType().getQualifier().makeSpecConstant();81608161// Keep the subtree that computes the specialization constant with the variable.8162// Later, a symbol node will adopt the subtree from the variable.8163variable->setConstSubtree(initializer);8164}8165} else {8166// normal assigning of a value to a variable...8167specializationCheck(loc, initializer->getType(), "initializer");8168TIntermSymbol* intermSymbol = intermediate.addSymbol(*variable, loc);8169TIntermTyped* initNode = intermediate.addAssign(EOpAssign, intermSymbol, initializer, loc);8170if (! initNode)8171assignError(loc, "=", intermSymbol->getCompleteString(intermediate.getEnhancedMsgs()), initializer->getCompleteString(intermediate.getEnhancedMsgs()));81728173return initNode;8174}81758176return nullptr;8177}81788179//8180// Reprocess any initializer-list (the "{ ... }" syntax) parts of the8181// initializer.8182//8183// Need to hierarchically assign correct types and implicit8184// conversions. Will do this mimicking the same process used for8185// creating a constructor-style initializer, ensuring we get the8186// same form. However, it has to in parallel walk the 'type'8187// passed in, as type cannot be deduced from an initializer list.8188//8189TIntermTyped* TParseContext::convertInitializerList(const TSourceLoc& loc, const TType& type, TIntermTyped* initializer)8190{8191// Will operate recursively. Once a subtree is found that is constructor style,8192// everything below it is already good: Only the "top part" of the initializer8193// can be an initializer list, where "top part" can extend for several (or all) levels.81948195// see if we have bottomed out in the tree within the initializer-list part8196TIntermAggregate* initList = initializer->getAsAggregate();8197if (! initList || initList->getOp() != EOpNull)8198return initializer;81998200// Of the initializer-list set of nodes, need to process bottom up,8201// so recurse deep, then process on the way up.82028203// Go down the tree here...8204if (type.isArray()) {8205// The type's array might be unsized, which could be okay, so base sizes on the size of the aggregate.8206// Later on, initializer execution code will deal with array size logic.8207TType arrayType;8208arrayType.shallowCopy(type); // sharing struct stuff is fine8209arrayType.copyArraySizes(*type.getArraySizes()); // but get a fresh copy of the array information, to edit below82108211// edit array sizes to fill in unsized dimensions8212arrayType.changeOuterArraySize((int)initList->getSequence().size());8213TIntermTyped* firstInit = initList->getSequence()[0]->getAsTyped();8214if (arrayType.isArrayOfArrays() && firstInit->getType().isArray() &&8215arrayType.getArraySizes()->getNumDims() == firstInit->getType().getArraySizes()->getNumDims() + 1) {8216for (int d = 1; d < arrayType.getArraySizes()->getNumDims(); ++d) {8217if (arrayType.getArraySizes()->getDimSize(d) == UnsizedArraySize)8218arrayType.getArraySizes()->setDimSize(d, firstInit->getType().getArraySizes()->getDimSize(d - 1));8219}8220}82218222TType elementType(arrayType, 0); // dereferenced type8223for (size_t i = 0; i < initList->getSequence().size(); ++i) {8224initList->getSequence()[i] = convertInitializerList(loc, elementType, initList->getSequence()[i]->getAsTyped());8225if (initList->getSequence()[i] == nullptr)8226return nullptr;8227}82288229return addConstructor(loc, initList, arrayType);8230} else if (type.isStruct()) {8231if (type.getStruct()->size() != initList->getSequence().size()) {8232error(loc, "wrong number of structure members", "initializer list", "");8233return nullptr;8234}8235for (size_t i = 0; i < type.getStruct()->size(); ++i) {8236initList->getSequence()[i] = convertInitializerList(loc, *(*type.getStruct())[i].type, initList->getSequence()[i]->getAsTyped());8237if (initList->getSequence()[i] == nullptr)8238return nullptr;8239}8240} else if (type.isMatrix()) {8241if (type.getMatrixCols() != (int)initList->getSequence().size()) {8242error(loc, "wrong number of matrix columns:", "initializer list", type.getCompleteString(intermediate.getEnhancedMsgs()).c_str());8243return nullptr;8244}8245TType vectorType(type, 0); // dereferenced type8246for (int i = 0; i < type.getMatrixCols(); ++i) {8247initList->getSequence()[i] = convertInitializerList(loc, vectorType, initList->getSequence()[i]->getAsTyped());8248if (initList->getSequence()[i] == nullptr)8249return nullptr;8250}8251} else if (type.isVector()) {8252if (type.getVectorSize() != (int)initList->getSequence().size()) {8253error(loc, "wrong vector size (or rows in a matrix column):", "initializer list", type.getCompleteString(intermediate.getEnhancedMsgs()).c_str());8254return nullptr;8255}8256TBasicType destType = type.getBasicType();8257for (int i = 0; i < type.getVectorSize(); ++i) {8258TBasicType initType = initList->getSequence()[i]->getAsTyped()->getBasicType();8259if (destType != initType && !intermediate.canImplicitlyPromote(initType, destType)) {8260error(loc, "type mismatch in initializer list", "initializer list", type.getCompleteString(intermediate.getEnhancedMsgs()).c_str());8261return nullptr;8262}82638264}8265} else {8266error(loc, "unexpected initializer-list type:", "initializer list", type.getCompleteString(intermediate.getEnhancedMsgs()).c_str());8267return nullptr;8268}82698270// Now that the subtree is processed, process this node as if the8271// initializer list is a set of arguments to a constructor.8272TIntermNode* emulatedConstructorArguments;8273if (initList->getSequence().size() == 1)8274emulatedConstructorArguments = initList->getSequence()[0];8275else8276emulatedConstructorArguments = initList;8277return addConstructor(loc, emulatedConstructorArguments, type);8278}82798280//8281// Test for the correctness of the parameters passed to various constructor functions8282// and also convert them to the right data type, if allowed and required.8283//8284// 'node' is what to construct from.8285// 'type' is what type to construct.8286//8287// Returns nullptr for an error or the constructed node (aggregate or typed) for no error.8288//8289TIntermTyped* TParseContext::addConstructor(const TSourceLoc& loc, TIntermNode* node, const TType& type)8290{8291if (node == nullptr || node->getAsTyped() == nullptr)8292return nullptr;8293rValueErrorCheck(loc, "constructor", node->getAsTyped());82948295TIntermAggregate* aggrNode = node->getAsAggregate();8296TOperator op = intermediate.mapTypeToConstructorOp(type);82978298// Combined texture-sampler constructors are completely semantic checked8299// in constructorTextureSamplerError()8300if (op == EOpConstructTextureSampler) {8301if (aggrNode != nullptr) {8302if (aggrNode->getSequence()[1]->getAsTyped()->getType().getSampler().shadow) {8303// Transfer depth into the texture (SPIR-V image) type, as a hint8304// for tools to know this texture/image is a depth image.8305aggrNode->getSequence()[0]->getAsTyped()->getWritableType().getSampler().shadow = true;8306}8307return intermediate.setAggregateOperator(aggrNode, op, type, loc);8308}8309}83108311TTypeList::const_iterator memberTypes;8312if (op == EOpConstructStruct)8313memberTypes = type.getStruct()->begin();83148315TType elementType;8316if (type.isArray()) {8317TType dereferenced(type, 0);8318elementType.shallowCopy(dereferenced);8319} else8320elementType.shallowCopy(type);83218322bool singleArg;8323if (aggrNode) {8324if (aggrNode->getOp() != EOpNull)8325singleArg = true;8326else8327singleArg = false;8328} else8329singleArg = true;83308331TIntermTyped *newNode;8332if (singleArg) {8333// If structure constructor or array constructor is being called8334// for only one parameter inside the structure, we need to call constructAggregate function once.8335if (type.isArray())8336newNode = constructAggregate(node, elementType, 1, node->getLoc());8337else if (op == EOpConstructStruct)8338newNode = constructAggregate(node, *(*memberTypes).type, 1, node->getLoc());8339else8340newNode = constructBuiltIn(type, op, node->getAsTyped(), node->getLoc(), false);83418342if (newNode && (type.isArray() || op == EOpConstructStruct))8343newNode = intermediate.setAggregateOperator(newNode, EOpConstructStruct, type, loc);83448345return newNode;8346}83478348//8349// Handle list of arguments.8350//8351TIntermSequence &sequenceVector = aggrNode->getSequence(); // Stores the information about the parameter to the constructor8352// if the structure constructor contains more than one parameter, then construct8353// each parameter83548355int paramCount = 0; // keeps track of the constructor parameter number being checked83568357// for each parameter to the constructor call, check to see if the right type is passed or convert them8358// to the right type if possible (and allowed).8359// for structure constructors, just check if the right type is passed, no conversion is allowed.8360for (TIntermSequence::iterator p = sequenceVector.begin();8361p != sequenceVector.end(); p++, paramCount++) {8362if (type.isArray())8363newNode = constructAggregate(*p, elementType, paramCount+1, node->getLoc());8364else if (op == EOpConstructStruct)8365newNode = constructAggregate(*p, *(memberTypes[paramCount]).type, paramCount+1, node->getLoc());8366else8367newNode = constructBuiltIn(type, op, (*p)->getAsTyped(), node->getLoc(), true);83688369if (newNode)8370*p = newNode;8371else8372return nullptr;8373}83748375TIntermTyped *ret_node = intermediate.setAggregateOperator(aggrNode, op, type, loc);83768377TIntermAggregate *agg_node = ret_node->getAsAggregate();8378if (agg_node && (agg_node->isVector() || agg_node->isArray() || agg_node->isMatrix()))8379agg_node->updatePrecision();83808381return ret_node;8382}83838384// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value8385// for the parameter to the constructor (passed to this function). Essentially, it converts8386// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a8387// float, then float is converted to int.8388//8389// Returns nullptr for an error or the constructed node.8390//8391TIntermTyped* TParseContext::constructBuiltIn(const TType& type, TOperator op, TIntermTyped* node, const TSourceLoc& loc,8392bool subset)8393{8394// If we are changing a matrix in both domain of basic type and to a non matrix,8395// do the shape change first (by default, below, basic type is changed before shape).8396// This avoids requesting a matrix of a new type that is going to be discarded anyway.8397// TODO: This could be generalized to more type combinations, but that would require8398// more extensive testing and full algorithm rework. For now, the need to do two changes makes8399// the recursive call work, and avoids the most egregious case of creating integer matrices.8400if (node->getType().isMatrix() && (type.isScalar() || type.isVector()) &&8401type.isFloatingDomain() != node->getType().isFloatingDomain()) {8402TType transitionType(node->getBasicType(), glslang::EvqTemporary, type.getVectorSize(), 0, 0, node->isVector());8403TOperator transitionOp = intermediate.mapTypeToConstructorOp(transitionType);8404node = constructBuiltIn(transitionType, transitionOp, node, loc, false);8405}84068407TIntermTyped* newNode;8408TOperator basicOp;84098410//8411// First, convert types as needed.8412//8413switch (op) {8414case EOpConstructVec2:8415case EOpConstructVec3:8416case EOpConstructVec4:8417case EOpConstructMat2x2:8418case EOpConstructMat2x3:8419case EOpConstructMat2x4:8420case EOpConstructMat3x2:8421case EOpConstructMat3x3:8422case EOpConstructMat3x4:8423case EOpConstructMat4x2:8424case EOpConstructMat4x3:8425case EOpConstructMat4x4:8426case EOpConstructFloat:8427basicOp = EOpConstructFloat;8428break;84298430case EOpConstructIVec2:8431case EOpConstructIVec3:8432case EOpConstructIVec4:8433case EOpConstructInt:8434basicOp = EOpConstructInt;8435break;84368437case EOpConstructUVec2:8438if (node->getType().getBasicType() == EbtReference) {8439requireExtensions(loc, 1, &E_GL_EXT_buffer_reference_uvec2, "reference conversion to uvec2");8440TIntermTyped* newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvPtrToUvec2, true, node,8441type);8442return newNode;8443} else if (node->getType().getBasicType() == EbtSampler) {8444requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "sampler conversion to uvec2");8445// force the basic type of the constructor param to uvec2, otherwise spv builder will8446// report some errors8447TIntermTyped* newSrcNode = intermediate.createConversion(EbtUint, node);8448newSrcNode->getAsTyped()->getWritableType().setVectorSize(2);84498450TIntermTyped* newNode =8451intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConstructUVec2, false, newSrcNode, type);8452return newNode;8453}8454[[fallthrough]];8455case EOpConstructUVec3:8456case EOpConstructUVec4:8457case EOpConstructUint:8458basicOp = EOpConstructUint;8459break;84608461case EOpConstructBVec2:8462case EOpConstructBVec3:8463case EOpConstructBVec4:8464case EOpConstructBool:8465basicOp = EOpConstructBool;8466break;8467case EOpConstructTextureSampler:8468if ((node->getType().getBasicType() == EbtUint || node->getType().getBasicType() == EbtInt) &&8469node->getType().getVectorSize() == 2) {8470requireExtensions(loc, 1, &E_GL_ARB_bindless_texture, "ivec2/uvec2 convert to texture handle");8471// No matter ivec2 or uvec2, Set EOpPackUint2x32 just to generate an opBitcast op code8472TIntermTyped* newNode =8473intermediate.addBuiltInFunctionCall(node->getLoc(), EOpPackUint2x32, true, node, type);8474return newNode;8475}8476[[fallthrough]];8477case EOpConstructDVec2:8478case EOpConstructDVec3:8479case EOpConstructDVec4:8480case EOpConstructDMat2x2:8481case EOpConstructDMat2x3:8482case EOpConstructDMat2x4:8483case EOpConstructDMat3x2:8484case EOpConstructDMat3x3:8485case EOpConstructDMat3x4:8486case EOpConstructDMat4x2:8487case EOpConstructDMat4x3:8488case EOpConstructDMat4x4:8489case EOpConstructDouble:8490basicOp = EOpConstructDouble;8491break;84928493case EOpConstructF16Vec2:8494case EOpConstructF16Vec3:8495case EOpConstructF16Vec4:8496case EOpConstructF16Mat2x2:8497case EOpConstructF16Mat2x3:8498case EOpConstructF16Mat2x4:8499case EOpConstructF16Mat3x2:8500case EOpConstructF16Mat3x3:8501case EOpConstructF16Mat3x4:8502case EOpConstructF16Mat4x2:8503case EOpConstructF16Mat4x3:8504case EOpConstructF16Mat4x4:8505case EOpConstructFloat16:8506basicOp = EOpConstructFloat16;8507// 8/16-bit storage extensions don't support constructing composites of 8/16-bit types,8508// so construct a 32-bit type and convert8509if (!intermediate.getArithemeticFloat16Enabled()) {8510TType tempType(EbtFloat, EvqTemporary, type.getVectorSize());8511newNode = node;8512if (tempType != newNode->getType()) {8513TOperator aggregateOp;8514if (op == EOpConstructFloat16)8515aggregateOp = EOpConstructFloat;8516else8517aggregateOp = (TOperator)(EOpConstructVec2 + op - EOpConstructF16Vec2);8518newNode = intermediate.setAggregateOperator(newNode, aggregateOp, tempType, node->getLoc());8519}8520newNode = intermediate.addConversion(EbtFloat16, newNode);8521return newNode;8522}8523break;85248525case EOpConstructI8Vec2:8526case EOpConstructI8Vec3:8527case EOpConstructI8Vec4:8528case EOpConstructInt8:8529basicOp = EOpConstructInt8;8530// 8/16-bit storage extensions don't support constructing composites of 8/16-bit types,8531// so construct a 32-bit type and convert8532if (!intermediate.getArithemeticInt8Enabled()) {8533TType tempType(EbtInt, EvqTemporary, type.getVectorSize());8534newNode = node;8535if (tempType != newNode->getType()) {8536TOperator aggregateOp;8537if (op == EOpConstructInt8)8538aggregateOp = EOpConstructInt;8539else8540aggregateOp = (TOperator)(EOpConstructIVec2 + op - EOpConstructI8Vec2);8541newNode = intermediate.setAggregateOperator(newNode, aggregateOp, tempType, node->getLoc());8542}8543newNode = intermediate.addConversion(EbtInt8, newNode);8544return newNode;8545}8546break;85478548case EOpConstructU8Vec2:8549case EOpConstructU8Vec3:8550case EOpConstructU8Vec4:8551case EOpConstructUint8:8552basicOp = EOpConstructUint8;8553// 8/16-bit storage extensions don't support constructing composites of 8/16-bit types,8554// so construct a 32-bit type and convert8555if (!intermediate.getArithemeticInt8Enabled()) {8556TType tempType(EbtUint, EvqTemporary, type.getVectorSize());8557newNode = node;8558if (tempType != newNode->getType()) {8559TOperator aggregateOp;8560if (op == EOpConstructUint8)8561aggregateOp = EOpConstructUint;8562else8563aggregateOp = (TOperator)(EOpConstructUVec2 + op - EOpConstructU8Vec2);8564newNode = intermediate.setAggregateOperator(newNode, aggregateOp, tempType, node->getLoc());8565}8566newNode = intermediate.addConversion(EbtUint8, newNode);8567return newNode;8568}8569break;85708571case EOpConstructI16Vec2:8572case EOpConstructI16Vec3:8573case EOpConstructI16Vec4:8574case EOpConstructInt16:8575basicOp = EOpConstructInt16;8576// 8/16-bit storage extensions don't support constructing composites of 8/16-bit types,8577// so construct a 32-bit type and convert8578if (!intermediate.getArithemeticInt16Enabled()) {8579TType tempType(EbtInt, EvqTemporary, type.getVectorSize());8580newNode = node;8581if (tempType != newNode->getType()) {8582TOperator aggregateOp;8583if (op == EOpConstructInt16)8584aggregateOp = EOpConstructInt;8585else8586aggregateOp = (TOperator)(EOpConstructIVec2 + op - EOpConstructI16Vec2);8587newNode = intermediate.setAggregateOperator(newNode, aggregateOp, tempType, node->getLoc());8588}8589newNode = intermediate.addConversion(EbtInt16, newNode);8590return newNode;8591}8592break;85938594case EOpConstructU16Vec2:8595case EOpConstructU16Vec3:8596case EOpConstructU16Vec4:8597case EOpConstructUint16:8598basicOp = EOpConstructUint16;8599// 8/16-bit storage extensions don't support constructing composites of 8/16-bit types,8600// so construct a 32-bit type and convert8601if (!intermediate.getArithemeticInt16Enabled()) {8602TType tempType(EbtUint, EvqTemporary, type.getVectorSize());8603newNode = node;8604if (tempType != newNode->getType()) {8605TOperator aggregateOp;8606if (op == EOpConstructUint16)8607aggregateOp = EOpConstructUint;8608else8609aggregateOp = (TOperator)(EOpConstructUVec2 + op - EOpConstructU16Vec2);8610newNode = intermediate.setAggregateOperator(newNode, aggregateOp, tempType, node->getLoc());8611}8612newNode = intermediate.addConversion(EbtUint16, newNode);8613return newNode;8614}8615break;86168617case EOpConstructI64Vec2:8618case EOpConstructI64Vec3:8619case EOpConstructI64Vec4:8620case EOpConstructInt64:8621basicOp = EOpConstructInt64;8622break;86238624case EOpConstructUint64:8625if (type.isScalar() && node->getType().isReference()) {8626TIntermTyped* newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvPtrToUint64, true, node, type);8627return newNode;8628}8629[[fallthrough]];8630case EOpConstructU64Vec2:8631case EOpConstructU64Vec3:8632case EOpConstructU64Vec4:8633basicOp = EOpConstructUint64;8634break;86358636case EOpConstructNonuniform:8637// Make a nonuniform copy of node8638newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpCopyObject, true, node, type);8639return newNode;86408641case EOpConstructReference:8642// construct reference from reference8643if (node->getType().isReference()) {8644newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConstructReference, true, node, type);8645return newNode;8646// construct reference from uint648647} else if (node->getType().isScalar() && node->getType().getBasicType() == EbtUint64) {8648TIntermTyped* newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvUint64ToPtr, true, node,8649type);8650return newNode;8651// construct reference from uvec28652} else if (node->getType().isVector() && node->getType().getBasicType() == EbtUint &&8653node->getVectorSize() == 2) {8654requireExtensions(loc, 1, &E_GL_EXT_buffer_reference_uvec2, "uvec2 conversion to reference");8655TIntermTyped* newNode = intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvUvec2ToPtr, true, node,8656type);8657return newNode;8658} else {8659return nullptr;8660}86618662case EOpConstructCooperativeMatrixNV:8663case EOpConstructCooperativeMatrixKHR:8664if (node->getType() == type) {8665return node;8666}8667if (!node->getType().isCoopMat()) {8668if (type.getBasicType() != node->getType().getBasicType()) {8669node = intermediate.addConversion(type.getBasicType(), node);8670if (node == nullptr)8671return nullptr;8672}8673node = intermediate.setAggregateOperator(node, op, type, node->getLoc());8674} else {8675TOperator op = EOpNull;8676switch (type.getBasicType()) {8677default:8678assert(0);8679break;8680case EbtInt:8681switch (node->getType().getBasicType()) {8682case EbtFloat: op = EOpConvFloatToInt; break;8683case EbtFloat16: op = EOpConvFloat16ToInt; break;8684case EbtUint8: op = EOpConvUint8ToInt; break;8685case EbtInt8: op = EOpConvInt8ToInt; break;8686case EbtUint16: op = EOpConvUint16ToInt; break;8687case EbtInt16: op = EOpConvInt16ToInt; break;8688case EbtUint: op = EOpConvUintToInt; break;8689default: assert(0);8690}8691break;8692case EbtUint:8693switch (node->getType().getBasicType()) {8694case EbtFloat: op = EOpConvFloatToUint; break;8695case EbtFloat16: op = EOpConvFloat16ToUint; break;8696case EbtUint8: op = EOpConvUint8ToUint; break;8697case EbtInt8: op = EOpConvInt8ToUint; break;8698case EbtUint16: op = EOpConvUint16ToUint; break;8699case EbtInt16: op = EOpConvInt16ToUint; break;8700case EbtInt: op = EOpConvIntToUint; break;8701default: assert(0);8702}8703break;8704case EbtInt16:8705switch (node->getType().getBasicType()) {8706case EbtFloat: op = EOpConvFloatToInt16; break;8707case EbtFloat16: op = EOpConvFloat16ToInt16; break;8708case EbtUint8: op = EOpConvUint8ToInt16; break;8709case EbtInt8: op = EOpConvInt8ToInt16; break;8710case EbtUint16: op = EOpConvUint16ToInt16; break;8711case EbtInt: op = EOpConvIntToInt16; break;8712case EbtUint: op = EOpConvUintToInt16; break;8713default: assert(0);8714}8715break;8716case EbtUint16:8717switch (node->getType().getBasicType()) {8718case EbtFloat: op = EOpConvFloatToUint16; break;8719case EbtFloat16: op = EOpConvFloat16ToUint16; break;8720case EbtUint8: op = EOpConvUint8ToUint16; break;8721case EbtInt8: op = EOpConvInt8ToUint16; break;8722case EbtInt16: op = EOpConvInt16ToUint16; break;8723case EbtInt: op = EOpConvIntToUint16; break;8724case EbtUint: op = EOpConvUintToUint16; break;8725default: assert(0);8726}8727break;8728case EbtInt8:8729switch (node->getType().getBasicType()) {8730case EbtFloat: op = EOpConvFloatToInt8; break;8731case EbtFloat16: op = EOpConvFloat16ToInt8; break;8732case EbtUint8: op = EOpConvUint8ToInt8; break;8733case EbtInt16: op = EOpConvInt16ToInt8; break;8734case EbtUint16: op = EOpConvUint16ToInt8; break;8735case EbtInt: op = EOpConvIntToInt8; break;8736case EbtUint: op = EOpConvUintToInt8; break;8737default: assert(0);8738}8739break;8740case EbtUint8:8741switch (node->getType().getBasicType()) {8742case EbtFloat: op = EOpConvFloatToUint8; break;8743case EbtFloat16: op = EOpConvFloat16ToUint8; break;8744case EbtInt8: op = EOpConvInt8ToUint8; break;8745case EbtInt16: op = EOpConvInt16ToUint8; break;8746case EbtUint16: op = EOpConvUint16ToUint8; break;8747case EbtInt: op = EOpConvIntToUint8; break;8748case EbtUint: op = EOpConvUintToUint8; break;8749default: assert(0);8750}8751break;8752case EbtFloat:8753switch (node->getType().getBasicType()) {8754case EbtFloat16: op = EOpConvFloat16ToFloat; break;8755case EbtInt8: op = EOpConvInt8ToFloat; break;8756case EbtUint8: op = EOpConvUint8ToFloat; break;8757case EbtInt16: op = EOpConvInt16ToFloat; break;8758case EbtUint16: op = EOpConvUint16ToFloat; break;8759case EbtInt: op = EOpConvIntToFloat; break;8760case EbtUint: op = EOpConvUintToFloat; break;8761default: assert(0);8762}8763break;8764case EbtFloat16:8765switch (node->getType().getBasicType()) {8766case EbtFloat: op = EOpConvFloatToFloat16; break;8767case EbtInt8: op = EOpConvInt8ToFloat16; break;8768case EbtUint8: op = EOpConvUint8ToFloat16; break;8769case EbtInt16: op = EOpConvInt16ToFloat16; break;8770case EbtUint16: op = EOpConvUint16ToFloat16; break;8771case EbtInt: op = EOpConvIntToFloat16; break;8772case EbtUint: op = EOpConvUintToFloat16; break;8773default: assert(0);8774}8775break;8776}87778778node = intermediate.addUnaryNode(op, node, node->getLoc(), type);8779// If it's a (non-specialization) constant, it must be folded.8780if (node->getAsUnaryNode()->getOperand()->getAsConstantUnion())8781return node->getAsUnaryNode()->getOperand()->getAsConstantUnion()->fold(op, node->getType());8782}87838784return node;87858786case EOpConstructAccStruct:8787if ((node->getType().isScalar() && node->getType().getBasicType() == EbtUint64)) {8788// construct acceleration structure from uint648789requireExtensions(loc, Num_ray_tracing_EXTs, ray_tracing_EXTs, "uint64_t conversion to acclerationStructureEXT");8790return intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvUint64ToAccStruct, true, node,8791type);8792} else if (node->getType().isVector() && node->getType().getBasicType() == EbtUint && node->getVectorSize() == 2) {8793// construct acceleration structure from uint648794requireExtensions(loc, Num_ray_tracing_EXTs, ray_tracing_EXTs, "uvec2 conversion to accelerationStructureEXT");8795return intermediate.addBuiltInFunctionCall(node->getLoc(), EOpConvUvec2ToAccStruct, true, node,8796type);8797} else8798return nullptr;87998800default:8801error(loc, "unsupported construction", "", "");88028803return nullptr;8804}8805newNode = intermediate.addUnaryMath(basicOp, node, node->getLoc());8806if (newNode == nullptr) {8807error(loc, "can't convert", "constructor", "");8808return nullptr;8809}88108811//8812// Now, if there still isn't an operation to do the construction, and we need one, add one.8813//88148815// Otherwise, skip out early.8816if (subset || (newNode != node && newNode->getType() == type))8817return newNode;88188819// setAggregateOperator will insert a new node for the constructor, as needed.8820return intermediate.setAggregateOperator(newNode, op, type, loc);8821}88228823// This function tests for the type of the parameters to the structure or array constructor. Raises8824// an error message if the expected type does not match the parameter passed to the constructor.8825//8826// Returns nullptr for an error or the input node itself if the expected and the given parameter types match.8827//8828TIntermTyped* TParseContext::constructAggregate(TIntermNode* node, const TType& type, int paramCount, const TSourceLoc& loc)8829{8830TIntermTyped* converted = intermediate.addConversion(EOpConstructStruct, type, node->getAsTyped());8831if (! converted || converted->getType() != type) {8832bool enhanced = intermediate.getEnhancedMsgs();8833error(loc, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,8834node->getAsTyped()->getType().getCompleteString(enhanced).c_str(), type.getCompleteString(enhanced).c_str());88358836return nullptr;8837}88388839return converted;8840}88418842// If a memory qualifier is present in 'to', also make it present in 'from'.8843void TParseContext::inheritMemoryQualifiers(const TQualifier& from, TQualifier& to)8844{8845if (from.isReadOnly())8846to.readonly = from.readonly;8847if (from.isWriteOnly())8848to.writeonly = from.writeonly;8849if (from.coherent)8850to.coherent = from.coherent;8851if (from.volatil)8852to.volatil = from.volatil;8853if (from.restrict)8854to.restrict = from.restrict;8855}88568857//8858// Update qualifier layoutBindlessImage & layoutBindlessSampler on block member8859//8860void TParseContext::updateBindlessQualifier(TType& memberType)8861{8862if (memberType.containsSampler()) {8863if (memberType.isStruct()) {8864TTypeList* typeList = memberType.getWritableStruct();8865for (unsigned int member = 0; member < typeList->size(); ++member) {8866TType* subMemberType = (*typeList)[member].type;8867updateBindlessQualifier(*subMemberType);8868}8869}8870else if (memberType.getSampler().isImage()) {8871intermediate.setBindlessImageMode(currentCaller, AstRefTypeLayout);8872memberType.getQualifier().layoutBindlessImage = true;8873}8874else {8875intermediate.setBindlessTextureMode(currentCaller, AstRefTypeLayout);8876memberType.getQualifier().layoutBindlessSampler = true;8877}8878}8879}88808881//8882// Do everything needed to add an interface block.8883//8884void TParseContext::declareBlock(const TSourceLoc& loc, TTypeList& typeList, const TString* instanceName,8885TArraySizes* arraySizes)8886{8887if (spvVersion.vulkan > 0 && spvVersion.vulkanRelaxed)8888blockStorageRemap(loc, blockName, currentBlockQualifier);8889blockStageIoCheck(loc, currentBlockQualifier);8890blockQualifierCheck(loc, currentBlockQualifier, instanceName != nullptr);8891if (arraySizes != nullptr) {8892arraySizesCheck(loc, currentBlockQualifier, arraySizes, nullptr, false);8893arrayOfArrayVersionCheck(loc, arraySizes);8894if (arraySizes->getNumDims() > 1)8895requireProfile(loc, ~EEsProfile, "array-of-array of block");8896}88978898// Inherit and check member storage qualifiers WRT to the block-level qualifier.8899for (unsigned int member = 0; member < typeList.size(); ++member) {8900TType& memberType = *typeList[member].type;8901TQualifier& memberQualifier = memberType.getQualifier();8902const TSourceLoc& memberLoc = typeList[member].loc;8903if (memberQualifier.storage != EvqTemporary && memberQualifier.storage != EvqGlobal && memberQualifier.storage != currentBlockQualifier.storage)8904error(memberLoc, "member storage qualifier cannot contradict block storage qualifier", memberType.getFieldName().c_str(), "");8905memberQualifier.storage = currentBlockQualifier.storage;8906globalQualifierFixCheck(memberLoc, memberQualifier);8907inheritMemoryQualifiers(currentBlockQualifier, memberQualifier);8908if (currentBlockQualifier.perPrimitiveNV)8909memberQualifier.perPrimitiveNV = currentBlockQualifier.perPrimitiveNV;8910if (currentBlockQualifier.perViewNV)8911memberQualifier.perViewNV = currentBlockQualifier.perViewNV;8912if (currentBlockQualifier.perTaskNV)8913memberQualifier.perTaskNV = currentBlockQualifier.perTaskNV;8914if (currentBlockQualifier.storage == EvqtaskPayloadSharedEXT)8915memberQualifier.storage = EvqtaskPayloadSharedEXT;8916if (memberQualifier.storage == EvqSpirvStorageClass)8917error(memberLoc, "member cannot have a spirv_storage_class qualifier", memberType.getFieldName().c_str(), "");8918if (memberQualifier.hasSpirvDecorate() && !memberQualifier.getSpirvDecorate().decorateIds.empty())8919error(memberLoc, "member cannot have a spirv_decorate_id qualifier", memberType.getFieldName().c_str(), "");8920if ((currentBlockQualifier.storage == EvqUniform || currentBlockQualifier.storage == EvqBuffer) && (memberQualifier.isInterpolation() || memberQualifier.isAuxiliary()))8921error(memberLoc, "member of uniform or buffer block cannot have an auxiliary or interpolation qualifier", memberType.getFieldName().c_str(), "");8922if (memberType.isArray())8923arraySizesCheck(memberLoc, currentBlockQualifier, memberType.getArraySizes(), nullptr, member == typeList.size() - 1);8924if (memberQualifier.hasOffset()) {8925if (spvVersion.spv == 0) {8926profileRequires(memberLoc, ~EEsProfile, 440, E_GL_ARB_enhanced_layouts, "\"offset\" on block member");8927profileRequires(memberLoc, EEsProfile, 300, E_GL_ARB_enhanced_layouts, "\"offset\" on block member");8928}8929}89308931// For bindless texture, sampler can be declared as uniform/storage block member,8932if (memberType.containsOpaque()) {8933if (memberType.containsSampler() && extensionTurnedOn(E_GL_ARB_bindless_texture))8934updateBindlessQualifier(memberType);8935else8936error(memberLoc, "member of block cannot be or contain a sampler, image, or atomic_uint type", typeList[member].type->getFieldName().c_str(), "");8937}89388939if (memberType.containsCoopMat())8940error(memberLoc, "member of block cannot be or contain a cooperative matrix type", typeList[member].type->getFieldName().c_str(), "");8941}89428943// This might be a redeclaration of a built-in block. If so, redeclareBuiltinBlock() will8944// do all the rest.8945if (! symbolTable.atBuiltInLevel() && builtInName(*blockName)) {8946redeclareBuiltinBlock(loc, typeList, *blockName, instanceName, arraySizes);8947return;8948}89498950// Not a redeclaration of a built-in; check that all names are user names.8951reservedErrorCheck(loc, *blockName);8952if (instanceName)8953reservedErrorCheck(loc, *instanceName);8954for (unsigned int member = 0; member < typeList.size(); ++member)8955reservedErrorCheck(typeList[member].loc, typeList[member].type->getFieldName());89568957// Make default block qualification, and adjust the member qualifications89588959TQualifier defaultQualification;8960switch (currentBlockQualifier.storage) {8961case EvqUniform: defaultQualification = globalUniformDefaults; break;8962case EvqBuffer: defaultQualification = globalBufferDefaults; break;8963case EvqVaryingIn: defaultQualification = globalInputDefaults; break;8964case EvqVaryingOut: defaultQualification = globalOutputDefaults; break;8965case EvqShared: defaultQualification = globalSharedDefaults; break;8966default: defaultQualification.clear(); break;8967}89688969// Special case for "push_constant uniform", which has a default of std430,8970// contrary to normal uniform defaults, and can't have a default tracked for it.8971if ((currentBlockQualifier.isPushConstant() && !currentBlockQualifier.hasPacking()) ||8972(currentBlockQualifier.isShaderRecord() && !currentBlockQualifier.hasPacking()))8973currentBlockQualifier.layoutPacking = ElpStd430;89748975// Special case for "taskNV in/out", which has a default of std430,8976if (currentBlockQualifier.isTaskMemory() && !currentBlockQualifier.hasPacking())8977currentBlockQualifier.layoutPacking = ElpStd430;89788979// fix and check for member layout qualifiers89808981mergeObjectLayoutQualifiers(defaultQualification, currentBlockQualifier, true);89828983// "The align qualifier can only be used on blocks or block members, and only for blocks declared with std140 or std430 layouts."8984if (currentBlockQualifier.hasAlign()) {8985if (defaultQualification.layoutPacking != ElpStd140 &&8986defaultQualification.layoutPacking != ElpStd430 &&8987defaultQualification.layoutPacking != ElpScalar) {8988error(loc, "can only be used with std140, std430, or scalar layout packing", "align", "");8989defaultQualification.layoutAlign = -1;8990}8991}89928993bool memberWithLocation = false;8994bool memberWithoutLocation = false;8995bool memberWithPerViewQualifier = false;8996for (unsigned int member = 0; member < typeList.size(); ++member) {8997TQualifier& memberQualifier = typeList[member].type->getQualifier();8998const TSourceLoc& memberLoc = typeList[member].loc;8999if (memberQualifier.hasStream()) {9000if (defaultQualification.layoutStream != memberQualifier.layoutStream)9001error(memberLoc, "member cannot contradict block", "stream", "");9002}90039004// "This includes a block's inheritance of the9005// current global default buffer, a block member's inheritance of the block's9006// buffer, and the requirement that any *xfb_buffer* declared on a block9007// member must match the buffer inherited from the block."9008if (memberQualifier.hasXfbBuffer()) {9009if (defaultQualification.layoutXfbBuffer != memberQualifier.layoutXfbBuffer)9010error(memberLoc, "member cannot contradict block (or what block inherited from global)", "xfb_buffer", "");9011}90129013if (memberQualifier.hasPacking())9014error(memberLoc, "member of block cannot have a packing layout qualifier", typeList[member].type->getFieldName().c_str(), "");9015if (memberQualifier.hasLocation()) {9016const char* feature = "location on block member";9017switch (currentBlockQualifier.storage) {9018case EvqVaryingIn:9019case EvqVaryingOut:9020requireProfile(memberLoc, ECoreProfile | ECompatibilityProfile | EEsProfile, feature);9021profileRequires(memberLoc, ECoreProfile | ECompatibilityProfile, 440, E_GL_ARB_enhanced_layouts, feature);9022profileRequires(memberLoc, EEsProfile, 320, Num_AEP_shader_io_blocks, AEP_shader_io_blocks, feature);9023memberWithLocation = true;9024break;9025default:9026error(memberLoc, "can only use in an in/out block", feature, "");9027break;9028}9029} else9030memberWithoutLocation = true;90319032// "The offset qualifier can only be used on block members of blocks declared with std140 or std430 layouts."9033// "The align qualifier can only be used on blocks or block members, and only for blocks declared with std140 or std430 layouts."9034if (memberQualifier.hasAlign() || memberQualifier.hasOffset()) {9035if (defaultQualification.layoutPacking != ElpStd140 &&9036defaultQualification.layoutPacking != ElpStd430 &&9037defaultQualification.layoutPacking != ElpScalar)9038error(memberLoc, "can only be used with std140, std430, or scalar layout packing", "offset/align", "");9039}90409041if (memberQualifier.isPerView()) {9042memberWithPerViewQualifier = true;9043}90449045TQualifier newMemberQualification = defaultQualification;9046mergeQualifiers(memberLoc, newMemberQualification, memberQualifier, false);9047memberQualifier = newMemberQualification;9048}90499050layoutMemberLocationArrayCheck(loc, memberWithLocation, arraySizes);90519052// Ensure that the block has an XfbBuffer assigned. This is needed9053// because if the block has a XfbOffset assigned, then it is9054// assumed that it has implicitly assigned the current global9055// XfbBuffer, and because it's members need to be assigned a9056// XfbOffset if they lack it.9057if (currentBlockQualifier.storage == EvqVaryingOut && globalOutputDefaults.hasXfbBuffer()) {9058if (!currentBlockQualifier.hasXfbBuffer() && currentBlockQualifier.hasXfbOffset())9059currentBlockQualifier.layoutXfbBuffer = globalOutputDefaults.layoutXfbBuffer;9060}90619062// Process the members9063fixBlockLocations(loc, currentBlockQualifier, typeList, memberWithLocation, memberWithoutLocation);9064fixXfbOffsets(currentBlockQualifier, typeList);9065fixBlockUniformOffsets(currentBlockQualifier, typeList);9066fixBlockUniformLayoutMatrix(currentBlockQualifier, &typeList, nullptr);9067fixBlockUniformLayoutPacking(currentBlockQualifier, &typeList, nullptr);9068for (unsigned int member = 0; member < typeList.size(); ++member)9069layoutTypeCheck(typeList[member].loc, *typeList[member].type);90709071if (memberWithPerViewQualifier) {9072for (unsigned int member = 0; member < typeList.size(); ++member) {9073checkAndResizeMeshViewDim(typeList[member].loc, *typeList[member].type, /*isBlockMember*/ true);9074}9075}90769077// reverse merge, so that currentBlockQualifier now has all layout information9078// (can't use defaultQualification directly, it's missing other non-layout-default-class qualifiers)9079mergeObjectLayoutQualifiers(currentBlockQualifier, defaultQualification, true);90809081//9082// Build and add the interface block as a new type named 'blockName'9083//90849085TType blockType(&typeList, *blockName, currentBlockQualifier);9086if (arraySizes != nullptr)9087blockType.transferArraySizes(arraySizes);90889089if (arraySizes == nullptr)9090ioArrayCheck(loc, blockType, instanceName ? *instanceName : *blockName);9091if (currentBlockQualifier.hasBufferReference()) {90929093if (currentBlockQualifier.storage != EvqBuffer)9094error(loc, "can only be used with buffer", "buffer_reference", "");90959096// Create the block reference type. If it was forward-declared, detect that9097// as a referent struct type with no members. Replace the referent type with9098// blockType.9099TType blockNameType(EbtReference, blockType, *blockName);9100TVariable* blockNameVar = new TVariable(blockName, blockNameType, true);9101if (! symbolTable.insert(*blockNameVar)) {9102TSymbol* existingName = symbolTable.find(*blockName);9103if (existingName->getType().isReference() &&9104existingName->getType().getReferentType()->getStruct() &&9105existingName->getType().getReferentType()->getStruct()->size() == 0 &&9106existingName->getType().getQualifier().storage == blockType.getQualifier().storage) {9107existingName->getType().getReferentType()->deepCopy(blockType);9108} else {9109error(loc, "block name cannot be redefined", blockName->c_str(), "");9110}9111}9112if (!instanceName) {9113return;9114}9115} else {9116//9117// Don't make a user-defined type out of block name; that will cause an error9118// if the same block name gets reused in a different interface.9119//9120// "Block names have no other use within a shader9121// beyond interface matching; it is a compile-time error to use a block name at global scope for anything9122// other than as a block name (e.g., use of a block name for a global variable name or function name is9123// currently reserved)."9124//9125// Use the symbol table to prevent normal reuse of the block's name, as a variable entry,9126// whose type is EbtBlock, but without all the structure; that will come from the type9127// the instances point to.9128//9129TType blockNameType(EbtBlock, blockType.getQualifier().storage);9130TVariable* blockNameVar = new TVariable(blockName, blockNameType);9131if (! symbolTable.insert(*blockNameVar)) {9132TSymbol* existingName = symbolTable.find(*blockName);9133if (existingName->getType().getBasicType() == EbtBlock) {9134if (existingName->getType().getQualifier().storage == blockType.getQualifier().storage) {9135error(loc, "Cannot reuse block name within the same interface:", blockName->c_str(), blockType.getStorageQualifierString());9136return;9137}9138} else {9139error(loc, "block name cannot redefine a non-block name", blockName->c_str(), "");9140return;9141}9142}9143}91449145// Add the variable, as anonymous or named instanceName.9146// Make an anonymous variable if no name was provided.9147if (! instanceName)9148instanceName = NewPoolTString("");91499150TVariable& variable = *new TVariable(instanceName, blockType);9151if (! symbolTable.insert(variable)) {9152if (*instanceName == "")9153error(loc, "nameless block contains a member that already has a name at global scope", blockName->c_str(), "");9154else9155error(loc, "block instance name redefinition", variable.getName().c_str(), "");91569157return;9158}91599160// Check for general layout qualifier errors9161layoutObjectCheck(loc, variable);91629163// fix up9164if (isIoResizeArray(blockType)) {9165ioArraySymbolResizeList.push_back(&variable);9166checkIoArraysConsistency(loc, true);9167} else9168fixIoArraySize(loc, variable.getWritableType());91699170// Save it in the AST for linker use.9171trackLinkage(variable);9172}91739174//9175// allow storage type of block to be remapped at compile time9176//9177void TParseContext::blockStorageRemap(const TSourceLoc&, const TString* instanceName, TQualifier& qualifier)9178{9179TBlockStorageClass type = intermediate.getBlockStorageOverride(instanceName->c_str());9180if (type != EbsNone) {9181qualifier.setBlockStorage(type);9182}9183}91849185// Do all block-declaration checking regarding the combination of in/out/uniform/buffer9186// with a particular stage.9187void TParseContext::blockStageIoCheck(const TSourceLoc& loc, const TQualifier& qualifier)9188{9189const char *extsrt[2] = { E_GL_NV_ray_tracing, E_GL_EXT_ray_tracing };9190switch (qualifier.storage) {9191case EvqUniform:9192profileRequires(loc, EEsProfile, 300, nullptr, "uniform block");9193profileRequires(loc, ENoProfile, 140, E_GL_ARB_uniform_buffer_object, "uniform block");9194if (currentBlockQualifier.layoutPacking == ElpStd430 && ! currentBlockQualifier.isPushConstant())9195requireExtensions(loc, 1, &E_GL_EXT_scalar_block_layout, "std430 requires the buffer storage qualifier");9196break;9197case EvqBuffer:9198requireProfile(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, "buffer block");9199profileRequires(loc, ECoreProfile | ECompatibilityProfile, 430, E_GL_ARB_shader_storage_buffer_object, "buffer block");9200profileRequires(loc, EEsProfile, 310, nullptr, "buffer block");9201break;9202case EvqVaryingIn:9203profileRequires(loc, ~EEsProfile, 150, E_GL_ARB_separate_shader_objects, "input block");9204// It is a compile-time error to have an input block in a vertex shader or an output block in a fragment shader9205// "Compute shaders do not permit user-defined input variables..."9206requireStage(loc, (EShLanguageMask)(EShLangTessControlMask|EShLangTessEvaluationMask|EShLangGeometryMask|9207EShLangFragmentMask|EShLangMeshMask), "input block");9208if (language == EShLangFragment) {9209profileRequires(loc, EEsProfile, 320, Num_AEP_shader_io_blocks, AEP_shader_io_blocks, "fragment input block");9210} else if (language == EShLangMesh && ! qualifier.isTaskMemory()) {9211error(loc, "input blocks cannot be used in a mesh shader", "out", "");9212}9213break;9214case EvqVaryingOut:9215profileRequires(loc, ~EEsProfile, 150, E_GL_ARB_separate_shader_objects, "output block");9216requireStage(loc, (EShLanguageMask)(EShLangVertexMask|EShLangTessControlMask|EShLangTessEvaluationMask|9217EShLangGeometryMask|EShLangMeshMask|EShLangTaskMask), "output block");9218// ES 310 can have a block before shader_io is turned on, so skip this test for built-ins9219if (language == EShLangVertex && ! parsingBuiltins) {9220profileRequires(loc, EEsProfile, 320, Num_AEP_shader_io_blocks, AEP_shader_io_blocks, "vertex output block");9221} else if (language == EShLangMesh && qualifier.isTaskMemory()) {9222error(loc, "can only use on input blocks in mesh shader", "taskNV", "");9223} else if (language == EShLangTask && ! qualifier.isTaskMemory()) {9224error(loc, "output blocks cannot be used in a task shader", "out", "");9225}9226break;9227case EvqShared:9228if (spvVersion.spv > 0 && spvVersion.spv < EShTargetSpv_1_4) {9229error(loc, "shared block requires at least SPIR-V 1.4", "shared block", "");9230}9231profileRequires(loc, EEsProfile | ECoreProfile | ECompatibilityProfile, 0, E_GL_EXT_shared_memory_block, "shared block");9232break;9233case EvqPayload:9234profileRequires(loc, ~EEsProfile, 460, 2, extsrt, "rayPayloadNV block");9235requireStage(loc, (EShLanguageMask)(EShLangRayGenMask | EShLangAnyHitMask | EShLangClosestHitMask | EShLangMissMask),9236"rayPayloadNV block");9237break;9238case EvqPayloadIn:9239profileRequires(loc, ~EEsProfile, 460, 2, extsrt, "rayPayloadInNV block");9240requireStage(loc, (EShLanguageMask)(EShLangAnyHitMask | EShLangClosestHitMask | EShLangMissMask),9241"rayPayloadInNV block");9242break;9243case EvqHitAttr:9244profileRequires(loc, ~EEsProfile, 460, 2, extsrt, "hitAttributeNV block");9245requireStage(loc, (EShLanguageMask)(EShLangIntersectMask | EShLangAnyHitMask | EShLangClosestHitMask), "hitAttributeNV block");9246break;9247case EvqCallableData:9248profileRequires(loc, ~EEsProfile, 460, 2, extsrt, "callableDataNV block");9249requireStage(loc, (EShLanguageMask)(EShLangRayGenMask | EShLangClosestHitMask | EShLangMissMask | EShLangCallableMask),9250"callableDataNV block");9251break;9252case EvqCallableDataIn:9253profileRequires(loc, ~EEsProfile, 460, 2, extsrt, "callableDataInNV block");9254requireStage(loc, (EShLanguageMask)(EShLangCallableMask), "callableDataInNV block");9255break;9256case EvqHitObjectAttrNV:9257profileRequires(loc, ~EEsProfile, 460, E_GL_NV_shader_invocation_reorder, "hitObjectAttributeNV block");9258requireStage(loc, (EShLanguageMask)(EShLangRayGenMask | EShLangClosestHitMask | EShLangMissMask), "hitObjectAttributeNV block");9259break;9260default:9261error(loc, "only uniform, buffer, in, or out blocks are supported", blockName->c_str(), "");9262break;9263}9264}92659266// Do all block-declaration checking regarding its qualifiers.9267void TParseContext::blockQualifierCheck(const TSourceLoc& loc, const TQualifier& qualifier, bool /*instanceName*/)9268{9269// The 4.5 specification says:9270//9271// interface-block :9272// layout-qualifieropt interface-qualifier block-name { member-list } instance-nameopt ;9273//9274// interface-qualifier :9275// in9276// out9277// patch in9278// patch out9279// uniform9280// buffer9281//9282// Note however memory qualifiers aren't included, yet the specification also says9283//9284// "...memory qualifiers may also be used in the declaration of shader storage blocks..."92859286if (qualifier.isInterpolation())9287error(loc, "cannot use interpolation qualifiers on an interface block", "flat/smooth/noperspective", "");9288if (qualifier.centroid)9289error(loc, "cannot use centroid qualifier on an interface block", "centroid", "");9290if (qualifier.isSample())9291error(loc, "cannot use sample qualifier on an interface block", "sample", "");9292if (qualifier.invariant)9293error(loc, "cannot use invariant qualifier on an interface block", "invariant", "");9294if (qualifier.isPushConstant())9295intermediate.addPushConstantCount();9296if (qualifier.isShaderRecord())9297intermediate.addShaderRecordCount();9298if (qualifier.isTaskMemory())9299intermediate.addTaskNVCount();9300}93019302//9303// "For a block, this process applies to the entire block, or until the first member9304// is reached that has a location layout qualifier. When a block member is declared with a location9305// qualifier, its location comes from that qualifier: The member's location qualifier overrides the block-level9306// declaration. Subsequent members are again assigned consecutive locations, based on the newest location,9307// until the next member declared with a location qualifier. The values used for locations do not have to be9308// declared in increasing order."9309void TParseContext::fixBlockLocations(const TSourceLoc& loc, TQualifier& qualifier, TTypeList& typeList, bool memberWithLocation, bool memberWithoutLocation)9310{9311// "If a block has no block-level location layout qualifier, it is required that either all or none of its members9312// have a location layout qualifier, or a compile-time error results."9313if (! qualifier.hasLocation() && memberWithLocation && memberWithoutLocation)9314error(loc, "either the block needs a location, or all members need a location, or no members have a location", "location", "");9315else {9316if (memberWithLocation) {9317// remove any block-level location and make it per *every* member9318int nextLocation = 0; // by the rule above, initial value is not relevant9319if (qualifier.hasAnyLocation()) {9320nextLocation = qualifier.layoutLocation;9321qualifier.layoutLocation = TQualifier::layoutLocationEnd;9322if (qualifier.hasComponent()) {9323// "It is a compile-time error to apply the *component* qualifier to a ... block"9324error(loc, "cannot apply to a block", "component", "");9325}9326if (qualifier.hasIndex()) {9327error(loc, "cannot apply to a block", "index", "");9328}9329}9330for (unsigned int member = 0; member < typeList.size(); ++member) {9331TQualifier& memberQualifier = typeList[member].type->getQualifier();9332const TSourceLoc& memberLoc = typeList[member].loc;9333if (! memberQualifier.hasLocation()) {9334if (nextLocation >= (int)TQualifier::layoutLocationEnd)9335error(memberLoc, "location is too large", "location", "");9336memberQualifier.layoutLocation = nextLocation;9337memberQualifier.layoutComponent = TQualifier::layoutComponentEnd;9338}9339nextLocation = memberQualifier.layoutLocation + intermediate.computeTypeLocationSize(9340*typeList[member].type, language);9341}9342}9343}9344}93459346void TParseContext::fixXfbOffsets(TQualifier& qualifier, TTypeList& typeList)9347{9348// "If a block is qualified with xfb_offset, all its9349// members are assigned transform feedback buffer offsets. If a block is not qualified with xfb_offset, any9350// members of that block not qualified with an xfb_offset will not be assigned transform feedback buffer9351// offsets."93529353if (! qualifier.hasXfbBuffer() || ! qualifier.hasXfbOffset())9354return;93559356int nextOffset = qualifier.layoutXfbOffset;9357for (unsigned int member = 0; member < typeList.size(); ++member) {9358TQualifier& memberQualifier = typeList[member].type->getQualifier();9359bool contains64BitType = false;9360bool contains32BitType = false;9361bool contains16BitType = false;9362int memberSize = intermediate.computeTypeXfbSize(*typeList[member].type, contains64BitType, contains32BitType, contains16BitType);9363// see if we need to auto-assign an offset to this member9364if (! memberQualifier.hasXfbOffset()) {9365// "if applied to an aggregate containing a double or 64-bit integer, the offset must also be a multiple of 8"9366if (contains64BitType)9367RoundToPow2(nextOffset, 8);9368else if (contains32BitType)9369RoundToPow2(nextOffset, 4);9370else if (contains16BitType)9371RoundToPow2(nextOffset, 2);9372memberQualifier.layoutXfbOffset = nextOffset;9373} else9374nextOffset = memberQualifier.layoutXfbOffset;9375nextOffset += memberSize;9376}93779378// The above gave all block members an offset, so we can take it off the block now,9379// which will avoid double counting the offset usage.9380qualifier.layoutXfbOffset = TQualifier::layoutXfbOffsetEnd;9381}93829383// Calculate and save the offset of each block member, using the recursively9384// defined block offset rules and the user-provided offset and align.9385//9386// Also, compute and save the total size of the block. For the block's size, arrayness9387// is not taken into account, as each element is backed by a separate buffer.9388//9389void TParseContext::fixBlockUniformOffsets(TQualifier& qualifier, TTypeList& typeList)9390{9391if (!storageCanHaveLayoutInBlock(qualifier.storage) && !qualifier.isTaskMemory())9392return;9393if (qualifier.layoutPacking != ElpStd140 && qualifier.layoutPacking != ElpStd430 && qualifier.layoutPacking != ElpScalar)9394return;93959396int offset = 0;9397int memberSize;9398for (unsigned int member = 0; member < typeList.size(); ++member) {9399TQualifier& memberQualifier = typeList[member].type->getQualifier();9400const TSourceLoc& memberLoc = typeList[member].loc;94019402// "When align is applied to an array, it effects only the start of the array, not the array's internal stride."94039404// modify just the children's view of matrix layout, if there is one for this member9405TLayoutMatrix subMatrixLayout = typeList[member].type->getQualifier().layoutMatrix;9406int dummyStride;9407int memberAlignment = intermediate.getMemberAlignment(*typeList[member].type, memberSize, dummyStride, qualifier.layoutPacking,9408subMatrixLayout != ElmNone ? subMatrixLayout == ElmRowMajor : qualifier.layoutMatrix == ElmRowMajor);9409if (memberQualifier.hasOffset()) {9410// "The specified offset must be a multiple9411// of the base alignment of the type of the block member it qualifies, or a compile-time error results."9412if (! IsMultipleOfPow2(memberQualifier.layoutOffset, memberAlignment))9413error(memberLoc, "must be a multiple of the member's alignment", "offset",9414"(layout offset = %d | member alignment = %d)", memberQualifier.layoutOffset, memberAlignment);94159416// GLSL: "It is a compile-time error to specify an offset that is smaller than the offset of the previous9417// member in the block or that lies within the previous member of the block"9418if (spvVersion.spv == 0) {9419if (memberQualifier.layoutOffset < offset)9420error(memberLoc, "cannot lie in previous members", "offset", "");94219422// "The offset qualifier forces the qualified member to start at or after the specified9423// integral-constant expression, which will be its byte offset from the beginning of the buffer.9424// "The actual offset of a member is computed as9425// follows: If offset was declared, start with that offset, otherwise start with the next available offset."9426offset = std::max(offset, memberQualifier.layoutOffset);9427} else {9428// TODO: Vulkan: "It is a compile-time error to have any offset, explicit or assigned,9429// that lies within another member of the block."94309431offset = memberQualifier.layoutOffset;9432}9433}94349435// "The actual alignment of a member will be the greater of the specified align alignment and the standard9436// (e.g., std140) base alignment for the member's type."9437if (memberQualifier.hasAlign())9438memberAlignment = std::max(memberAlignment, memberQualifier.layoutAlign);94399440// "If the resulting offset is not a multiple of the actual alignment,9441// increase it to the first offset that is a multiple of9442// the actual alignment."9443RoundToPow2(offset, memberAlignment);9444typeList[member].type->getQualifier().layoutOffset = offset;9445offset += memberSize;9446}9447}94489449//9450// Spread LayoutMatrix to uniform block member, if a uniform block member is a struct,9451// we need spread LayoutMatrix to this struct member too. and keep this rule for recursive.9452//9453void TParseContext::fixBlockUniformLayoutMatrix(TQualifier& qualifier, TTypeList* originTypeList,9454TTypeList* tmpTypeList)9455{9456assert(tmpTypeList == nullptr || originTypeList->size() == tmpTypeList->size());9457for (unsigned int member = 0; member < originTypeList->size(); ++member) {9458if (qualifier.layoutPacking != ElpNone) {9459if (tmpTypeList == nullptr) {9460if (((*originTypeList)[member].type->isMatrix() ||9461(*originTypeList)[member].type->getBasicType() == EbtStruct) &&9462(*originTypeList)[member].type->getQualifier().layoutMatrix == ElmNone) {9463(*originTypeList)[member].type->getQualifier().layoutMatrix = qualifier.layoutMatrix;9464}9465} else {9466if (((*tmpTypeList)[member].type->isMatrix() ||9467(*tmpTypeList)[member].type->getBasicType() == EbtStruct) &&9468(*tmpTypeList)[member].type->getQualifier().layoutMatrix == ElmNone) {9469(*tmpTypeList)[member].type->getQualifier().layoutMatrix = qualifier.layoutMatrix;9470}9471}9472}94739474if ((*originTypeList)[member].type->getBasicType() == EbtStruct) {9475TQualifier* memberQualifier = nullptr;9476// block member can be declare a matrix style, so it should be update to the member's style9477if ((*originTypeList)[member].type->getQualifier().layoutMatrix == ElmNone) {9478memberQualifier = &qualifier;9479} else {9480memberQualifier = &((*originTypeList)[member].type->getQualifier());9481}94829483const TType* tmpType = tmpTypeList == nullptr ?9484(*originTypeList)[member].type->clone() : (*tmpTypeList)[member].type;94859486fixBlockUniformLayoutMatrix(*memberQualifier, (*originTypeList)[member].type->getWritableStruct(),9487tmpType->getWritableStruct());94889489const TTypeList* structure = recordStructCopy(matrixFixRecord, (*originTypeList)[member].type, tmpType);94909491if (tmpTypeList == nullptr) {9492(*originTypeList)[member].type->setStruct(const_cast<TTypeList*>(structure));9493}9494if (tmpTypeList != nullptr) {9495(*tmpTypeList)[member].type->setStruct(const_cast<TTypeList*>(structure));9496}9497}9498}9499}95009501//9502// Spread LayoutPacking to matrix or aggregate block members. If a block member is a struct or9503// array of struct, spread LayoutPacking recursively to its matrix or aggregate members.9504//9505void TParseContext::fixBlockUniformLayoutPacking(TQualifier& qualifier, TTypeList* originTypeList,9506TTypeList* tmpTypeList)9507{9508assert(tmpTypeList == nullptr || originTypeList->size() == tmpTypeList->size());9509for (unsigned int member = 0; member < originTypeList->size(); ++member) {9510if (qualifier.layoutPacking != ElpNone) {9511if (tmpTypeList == nullptr) {9512if ((*originTypeList)[member].type->getQualifier().layoutPacking == ElpNone &&9513!(*originTypeList)[member].type->isScalarOrVector()) {9514(*originTypeList)[member].type->getQualifier().layoutPacking = qualifier.layoutPacking;9515}9516} else {9517if ((*tmpTypeList)[member].type->getQualifier().layoutPacking == ElpNone &&9518!(*tmpTypeList)[member].type->isScalarOrVector()) {9519(*tmpTypeList)[member].type->getQualifier().layoutPacking = qualifier.layoutPacking;9520}9521}9522}95239524if ((*originTypeList)[member].type->getBasicType() == EbtStruct) {9525// Deep copy the type in pool.9526// Because, struct use in different block may have different layout qualifier.9527// We have to new a object to distinguish between them.9528const TType* tmpType = tmpTypeList == nullptr ?9529(*originTypeList)[member].type->clone() : (*tmpTypeList)[member].type;95309531fixBlockUniformLayoutPacking(qualifier, (*originTypeList)[member].type->getWritableStruct(),9532tmpType->getWritableStruct());95339534const TTypeList* structure = recordStructCopy(packingFixRecord, (*originTypeList)[member].type, tmpType);95359536if (tmpTypeList == nullptr) {9537(*originTypeList)[member].type->setStruct(const_cast<TTypeList*>(structure));9538}9539if (tmpTypeList != nullptr) {9540(*tmpTypeList)[member].type->setStruct(const_cast<TTypeList*>(structure));9541}9542}9543}9544}95459546// For an identifier that is already declared, add more qualification to it.9547void TParseContext::addQualifierToExisting(const TSourceLoc& loc, TQualifier qualifier, const TString& identifier)9548{9549TSymbol* symbol = symbolTable.find(identifier);95509551// A forward declaration of a block reference looks to the grammar like adding9552// a qualifier to an existing symbol. Detect this and create the block reference9553// type with an empty type list, which will be filled in later in9554// TParseContext::declareBlock.9555if (!symbol && qualifier.hasBufferReference()) {9556TTypeList typeList;9557TType blockType(&typeList, identifier, qualifier);9558TType blockNameType(EbtReference, blockType, identifier);9559TVariable* blockNameVar = new TVariable(&identifier, blockNameType, true);9560if (! symbolTable.insert(*blockNameVar)) {9561error(loc, "block name cannot redefine a non-block name", blockName->c_str(), "");9562}9563return;9564}95659566if (! symbol) {9567error(loc, "identifier not previously declared", identifier.c_str(), "");9568return;9569}9570if (symbol->getAsFunction()) {9571error(loc, "cannot re-qualify a function name", identifier.c_str(), "");9572return;9573}95749575if (qualifier.isAuxiliary() ||9576qualifier.isMemory() ||9577qualifier.isInterpolation() ||9578qualifier.hasLayout() ||9579qualifier.storage != EvqTemporary ||9580qualifier.precision != EpqNone) {9581error(loc, "cannot add storage, auxiliary, memory, interpolation, layout, or precision qualifier to an existing variable", identifier.c_str(), "");9582return;9583}95849585// For read-only built-ins, add a new symbol for holding the modified qualifier.9586// This will bring up an entire block, if a block type has to be modified (e.g., gl_Position inside a block)9587if (symbol->isReadOnly())9588symbol = symbolTable.copyUp(symbol);95899590if (qualifier.invariant) {9591if (intermediate.inIoAccessed(identifier))9592error(loc, "cannot change qualification after use", "invariant", "");9593symbol->getWritableType().getQualifier().invariant = true;9594invariantCheck(loc, symbol->getType().getQualifier());9595} else if (qualifier.isNoContraction()) {9596if (intermediate.inIoAccessed(identifier))9597error(loc, "cannot change qualification after use", "precise", "");9598symbol->getWritableType().getQualifier().setNoContraction();9599} else if (qualifier.specConstant) {9600symbol->getWritableType().getQualifier().makeSpecConstant();9601if (qualifier.hasSpecConstantId())9602symbol->getWritableType().getQualifier().layoutSpecConstantId = qualifier.layoutSpecConstantId;9603} else9604warn(loc, "unknown requalification", "", "");9605}96069607void TParseContext::addQualifierToExisting(const TSourceLoc& loc, TQualifier qualifier, TIdentifierList& identifiers)9608{9609for (unsigned int i = 0; i < identifiers.size(); ++i)9610addQualifierToExisting(loc, qualifier, *identifiers[i]);9611}96129613// Make sure 'invariant' isn't being applied to a non-allowed object.9614void TParseContext::invariantCheck(const TSourceLoc& loc, const TQualifier& qualifier)9615{9616if (! qualifier.invariant)9617return;96189619bool pipeOut = qualifier.isPipeOutput();9620bool pipeIn = qualifier.isPipeInput();9621if ((version >= 300 && isEsProfile()) || (!isEsProfile() && version >= 420)) {9622if (! pipeOut)9623error(loc, "can only apply to an output", "invariant", "");9624} else {9625if ((language == EShLangVertex && pipeIn) || (! pipeOut && ! pipeIn))9626error(loc, "can only apply to an output, or to an input in a non-vertex stage\n", "invariant", "");9627}9628}96299630//9631// Updating default qualifier for the case of a declaration with just a qualifier,9632// no type, block, or identifier.9633//9634void TParseContext::updateStandaloneQualifierDefaults(const TSourceLoc& loc, const TPublicType& publicType)9635{9636if (publicType.shaderQualifiers.vertices != TQualifier::layoutNotSet) {9637assert(language == EShLangTessControl || language == EShLangGeometry || language == EShLangMesh);9638const char* id = (language == EShLangTessControl) ? "vertices" : "max_vertices";96399640if (publicType.qualifier.storage != EvqVaryingOut)9641error(loc, "can only apply to 'out'", id, "");9642if (! intermediate.setVertices(publicType.shaderQualifiers.vertices))9643error(loc, "cannot change previously set layout value", id, "");96449645if (language == EShLangTessControl)9646checkIoArraysConsistency(loc);9647}9648if (publicType.shaderQualifiers.primitives != TQualifier::layoutNotSet) {9649assert(language == EShLangMesh);9650const char* id = "max_primitives";96519652if (publicType.qualifier.storage != EvqVaryingOut)9653error(loc, "can only apply to 'out'", id, "");9654if (! intermediate.setPrimitives(publicType.shaderQualifiers.primitives))9655error(loc, "cannot change previously set layout value", id, "");9656}9657if (publicType.shaderQualifiers.invocations != TQualifier::layoutNotSet) {9658if (publicType.qualifier.storage != EvqVaryingIn)9659error(loc, "can only apply to 'in'", "invocations", "");9660if (! intermediate.setInvocations(publicType.shaderQualifiers.invocations))9661error(loc, "cannot change previously set layout value", "invocations", "");9662}9663if (publicType.shaderQualifiers.geometry != ElgNone) {9664if (publicType.qualifier.storage == EvqVaryingIn) {9665switch (publicType.shaderQualifiers.geometry) {9666case ElgPoints:9667case ElgLines:9668case ElgLinesAdjacency:9669case ElgTriangles:9670case ElgTrianglesAdjacency:9671case ElgQuads:9672case ElgIsolines:9673if (language == EShLangMesh) {9674error(loc, "cannot apply to input", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9675break;9676}9677if (intermediate.setInputPrimitive(publicType.shaderQualifiers.geometry)) {9678if (language == EShLangGeometry)9679checkIoArraysConsistency(loc);9680} else9681error(loc, "cannot change previously set input primitive", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9682break;9683default:9684error(loc, "cannot apply to input", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9685}9686} else if (publicType.qualifier.storage == EvqVaryingOut) {9687switch (publicType.shaderQualifiers.geometry) {9688case ElgLines:9689case ElgTriangles:9690if (language != EShLangMesh) {9691error(loc, "cannot apply to 'out'", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9692break;9693}9694[[fallthrough]];9695case ElgPoints:9696case ElgLineStrip:9697case ElgTriangleStrip:9698if (! intermediate.setOutputPrimitive(publicType.shaderQualifiers.geometry))9699error(loc, "cannot change previously set output primitive", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9700break;9701default:9702error(loc, "cannot apply to 'out'", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), "");9703}9704} else9705error(loc, "cannot apply to:", TQualifier::getGeometryString(publicType.shaderQualifiers.geometry), GetStorageQualifierString(publicType.qualifier.storage));9706}9707if (publicType.shaderQualifiers.spacing != EvsNone) {9708if (publicType.qualifier.storage == EvqVaryingIn) {9709if (! intermediate.setVertexSpacing(publicType.shaderQualifiers.spacing))9710error(loc, "cannot change previously set vertex spacing", TQualifier::getVertexSpacingString(publicType.shaderQualifiers.spacing), "");9711} else9712error(loc, "can only apply to 'in'", TQualifier::getVertexSpacingString(publicType.shaderQualifiers.spacing), "");9713}9714if (publicType.shaderQualifiers.order != EvoNone) {9715if (publicType.qualifier.storage == EvqVaryingIn) {9716if (! intermediate.setVertexOrder(publicType.shaderQualifiers.order))9717error(loc, "cannot change previously set vertex order", TQualifier::getVertexOrderString(publicType.shaderQualifiers.order), "");9718} else9719error(loc, "can only apply to 'in'", TQualifier::getVertexOrderString(publicType.shaderQualifiers.order), "");9720}9721if (publicType.shaderQualifiers.pointMode) {9722if (publicType.qualifier.storage == EvqVaryingIn)9723intermediate.setPointMode();9724else9725error(loc, "can only apply to 'in'", "point_mode", "");9726}97279728for (int i = 0; i < 3; ++i) {9729if (publicType.shaderQualifiers.localSizeNotDefault[i]) {9730if (publicType.qualifier.storage == EvqVaryingIn) {9731if (! intermediate.setLocalSize(i, publicType.shaderQualifiers.localSize[i]))9732error(loc, "cannot change previously set size", "local_size", "");9733else {9734int max = 0;9735if (language == EShLangCompute) {9736switch (i) {9737case 0: max = resources.maxComputeWorkGroupSizeX; break;9738case 1: max = resources.maxComputeWorkGroupSizeY; break;9739case 2: max = resources.maxComputeWorkGroupSizeZ; break;9740default: break;9741}9742if (intermediate.getLocalSize(i) > (unsigned int)max)9743error(loc, "too large; see gl_MaxComputeWorkGroupSize", "local_size", "");9744} else if (language == EShLangMesh) {9745switch (i) {9746case 0:9747max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9748resources.maxMeshWorkGroupSizeX_EXT :9749resources.maxMeshWorkGroupSizeX_NV;9750break;9751case 1:9752max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9753resources.maxMeshWorkGroupSizeY_EXT :9754resources.maxMeshWorkGroupSizeY_NV ;9755break;9756case 2:9757max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9758resources.maxMeshWorkGroupSizeZ_EXT :9759resources.maxMeshWorkGroupSizeZ_NV ;9760break;9761default: break;9762}9763if (intermediate.getLocalSize(i) > (unsigned int)max) {9764TString maxsErrtring = "too large, see ";9765maxsErrtring.append(extensionTurnedOn(E_GL_EXT_mesh_shader) ?9766"gl_MaxMeshWorkGroupSizeEXT" : "gl_MaxMeshWorkGroupSizeNV");9767error(loc, maxsErrtring.c_str(), "local_size", "");9768}9769} else if (language == EShLangTask) {9770switch (i) {9771case 0:9772max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9773resources.maxTaskWorkGroupSizeX_EXT :9774resources.maxTaskWorkGroupSizeX_NV;9775break;9776case 1:9777max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9778resources.maxTaskWorkGroupSizeY_EXT:9779resources.maxTaskWorkGroupSizeY_NV;9780break;9781case 2:9782max = extensionTurnedOn(E_GL_EXT_mesh_shader) ?9783resources.maxTaskWorkGroupSizeZ_EXT:9784resources.maxTaskWorkGroupSizeZ_NV;9785break;9786default: break;9787}9788if (intermediate.getLocalSize(i) > (unsigned int)max) {9789TString maxsErrtring = "too large, see ";9790maxsErrtring.append(extensionTurnedOn(E_GL_EXT_mesh_shader) ?9791"gl_MaxTaskWorkGroupSizeEXT" : "gl_MaxTaskWorkGroupSizeNV");9792error(loc, maxsErrtring.c_str(), "local_size", "");9793}9794} else {9795assert(0);9796}97979798// Fix the existing constant gl_WorkGroupSize with this new information.9799TVariable* workGroupSize = getEditableVariable("gl_WorkGroupSize");9800if (workGroupSize != nullptr)9801workGroupSize->getWritableConstArray()[i].setUConst(intermediate.getLocalSize(i));9802}9803} else9804error(loc, "can only apply to 'in'", "local_size", "");9805}9806if (publicType.shaderQualifiers.localSizeSpecId[i] != TQualifier::layoutNotSet) {9807if (publicType.qualifier.storage == EvqVaryingIn) {9808if (! intermediate.setLocalSizeSpecId(i, publicType.shaderQualifiers.localSizeSpecId[i]))9809error(loc, "cannot change previously set size", "local_size", "");9810} else9811error(loc, "can only apply to 'in'", "local_size id", "");9812// Set the workgroup built-in variable as a specialization constant9813TVariable* workGroupSize = getEditableVariable("gl_WorkGroupSize");9814if (workGroupSize != nullptr)9815workGroupSize->getWritableType().getQualifier().specConstant = true;9816}9817}98189819if (publicType.shaderQualifiers.earlyFragmentTests) {9820if (publicType.qualifier.storage == EvqVaryingIn)9821intermediate.setEarlyFragmentTests();9822else9823error(loc, "can only apply to 'in'", "early_fragment_tests", "");9824}9825if (publicType.shaderQualifiers.earlyAndLateFragmentTestsAMD) {9826if (publicType.qualifier.storage == EvqVaryingIn)9827intermediate.setEarlyAndLateFragmentTestsAMD();9828else9829error(loc, "can only apply to 'in'", "early_and_late_fragment_tests_amd", "");9830}9831if (publicType.shaderQualifiers.postDepthCoverage) {9832if (publicType.qualifier.storage == EvqVaryingIn)9833intermediate.setPostDepthCoverage();9834else9835error(loc, "can only apply to 'in'", "post_coverage_coverage", "");9836}9837if (publicType.shaderQualifiers.nonCoherentColorAttachmentReadEXT) {9838if (publicType.qualifier.storage == EvqVaryingIn)9839intermediate.setNonCoherentColorAttachmentReadEXT();9840else9841error(loc, "can only apply to 'in'", "non_coherent_color_attachment_readEXT", "");9842}9843if (publicType.shaderQualifiers.nonCoherentDepthAttachmentReadEXT) {9844if (publicType.qualifier.storage == EvqVaryingIn)9845intermediate.setNonCoherentDepthAttachmentReadEXT();9846else9847error(loc, "can only apply to 'in'", "non_coherent_depth_attachment_readEXT", "");9848}9849if (publicType.shaderQualifiers.nonCoherentStencilAttachmentReadEXT) {9850if (publicType.qualifier.storage == EvqVaryingIn)9851intermediate.setNonCoherentStencilAttachmentReadEXT();9852else9853error(loc, "can only apply to 'in'", "non_coherent_stencil_attachment_readEXT", "");9854}9855if (publicType.shaderQualifiers.hasBlendEquation()) {9856if (publicType.qualifier.storage != EvqVaryingOut)9857error(loc, "can only apply to 'out'", "blend equation", "");9858}9859if (publicType.shaderQualifiers.interlockOrdering) {9860if (publicType.qualifier.storage == EvqVaryingIn) {9861if (!intermediate.setInterlockOrdering(publicType.shaderQualifiers.interlockOrdering))9862error(loc, "cannot change previously set fragment shader interlock ordering", TQualifier::getInterlockOrderingString(publicType.shaderQualifiers.interlockOrdering), "");9863}9864else9865error(loc, "can only apply to 'in'", TQualifier::getInterlockOrderingString(publicType.shaderQualifiers.interlockOrdering), "");9866}98679868if (publicType.shaderQualifiers.layoutDerivativeGroupQuads &&9869publicType.shaderQualifiers.layoutDerivativeGroupLinear) {9870error(loc, "cannot be both specified", "derivative_group_quadsNV and derivative_group_linearNV", "");9871}98729873if (publicType.shaderQualifiers.layoutDerivativeGroupQuads) {9874if (publicType.qualifier.storage == EvqVaryingIn) {9875if ((intermediate.getLocalSize(0) & 1) ||9876(intermediate.getLocalSize(1) & 1))9877error(loc, "requires local_size_x and local_size_y to be multiple of two", "derivative_group_quadsNV", "");9878else9879intermediate.setLayoutDerivativeMode(LayoutDerivativeGroupQuads);9880}9881else9882error(loc, "can only apply to 'in'", "derivative_group_quadsNV", "");9883}9884if (publicType.shaderQualifiers.layoutDerivativeGroupLinear) {9885if (publicType.qualifier.storage == EvqVaryingIn) {9886if((intermediate.getLocalSize(0) *9887intermediate.getLocalSize(1) *9888intermediate.getLocalSize(2)) % 4 != 0)9889error(loc, "requires total group size to be multiple of four", "derivative_group_linearNV", "");9890else9891intermediate.setLayoutDerivativeMode(LayoutDerivativeGroupLinear);9892}9893else9894error(loc, "can only apply to 'in'", "derivative_group_linearNV", "");9895}9896// Check mesh out array sizes, once all the necessary out qualifiers are defined.9897if ((language == EShLangMesh) &&9898(intermediate.getVertices() != TQualifier::layoutNotSet) &&9899(intermediate.getPrimitives() != TQualifier::layoutNotSet) &&9900(intermediate.getOutputPrimitive() != ElgNone))9901{9902checkIoArraysConsistency(loc);9903}99049905if (publicType.shaderQualifiers.layoutPrimitiveCulling) {9906if (publicType.qualifier.storage != EvqTemporary)9907error(loc, "layout qualifier can not have storage qualifiers", "primitive_culling","", "");9908else {9909intermediate.setLayoutPrimitiveCulling();9910}9911// Exit early as further checks are not valid9912return;9913}99149915const TQualifier& qualifier = publicType.qualifier;99169917if (qualifier.isAuxiliary() ||9918qualifier.isMemory() ||9919qualifier.isInterpolation() ||9920qualifier.precision != EpqNone)9921error(loc, "cannot use auxiliary, memory, interpolation, or precision qualifier in a default qualifier declaration (declaration with no type)", "qualifier", "");99229923// "The offset qualifier can only be used on block members of blocks..."9924// "The align qualifier can only be used on blocks or block members..."9925if (qualifier.hasOffset() ||9926qualifier.hasAlign())9927error(loc, "cannot use offset or align qualifiers in a default qualifier declaration (declaration with no type)", "layout qualifier", "");99289929layoutQualifierCheck(loc, qualifier);99309931switch (qualifier.storage) {9932case EvqUniform:9933if (qualifier.hasMatrix())9934globalUniformDefaults.layoutMatrix = qualifier.layoutMatrix;9935if (qualifier.hasPacking())9936globalUniformDefaults.layoutPacking = qualifier.layoutPacking;9937break;9938case EvqBuffer:9939if (qualifier.hasMatrix())9940globalBufferDefaults.layoutMatrix = qualifier.layoutMatrix;9941if (qualifier.hasPacking())9942globalBufferDefaults.layoutPacking = qualifier.layoutPacking;9943break;9944case EvqVaryingIn:9945break;9946case EvqVaryingOut:9947if (qualifier.hasStream())9948globalOutputDefaults.layoutStream = qualifier.layoutStream;9949if (qualifier.hasXfbBuffer())9950globalOutputDefaults.layoutXfbBuffer = qualifier.layoutXfbBuffer;9951if (globalOutputDefaults.hasXfbBuffer() && qualifier.hasXfbStride()) {9952if (! intermediate.setXfbBufferStride(globalOutputDefaults.layoutXfbBuffer, qualifier.layoutXfbStride))9953error(loc, "all stride settings must match for xfb buffer", "xfb_stride", "%d", qualifier.layoutXfbBuffer);9954}9955break;9956case EvqShared:9957if (qualifier.hasMatrix())9958globalSharedDefaults.layoutMatrix = qualifier.layoutMatrix;9959if (qualifier.hasPacking())9960globalSharedDefaults.layoutPacking = qualifier.layoutPacking;9961break;9962default:9963error(loc, "default qualifier requires 'uniform', 'buffer', 'in', 'out' or 'shared' storage qualification", "", "");9964return;9965}99669967if (qualifier.hasBinding())9968error(loc, "cannot declare a default, include a type or full declaration", "binding", "");9969if (qualifier.hasAnyLocation())9970error(loc, "cannot declare a default, use a full declaration", "location/component/index", "");9971if (qualifier.hasXfbOffset())9972error(loc, "cannot declare a default, use a full declaration", "xfb_offset", "");9973if (qualifier.isPushConstant())9974error(loc, "cannot declare a default, can only be used on a block", "push_constant", "");9975if (qualifier.hasBufferReference())9976error(loc, "cannot declare a default, can only be used on a block", "buffer_reference", "");9977if (qualifier.hasSpecConstantId())9978error(loc, "cannot declare a default, can only be used on a scalar", "constant_id", "");9979if (qualifier.isShaderRecord())9980error(loc, "cannot declare a default, can only be used on a block", "shaderRecordNV", "");9981}99829983//9984// Take the sequence of statements that has been built up since the last case/default,9985// put it on the list of top-level nodes for the current (inner-most) switch statement,9986// and follow that by the case/default we are on now. (See switch topology comment on9987// TIntermSwitch.)9988//9989void TParseContext::wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode)9990{9991TIntermSequence* switchSequence = switchSequenceStack.back();99929993if (statements) {9994if (switchSequence->size() == 0)9995error(statements->getLoc(), "cannot have statements before first case/default label", "switch", "");9996statements->setOperator(EOpSequence);9997switchSequence->push_back(statements);9998}9999if (branchNode) {10000// check all previous cases for the same label (or both are 'default')10001for (unsigned int s = 0; s < switchSequence->size(); ++s) {10002TIntermBranch* prevBranch = (*switchSequence)[s]->getAsBranchNode();10003if (prevBranch) {10004TIntermTyped* prevExpression = prevBranch->getExpression();10005TIntermTyped* newExpression = branchNode->getAsBranchNode()->getExpression();10006if (prevExpression == nullptr && newExpression == nullptr)10007error(branchNode->getLoc(), "duplicate label", "default", "");10008else if (prevExpression != nullptr &&10009newExpression != nullptr &&10010prevExpression->getAsConstantUnion() &&10011newExpression->getAsConstantUnion() &&10012prevExpression->getAsConstantUnion()->getConstArray()[0].getIConst() ==10013newExpression->getAsConstantUnion()->getConstArray()[0].getIConst())10014error(branchNode->getLoc(), "duplicated value", "case", "");10015}10016}10017switchSequence->push_back(branchNode);10018}10019}1002010021//10022// Turn the top-level node sequence built up of wrapupSwitchSubsequence9)10023// into a switch node.10024//10025TIntermNode* TParseContext::addSwitch(const TSourceLoc& loc, TIntermTyped* expression, TIntermAggregate* lastStatements)10026{10027profileRequires(loc, EEsProfile, 300, nullptr, "switch statements");10028profileRequires(loc, ENoProfile, 130, nullptr, "switch statements");1002910030wrapupSwitchSubsequence(lastStatements, nullptr);1003110032if (expression == nullptr ||10033(expression->getBasicType() != EbtInt && expression->getBasicType() != EbtUint) ||10034expression->getType().isArray() || expression->getType().isMatrix() || expression->getType().isVector())10035error(loc, "condition must be a scalar integer expression", "switch", "");1003610037// If there is nothing to do, drop the switch but still execute the expression10038TIntermSequence* switchSequence = switchSequenceStack.back();10039if (switchSequence->size() == 0)10040return expression;1004110042if (lastStatements == nullptr) {10043// This was originally an ERRROR, because early versions of the specification said10044// "it is an error to have no statement between a label and the end of the switch statement."10045// The specifications were updated to remove this (being ill-defined what a "statement" was),10046// so, this became a warning. However, 3.0 tests still check for the error.10047if (isEsProfile() && (version <= 300 || version >= 320) && ! relaxedErrors())10048error(loc, "last case/default label not followed by statements", "switch", "");10049else if (!isEsProfile() && (version <= 430 || version >= 460))10050error(loc, "last case/default label not followed by statements", "switch", "");10051else10052warn(loc, "last case/default label not followed by statements", "switch", "");100531005410055// emulate a break for error recovery10056lastStatements = intermediate.makeAggregate(intermediate.addBranch(EOpBreak, loc));10057lastStatements->setOperator(EOpSequence);10058switchSequence->push_back(lastStatements);10059}1006010061TIntermAggregate* body = new TIntermAggregate(EOpSequence);10062body->getSequence() = *switchSequenceStack.back();10063body->setLoc(loc);1006410065TIntermSwitch* switchNode = new TIntermSwitch(expression, body);10066switchNode->setLoc(loc);1006710068return switchNode;10069}1007010071//10072// When a struct used in block, and has it's own layout packing, layout matrix,10073// record the origin structure of a struct to map, and Record the structure copy to the copy table,10074//10075const TTypeList* TParseContext::recordStructCopy(TStructRecord& record, const TType* originType, const TType* tmpType)10076{10077size_t memberCount = tmpType->getStruct()->size();10078size_t originHash = 0, tmpHash = 0;10079std::hash<size_t> hasher;10080for (size_t i = 0; i < memberCount; i++) {10081size_t originMemberHash = hasher(originType->getStruct()->at(i).type->getQualifier().layoutPacking +10082originType->getStruct()->at(i).type->getQualifier().layoutMatrix);10083size_t tmpMemberHash = hasher(tmpType->getStruct()->at(i).type->getQualifier().layoutPacking +10084tmpType->getStruct()->at(i).type->getQualifier().layoutMatrix);10085originHash = hasher((originHash ^ originMemberHash) << 1);10086tmpHash = hasher((tmpHash ^ tmpMemberHash) << 1);10087}10088const TTypeList* originStruct = originType->getStruct();10089const TTypeList* tmpStruct = tmpType->getStruct();10090if (originHash != tmpHash) {10091auto fixRecords = record.find(originStruct);10092if (fixRecords != record.end()) {10093auto fixRecord = fixRecords->second.find(tmpHash);10094if (fixRecord != fixRecords->second.end()) {10095return fixRecord->second;10096} else {10097record[originStruct][tmpHash] = tmpStruct;10098return tmpStruct;10099}10100} else {10101record[originStruct] = std::map<size_t, const TTypeList*>();10102record[originStruct][tmpHash] = tmpStruct;10103return tmpStruct;10104}10105}10106return originStruct;10107}1010810109TLayoutFormat TParseContext::mapLegacyLayoutFormat(TLayoutFormat legacyLayoutFormat, TBasicType imageType)10110{10111TLayoutFormat layoutFormat = ElfNone;10112if (imageType == EbtFloat) {10113switch (legacyLayoutFormat) {10114case ElfSize1x16: layoutFormat = ElfR16f; break;10115case ElfSize1x32: layoutFormat = ElfR32f; break;10116case ElfSize2x32: layoutFormat = ElfRg32f; break;10117case ElfSize4x32: layoutFormat = ElfRgba32f; break;10118default: break;10119}10120} else if (imageType == EbtUint) {10121switch (legacyLayoutFormat) {10122case ElfSize1x8: layoutFormat = ElfR8ui; break;10123case ElfSize1x16: layoutFormat = ElfR16ui; break;10124case ElfSize1x32: layoutFormat = ElfR32ui; break;10125case ElfSize2x32: layoutFormat = ElfRg32ui; break;10126case ElfSize4x32: layoutFormat = ElfRgba32ui; break;10127default: break;10128}10129} else if (imageType == EbtInt) {10130switch (legacyLayoutFormat) {10131case ElfSize1x8: layoutFormat = ElfR8i; break;10132case ElfSize1x16: layoutFormat = ElfR16i; break;10133case ElfSize1x32: layoutFormat = ElfR32i; break;10134case ElfSize2x32: layoutFormat = ElfRg32i; break;10135case ElfSize4x32: layoutFormat = ElfRgba32i; break;10136default: break;10137}10138}1013910140return layoutFormat;10141}1014210143} // end namespace glslang101441014510146