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 | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 19 | class BuiltInFunctionEmulator; |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 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: |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 32 | OutputHLSL(sh::GLenum shaderType, int shaderVersion, |
| 33 | const TExtensionBehavior &extensionBehavior, |
| 34 | const char *sourcePath, ShShaderOutput outputType, |
| 35 | int numRenderTargets, const std::vector<Uniform> &uniforms, |
| 36 | int compileOptions); |
| 37 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 38 | ~OutputHLSL(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 40 | void output(TIntermNode *treeRoot, TInfoSinkBase &objSink); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 41 | |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 42 | const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const; |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 43 | const std::map<std::string, unsigned int> &getUniformRegisterMap() const; |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 44 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 45 | static TString initializer(const TType &type); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 46 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 47 | TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); } |
| 48 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 49 | protected: |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 50 | void header(const BuiltInFunctionEmulator *builtInFunctionEmulator); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 51 | |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 52 | // Visit AST nodes and output their code to the body stream |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 53 | void visitSymbol(TIntermSymbol*); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 54 | void visitRaw(TIntermRaw*); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 55 | void visitConstantUnion(TIntermConstantUnion*); |
| 56 | bool visitBinary(Visit visit, TIntermBinary*); |
| 57 | bool visitUnary(Visit visit, TIntermUnary*); |
| 58 | bool visitSelection(Visit visit, TIntermSelection*); |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 59 | bool visitSwitch(Visit visit, TIntermSwitch *); |
| 60 | bool visitCase(Visit visit, TIntermCase *); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 61 | bool visitAggregate(Visit visit, TIntermAggregate*); |
| 62 | bool visitLoop(Visit visit, TIntermLoop*); |
| 63 | bool visitBranch(Visit visit, TIntermBranch*); |
| 64 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 65 | void traverseStatements(TIntermNode *node); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 66 | bool isSingleStatement(TIntermNode *node); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 67 | bool handleExcessiveLoop(TIntermLoop *node); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 68 | |
| 69 | // Emit one of three strings depending on traverse phase. Called with literal strings so using const char* instead of TString. |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 70 | void outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 71 | void outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 72 | void outputLineDirective(int line); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 73 | TString argumentString(const TIntermSymbol *symbol); |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 74 | int vectorSize(const TType &type) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 76 | // Emit constructor. Called with literal names so using const char* instead of TString. |
| 77 | void outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 78 | const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion); |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 79 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 80 | void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out); |
| 81 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 82 | void writeEmulatedFunctionTriplet(Visit visit, const char *preStr); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 83 | void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs); |
| 84 | |
| 85 | // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting) |
| 86 | bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression); |
| 87 | void writeDeferredGlobalInitializers(TInfoSinkBase &out); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 88 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 89 | // Returns the function name |
| 90 | TString addStructEqualityFunction(const TStructure &structure); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 91 | TString addArrayEqualityFunction(const TType &type); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 92 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 93 | sh::GLenum mShaderType; |
| 94 | int mShaderVersion; |
| 95 | const TExtensionBehavior &mExtensionBehavior; |
| 96 | const char *mSourcePath; |
shannon.woods@transgaming.com | b73964e | 2013-01-25 21:49:14 +0000 | [diff] [blame] | 97 | const ShShaderOutput mOutputType; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 98 | int mCompileOptions; |
| 99 | |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 100 | UnfoldShortCircuit *mUnfoldShortCircuit; |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 101 | bool mInsideFunction; |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 102 | |
| 103 | // Output streams |
| 104 | TInfoSinkBase mHeader; |
| 105 | TInfoSinkBase mBody; |
| 106 | TInfoSinkBase mFooter; |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 107 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 108 | // A stack is useful when we want to traverse in the header, or in helper functions, but not always |
| 109 | // write to the body. Instead use an InfoSink stack to keep our current state intact. |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 110 | // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler. |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 111 | std::stack<TInfoSinkBase *> mInfoSinkStack; |
| 112 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 113 | ReferencedSymbols mReferencedUniforms; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 114 | ReferencedSymbols mReferencedInterfaceBlocks; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 115 | ReferencedSymbols mReferencedAttributes; |
| 116 | ReferencedSymbols mReferencedVaryings; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 117 | ReferencedSymbols mReferencedOutputVariables; |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 118 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 119 | StructureHLSL *mStructureHLSL; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 120 | UniformHLSL *mUniformHLSL; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 121 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 122 | struct TextureFunction |
| 123 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 124 | enum Method |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 125 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 126 | IMPLICIT, // Mipmap LOD determined implicitly (standard lookup) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 127 | BIAS, |
| 128 | LOD, |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 129 | LOD0, |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 130 | LOD0BIAS, |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 131 | SIZE, // textureSize() |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 132 | FETCH, |
| 133 | GRAD |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | TBasicType sampler; |
| 137 | int coords; |
| 138 | bool proj; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 139 | bool offset; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 140 | Method method; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 141 | |
| 142 | TString name() const; |
| 143 | |
| 144 | bool operator<(const TextureFunction &rhs) const; |
| 145 | }; |
| 146 | |
| 147 | typedef std::set<TextureFunction> TextureFunctionSet; |
| 148 | |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 149 | // Parameters determining what goes in the header output |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 150 | TextureFunctionSet mUsesTexture; |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 151 | bool mUsesFragColor; |
| 152 | bool mUsesFragData; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 153 | bool mUsesDepthRange; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 154 | bool mUsesFragCoord; |
| 155 | bool mUsesPointCoord; |
| 156 | bool mUsesFrontFacing; |
| 157 | bool mUsesPointSize; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 158 | bool mUsesInstanceID; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 159 | bool mUsesFragDepth; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 160 | bool mUsesXor; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 161 | bool mUsesDiscardRewriting; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 162 | bool mUsesNestedBreak; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 163 | bool mRequiresIEEEStrictCompiling; |
| 164 | |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 165 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 166 | int mNumRenderTargets; |
| 167 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 168 | int mUniqueIndex; // For creating unique names |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 169 | |
| 170 | bool mContainsLoopDiscontinuity; |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 171 | bool mContainsAnyLoop; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 172 | bool mOutputLod0Function; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 173 | bool mInsideDiscontinuousLoop; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 174 | int mNestedLoopDepth; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 175 | |
| 176 | TIntermSymbol *mExcessiveLoopIndex; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 177 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 178 | TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName); |
daniel@transgaming.com | f4d9fef | 2012-12-20 21:12:13 +0000 | [diff] [blame] | 179 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 180 | std::map<TIntermTyped*, TString> mFlaggedStructMappedNames; |
| 181 | std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames; |
| 182 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 183 | // Some initializers use varyings, uniforms or attributes, thus we can't evaluate some variables |
| 184 | // at global static scope in HLSL. These variables depend on values which we retrieve from the |
| 185 | // shader input structure, which we set in the D3D main function. Instead, we can initialize |
| 186 | // these static globals after we initialize our other globals. |
| 187 | std::vector<std::pair<TIntermSymbol*, TIntermTyped*>> mDeferredGlobalInitializers; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 188 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame^] | 189 | struct EqualityFunction |
| 190 | { |
| 191 | TString functionName; |
| 192 | TString functionDefinition; |
| 193 | |
| 194 | virtual ~EqualityFunction() {} |
| 195 | }; |
| 196 | |
| 197 | // A list of all equality comparison functions. It's important to preserve the order at |
| 198 | // which we add the functions, since nested structures call each other recursively, and |
| 199 | // structure equality functions may need to call array equality functions and vice versa. |
| 200 | // The ownership of the pointers is maintained by the type-specific arrays. |
| 201 | std::vector<EqualityFunction*> mEqualityFunctions; |
| 202 | |
| 203 | struct StructEqualityFunction : public EqualityFunction |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 204 | { |
| 205 | const TStructure *structure; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 206 | }; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame^] | 207 | std::vector<StructEqualityFunction*> mStructEqualityFunctions; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 208 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame^] | 209 | struct ArrayEqualityFunction : public EqualityFunction |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 210 | { |
| 211 | TType type; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 212 | }; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame^] | 213 | std::vector<ArrayEqualityFunction*> mArrayEqualityFunctions; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 214 | }; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 215 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 218 | #endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_ |