daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 7 | #ifndef COMPILER_TRANSLATOR_OUTPUTHLSL_H_ |
| 8 | #define COMPILER_TRANSLATOR_OUTPUTHLSL_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | |
alokp@chromium.org | 4e89d23 | 2010-05-14 19:37:21 +0000 | [diff] [blame] | 10 | #include <list> |
| 11 | #include <set> |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 12 | #include <map> |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 13 | #include <stack> |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 14 | |
Jamie Madill | f51639a | 2014-06-25 16:04:57 -0400 | [diff] [blame] | 15 | #include "angle_gl.h" |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 16 | #include "compiler/translator/IntermNode.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 17 | #include "compiler/translator/ParseContext.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 18 | |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 19 | class BuiltInFunctionEmulatorHLSL; |
| 20 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | namespace sh |
| 22 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 23 | class UnfoldShortCircuit; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 24 | class StructureHLSL; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 25 | class UniformHLSL; |
| 26 | |
| 27 | typedef std::map<TString, TIntermSymbol*> ReferencedSymbols; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 28 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 29 | class OutputHLSL : public TIntermTraverser |
| 30 | { |
| 31 | public: |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 32 | OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 33 | ~OutputHLSL(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 34 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 35 | void output(); |
| 36 | |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 37 | const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const; |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 38 | const std::map<std::string, unsigned int> &getUniformRegisterMap() const; |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 39 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 40 | static TString initializer(const TType &type); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 41 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 42 | TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); } |
| 43 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 44 | protected: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 45 | void header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 46 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 47 | // Visit AST nodes and output their code to the body stream |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 48 | void visitSymbol(TIntermSymbol*); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 49 | void visitRaw(TIntermRaw*); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | void visitConstantUnion(TIntermConstantUnion*); |
| 51 | bool visitBinary(Visit visit, TIntermBinary*); |
| 52 | bool visitUnary(Visit visit, TIntermUnary*); |
| 53 | bool visitSelection(Visit visit, TIntermSelection*); |
| 54 | bool visitAggregate(Visit visit, TIntermAggregate*); |
| 55 | bool visitLoop(Visit visit, TIntermLoop*); |
| 56 | bool visitBranch(Visit visit, TIntermBranch*); |
| 57 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 58 | void traverseStatements(TIntermNode *node); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 59 | bool isSingleStatement(TIntermNode *node); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 60 | bool handleExcessiveLoop(TIntermLoop *node); |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 61 | void outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 62 | void outputLineDirective(int line); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 63 | TString argumentString(const TIntermSymbol *symbol); |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 64 | int vectorSize(const TType &type) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 66 | void outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 67 | const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion); |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 68 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 69 | void writeEmulatedFunctionTriplet(Visit visit, const char *preStr); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 70 | void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs); |
| 71 | |
| 72 | // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting) |
| 73 | bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression); |
| 74 | void writeDeferredGlobalInitializers(TInfoSinkBase &out); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 75 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 76 | TParseContext &mContext; |
shannon.woods@transgaming.com | b73964e | 2013-01-25 21:49:14 +0000 | [diff] [blame] | 77 | const ShShaderOutput mOutputType; |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 78 | UnfoldShortCircuit *mUnfoldShortCircuit; |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 79 | bool mInsideFunction; |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 80 | |
| 81 | // Output streams |
| 82 | TInfoSinkBase mHeader; |
| 83 | TInfoSinkBase mBody; |
| 84 | TInfoSinkBase mFooter; |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 85 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 86 | // A stack is useful when we want to traverse in the header, or in helper functions, but not always |
| 87 | // write to the body. Instead use an InfoSink stack to keep our current state intact. |
| 88 | std::stack<TInfoSinkBase *> mInfoSinkStack; |
| 89 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 90 | ReferencedSymbols mReferencedUniforms; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 91 | ReferencedSymbols mReferencedInterfaceBlocks; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 92 | ReferencedSymbols mReferencedAttributes; |
| 93 | ReferencedSymbols mReferencedVaryings; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 94 | ReferencedSymbols mReferencedOutputVariables; |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 95 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 96 | StructureHLSL *mStructureHLSL; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 97 | UniformHLSL *mUniformHLSL; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 98 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 99 | struct TextureFunction |
| 100 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 101 | enum Method |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 102 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 103 | IMPLICIT, // Mipmap LOD determined implicitly (standard lookup) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 104 | BIAS, |
| 105 | LOD, |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 106 | LOD0, |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 107 | LOD0BIAS, |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 108 | SIZE, // textureSize() |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 109 | FETCH, |
| 110 | GRAD |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | TBasicType sampler; |
| 114 | int coords; |
| 115 | bool proj; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 116 | bool offset; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 117 | Method method; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 118 | |
| 119 | TString name() const; |
| 120 | |
| 121 | bool operator<(const TextureFunction &rhs) const; |
| 122 | }; |
| 123 | |
| 124 | typedef std::set<TextureFunction> TextureFunctionSet; |
| 125 | |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 126 | // Parameters determining what goes in the header output |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 127 | TextureFunctionSet mUsesTexture; |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 128 | bool mUsesFragColor; |
| 129 | bool mUsesFragData; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 130 | bool mUsesDepthRange; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 131 | bool mUsesFragCoord; |
| 132 | bool mUsesPointCoord; |
| 133 | bool mUsesFrontFacing; |
| 134 | bool mUsesPointSize; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame^] | 135 | bool mUsesInstanceID; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 136 | bool mUsesFragDepth; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 137 | bool mUsesXor; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 138 | bool mUsesDiscardRewriting; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 139 | bool mUsesNestedBreak; |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 140 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 141 | int mNumRenderTargets; |
| 142 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 143 | int mUniqueIndex; // For creating unique names |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 144 | |
| 145 | bool mContainsLoopDiscontinuity; |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 146 | bool mContainsAnyLoop; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 147 | bool mOutputLod0Function; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 148 | bool mInsideDiscontinuousLoop; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 149 | int mNestedLoopDepth; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 150 | |
| 151 | TIntermSymbol *mExcessiveLoopIndex; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 152 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 153 | TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName); |
daniel@transgaming.com | f4d9fef | 2012-12-20 21:12:13 +0000 | [diff] [blame] | 154 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 155 | std::map<TIntermTyped*, TString> mFlaggedStructMappedNames; |
| 156 | std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames; |
| 157 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 158 | // Some initializers use varyings, uniforms or attributes, thus we can't evaluate some variables |
| 159 | // at global static scope in HLSL. These variables depend on values which we retrieve from the |
| 160 | // shader input structure, which we set in the D3D main function. Instead, we can initialize |
| 161 | // these static globals after we initialize our other globals. |
| 162 | std::vector<std::pair<TIntermSymbol*, TIntermTyped*>> mDeferredGlobalInitializers; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | }; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 164 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 167 | #endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_ |