blob: 833f4736bf953ea71d88d4e5190b118ba378e669 [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>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000011#include <map>
Jamie Madill32aab012015-01-27 14:12:26 -050012#include <stack>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000013
Jamie Madillf51639a2014-06-25 16:04:57 -040014#include "angle_gl.h"
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070015#include "compiler/translator/ASTMetadataHLSL.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040016#include "compiler/translator/IntermNode.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040017#include "compiler/translator/ParseContext.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020019class BuiltInFunctionEmulator;
Olli Etuahoe17e3192015-01-02 12:47:59 +020020
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021namespace sh
22{
Corentin Wallezc2ed9382016-04-15 13:29:25 +000023class StructureHLSL;
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024class TextureFunctionHLSL;
25class UnfoldShortCircuit;
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:
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080033 OutputHLSL(sh::GLenum shaderType,
34 int shaderVersion,
35 const TExtensionBehavior &extensionBehavior,
36 const char *sourcePath,
37 ShShaderOutput outputType,
38 int numRenderTargets,
39 const std::vector<Uniform> &uniforms,
40 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020041
daniel@transgaming.comb5875982010-04-15 20:44:53 +000042 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020044 void output(TIntermNode *treeRoot, TInfoSinkBase &objSink);
daniel@transgaming.com950f9932010-04-13 03:26:14 +000045
Jamie Madill4e1fd412014-07-10 17:50:10 -040046 const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const;
Jamie Madill9fe25e92014-07-18 10:33:08 -040047 const std::map<std::string, unsigned int> &getUniformRegisterMap() const;
Jamie Madill4e1fd412014-07-10 17:50:10 -040048
daniel@transgaming.comb5875982010-04-15 20:44:53 +000049 static TString initializer(const TType &type);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000050
Jamie Madill32aab012015-01-27 14:12:26 -050051 TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); }
52
Olli Etuaho18b9deb2015-11-05 12:14:50 +020053 static bool canWriteAsHLSLLiteral(TIntermTyped *expression);
54
daniel@transgaming.com950f9932010-04-13 03:26:14 +000055 protected:
Jamie Madill8c46ab12015-12-07 16:39:19 -050056 void header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057
daniel@transgaming.com950f9932010-04-13 03:26:14 +000058 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059 void visitSymbol(TIntermSymbol*);
Jamie Madill4cfb1e82014-07-07 12:49:23 -040060 void visitRaw(TIntermRaw*);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 void visitConstantUnion(TIntermConstantUnion*);
Olli Etuahob6fa0432016-09-28 16:28:05 +010062 bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063 bool visitBinary(Visit visit, TIntermBinary*);
64 bool visitUnary(Visit visit, TIntermUnary*);
Olli Etuahod0bad2c2016-09-09 18:01:16 +030065 bool visitTernary(Visit visit, TIntermTernary *);
Olli Etuaho57961272016-09-14 13:57:46 +030066 bool visitIfElse(Visit visit, TIntermIfElse *);
Olli Etuaho3c1dfb52015-02-20 11:34:03 +020067 bool visitSwitch(Visit visit, TIntermSwitch *);
68 bool visitCase(Visit visit, TIntermCase *);
Olli Etuaho336b1472016-10-05 16:37:55 +010069 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070 bool visitAggregate(Visit visit, TIntermAggregate*);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010071 bool visitBlock(Visit visit, TIntermBlock *node);
Olli Etuaho13389b62016-10-16 11:48:18 +010072 bool visitDeclaration(Visit visit, TIntermDeclaration *node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073 bool visitLoop(Visit visit, TIntermLoop*);
74 bool visitBranch(Visit visit, TIntermBranch*);
75
Jamie Madill8c46ab12015-12-07 16:39:19 -050076 bool handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010077
78 // 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 -050079 void outputTriplet(TInfoSinkBase &out,
80 Visit visit,
81 const char *preString,
82 const char *inString,
83 const char *postString);
84 void outputLineDirective(TInfoSinkBase &out, int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000085 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000086 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
Daniel Bratell29190082015-02-20 16:42:54 +010088 // Emit constructor. Called with literal names so using const char* instead of TString.
Jamie Madill8c46ab12015-12-07 16:39:19 -050089 void outputConstructor(TInfoSinkBase &out,
90 Visit visit,
91 const TType &type,
92 const char *name,
93 const TIntermSequence *parameters);
94 const TConstantUnion *writeConstantUnion(TInfoSinkBase &out,
95 const TType &type,
96 const TConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000097
Olli Etuaho7fb49552015-03-18 17:27:44 +020098 void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out);
99
Jamie Madill8c46ab12015-12-07 16:39:19 -0500100 void writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -0500101 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
102
103 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
104 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200105 // Returns true if variable initializer could be written using literal {} notation.
106 bool writeConstantInitialization(TInfoSinkBase &out,
107 TIntermSymbol *symbolNode,
108 TIntermTyped *expression);
109
Jamie Madill37997142015-01-28 10:06:34 -0500110 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Olli Etuaho57961272016-09-14 13:57:46 +0300111 void writeIfElse(TInfoSinkBase &out, TIntermIfElse *node);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200112
Jamie Madill55e79e02015-02-09 15:35:00 -0500113 // Returns the function name
114 TString addStructEqualityFunction(const TStructure &structure);
Olli Etuaho7fb49552015-03-18 17:27:44 +0200115 TString addArrayEqualityFunction(const TType &type);
Olli Etuaho12690762015-03-31 12:55:28 +0300116 TString addArrayAssignmentFunction(const TType &type);
Olli Etuaho9638c352015-04-01 14:34:52 +0300117 TString addArrayConstructIntoFunction(const TType &type);
Jamie Madill55e79e02015-02-09 15:35:00 -0500118
Jamie Madill2e295e22015-04-29 10:41:33 -0400119 // Ensures if the type is a struct, the struct is defined
120 void ensureStructDefined(const TType &type);
121
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200122 sh::GLenum mShaderType;
123 int mShaderVersion;
124 const TExtensionBehavior &mExtensionBehavior;
125 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000126 const ShShaderOutput mOutputType;
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800127 ShCompileOptions mCompileOptions;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200128
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000129 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000130
131 // Output streams
132 TInfoSinkBase mHeader;
133 TInfoSinkBase mBody;
134 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000135
Jamie Madill32aab012015-01-27 14:12:26 -0500136 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
137 // write to the body. Instead use an InfoSink stack to keep our current state intact.
Olli Etuaho7fb49552015-03-18 17:27:44 +0200138 // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler.
Jamie Madill32aab012015-01-27 14:12:26 -0500139 std::stack<TInfoSinkBase *> mInfoSinkStack;
140
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000141 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000142 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000143 ReferencedSymbols mReferencedAttributes;
144 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400145 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000146
Jamie Madill8daaba12014-06-13 10:04:33 -0400147 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400148 UniformHLSL *mUniformHLSL;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300149 TextureFunctionHLSL *mTextureFunctionHLSL;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400150
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000151 // Parameters determining what goes in the header output
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000152 bool mUsesFragColor;
153 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000154 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000155 bool mUsesFragCoord;
156 bool mUsesPointCoord;
157 bool mUsesFrontFacing;
158 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000159 bool mUsesInstanceID;
Corentin Wallezb076add2016-01-11 16:45:46 -0500160 bool mUsesVertexID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400161 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000162 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500163 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400164 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530165 bool mRequiresIEEEStrictCompiling;
166
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000167
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000168 int mNumRenderTargets;
169
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000170 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000171
Corentin Wallez1239ee92015-03-19 14:38:02 -0700172 CallDAG mCallDag;
173 MetadataList mASTMetadataList;
174 ASTMetadataHLSL *mCurrentFunctionMetadata;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000175 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000176 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400177 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000178
179 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000180
Jamie Madill98493dd2013-07-08 14:39:03 -0400181 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000182
Jamie Madill570e04d2013-06-21 09:15:33 -0400183 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
184 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
185
Olli Etuaho12690762015-03-31 12:55:28 +0300186 struct HelperFunction
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200187 {
188 TString functionName;
189 TString functionDefinition;
190
Olli Etuaho12690762015-03-31 12:55:28 +0300191 virtual ~HelperFunction() {}
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200192 };
193
194 // A list of all equality comparison functions. It's important to preserve the order at
195 // which we add the functions, since nested structures call each other recursively, and
196 // structure equality functions may need to call array equality functions and vice versa.
197 // The ownership of the pointers is maintained by the type-specific arrays.
Olli Etuaho12690762015-03-31 12:55:28 +0300198 std::vector<HelperFunction*> mEqualityFunctions;
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200199
Olli Etuaho12690762015-03-31 12:55:28 +0300200 struct StructEqualityFunction : public HelperFunction
Jamie Madill55e79e02015-02-09 15:35:00 -0500201 {
202 const TStructure *structure;
Jamie Madill55e79e02015-02-09 15:35:00 -0500203 };
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200204 std::vector<StructEqualityFunction*> mStructEqualityFunctions;
Jamie Madill55e79e02015-02-09 15:35:00 -0500205
Olli Etuaho12690762015-03-31 12:55:28 +0300206 struct ArrayHelperFunction : public HelperFunction
Olli Etuaho7fb49552015-03-18 17:27:44 +0200207 {
208 TType type;
Olli Etuaho7fb49552015-03-18 17:27:44 +0200209 };
Olli Etuaho12690762015-03-31 12:55:28 +0300210 std::vector<ArrayHelperFunction*> mArrayEqualityFunctions;
211
212 std::vector<ArrayHelperFunction> mArrayAssignmentFunctions;
Olli Etuaho9638c352015-04-01 14:34:52 +0300213
214 // The construct-into functions are functions that fill an N-element array passed as an out parameter
215 // with the other N parameters of the function. This is used to work around that arrays can't be
216 // return values in HLSL.
217 std::vector<ArrayHelperFunction> mArrayConstructIntoFunctions;
Olli Etuaho96963162016-03-21 11:54:33 +0200218
219 private:
220 TString samplerNamePrefixFromStruct(TIntermTyped *node);
221 bool ancestorEvaluatesToSamplerInStruct(Visit visit);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222};
Jamie Madill8daaba12014-06-13 10:04:33 -0400223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224}
225
Geoff Lang0a73dd82014-11-19 16:18:08 -0500226#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_