1 // 2 // Copyright (C) 2016-2018 Google, Inc. 3 // Copyright (C) 2016 LunarG, Inc. 4 // 5 // All rights reserved. 6 // 7 // Redistribution and use in source and binary forms, with or without 8 // modification, are permitted provided that the following conditions 9 // are met: 10 // 11 // Redistributions of source code must retain the above copyright 12 // notice, this list of conditions and the following disclaimer. 13 // 14 // Redistributions in binary form must reproduce the above 15 // copyright notice, this list of conditions and the following 16 // disclaimer in the documentation and/or other materials provided 17 // with the distribution. 18 // 19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its 20 // contributors may be used to endorse or promote products derived 21 // from this software without specific prior written permission. 22 // 23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 // POSSIBILITY OF SUCH DAMAGE. 35 // 36 #ifndef HLSL_PARSE_INCLUDED_ 37 #define HLSL_PARSE_INCLUDED_ 38 39 #include "../MachineIndependent/parseVersions.h" 40 #include "../MachineIndependent/ParseHelper.h" 41 #include "../MachineIndependent/attribute.h" 42 43 #include <array> 44 45 namespace glslang { 46 47 class TFunctionDeclarator; 48 49 class HlslParseContext : public TParseContextBase { 50 public: 51 HlslParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins, 52 int version, EProfile, const SpvVersion& spvVersion, EShLanguage, TInfoSink&, 53 const TString sourceEntryPointName, 54 bool forwardCompatible = false, EShMessages messages = EShMsgDefault); 55 virtual ~HlslParseContext(); 56 void initializeExtensionBehavior() override; 57 58 void setLimits(const TBuiltInResource&) override; 59 bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false) override; getGlobalUniformBlockName()60 virtual const char* getGlobalUniformBlockName() const override { return "$Global"; } setUniformBlockDefaults(TType & block)61 virtual void setUniformBlockDefaults(TType& block) const override 62 { 63 block.getQualifier().layoutPacking = globalUniformDefaults.layoutPacking; 64 block.getQualifier().layoutMatrix = globalUniformDefaults.layoutMatrix; 65 } 66 reservedPpErrorCheck(const TSourceLoc &,const char *,const char *)67 void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) override { } lineContinuationCheck(const TSourceLoc &,bool)68 bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) override { return true; } lineDirectiveShouldSetNextLine()69 bool lineDirectiveShouldSetNextLine() const override { return true; } 70 bool builtInName(const TString&); 71 72 void handlePragma(const TSourceLoc&, const TVector<TString>&) override; 73 TIntermTyped* handleVariable(const TSourceLoc&, const TString* string); 74 TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index); 75 TIntermTyped* handleBracketOperator(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index); 76 77 TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right); 78 TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode); 79 TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field); 80 bool isBuiltInMethod(const TSourceLoc&, TIntermTyped* base, const TString& field); 81 void assignToInterface(TVariable& variable); 82 void handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype); 83 TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&, const TAttributes&, TIntermNode*& entryPointTree); 84 TIntermNode* transformEntryPoint(const TSourceLoc&, TFunction&, const TAttributes&); 85 void handleEntryPointAttributes(const TSourceLoc&, const TAttributes&); 86 void transferTypeAttributes(const TSourceLoc&, const TAttributes&, TType&, bool allowEntry = false); 87 void handleFunctionBody(const TSourceLoc&, TFunction&, TIntermNode* functionBody, TIntermNode*& node); 88 void remapEntryPointIO(TFunction& function, TVariable*& returnValue, TVector<TVariable*>& inputs, TVector<TVariable*>& outputs); 89 void remapNonEntryPointIO(TFunction& function); 90 TIntermNode* handleDeclare(const TSourceLoc&, TIntermTyped*); 91 TIntermNode* handleReturnValue(const TSourceLoc&, TIntermTyped*); 92 void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg); 93 TIntermTyped* handleAssign(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right); 94 TIntermTyped* handleAssignToMatrixSwizzle(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right); 95 TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermTyped*); 96 TIntermAggregate* assignClipCullDistance(const TSourceLoc&, TOperator, int semanticId, TIntermTyped* left, TIntermTyped* right); 97 TIntermTyped* assignPosition(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right); 98 TIntermTyped* assignFromFragCoord(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right); 99 void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); 100 void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); 101 void decomposeStructBufferMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); 102 void decomposeGeometryMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments); 103 void pushFrontArguments(TIntermTyped* front, TIntermTyped*& arguments); 104 void addInputArgumentConversions(const TFunction&, TIntermTyped*&); 105 void expandArguments(const TSourceLoc&, const TFunction&, TIntermTyped*&); 106 TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermOperator&); 107 void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&); 108 TFunction* makeConstructorCall(const TSourceLoc&, const TType&); 109 void handleSemantic(TSourceLoc, TQualifier&, TBuiltInVariable, const TString& upperCase); 110 void handlePackOffset(const TSourceLoc&, TQualifier&, const glslang::TString& location, 111 const glslang::TString* component); 112 void handleRegister(const TSourceLoc&, TQualifier&, const glslang::TString* profile, const glslang::TString& desc, 113 int subComponent, const glslang::TString*); 114 TIntermTyped* convertConditionalExpression(const TSourceLoc&, TIntermTyped*, bool mustBeScalar = true); 115 TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler); 116 117 bool parseMatrixSwizzleSelector(const TSourceLoc&, const TString&, int cols, int rows, TSwizzleSelectors<TMatrixSelector>&); 118 int getMatrixComponentsColumn(int rows, const TSwizzleSelectors<TMatrixSelector>&); 119 void assignError(const TSourceLoc&, const char* op, TString left, TString right); 120 void unaryOpError(const TSourceLoc&, const char* op, TString operand); 121 void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right); 122 void variableCheck(TIntermTyped*& nodePtr); 123 void constantValueCheck(TIntermTyped* node, const char* token); 124 void integerCheck(const TIntermTyped* node, const char* token); 125 void globalCheck(const TSourceLoc&, const char* token); 126 bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&); 127 void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, TArraySize&); 128 void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&); 129 void structArrayCheck(const TSourceLoc&, const TType& structure); 130 bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType); 131 void globalQualifierFix(const TSourceLoc&, TQualifier&); 132 bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType); 133 void mergeQualifiers(TQualifier& dst, const TQualifier& src); 134 int computeSamplerTypeIndex(TSampler&); 135 TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&); 136 void paramFix(TType& type); 137 void specializationCheck(const TSourceLoc&, const TType&, const char* op); 138 139 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&); 140 void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&, const TIntermTyped*); 141 void setSpecConstantId(const TSourceLoc&, TQualifier&, int value); 142 void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly); 143 void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&); 144 145 const TFunction* findFunction(const TSourceLoc& loc, TFunction& call, bool& builtIn, int& thisDepth, TIntermTyped*& args); 146 void addGenMulArgumentConversion(const TSourceLoc& loc, TFunction& call, TIntermTyped*& args); 147 void declareTypedef(const TSourceLoc&, const TString& identifier, const TType&); 148 void declareStruct(const TSourceLoc&, TString& structName, TType&); 149 TSymbol* lookupUserType(const TString&, TType&); 150 TIntermNode* declareVariable(const TSourceLoc&, const TString& identifier, TType&, TIntermTyped* initializer = nullptr); 151 void lengthenList(const TSourceLoc&, TIntermSequence& list, int size, TIntermTyped* scalarInit); 152 TIntermTyped* handleConstructor(const TSourceLoc&, TIntermTyped*, const TType&); 153 TIntermTyped* addConstructor(const TSourceLoc&, TIntermTyped*, const TType&); 154 TIntermTyped* convertArray(TIntermTyped*, const TType&); 155 TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&); 156 TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset); 157 void declareBlock(const TSourceLoc&, TType&, const TString* instanceName = nullptr); 158 void declareStructBufferCounter(const TSourceLoc& loc, const TType& bufferType, const TString& name); 159 void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation); 160 void fixXfbOffsets(TQualifier&, TTypeList&); 161 void fixBlockUniformOffsets(const TQualifier&, TTypeList&); 162 void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier); 163 void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&); 164 void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&); 165 void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode); 166 TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body, const TAttributes&); 167 nestLooping()168 void nestLooping() { ++loopNestingLevel; } unnestLooping()169 void unnestLooping() { --loopNestingLevel; } nestAnnotations()170 void nestAnnotations() { ++annotationNestingLevel; } unnestAnnotations()171 void unnestAnnotations() { --annotationNestingLevel; } getAnnotationNestingLevel()172 int getAnnotationNestingLevel() { return annotationNestingLevel; } pushScope()173 void pushScope() { symbolTable.push(); } popScope()174 void popScope() { symbolTable.pop(nullptr); } 175 176 void pushThisScope(const TType&, const TVector<TFunctionDeclarator>&); popThisScope()177 void popThisScope() { symbolTable.pop(nullptr); } 178 pushImplicitThis(TVariable * thisParameter)179 void pushImplicitThis(TVariable* thisParameter) { implicitThisStack.push_back(thisParameter); } popImplicitThis()180 void popImplicitThis() { implicitThisStack.pop_back(); } getImplicitThis(int thisDepth)181 TVariable* getImplicitThis(int thisDepth) const { return implicitThisStack[implicitThisStack.size() - thisDepth]; } 182 183 void pushNamespace(const TString& name); 184 void popNamespace(); 185 void getFullNamespaceName(TString*&) const; 186 void addScopeMangler(TString&); 187 beginParameterParsing(TFunction & function)188 void beginParameterParsing(TFunction& function) 189 { 190 parsingEntrypointParameters = isEntrypointName(function.getName()); 191 } 192 pushSwitchSequence(TIntermSequence * sequence)193 void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); } popSwitchSequence()194 void popSwitchSequence() { switchSequenceStack.pop_back(); } 195 196 virtual void growGlobalUniformBlock(const TSourceLoc&, TType&, const TString& memberName, 197 TTypeList* typeList = nullptr) override; 198 199 // Apply L-value conversions. E.g, turning a write to a RWTexture into an ImageStore. 200 TIntermTyped* handleLvalue(const TSourceLoc&, const char* op, TIntermTyped*& node); 201 bool lValueErrorCheck(const TSourceLoc&, const char* op, TIntermTyped*) override; 202 203 TLayoutFormat getLayoutFromTxType(const TSourceLoc&, const TType&); 204 205 bool handleOutputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry); 206 bool handleInputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry); 207 208 // Determine selection control from attributes 209 void handleSelectionAttributes(const TSourceLoc& loc, TIntermSelection*, const TAttributes& attributes); 210 void handleSwitchAttributes(const TSourceLoc& loc, TIntermSwitch*, const TAttributes& attributes); 211 212 // Determine loop control from attributes 213 void handleLoopAttributes(const TSourceLoc& loc, TIntermLoop*, const TAttributes& attributes); 214 215 // Share struct buffer deep types 216 void shareStructBufferType(TType&); 217 218 // Set texture return type of the given sampler. Returns success (not all types are valid). 219 bool setTextureReturnType(TSampler& sampler, const TType& retType, const TSourceLoc& loc); 220 221 // Obtain the sampler return type of the given sampler in retType. 222 void getTextureReturnType(const TSampler& sampler, TType& retType) const; 223 224 TAttributeType attributeFromName(const TString& nameSpace, const TString& name) const; 225 226 protected: 227 struct TFlattenData { TFlattenDataTFlattenData228 TFlattenData() : nextBinding(TQualifier::layoutBindingEnd), 229 nextLocation(TQualifier::layoutLocationEnd) { } TFlattenDataTFlattenData230 TFlattenData(int nb, int nl) : nextBinding(nb), nextLocation(nl) { } 231 232 TVector<TVariable*> members; // individual flattened variables 233 TVector<int> offsets; // offset to next tree level 234 unsigned int nextBinding; // next binding to use. 235 unsigned int nextLocation; // next location to use 236 }; 237 238 void fixConstInit(const TSourceLoc&, const TString& identifier, TType& type, TIntermTyped*& initializer); 239 void inheritGlobalDefaults(TQualifier& dst) const; 240 TVariable* makeInternalVariable(const char* name, const TType&) const; makeInternalVariable(const TString & name,const TType & type)241 TVariable* makeInternalVariable(const TString& name, const TType& type) const { 242 return makeInternalVariable(name.c_str(), type); 243 } 244 TIntermSymbol* makeInternalVariableNode(const TSourceLoc&, const char* name, const TType&) const; 245 TVariable* declareNonArray(const TSourceLoc&, const TString& identifier, const TType&, bool track); 246 void declareArray(const TSourceLoc&, const TString& identifier, const TType&, TSymbol*&, bool track); 247 TIntermNode* executeDeclaration(const TSourceLoc&, TVariable* variable); 248 TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable); 249 TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer, TIntermTyped* scalarInit); 250 bool isScalarConstructor(const TIntermNode*); 251 TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage); isEntrypointName(const TString & name)252 bool isEntrypointName(const TString& name) { return name.compare(intermediate.getEntryPointName().c_str()) == 0; } 253 254 // Return true if this node requires L-value conversion (e.g, to an imageStore). 255 bool shouldConvertLValue(const TIntermNode*) const; 256 257 // Array and struct flattening 258 TIntermTyped* flattenAccess(TIntermTyped* base, int member); 259 TIntermTyped* flattenAccess(long long uniqueId, int member, TStorageQualifier outerStorage, const TType&, int subset = -1); 260 int findSubtreeOffset(const TIntermNode&) const; 261 int findSubtreeOffset(const TType&, int subset, const TVector<int>& offsets) const; 262 bool shouldFlatten(const TType&, TStorageQualifier, bool topLevel) const; 263 bool wasFlattened(const TIntermTyped* node) const; wasFlattened(long long id)264 bool wasFlattened(long long id) const { return flattenMap.find(id) != flattenMap.end(); } 265 int addFlattenedMember(const TVariable&, const TType&, TFlattenData&, const TString& name, bool linkage, 266 const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes); 267 268 // Structure splitting (splits interstage built-in types into its own struct) 269 void split(const TVariable&); 270 void splitBuiltIn(const TString& baseName, const TType& memberType, const TArraySizes*, const TQualifier&); 271 const TType& split(const TType& type, const TString& name, const TQualifier&); 272 bool wasSplit(const TIntermTyped* node) const; wasSplit(long long id)273 bool wasSplit(long long id) const { return splitNonIoVars.find(id) != splitNonIoVars.end(); } 274 TVariable* getSplitNonIoVar(long long id) const; 275 void addPatchConstantInvocation(); 276 void fixTextureShadowModes(); 277 void finalizeAppendMethods(); 278 TIntermTyped* makeIntegerIndex(TIntermTyped*); 279 280 void fixBuiltInIoType(TType&); 281 282 void flatten(const TVariable& variable, bool linkage, bool arrayed = false); 283 int flatten(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage, 284 const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes); 285 int flattenStruct(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage, 286 const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes); 287 int flattenArray(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage, 288 const TQualifier& outerQualifier); 289 290 bool hasUniform(const TQualifier& qualifier) const; 291 void clearUniform(TQualifier& qualifier); 292 bool isInputBuiltIn(const TQualifier& qualifier) const; 293 bool hasInput(const TQualifier& qualifier) const; 294 void correctOutput(TQualifier& qualifier); 295 bool isOutputBuiltIn(const TQualifier& qualifier) const; 296 bool hasOutput(const TQualifier& qualifier) const; 297 void correctInput(TQualifier& qualifier); 298 void correctUniform(TQualifier& qualifier); 299 void clearUniformInputOutput(TQualifier& qualifier); 300 301 // Test method names 302 bool isStructBufferMethod(const TString& name) const; 303 void counterBufferType(const TSourceLoc& loc, TType& type); 304 305 // Return standard sample position array 306 TIntermConstantUnion* getSamplePosArray(int count); 307 308 TType* getStructBufferContentType(const TType& type) const; isStructBufferType(const TType & type)309 bool isStructBufferType(const TType& type) const { return getStructBufferContentType(type) != nullptr; } 310 TIntermTyped* indexStructBufferContent(const TSourceLoc& loc, TIntermTyped* buffer) const; 311 TIntermTyped* getStructBufferCounter(const TSourceLoc& loc, TIntermTyped* buffer); 312 TString getStructBuffCounterName(const TString&) const; 313 void addStructBuffArguments(const TSourceLoc& loc, TIntermAggregate*&); 314 void addStructBufferHiddenCounterParam(const TSourceLoc& loc, TParameter&, TIntermAggregate*&); 315 316 // Return true if this type is a reference. This is not currently a type method in case that's 317 // a language specific answer. isReference(const TType & type)318 bool isReference(const TType& type) const { return isStructBufferType(type); } 319 320 // Return true if this a buffer type that has an associated counter buffer. 321 bool hasStructBuffCounter(const TType&) const; 322 323 // Finalization step: remove unused buffer blocks from linkage (we don't know until the 324 // shader is entirely compiled) 325 void removeUnusedStructBufferCounters(); 326 327 static bool isClipOrCullDistance(TBuiltInVariable); isClipOrCullDistance(const TQualifier & qual)328 static bool isClipOrCullDistance(const TQualifier& qual) { return isClipOrCullDistance(qual.builtIn); } isClipOrCullDistance(const TType & type)329 static bool isClipOrCullDistance(const TType& type) { return isClipOrCullDistance(type.getQualifier()); } 330 331 // Find the patch constant function (issues error, returns nullptr if not found) 332 const TFunction* findPatchConstantFunction(const TSourceLoc& loc); 333 334 // Pass through to base class after remembering built-in mappings. 335 using TParseContextBase::trackLinkage; 336 void trackLinkage(TSymbol& variable) override; 337 338 void finish() override; // post-processing 339 340 // Linkage symbol helpers 341 TIntermSymbol* findTessLinkageSymbol(TBuiltInVariable biType) const; 342 343 // Current state of parsing 344 int annotationNestingLevel; // 0 if outside all annotations 345 346 HlslParseContext(HlslParseContext&); 347 HlslParseContext& operator=(HlslParseContext&); 348 349 static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex() 350 TQualifier globalBufferDefaults; 351 TQualifier globalUniformDefaults; 352 TQualifier globalInputDefaults; 353 TQualifier globalOutputDefaults; 354 TString currentCaller; // name of last function body entered (not valid when at global scope) 355 TIdSetType inductiveLoopIds; 356 TVector<TIntermTyped*> needsIndexLimitationChecking; 357 358 // 359 // Geometry shader input arrays: 360 // - array sizing is based on input primitive and/or explicit size 361 // 362 // Tessellation control output arrays: 363 // - array sizing is based on output layout(vertices=...) and/or explicit size 364 // 365 // Both: 366 // - array sizing is retroactive 367 // - built-in block redeclarations interact with this 368 // 369 // Design: 370 // - use a per-context "resize-list", a list of symbols whose array sizes 371 // can be fixed 372 // 373 // - the resize-list starts empty at beginning of user-shader compilation, it does 374 // not have built-ins in it 375 // 376 // - on built-in array use: copyUp() symbol and add it to the resize-list 377 // 378 // - on user array declaration: add it to the resize-list 379 // 380 // - on block redeclaration: copyUp() symbol and add it to the resize-list 381 // * note, that appropriately gives an error if redeclaring a block that 382 // was already used and hence already copied-up 383 // 384 // - on seeing a layout declaration that sizes the array, fix everything in the 385 // resize-list, giving errors for mismatch 386 // 387 // - on seeing an array size declaration, give errors on mismatch between it and previous 388 // array-sizing declarations 389 // 390 TVector<TSymbol*> ioArraySymbolResizeList; 391 392 TMap<long long, TFlattenData> flattenMap; 393 394 // IO-type map. Maps a pure symbol-table form of a structure-member list into 395 // each of the (up to) three kinds of IO, as each as different allowed decorations, 396 // but HLSL allows mixing all in the same structure. 397 struct tIoKinds { 398 TTypeList* input; 399 TTypeList* output; 400 TTypeList* uniform; 401 }; 402 TMap<const TTypeList*, tIoKinds> ioTypeMap; 403 404 // Structure splitting data: 405 TMap<long long, TVariable*> splitNonIoVars; // variables with the built-in interstage IO removed, indexed by unique ID. 406 407 // Structuredbuffer shared types. Typically there are only a few. 408 TVector<TType*> structBufferTypes; 409 410 // This tracks texture sample user structure return types. Only a limited number are supported, as 411 // may fit in TSampler::structReturnIndex. 412 TVector<TTypeList*> textureReturnStruct; 413 414 TMap<TString, bool> structBufferCounter; // true if counter buffer is in use 415 416 // The built-in interstage IO map considers e.g, EvqPosition on input and output separately, so that we 417 // can build the linkage correctly if position appears on both sides. Otherwise, multiple positions 418 // are considered identical. 419 struct tInterstageIoData { tInterstageIoDatatInterstageIoData420 tInterstageIoData(TBuiltInVariable bi, TStorageQualifier q) : 421 builtIn(bi), storage(q) { } 422 423 TBuiltInVariable builtIn; 424 TStorageQualifier storage; 425 426 // ordering for maps 427 bool operator<(const tInterstageIoData d) const { 428 return (builtIn != d.builtIn) ? (builtIn < d.builtIn) : (storage < d.storage); 429 } 430 }; 431 432 TMap<tInterstageIoData, TVariable*> splitBuiltIns; // split built-ins, indexed by built-in type. 433 TVariable* inputPatch; // input patch is special for PCF: it's the only non-builtin PCF input, 434 // and is handled as a pseudo-builtin. 435 436 unsigned int nextInLocation; 437 unsigned int nextOutLocation; 438 439 TFunction* entryPointFunction; 440 TIntermNode* entryPointFunctionBody; 441 442 TString patchConstantFunctionName; // hull shader patch constant function name, from function level attribute. 443 TMap<TBuiltInVariable, TSymbol*> builtInTessLinkageSymbols; // used for tessellation, finding declared built-ins 444 445 TVector<TString> currentTypePrefix; // current scoping prefix for nested structures 446 TVector<TVariable*> implicitThisStack; // currently active 'this' variables for nested structures 447 448 TVariable* gsStreamOutput; // geometry shader stream outputs, for emit (Append method) 449 450 TVariable* clipDistanceOutput; // synthesized clip distance out variable (shader might have >1) 451 TVariable* cullDistanceOutput; // synthesized cull distance out variable (shader might have >1) 452 TVariable* clipDistanceInput; // synthesized clip distance in variable (shader might have >1) 453 TVariable* cullDistanceInput; // synthesized cull distance in variable (shader might have >1) 454 455 static const int maxClipCullRegs = 2; 456 std::array<int, maxClipCullRegs> clipSemanticNSizeIn; // vector, indexed by clip semantic ID 457 std::array<int, maxClipCullRegs> cullSemanticNSizeIn; // vector, indexed by cull semantic ID 458 std::array<int, maxClipCullRegs> clipSemanticNSizeOut; // vector, indexed by clip semantic ID 459 std::array<int, maxClipCullRegs> cullSemanticNSizeOut; // vector, indexed by cull semantic ID 460 461 // This tracks the first (mip level) argument to the .mips[][] operator. Since this can be nested as 462 // in tx.mips[tx.mips[0][1].x][2], we need a stack. We also track the TSourceLoc for error reporting 463 // purposes. 464 struct tMipsOperatorData { tMipsOperatorDatatMipsOperatorData465 tMipsOperatorData(TSourceLoc l, TIntermTyped* m) : loc(l), mipLevel(m) { } 466 TSourceLoc loc; 467 TIntermTyped* mipLevel; 468 }; 469 470 TVector<tMipsOperatorData> mipsOperatorMipArg; 471 472 // The geometry output stream is not copied out from the entry point as a typical output variable 473 // is. It's written via EmitVertex (hlsl=Append), which may happen in arbitrary control flow. 474 // For this we need the real output symbol. Since it may not be known at the time and Append() 475 // method is parsed, the sequence will be patched during finalization. 476 struct tGsAppendData { 477 TIntermAggregate* node; 478 TSourceLoc loc; 479 }; 480 481 TVector<tGsAppendData> gsAppends; 482 483 // A texture object may be used with shadow and non-shadow samplers, but both may not be 484 // alive post-DCE in the same shader. We do not know at compilation time which are alive: that's 485 // only known post-DCE. If a texture is used both ways, we create two textures, and 486 // leave the elimiation of one to the optimizer. This maps the shader variant to 487 // the shadow variant. 488 // 489 // This can be removed if and when the texture shadow code in 490 // HlslParseContext::handleSamplerTextureCombine is removed. 491 struct tShadowTextureSymbols { tShadowTextureSymbolstShadowTextureSymbols492 tShadowTextureSymbols() { symId.fill(-1); } 493 settShadowTextureSymbols494 void set(bool shadow, long long id) { symId[int(shadow)] = id; } gettShadowTextureSymbols495 long long get(bool shadow) const { return symId[int(shadow)]; } 496 497 // True if this texture has been seen with both shadow and non-shadow modes overloadedtShadowTextureSymbols498 bool overloaded() const { return symId[0] != -1 && symId[1] != -1; } isShadowIdtShadowTextureSymbols499 bool isShadowId(long long id) const { return symId[1] == id; } 500 501 private: 502 std::array<long long, 2> symId; 503 }; 504 505 TMap<long long, tShadowTextureSymbols*> textureShadowVariant; 506 bool parsingEntrypointParameters; 507 }; 508 509 // This is the prefix we use for built-in methods to avoid namespace collisions with 510 // global scope user functions. 511 // TODO: this would be better as a nonparseable character, but that would 512 // require changing the scanner. 513 #define BUILTIN_PREFIX "__BI_" 514 515 } // end namespace glslang 516 517 #endif // HLSL_PARSE_INCLUDED_ 518