blob: 62b37c12a83132a4713d02dca1f649b24aad7bb8 [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
daniel@transgaming.com950f9932010-04-13 03:26:14 +000050 protected:
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020051 void header(const BuiltInFunctionEmulator *builtInFunctionEmulator);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052
daniel@transgaming.com950f9932010-04-13 03:26:14 +000053 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054 void visitSymbol(TIntermSymbol*);
Jamie Madill4cfb1e82014-07-07 12:49:23 -040055 void visitRaw(TIntermRaw*);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056 void visitConstantUnion(TIntermConstantUnion*);
57 bool visitBinary(Visit visit, TIntermBinary*);
58 bool visitUnary(Visit visit, TIntermUnary*);
59 bool visitSelection(Visit visit, TIntermSelection*);
Olli Etuaho3c1dfb52015-02-20 11:34:03 +020060 bool visitSwitch(Visit visit, TIntermSwitch *);
61 bool visitCase(Visit visit, TIntermCase *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 bool visitAggregate(Visit visit, TIntermAggregate*);
63 bool visitLoop(Visit visit, TIntermLoop*);
64 bool visitBranch(Visit visit, TIntermBranch*);
65
daniel@transgaming.com44fffee2012-04-28 00:34:20 +000066 void traverseStatements(TIntermNode *node);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000067 bool isSingleStatement(TIntermNode *node);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +000068 bool handleExcessiveLoop(TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010069
70 // Emit one of three strings depending on traverse phase. Called with literal strings so using const char* instead of TString.
Olli Etuaho7fb49552015-03-18 17:27:44 +020071 void outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString, TInfoSinkBase &out);
Daniel Bratell29190082015-02-20 16:42:54 +010072 void outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000073 void outputLineDirective(int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000074 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000075 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076
Daniel Bratell29190082015-02-20 16:42:54 +010077 // Emit constructor. Called with literal names so using const char* instead of TString.
78 void outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +000079 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000080
Olli Etuaho7fb49552015-03-18 17:27:44 +020081 void outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out);
82
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020083 void writeEmulatedFunctionTriplet(Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -050084 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
85
86 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
87 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
88 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020089
Jamie Madill55e79e02015-02-09 15:35:00 -050090 // Returns the function name
91 TString addStructEqualityFunction(const TStructure &structure);
Olli Etuaho7fb49552015-03-18 17:27:44 +020092 TString addArrayEqualityFunction(const TType &type);
Olli Etuaho12690762015-03-31 12:55:28 +030093 TString addArrayAssignmentFunction(const TType &type);
Olli Etuaho9638c352015-04-01 14:34:52 +030094 TString addArrayConstructIntoFunction(const TType &type);
Jamie Madill55e79e02015-02-09 15:35:00 -050095
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020096 sh::GLenum mShaderType;
97 int mShaderVersion;
98 const TExtensionBehavior &mExtensionBehavior;
99 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000100 const ShShaderOutput mOutputType;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200101 int mCompileOptions;
102
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000103 UnfoldShortCircuit *mUnfoldShortCircuit;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000104 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000105
106 // Output streams
107 TInfoSinkBase mHeader;
108 TInfoSinkBase mBody;
109 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000110
Jamie Madill32aab012015-01-27 14:12:26 -0500111 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
112 // write to the body. Instead use an InfoSink stack to keep our current state intact.
Olli Etuaho7fb49552015-03-18 17:27:44 +0200113 // TODO (jmadill): Just passing an InfoSink in function parameters would be simpler.
Jamie Madill32aab012015-01-27 14:12:26 -0500114 std::stack<TInfoSinkBase *> mInfoSinkStack;
115
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000116 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000117 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000118 ReferencedSymbols mReferencedAttributes;
119 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400120 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000121
Jamie Madill8daaba12014-06-13 10:04:33 -0400122 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400123 UniformHLSL *mUniformHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400124
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400125 struct TextureFunction
126 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400127 enum Method
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400128 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400129 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400130 BIAS,
131 LOD,
Nicolas Capens75fb4752013-07-10 15:14:47 -0400132 LOD0,
Nicolas Capens84cfa122014-04-14 13:48:45 -0400133 LOD0BIAS,
Nicolas Capensfc014542014-02-18 14:47:13 -0500134 SIZE, // textureSize()
Nicolas Capensd11d5492014-02-19 17:06:10 -0500135 FETCH,
136 GRAD
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400137 };
138
139 TBasicType sampler;
140 int coords;
141 bool proj;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500142 bool offset;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400143 Method method;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400144
145 TString name() const;
146
147 bool operator<(const TextureFunction &rhs) const;
148 };
149
150 typedef std::set<TextureFunction> TextureFunctionSet;
151
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000152 // Parameters determining what goes in the header output
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400153 TextureFunctionSet mUsesTexture;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000154 bool mUsesFragColor;
155 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000156 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000157 bool mUsesFragCoord;
158 bool mUsesPointCoord;
159 bool mUsesFrontFacing;
160 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000161 bool mUsesInstanceID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400162 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000163 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500164 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400165 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530166 bool mRequiresIEEEStrictCompiling;
167
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000168
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000169 int mNumRenderTargets;
170
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000171 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000172
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700173 std::vector<ASTMetadataHLSL> mASTAnalyses;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000174 bool mContainsLoopDiscontinuity;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800175 bool mContainsAnyLoop;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000176 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000177 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400178 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000179
180 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000181
Jamie Madill98493dd2013-07-08 14:39:03 -0400182 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000183
Jamie Madill570e04d2013-06-21 09:15:33 -0400184 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
185 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
186
Jamie Madill37997142015-01-28 10:06:34 -0500187 // Some initializers use varyings, uniforms or attributes, thus we can't evaluate some variables
188 // at global static scope in HLSL. These variables depend on values which we retrieve from the
189 // shader input structure, which we set in the D3D main function. Instead, we can initialize
190 // these static globals after we initialize our other globals.
191 std::vector<std::pair<TIntermSymbol*, TIntermTyped*>> mDeferredGlobalInitializers;
Jamie Madill55e79e02015-02-09 15:35:00 -0500192
Olli Etuaho12690762015-03-31 12:55:28 +0300193 struct HelperFunction
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200194 {
195 TString functionName;
196 TString functionDefinition;
197
Olli Etuaho12690762015-03-31 12:55:28 +0300198 virtual ~HelperFunction() {}
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200199 };
200
201 // A list of all equality comparison functions. It's important to preserve the order at
202 // which we add the functions, since nested structures call each other recursively, and
203 // structure equality functions may need to call array equality functions and vice versa.
204 // The ownership of the pointers is maintained by the type-specific arrays.
Olli Etuaho12690762015-03-31 12:55:28 +0300205 std::vector<HelperFunction*> mEqualityFunctions;
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200206
Olli Etuaho12690762015-03-31 12:55:28 +0300207 struct StructEqualityFunction : public HelperFunction
Jamie Madill55e79e02015-02-09 15:35:00 -0500208 {
209 const TStructure *structure;
Jamie Madill55e79e02015-02-09 15:35:00 -0500210 };
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200211 std::vector<StructEqualityFunction*> mStructEqualityFunctions;
Jamie Madill55e79e02015-02-09 15:35:00 -0500212
Olli Etuaho12690762015-03-31 12:55:28 +0300213 struct ArrayHelperFunction : public HelperFunction
Olli Etuaho7fb49552015-03-18 17:27:44 +0200214 {
215 TType type;
Olli Etuaho7fb49552015-03-18 17:27:44 +0200216 };
Olli Etuaho12690762015-03-31 12:55:28 +0300217 std::vector<ArrayHelperFunction*> mArrayEqualityFunctions;
218
219 std::vector<ArrayHelperFunction> mArrayAssignmentFunctions;
Olli Etuaho9638c352015-04-01 14:34:52 +0300220
221 // The construct-into functions are functions that fill an N-element array passed as an out parameter
222 // with the other N parameters of the function. This is used to work around that arrays can't be
223 // return values in HLSL.
224 std::vector<ArrayHelperFunction> mArrayConstructIntoFunctions;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225};
Jamie Madill8daaba12014-06-13 10:04:33 -0400226
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227}
228
Geoff Lang0a73dd82014-11-19 16:18:08 -0500229#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_