blob: b76062efafaf0a44672d110253defb5df5a5da54 [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 *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000069 bool visitAggregate(Visit visit, TIntermAggregate*);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010070 bool visitBlock(Visit visit, TIntermBlock *node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071 bool visitLoop(Visit visit, TIntermLoop*);
72 bool visitBranch(Visit visit, TIntermBranch*);
73
daniel@transgaming.comb5875982010-04-15 20:44:53 +000074 bool isSingleStatement(TIntermNode *node);
Jamie Madill8c46ab12015-12-07 16:39:19 -050075 bool handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010076
77 // 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 -050078 void outputTriplet(TInfoSinkBase &out,
79 Visit visit,
80 const char *preString,
81 const char *inString,
82 const char *postString);
83 void outputLineDirective(TInfoSinkBase &out, int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000084 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000085 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
Daniel Bratell29190082015-02-20 16:42:54 +010087 // Emit constructor. Called with literal names so using const char* instead of TString.
Jamie Madill8c46ab12015-12-07 16:39:19 -050088 void outputConstructor(TInfoSinkBase &out,
89 Visit visit,
90 const TType &type,
91 const char *name,
92 const TIntermSequence *parameters);
93 const TConstantUnion *writeConstantUnion(TInfoSinkBase &out,
94 const TType &type,
95 const TConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000096
Olli Etuaho7fb49552015-03-18 17:27:44 +020097 void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out);
98
Jamie Madill8c46ab12015-12-07 16:39:19 -050099 void writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -0500100 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
101
102 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
103 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200104 // Returns true if variable initializer could be written using literal {} notation.
105 bool writeConstantInitialization(TInfoSinkBase &out,
106 TIntermSymbol *symbolNode,
107 TIntermTyped *expression);
108
Jamie Madill37997142015-01-28 10:06:34 -0500109 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Olli Etuaho57961272016-09-14 13:57:46 +0300110 void writeIfElse(TInfoSinkBase &out, TIntermIfElse *node);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200111
Jamie Madill55e79e02015-02-09 15:35:00 -0500112 // Returns the function name
113 TString addStructEqualityFunction(const TStructure &structure);
Olli Etuaho7fb49552015-03-18 17:27:44 +0200114 TString addArrayEqualityFunction(const TType &type);
Olli Etuaho12690762015-03-31 12:55:28 +0300115 TString addArrayAssignmentFunction(const TType &type);
Olli Etuaho9638c352015-04-01 14:34:52 +0300116 TString addArrayConstructIntoFunction(const TType &type);
Jamie Madill55e79e02015-02-09 15:35:00 -0500117
Jamie Madill2e295e22015-04-29 10:41:33 -0400118 // Ensures if the type is a struct, the struct is defined
119 void ensureStructDefined(const TType &type);
120
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200121 sh::GLenum mShaderType;
122 int mShaderVersion;
123 const TExtensionBehavior &mExtensionBehavior;
124 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000125 const ShShaderOutput mOutputType;
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800126 ShCompileOptions mCompileOptions;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200127
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000128 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000129
130 // Output streams
131 TInfoSinkBase mHeader;
132 TInfoSinkBase mBody;
133 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000134
Jamie Madill32aab012015-01-27 14:12:26 -0500135 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
136 // write to the body. Instead use an InfoSink stack to keep our current state intact.
Olli Etuaho7fb49552015-03-18 17:27:44 +0200137 // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler.
Jamie Madill32aab012015-01-27 14:12:26 -0500138 std::stack<TInfoSinkBase *> mInfoSinkStack;
139
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000140 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000141 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000142 ReferencedSymbols mReferencedAttributes;
143 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400144 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000145
Jamie Madill8daaba12014-06-13 10:04:33 -0400146 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400147 UniformHLSL *mUniformHLSL;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300148 TextureFunctionHLSL *mTextureFunctionHLSL;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400149
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000150 // Parameters determining what goes in the header output
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000151 bool mUsesFragColor;
152 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000153 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000154 bool mUsesFragCoord;
155 bool mUsesPointCoord;
156 bool mUsesFrontFacing;
157 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000158 bool mUsesInstanceID;
Corentin Wallezb076add2016-01-11 16:45:46 -0500159 bool mUsesVertexID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400160 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000161 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500162 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400163 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530164 bool mRequiresIEEEStrictCompiling;
165
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000166
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000167 int mNumRenderTargets;
168
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000169 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170
Corentin Wallez1239ee92015-03-19 14:38:02 -0700171 CallDAG mCallDag;
172 MetadataList mASTMetadataList;
173 ASTMetadataHLSL *mCurrentFunctionMetadata;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000174 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000175 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400176 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000177
178 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000179
Jamie Madill98493dd2013-07-08 14:39:03 -0400180 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000181
Jamie Madill570e04d2013-06-21 09:15:33 -0400182 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
183 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
184
Olli Etuaho12690762015-03-31 12:55:28 +0300185 struct HelperFunction
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200186 {
187 TString functionName;
188 TString functionDefinition;
189
Olli Etuaho12690762015-03-31 12:55:28 +0300190 virtual ~HelperFunction() {}
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200191 };
192
193 // A list of all equality comparison functions. It's important to preserve the order at
194 // which we add the functions, since nested structures call each other recursively, and
195 // structure equality functions may need to call array equality functions and vice versa.
196 // The ownership of the pointers is maintained by the type-specific arrays.
Olli Etuaho12690762015-03-31 12:55:28 +0300197 std::vector<HelperFunction*> mEqualityFunctions;
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200198
Olli Etuaho12690762015-03-31 12:55:28 +0300199 struct StructEqualityFunction : public HelperFunction
Jamie Madill55e79e02015-02-09 15:35:00 -0500200 {
201 const TStructure *structure;
Jamie Madill55e79e02015-02-09 15:35:00 -0500202 };
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200203 std::vector<StructEqualityFunction*> mStructEqualityFunctions;
Jamie Madill55e79e02015-02-09 15:35:00 -0500204
Olli Etuaho12690762015-03-31 12:55:28 +0300205 struct ArrayHelperFunction : public HelperFunction
Olli Etuaho7fb49552015-03-18 17:27:44 +0200206 {
207 TType type;
Olli Etuaho7fb49552015-03-18 17:27:44 +0200208 };
Olli Etuaho12690762015-03-31 12:55:28 +0300209 std::vector<ArrayHelperFunction*> mArrayEqualityFunctions;
210
211 std::vector<ArrayHelperFunction> mArrayAssignmentFunctions;
Olli Etuaho9638c352015-04-01 14:34:52 +0300212
213 // The construct-into functions are functions that fill an N-element array passed as an out parameter
214 // with the other N parameters of the function. This is used to work around that arrays can't be
215 // return values in HLSL.
216 std::vector<ArrayHelperFunction> mArrayConstructIntoFunctions;
Olli Etuaho96963162016-03-21 11:54:33 +0200217
218 private:
219 TString samplerNamePrefixFromStruct(TIntermTyped *node);
220 bool ancestorEvaluatesToSamplerInStruct(Visit visit);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221};
Jamie Madill8daaba12014-06-13 10:04:33 -0400222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223}
224
Geoff Lang0a73dd82014-11-19 16:18:08 -0500225#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_