hlslParseHelper.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //
  2. // Copyright (C) 2016 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. #include "../glslang/MachineIndependent/parseVersions.h"
  39. #include "../glslang/MachineIndependent/ParseHelper.h"
  40. #include "../glslang/MachineIndependent/attribute.h"
  41. #include <array>
  42. namespace glslang {
  43. class TFunctionDeclarator;
  44. class HlslParseContext : public TParseContextBase {
  45. public:
  46. HlslParseContext(TSymbolTable&, TIntermediate&, bool parsingBuiltins,
  47. int version, EProfile, const SpvVersion& spvVersion, EShLanguage, TInfoSink&,
  48. const TString sourceEntryPointName,
  49. bool forwardCompatible = false, EShMessages messages = EShMsgDefault);
  50. virtual ~HlslParseContext();
  51. void initializeExtensionBehavior() override;
  52. void setLimits(const TBuiltInResource&) override;
  53. bool parseShaderStrings(TPpContext&, TInputScanner& input, bool versionWillBeError = false) override;
  54. virtual const char* getGlobalUniformBlockName() const override { return "$Global"; }
  55. virtual void setUniformBlockDefaults(TType& block) const override
  56. {
  57. block.getQualifier().layoutPacking = ElpStd140;
  58. block.getQualifier().layoutMatrix = ElmRowMajor;
  59. }
  60. void reservedPpErrorCheck(const TSourceLoc&, const char* /*name*/, const char* /*op*/) override { }
  61. bool lineContinuationCheck(const TSourceLoc&, bool /*endOfComment*/) override { return true; }
  62. bool lineDirectiveShouldSetNextLine() const override { return true; }
  63. bool builtInName(const TString&);
  64. void handlePragma(const TSourceLoc&, const TVector<TString>&) override;
  65. TIntermTyped* handleVariable(const TSourceLoc&, const TString* string);
  66. TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
  67. TIntermTyped* handleBracketOperator(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
  68. TIntermTyped* handleBinaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* left, TIntermTyped* right);
  69. TIntermTyped* handleUnaryMath(const TSourceLoc&, const char* str, TOperator op, TIntermTyped* childNode);
  70. TIntermTyped* handleDotDereference(const TSourceLoc&, TIntermTyped* base, const TString& field);
  71. bool isBuiltInMethod(const TSourceLoc&, TIntermTyped* base, const TString& field);
  72. void assignToInterface(TVariable& variable);
  73. void handleFunctionDeclarator(const TSourceLoc&, TFunction& function, bool prototype);
  74. TIntermAggregate* handleFunctionDefinition(const TSourceLoc&, TFunction&, const TAttributes&, TIntermNode*& entryPointTree);
  75. TIntermNode* transformEntryPoint(const TSourceLoc&, TFunction&, const TAttributes&);
  76. void handleEntryPointAttributes(const TSourceLoc&, const TAttributes&);
  77. void transferTypeAttributes(const TSourceLoc&, const TAttributes&, TType&, bool allowEntry = false);
  78. void handleFunctionBody(const TSourceLoc&, TFunction&, TIntermNode* functionBody, TIntermNode*& node);
  79. void remapEntryPointIO(TFunction& function, TVariable*& returnValue, TVector<TVariable*>& inputs, TVector<TVariable*>& outputs);
  80. void remapNonEntryPointIO(TFunction& function);
  81. TIntermNode* handleReturnValue(const TSourceLoc&, TIntermTyped*);
  82. void handleFunctionArgument(TFunction*, TIntermTyped*& arguments, TIntermTyped* newArg);
  83. TIntermTyped* handleAssign(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
  84. TIntermTyped* handleAssignToMatrixSwizzle(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
  85. TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermTyped*);
  86. TIntermAggregate* assignClipCullDistance(const TSourceLoc&, TOperator, int semanticId, TIntermTyped* left, TIntermTyped* right);
  87. TIntermTyped* assignPosition(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
  88. void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
  89. void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
  90. void decomposeStructBufferMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
  91. void decomposeGeometryMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
  92. void pushFrontArguments(TIntermTyped* front, TIntermTyped*& arguments);
  93. void addInputArgumentConversions(const TFunction&, TIntermTyped*&);
  94. void expandArguments(const TSourceLoc&, const TFunction&, TIntermTyped*&);
  95. TIntermTyped* addOutputArgumentConversions(const TFunction&, TIntermOperator&);
  96. void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&);
  97. TFunction* makeConstructorCall(const TSourceLoc&, const TType&);
  98. void handleSemantic(TSourceLoc, TQualifier&, TBuiltInVariable, const TString& upperCase);
  99. void handlePackOffset(const TSourceLoc&, TQualifier&, const glslang::TString& location,
  100. const glslang::TString* component);
  101. void handleRegister(const TSourceLoc&, TQualifier&, const glslang::TString* profile, const glslang::TString& desc,
  102. int subComponent, const glslang::TString*);
  103. TIntermTyped* convertConditionalExpression(const TSourceLoc&, TIntermTyped*, bool mustBeScalar = true);
  104. TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler);
  105. bool parseMatrixSwizzleSelector(const TSourceLoc&, const TString&, int cols, int rows, TSwizzleSelectors<TMatrixSelector>&);
  106. int getMatrixComponentsColumn(int rows, const TSwizzleSelectors<TMatrixSelector>&);
  107. void assignError(const TSourceLoc&, const char* op, TString left, TString right);
  108. void unaryOpError(const TSourceLoc&, const char* op, TString operand);
  109. void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right);
  110. void variableCheck(TIntermTyped*& nodePtr);
  111. void constantValueCheck(TIntermTyped* node, const char* token);
  112. void integerCheck(const TIntermTyped* node, const char* token);
  113. void globalCheck(const TSourceLoc&, const char* token);
  114. bool constructorError(const TSourceLoc&, TIntermNode*, TFunction&, TOperator, TType&);
  115. bool constructorTextureSamplerError(const TSourceLoc&, const TFunction&);
  116. void arraySizeCheck(const TSourceLoc&, TIntermTyped* expr, TArraySize&);
  117. void arraySizeRequiredCheck(const TSourceLoc&, const TArraySizes&);
  118. void structArrayCheck(const TSourceLoc&, const TType& structure);
  119. bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
  120. void globalQualifierFix(const TSourceLoc&, TQualifier&);
  121. bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType);
  122. void mergeQualifiers(TQualifier& dst, const TQualifier& src);
  123. int computeSamplerTypeIndex(TSampler&);
  124. TSymbol* redeclareBuiltinVariable(const TSourceLoc&, const TString&, const TQualifier&, const TShaderQualifiers&);
  125. void paramFix(TType& type);
  126. void specializationCheck(const TSourceLoc&, const TType&, const char* op);
  127. void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&);
  128. void setLayoutQualifier(const TSourceLoc&, TQualifier&, TString&, const TIntermTyped*);
  129. void setSpecConstantId(const TSourceLoc&, TQualifier&, int value);
  130. void mergeObjectLayoutQualifiers(TQualifier& dest, const TQualifier& src, bool inheritOnly);
  131. void checkNoShaderLayouts(const TSourceLoc&, const TShaderQualifiers&);
  132. const TFunction* findFunction(const TSourceLoc& loc, TFunction& call, bool& builtIn, int& thisDepth, TIntermTyped*& args);
  133. void addGenMulArgumentConversion(const TSourceLoc& loc, TFunction& call, TIntermTyped*& args);
  134. void declareTypedef(const TSourceLoc&, const TString& identifier, const TType&);
  135. void declareStruct(const TSourceLoc&, TString& structName, TType&);
  136. TSymbol* lookupUserType(const TString&, TType&);
  137. TIntermNode* declareVariable(const TSourceLoc&, const TString& identifier, TType&, TIntermTyped* initializer = 0);
  138. void lengthenList(const TSourceLoc&, TIntermSequence& list, int size, TIntermTyped* scalarInit);
  139. TIntermTyped* handleConstructor(const TSourceLoc&, TIntermTyped*, const TType&);
  140. TIntermTyped* addConstructor(const TSourceLoc&, TIntermTyped*, const TType&);
  141. TIntermTyped* convertArray(TIntermTyped*, const TType&);
  142. TIntermTyped* constructAggregate(TIntermNode*, const TType&, int, const TSourceLoc&);
  143. TIntermTyped* constructBuiltIn(const TType&, TOperator, TIntermTyped*, const TSourceLoc&, bool subset);
  144. void declareBlock(const TSourceLoc&, TType&, const TString* instanceName = 0);
  145. void declareStructBufferCounter(const TSourceLoc& loc, const TType& bufferType, const TString& name);
  146. void fixBlockLocations(const TSourceLoc&, TQualifier&, TTypeList&, bool memberWithLocation, bool memberWithoutLocation);
  147. void fixBlockXfbOffsets(TQualifier&, TTypeList&);
  148. void fixBlockUniformOffsets(const TQualifier&, TTypeList&);
  149. void addQualifierToExisting(const TSourceLoc&, TQualifier, const TString& identifier);
  150. void addQualifierToExisting(const TSourceLoc&, TQualifier, TIdentifierList&);
  151. void updateStandaloneQualifierDefaults(const TSourceLoc&, const TPublicType&);
  152. void wrapupSwitchSubsequence(TIntermAggregate* statements, TIntermNode* branchNode);
  153. TIntermNode* addSwitch(const TSourceLoc&, TIntermTyped* expression, TIntermAggregate* body, const TAttributes&);
  154. void nestLooping() { ++loopNestingLevel; }
  155. void unnestLooping() { --loopNestingLevel; }
  156. void nestAnnotations() { ++annotationNestingLevel; }
  157. void unnestAnnotations() { --annotationNestingLevel; }
  158. int getAnnotationNestingLevel() { return annotationNestingLevel; }
  159. void pushScope() { symbolTable.push(); }
  160. void popScope() { symbolTable.pop(0); }
  161. void pushThisScope(const TType&, const TVector<TFunctionDeclarator>&);
  162. void popThisScope() { symbolTable.pop(0); }
  163. void pushImplicitThis(TVariable* thisParameter) { implicitThisStack.push_back(thisParameter); }
  164. void popImplicitThis() { implicitThisStack.pop_back(); }
  165. TVariable* getImplicitThis(int thisDepth) const { return implicitThisStack[implicitThisStack.size() - thisDepth]; }
  166. void pushNamespace(const TString& name);
  167. void popNamespace();
  168. void getFullNamespaceName(TString*&) const;
  169. void addScopeMangler(TString&);
  170. void pushSwitchSequence(TIntermSequence* sequence) { switchSequenceStack.push_back(sequence); }
  171. void popSwitchSequence() { switchSequenceStack.pop_back(); }
  172. virtual void growGlobalUniformBlock(const TSourceLoc&, TType&, const TString& memberName,
  173. TTypeList* typeList = nullptr) override;
  174. // Apply L-value conversions. E.g, turning a write to a RWTexture into an ImageStore.
  175. TIntermTyped* handleLvalue(const TSourceLoc&, const char* op, TIntermTyped*& node);
  176. bool lValueErrorCheck(const TSourceLoc&, const char* op, TIntermTyped*) override;
  177. TLayoutFormat getLayoutFromTxType(const TSourceLoc&, const TType&);
  178. bool handleOutputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry);
  179. bool handleInputGeometry(const TSourceLoc&, const TLayoutGeometry& geometry);
  180. // Determine selection control from attributes
  181. void handleSelectionAttributes(const TSourceLoc& loc, TIntermSelection*, const TAttributes& attributes);
  182. void handleSwitchAttributes(const TSourceLoc& loc, TIntermSwitch*, const TAttributes& attributes);
  183. // Determine loop control from attributes
  184. void handleLoopAttributes(const TSourceLoc& loc, TIntermLoop*, const TAttributes& attributes);
  185. // Share struct buffer deep types
  186. void shareStructBufferType(TType&);
  187. // Set texture return type of the given sampler. Returns success (not all types are valid).
  188. bool setTextureReturnType(TSampler& sampler, const TType& retType, const TSourceLoc& loc);
  189. // Obtain the sampler return type of the given sampler in retType.
  190. void getTextureReturnType(const TSampler& sampler, TType& retType) const;
  191. TAttributeType attributeFromName(const TString& nameSpace, const TString& name) const;
  192. protected:
  193. struct TFlattenData {
  194. TFlattenData() : nextBinding(TQualifier::layoutBindingEnd),
  195. nextLocation(TQualifier::layoutLocationEnd) { }
  196. TFlattenData(int nb, int nl) : nextBinding(nb), nextLocation(nl) { }
  197. TVector<TVariable*> members; // individual flattened variables
  198. TVector<int> offsets; // offset to next tree level
  199. unsigned int nextBinding; // next binding to use.
  200. unsigned int nextLocation; // next location to use
  201. };
  202. void fixConstInit(const TSourceLoc&, const TString& identifier, TType& type, TIntermTyped*& initializer);
  203. void inheritGlobalDefaults(TQualifier& dst) const;
  204. TVariable* makeInternalVariable(const char* name, const TType&) const;
  205. TVariable* makeInternalVariable(const TString& name, const TType& type) const {
  206. return makeInternalVariable(name.c_str(), type);
  207. }
  208. TIntermSymbol* makeInternalVariableNode(const TSourceLoc&, const char* name, const TType&) const;
  209. TVariable* declareNonArray(const TSourceLoc&, const TString& identifier, const TType&, bool track);
  210. void declareArray(const TSourceLoc&, const TString& identifier, const TType&, TSymbol*&, bool track);
  211. TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable);
  212. TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer, TIntermTyped* scalarInit);
  213. bool isScalarConstructor(const TIntermNode*);
  214. TOperator mapAtomicOp(const TSourceLoc& loc, TOperator op, bool isImage);
  215. // Return true if this node requires L-value conversion (e.g, to an imageStore).
  216. bool shouldConvertLValue(const TIntermNode*) const;
  217. // Array and struct flattening
  218. TIntermTyped* flattenAccess(TIntermTyped* base, int member);
  219. TIntermTyped* flattenAccess(int uniqueId, int member, TStorageQualifier outerStorage, const TType&, int subset = -1);
  220. int findSubtreeOffset(const TIntermNode&) const;
  221. int findSubtreeOffset(const TType&, int subset, const TVector<int>& offsets) const;
  222. bool shouldFlatten(const TType&, TStorageQualifier, bool topLevel) const;
  223. bool wasFlattened(const TIntermTyped* node) const;
  224. bool wasFlattened(int id) const { return flattenMap.find(id) != flattenMap.end(); }
  225. int addFlattenedMember(const TVariable&, const TType&, TFlattenData&, const TString& name, bool linkage,
  226. const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes);
  227. // Structure splitting (splits interstage built-in types into its own struct)
  228. void split(const TVariable&);
  229. void splitBuiltIn(const TString& baseName, const TType& memberType, const TArraySizes*, const TQualifier&);
  230. const TType& split(const TType& type, const TString& name, const TQualifier&);
  231. bool wasSplit(const TIntermTyped* node) const;
  232. bool wasSplit(int id) const { return splitNonIoVars.find(id) != splitNonIoVars.end(); }
  233. TVariable* getSplitNonIoVar(int id) const;
  234. void addPatchConstantInvocation();
  235. void fixTextureShadowModes();
  236. TIntermTyped* makeIntegerIndex(TIntermTyped*);
  237. void fixBuiltInIoType(TType&);
  238. void flatten(const TVariable& variable, bool linkage);
  239. int flatten(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage,
  240. const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes);
  241. int flattenStruct(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage,
  242. const TQualifier& outerQualifier, const TArraySizes* builtInArraySizes);
  243. int flattenArray(const TVariable& variable, const TType&, TFlattenData&, TString name, bool linkage,
  244. const TQualifier& outerQualifier);
  245. bool hasUniform(const TQualifier& qualifier) const;
  246. void clearUniform(TQualifier& qualifier);
  247. bool isInputBuiltIn(const TQualifier& qualifier) const;
  248. bool hasInput(const TQualifier& qualifier) const;
  249. void correctOutput(TQualifier& qualifier);
  250. bool isOutputBuiltIn(const TQualifier& qualifier) const;
  251. bool hasOutput(const TQualifier& qualifier) const;
  252. void correctInput(TQualifier& qualifier);
  253. void correctUniform(TQualifier& qualifier);
  254. void clearUniformInputOutput(TQualifier& qualifier);
  255. // Test method names
  256. bool isStructBufferMethod(const TString& name) const;
  257. void counterBufferType(const TSourceLoc& loc, TType& type);
  258. // Return standard sample position array
  259. TIntermConstantUnion* getSamplePosArray(int count);
  260. TType* getStructBufferContentType(const TType& type) const;
  261. bool isStructBufferType(const TType& type) const { return getStructBufferContentType(type) != nullptr; }
  262. TIntermTyped* indexStructBufferContent(const TSourceLoc& loc, TIntermTyped* buffer) const;
  263. TIntermTyped* getStructBufferCounter(const TSourceLoc& loc, TIntermTyped* buffer);
  264. TString getStructBuffCounterName(const TString&) const;
  265. void addStructBuffArguments(const TSourceLoc& loc, TIntermAggregate*&);
  266. void addStructBufferHiddenCounterParam(const TSourceLoc& loc, TParameter&, TIntermAggregate*&);
  267. // Return true if this type is a reference. This is not currently a type method in case that's
  268. // a language specific answer.
  269. bool isReference(const TType& type) const { return isStructBufferType(type); }
  270. // Return true if this a buffer type that has an associated counter buffer.
  271. bool hasStructBuffCounter(const TType&) const;
  272. // Finalization step: remove unused buffer blocks from linkage (we don't know until the
  273. // shader is entirely compiled)
  274. void removeUnusedStructBufferCounters();
  275. static bool isClipOrCullDistance(TBuiltInVariable);
  276. static bool isClipOrCullDistance(const TQualifier& qual) { return isClipOrCullDistance(qual.builtIn); }
  277. static bool isClipOrCullDistance(const TType& type) { return isClipOrCullDistance(type.getQualifier()); }
  278. // Find the patch constant function (issues error, returns nullptr if not found)
  279. const TFunction* findPatchConstantFunction(const TSourceLoc& loc);
  280. // Pass through to base class after remembering built-in mappings.
  281. using TParseContextBase::trackLinkage;
  282. void trackLinkage(TSymbol& variable) override;
  283. void finish() override; // post-processing
  284. // Linkage symbol helpers
  285. TIntermSymbol* findTessLinkageSymbol(TBuiltInVariable biType) const;
  286. // Current state of parsing
  287. int annotationNestingLevel; // 0 if outside all annotations
  288. HlslParseContext(HlslParseContext&);
  289. HlslParseContext& operator=(HlslParseContext&);
  290. static const int maxSamplerIndex = EsdNumDims * (EbtNumTypes * (2 * 2 * 2)); // see computeSamplerTypeIndex()
  291. TQualifier globalBufferDefaults;
  292. TQualifier globalUniformDefaults;
  293. TQualifier globalInputDefaults;
  294. TQualifier globalOutputDefaults;
  295. TString currentCaller; // name of last function body entered (not valid when at global scope)
  296. TIdSetType inductiveLoopIds;
  297. TVector<TIntermTyped*> needsIndexLimitationChecking;
  298. //
  299. // Geometry shader input arrays:
  300. // - array sizing is based on input primitive and/or explicit size
  301. //
  302. // Tessellation control output arrays:
  303. // - array sizing is based on output layout(vertices=...) and/or explicit size
  304. //
  305. // Both:
  306. // - array sizing is retroactive
  307. // - built-in block redeclarations interact with this
  308. //
  309. // Design:
  310. // - use a per-context "resize-list", a list of symbols whose array sizes
  311. // can be fixed
  312. //
  313. // - the resize-list starts empty at beginning of user-shader compilation, it does
  314. // not have built-ins in it
  315. //
  316. // - on built-in array use: copyUp() symbol and add it to the resize-list
  317. //
  318. // - on user array declaration: add it to the resize-list
  319. //
  320. // - on block redeclaration: copyUp() symbol and add it to the resize-list
  321. // * note, that appropriately gives an error if redeclaring a block that
  322. // was already used and hence already copied-up
  323. //
  324. // - on seeing a layout declaration that sizes the array, fix everything in the
  325. // resize-list, giving errors for mismatch
  326. //
  327. // - on seeing an array size declaration, give errors on mismatch between it and previous
  328. // array-sizing declarations
  329. //
  330. TVector<TSymbol*> ioArraySymbolResizeList;
  331. TMap<int, TFlattenData> flattenMap;
  332. // IO-type map. Maps a pure symbol-table form of a structure-member list into
  333. // each of the (up to) three kinds of IO, as each as different allowed decorations,
  334. // but HLSL allows mixing all in the same structure.
  335. struct tIoKinds {
  336. TTypeList* input;
  337. TTypeList* output;
  338. TTypeList* uniform;
  339. };
  340. TMap<const TTypeList*, tIoKinds> ioTypeMap;
  341. // Structure splitting data:
  342. TMap<int, TVariable*> splitNonIoVars; // variables with the built-in interstage IO removed, indexed by unique ID.
  343. // Structuredbuffer shared types. Typically there are only a few.
  344. TVector<TType*> structBufferTypes;
  345. // This tracks texture sample user structure return types. Only a limited number are supported, as
  346. // may fit in TSampler::structReturnIndex.
  347. TVector<TTypeList*> textureReturnStruct;
  348. TMap<TString, bool> structBufferCounter; // true if counter buffer is in use
  349. // The built-in interstage IO map considers e.g, EvqPosition on input and output separately, so that we
  350. // can build the linkage correctly if position appears on both sides. Otherwise, multiple positions
  351. // are considered identical.
  352. struct tInterstageIoData {
  353. tInterstageIoData(TBuiltInVariable bi, TStorageQualifier q) :
  354. builtIn(bi), storage(q) { }
  355. TBuiltInVariable builtIn;
  356. TStorageQualifier storage;
  357. // ordering for maps
  358. bool operator<(const tInterstageIoData d) const {
  359. return (builtIn != d.builtIn) ? (builtIn < d.builtIn) : (storage < d.storage);
  360. }
  361. };
  362. TMap<tInterstageIoData, TVariable*> splitBuiltIns; // split built-ins, indexed by built-in type.
  363. TVariable* inputPatch; // input patch is special for PCF: it's the only non-builtin PCF input,
  364. // and is handled as a pseudo-builtin.
  365. unsigned int nextInLocation;
  366. unsigned int nextOutLocation;
  367. TFunction* entryPointFunction;
  368. TIntermNode* entryPointFunctionBody;
  369. TString patchConstantFunctionName; // hull shader patch constant function name, from function level attribute.
  370. TMap<TBuiltInVariable, TSymbol*> builtInTessLinkageSymbols; // used for tessellation, finding declared built-ins
  371. TVector<TString> currentTypePrefix; // current scoping prefix for nested structures
  372. TVector<TVariable*> implicitThisStack; // currently active 'this' variables for nested structures
  373. TVariable* gsStreamOutput; // geometry shader stream outputs, for emit (Append method)
  374. TVariable* clipDistanceOutput; // synthesized clip distance out variable (shader might have >1)
  375. TVariable* cullDistanceOutput; // synthesized cull distance out variable (shader might have >1)
  376. TVariable* clipDistanceInput; // synthesized clip distance in variable (shader might have >1)
  377. TVariable* cullDistanceInput; // synthesized cull distance in variable (shader might have >1)
  378. static const int maxClipCullRegs = 2;
  379. std::array<int, maxClipCullRegs> clipSemanticNSizeIn; // vector, indexed by clip semantic ID
  380. std::array<int, maxClipCullRegs> cullSemanticNSizeIn; // vector, indexed by cull semantic ID
  381. std::array<int, maxClipCullRegs> clipSemanticNSizeOut; // vector, indexed by clip semantic ID
  382. std::array<int, maxClipCullRegs> cullSemanticNSizeOut; // vector, indexed by cull semantic ID
  383. // This tracks the first (mip level) argument to the .mips[][] operator. Since this can be nested as
  384. // in tx.mips[tx.mips[0][1].x][2], we need a stack. We also track the TSourceLoc for error reporting
  385. // purposes.
  386. struct tMipsOperatorData {
  387. tMipsOperatorData(TSourceLoc l, TIntermTyped* m) : loc(l), mipLevel(m) { }
  388. TSourceLoc loc;
  389. TIntermTyped* mipLevel;
  390. };
  391. TVector<tMipsOperatorData> mipsOperatorMipArg;
  392. // A texture object may be used with shadow and non-shadow samplers, but both may not be
  393. // alive post-DCE in the same shader. We do not know at compilation time which are alive: that's
  394. // only known post-DCE. If a texture is used both ways, we create two textures, and
  395. // leave the elimiation of one to the optimizer. This maps the shader variant to
  396. // the shadow variant.
  397. //
  398. // This can be removed if and when the texture shadow code in
  399. // HlslParseContext::handleSamplerTextureCombine is removed.
  400. struct tShadowTextureSymbols {
  401. tShadowTextureSymbols() { symId.fill(-1); }
  402. void set(bool shadow, int id) { symId[int(shadow)] = id; }
  403. int get(bool shadow) const { return symId[int(shadow)]; }
  404. // True if this texture has been seen with both shadow and non-shadow modes
  405. bool overloaded() const { return symId[0] != -1 && symId[1] != -1; }
  406. bool isShadowId(int id) const { return symId[1] == id; }
  407. private:
  408. std::array<int, 2> symId;
  409. };
  410. TMap<int, tShadowTextureSymbols*> textureShadowVariant;
  411. };
  412. // This is the prefix we use for built-in methods to avoid namespace collisions with
  413. // global scope user functions.
  414. // TODO: this would be better as a nonparseable character, but that would
  415. // require changing the scanner.
  416. #define BUILTIN_PREFIX "__BI_"
  417. } // end namespace glslang
  418. #endif // HLSL_PARSE_INCLUDED_