blob: ebc00339a6df6cc9e24943336c418e77bd8408bf [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*);
62 bool visitBinary(Visit visit, TIntermBinary*);
63 bool visitUnary(Visit visit, TIntermUnary*);
Olli Etuahod0bad2c2016-09-09 18:01:16 +030064 bool visitTernary(Visit visit, TIntermTernary *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065 bool visitSelection(Visit visit, TIntermSelection*);
Olli Etuaho3c1dfb52015-02-20 11:34:03 +020066 bool visitSwitch(Visit visit, TIntermSwitch *);
67 bool visitCase(Visit visit, TIntermCase *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068 bool visitAggregate(Visit visit, TIntermAggregate*);
69 bool visitLoop(Visit visit, TIntermLoop*);
70 bool visitBranch(Visit visit, TIntermBranch*);
71
daniel@transgaming.comb5875982010-04-15 20:44:53 +000072 bool isSingleStatement(TIntermNode *node);
Jamie Madill8c46ab12015-12-07 16:39:19 -050073 bool handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010074
75 // 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 -050076 void outputTriplet(TInfoSinkBase &out,
77 Visit visit,
78 const char *preString,
79 const char *inString,
80 const char *postString);
81 void outputLineDirective(TInfoSinkBase &out, int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000082 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000083 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
Daniel Bratell29190082015-02-20 16:42:54 +010085 // Emit constructor. Called with literal names so using const char* instead of TString.
Jamie Madill8c46ab12015-12-07 16:39:19 -050086 void outputConstructor(TInfoSinkBase &out,
87 Visit visit,
88 const TType &type,
89 const char *name,
90 const TIntermSequence *parameters);
91 const TConstantUnion *writeConstantUnion(TInfoSinkBase &out,
92 const TType &type,
93 const TConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000094
Olli Etuaho7fb49552015-03-18 17:27:44 +020095 void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out);
96
Jamie Madill8c46ab12015-12-07 16:39:19 -050097 void writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -050098 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
99
100 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
101 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200102 // Returns true if variable initializer could be written using literal {} notation.
103 bool writeConstantInitialization(TInfoSinkBase &out,
104 TIntermSymbol *symbolNode,
105 TIntermTyped *expression);
106
Jamie Madill37997142015-01-28 10:06:34 -0500107 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500108 void writeSelection(TInfoSinkBase &out, TIntermSelection *node);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200109
Jamie Madill55e79e02015-02-09 15:35:00 -0500110 // Returns the function name
111 TString addStructEqualityFunction(const TStructure &structure);
Olli Etuaho7fb49552015-03-18 17:27:44 +0200112 TString addArrayEqualityFunction(const TType &type);
Olli Etuaho12690762015-03-31 12:55:28 +0300113 TString addArrayAssignmentFunction(const TType &type);
Olli Etuaho9638c352015-04-01 14:34:52 +0300114 TString addArrayConstructIntoFunction(const TType &type);
Jamie Madill55e79e02015-02-09 15:35:00 -0500115
Jamie Madill2e295e22015-04-29 10:41:33 -0400116 // Ensures if the type is a struct, the struct is defined
117 void ensureStructDefined(const TType &type);
118
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200119 sh::GLenum mShaderType;
120 int mShaderVersion;
121 const TExtensionBehavior &mExtensionBehavior;
122 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000123 const ShShaderOutput mOutputType;
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800124 ShCompileOptions mCompileOptions;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200125
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000126 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000127
128 // Output streams
129 TInfoSinkBase mHeader;
130 TInfoSinkBase mBody;
131 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000132
Jamie Madill32aab012015-01-27 14:12:26 -0500133 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
134 // write to the body. Instead use an InfoSink stack to keep our current state intact.
Olli Etuaho7fb49552015-03-18 17:27:44 +0200135 // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler.
Jamie Madill32aab012015-01-27 14:12:26 -0500136 std::stack<TInfoSinkBase *> mInfoSinkStack;
137
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000138 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000139 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000140 ReferencedSymbols mReferencedAttributes;
141 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400142 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000143
Jamie Madill8daaba12014-06-13 10:04:33 -0400144 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400145 UniformHLSL *mUniformHLSL;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300146 TextureFunctionHLSL *mTextureFunctionHLSL;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400147
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000148 // Parameters determining what goes in the header output
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000149 bool mUsesFragColor;
150 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000151 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000152 bool mUsesFragCoord;
153 bool mUsesPointCoord;
154 bool mUsesFrontFacing;
155 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000156 bool mUsesInstanceID;
Corentin Wallezb076add2016-01-11 16:45:46 -0500157 bool mUsesVertexID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400158 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000159 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500160 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400161 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530162 bool mRequiresIEEEStrictCompiling;
163
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000164
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000165 int mNumRenderTargets;
166
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000167 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000168
Corentin Wallez1239ee92015-03-19 14:38:02 -0700169 CallDAG mCallDag;
170 MetadataList mASTMetadataList;
171 ASTMetadataHLSL *mCurrentFunctionMetadata;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000172 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000173 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400174 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000175
176 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000177
Jamie Madill98493dd2013-07-08 14:39:03 -0400178 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000179
Jamie Madill570e04d2013-06-21 09:15:33 -0400180 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
181 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
182
Olli Etuaho12690762015-03-31 12:55:28 +0300183 struct HelperFunction
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200184 {
185 TString functionName;
186 TString functionDefinition;
187
Olli Etuaho12690762015-03-31 12:55:28 +0300188 virtual ~HelperFunction() {}
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200189 };
190
191 // A list of all equality comparison functions. It's important to preserve the order at
192 // which we add the functions, since nested structures call each other recursively, and
193 // structure equality functions may need to call array equality functions and vice versa.
194 // The ownership of the pointers is maintained by the type-specific arrays.
Olli Etuaho12690762015-03-31 12:55:28 +0300195 std::vector<HelperFunction*> mEqualityFunctions;
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200196
Olli Etuaho12690762015-03-31 12:55:28 +0300197 struct StructEqualityFunction : public HelperFunction
Jamie Madill55e79e02015-02-09 15:35:00 -0500198 {
199 const TStructure *structure;
Jamie Madill55e79e02015-02-09 15:35:00 -0500200 };
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200201 std::vector<StructEqualityFunction*> mStructEqualityFunctions;
Jamie Madill55e79e02015-02-09 15:35:00 -0500202
Olli Etuaho12690762015-03-31 12:55:28 +0300203 struct ArrayHelperFunction : public HelperFunction
Olli Etuaho7fb49552015-03-18 17:27:44 +0200204 {
205 TType type;
Olli Etuaho7fb49552015-03-18 17:27:44 +0200206 };
Olli Etuaho12690762015-03-31 12:55:28 +0300207 std::vector<ArrayHelperFunction*> mArrayEqualityFunctions;
208
209 std::vector<ArrayHelperFunction> mArrayAssignmentFunctions;
Olli Etuaho9638c352015-04-01 14:34:52 +0300210
211 // The construct-into functions are functions that fill an N-element array passed as an out parameter
212 // with the other N parameters of the function. This is used to work around that arrays can't be
213 // return values in HLSL.
214 std::vector<ArrayHelperFunction> mArrayConstructIntoFunctions;
Olli Etuaho96963162016-03-21 11:54:33 +0200215
216 private:
217 TString samplerNamePrefixFromStruct(TIntermTyped *node);
218 bool ancestorEvaluatesToSamplerInStruct(Visit visit);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219};
Jamie Madill8daaba12014-06-13 10:04:33 -0400220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Geoff Lang0a73dd82014-11-19 16:18:08 -0500223#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_