blob: 8756d0ba4c1f7a97fcabe117489fd7a2bcaf20fc [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_OUTPUTHLSL_H_
8#define COMPILER_TRANSLATOR_OUTPUTHLSL_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
alokp@chromium.org4e89d232010-05-14 19:37:21 +000010#include <list>
11#include <set>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000012#include <map>
Jamie Madill32aab012015-01-27 14:12:26 -050013#include <stack>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000014
Jamie Madillf51639a2014-06-25 16:04:57 -040015#include "angle_gl.h"
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070016#include "compiler/translator/ASTMetadataHLSL.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040017#include "compiler/translator/IntermNode.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040018#include "compiler/translator/ParseContext.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020020class BuiltInFunctionEmulator;
Olli Etuahoe17e3192015-01-02 12:47:59 +020021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace sh
23{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000024class UnfoldShortCircuit;
Jamie Madill8daaba12014-06-13 10:04:33 -040025class StructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -040026class UniformHLSL;
27
28typedef std::map<TString, TIntermSymbol*> ReferencedSymbols;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030class OutputHLSL : public TIntermTraverser
31{
32 public:
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020033 OutputHLSL(sh::GLenum shaderType, int shaderVersion,
34 const TExtensionBehavior &extensionBehavior,
35 const char *sourcePath, ShShaderOutput outputType,
36 int numRenderTargets, const std::vector<Uniform> &uniforms,
37 int compileOptions);
38
daniel@transgaming.comb5875982010-04-15 20:44:53 +000039 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020041 void output(TIntermNode *treeRoot, TInfoSinkBase &objSink);
daniel@transgaming.com950f9932010-04-13 03:26:14 +000042
Jamie Madill4e1fd412014-07-10 17:50:10 -040043 const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const;
Jamie Madill9fe25e92014-07-18 10:33:08 -040044 const std::map<std::string, unsigned int> &getUniformRegisterMap() const;
Jamie Madill4e1fd412014-07-10 17:50:10 -040045
daniel@transgaming.comb5875982010-04-15 20:44:53 +000046 static TString initializer(const TType &type);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000047
Jamie Madill32aab012015-01-27 14:12:26 -050048 TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); }
49
Olli Etuaho18b9deb2015-11-05 12:14:50 +020050 static bool canWriteAsHLSLLiteral(TIntermTyped *expression);
51
daniel@transgaming.com950f9932010-04-13 03:26:14 +000052 protected:
Jamie Madill8c46ab12015-12-07 16:39:19 -050053 void header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
daniel@transgaming.com950f9932010-04-13 03:26:14 +000055 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056 void visitSymbol(TIntermSymbol*);
Jamie Madill4cfb1e82014-07-07 12:49:23 -040057 void visitRaw(TIntermRaw*);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058 void visitConstantUnion(TIntermConstantUnion*);
59 bool visitBinary(Visit visit, TIntermBinary*);
60 bool visitUnary(Visit visit, TIntermUnary*);
61 bool visitSelection(Visit visit, TIntermSelection*);
Olli Etuaho3c1dfb52015-02-20 11:34:03 +020062 bool visitSwitch(Visit visit, TIntermSwitch *);
63 bool visitCase(Visit visit, TIntermCase *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064 bool visitAggregate(Visit visit, TIntermAggregate*);
65 bool visitLoop(Visit visit, TIntermLoop*);
66 bool visitBranch(Visit visit, TIntermBranch*);
67
daniel@transgaming.comb5875982010-04-15 20:44:53 +000068 bool isSingleStatement(TIntermNode *node);
Jamie Madill8c46ab12015-12-07 16:39:19 -050069 bool handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010070
71 // Emit one of three strings depending on traverse phase. Called with literal strings so using const char* instead of TString.
Jamie Madill8c46ab12015-12-07 16:39:19 -050072 void outputTriplet(TInfoSinkBase &out,
73 Visit visit,
74 const char *preString,
75 const char *inString,
76 const char *postString);
77 void outputLineDirective(TInfoSinkBase &out, int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000078 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000079 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080
Daniel Bratell29190082015-02-20 16:42:54 +010081 // Emit constructor. Called with literal names so using const char* instead of TString.
Jamie Madill8c46ab12015-12-07 16:39:19 -050082 void outputConstructor(TInfoSinkBase &out,
83 Visit visit,
84 const TType &type,
85 const char *name,
86 const TIntermSequence *parameters);
87 const TConstantUnion *writeConstantUnion(TInfoSinkBase &out,
88 const TType &type,
89 const TConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000090
Olli Etuaho7fb49552015-03-18 17:27:44 +020091 void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out);
92
Jamie Madill8c46ab12015-12-07 16:39:19 -050093 void writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -050094 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
95
96 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
97 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
Olli Etuaho18b9deb2015-11-05 12:14:50 +020098 // Returns true if variable initializer could be written using literal {} notation.
99 bool writeConstantInitialization(TInfoSinkBase &out,
100 TIntermSymbol *symbolNode,
101 TIntermTyped *expression);
102
Jamie Madill37997142015-01-28 10:06:34 -0500103 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500104 void writeSelection(TInfoSinkBase &out, TIntermSelection *node);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200105
Jamie Madill55e79e02015-02-09 15:35:00 -0500106 // Returns the function name
107 TString addStructEqualityFunction(const TStructure &structure);
Olli Etuaho7fb49552015-03-18 17:27:44 +0200108 TString addArrayEqualityFunction(const TType &type);
Olli Etuaho12690762015-03-31 12:55:28 +0300109 TString addArrayAssignmentFunction(const TType &type);
Olli Etuaho9638c352015-04-01 14:34:52 +0300110 TString addArrayConstructIntoFunction(const TType &type);
Jamie Madill55e79e02015-02-09 15:35:00 -0500111
Jamie Madill2e295e22015-04-29 10:41:33 -0400112 // Ensures if the type is a struct, the struct is defined
113 void ensureStructDefined(const TType &type);
114
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200115 sh::GLenum mShaderType;
116 int mShaderVersion;
117 const TExtensionBehavior &mExtensionBehavior;
118 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000119 const ShShaderOutput mOutputType;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200120 int mCompileOptions;
121
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000122 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000123
124 // Output streams
125 TInfoSinkBase mHeader;
126 TInfoSinkBase mBody;
127 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000128
Jamie Madill32aab012015-01-27 14:12:26 -0500129 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
130 // write to the body. Instead use an InfoSink stack to keep our current state intact.
Olli Etuaho7fb49552015-03-18 17:27:44 +0200131 // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler.
Jamie Madill32aab012015-01-27 14:12:26 -0500132 std::stack<TInfoSinkBase *> mInfoSinkStack;
133
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000134 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000135 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000136 ReferencedSymbols mReferencedAttributes;
137 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400138 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000139
Jamie Madill8daaba12014-06-13 10:04:33 -0400140 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400141 UniformHLSL *mUniformHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400142
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400143 struct TextureFunction
144 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400145 enum Method
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400146 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400147 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400148 BIAS,
149 LOD,
Nicolas Capens75fb4752013-07-10 15:14:47 -0400150 LOD0,
Nicolas Capens84cfa122014-04-14 13:48:45 -0400151 LOD0BIAS,
Nicolas Capensfc014542014-02-18 14:47:13 -0500152 SIZE, // textureSize()
Nicolas Capensd11d5492014-02-19 17:06:10 -0500153 FETCH,
154 GRAD
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400155 };
156
157 TBasicType sampler;
158 int coords;
159 bool proj;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500160 bool offset;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400161 Method method;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400162
163 TString name() const;
164
165 bool operator<(const TextureFunction &rhs) const;
166 };
167
168 typedef std::set<TextureFunction> TextureFunctionSet;
169
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000170 // Parameters determining what goes in the header output
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400171 TextureFunctionSet mUsesTexture;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000172 bool mUsesFragColor;
173 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000174 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000175 bool mUsesFragCoord;
176 bool mUsesPointCoord;
177 bool mUsesFrontFacing;
178 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000179 bool mUsesInstanceID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400180 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000181 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500182 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400183 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530184 bool mRequiresIEEEStrictCompiling;
185
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000186
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000187 int mNumRenderTargets;
188
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000189 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000190
Corentin Wallez1239ee92015-03-19 14:38:02 -0700191 CallDAG mCallDag;
192 MetadataList mASTMetadataList;
193 ASTMetadataHLSL *mCurrentFunctionMetadata;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000194 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000195 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400196 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000197
198 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000199
Jamie Madill98493dd2013-07-08 14:39:03 -0400200 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000201
Jamie Madill570e04d2013-06-21 09:15:33 -0400202 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
203 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
204
Olli Etuahod81ed842015-05-12 12:46:35 +0300205 // Some initializers may have been unfolded into if statements, thus we can't evaluate all initializers
206 // at global static scope in HLSL. Instead, we can initialize these static globals inside a helper function.
207 // This also enables initialization of globals with uniforms.
208 TIntermSequence mDeferredGlobalInitializers;
Jamie Madill55e79e02015-02-09 15:35:00 -0500209
Olli Etuaho12690762015-03-31 12:55:28 +0300210 struct HelperFunction
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200211 {
212 TString functionName;
213 TString functionDefinition;
214
Olli Etuaho12690762015-03-31 12:55:28 +0300215 virtual ~HelperFunction() {}
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200216 };
217
218 // A list of all equality comparison functions. It's important to preserve the order at
219 // which we add the functions, since nested structures call each other recursively, and
220 // structure equality functions may need to call array equality functions and vice versa.
221 // The ownership of the pointers is maintained by the type-specific arrays.
Olli Etuaho12690762015-03-31 12:55:28 +0300222 std::vector<HelperFunction*> mEqualityFunctions;
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200223
Olli Etuaho12690762015-03-31 12:55:28 +0300224 struct StructEqualityFunction : public HelperFunction
Jamie Madill55e79e02015-02-09 15:35:00 -0500225 {
226 const TStructure *structure;
Jamie Madill55e79e02015-02-09 15:35:00 -0500227 };
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200228 std::vector<StructEqualityFunction*> mStructEqualityFunctions;
Jamie Madill55e79e02015-02-09 15:35:00 -0500229
Olli Etuaho12690762015-03-31 12:55:28 +0300230 struct ArrayHelperFunction : public HelperFunction
Olli Etuaho7fb49552015-03-18 17:27:44 +0200231 {
232 TType type;
Olli Etuaho7fb49552015-03-18 17:27:44 +0200233 };
Olli Etuaho12690762015-03-31 12:55:28 +0300234 std::vector<ArrayHelperFunction*> mArrayEqualityFunctions;
235
236 std::vector<ArrayHelperFunction> mArrayAssignmentFunctions;
Olli Etuaho9638c352015-04-01 14:34:52 +0300237
238 // The construct-into functions are functions that fill an N-element array passed as an out parameter
239 // with the other N parameters of the function. This is used to work around that arrays can't be
240 // return values in HLSL.
241 std::vector<ArrayHelperFunction> mArrayConstructIntoFunctions;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242};
Jamie Madill8daaba12014-06-13 10:04:33 -0400243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244}
245
Geoff Lang0a73dd82014-11-19 16:18:08 -0500246#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_