blob: 122b81e2d2273038e49bcc6317838baff2c5c6b2 [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"
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{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000023class UnfoldShortCircuit;
Jamie Madill8daaba12014-06-13 10:04:33 -040024class StructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -040025class UniformHLSL;
26
27typedef std::map<TString, TIntermSymbol*> ReferencedSymbols;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029class OutputHLSL : public TIntermTraverser
30{
31 public:
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020032 OutputHLSL(sh::GLenum shaderType, int shaderVersion,
33 const TExtensionBehavior &extensionBehavior,
34 const char *sourcePath, ShShaderOutput outputType,
35 int numRenderTargets, const std::vector<Uniform> &uniforms,
36 int compileOptions);
37
daniel@transgaming.comb5875982010-04-15 20:44:53 +000038 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020040 void output(TIntermNode *treeRoot, TInfoSinkBase &objSink);
daniel@transgaming.com950f9932010-04-13 03:26:14 +000041
Jamie Madill4e1fd412014-07-10 17:50:10 -040042 const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const;
Jamie Madill9fe25e92014-07-18 10:33:08 -040043 const std::map<std::string, unsigned int> &getUniformRegisterMap() const;
Jamie Madill4e1fd412014-07-10 17:50:10 -040044
daniel@transgaming.comb5875982010-04-15 20:44:53 +000045 static TString initializer(const TType &type);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000046
Jamie Madill32aab012015-01-27 14:12:26 -050047 TInfoSinkBase &getInfoSink() { ASSERT(!mInfoSinkStack.empty()); return *mInfoSinkStack.top(); }
48
daniel@transgaming.com950f9932010-04-13 03:26:14 +000049 protected:
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020050 void header(const BuiltInFunctionEmulator *builtInFunctionEmulator);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051
daniel@transgaming.com950f9932010-04-13 03:26:14 +000052 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053 void visitSymbol(TIntermSymbol*);
Jamie Madill4cfb1e82014-07-07 12:49:23 -040054 void visitRaw(TIntermRaw*);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 void visitConstantUnion(TIntermConstantUnion*);
56 bool visitBinary(Visit visit, TIntermBinary*);
57 bool visitUnary(Visit visit, TIntermUnary*);
58 bool visitSelection(Visit visit, TIntermSelection*);
Olli Etuaho3c1dfb52015-02-20 11:34:03 +020059 bool visitSwitch(Visit visit, TIntermSwitch *);
60 bool visitCase(Visit visit, TIntermCase *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 bool visitAggregate(Visit visit, TIntermAggregate*);
62 bool visitLoop(Visit visit, TIntermLoop*);
63 bool visitBranch(Visit visit, TIntermBranch*);
64
daniel@transgaming.com44fffee2012-04-28 00:34:20 +000065 void traverseStatements(TIntermNode *node);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000066 bool isSingleStatement(TIntermNode *node);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +000067 bool handleExcessiveLoop(TIntermLoop *node);
Daniel Bratell29190082015-02-20 16:42:54 +010068
69 // Emit one of three strings depending on traverse phase. Called with literal strings so using const char* instead of TString.
70 void outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000071 void outputLineDirective(int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000072 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000073 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074
Daniel Bratell29190082015-02-20 16:42:54 +010075 // Emit constructor. Called with literal names so using const char* instead of TString.
76 void outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +000077 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000078
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020079 void writeEmulatedFunctionTriplet(Visit visit, const char *preStr);
Jamie Madill37997142015-01-28 10:06:34 -050080 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
81
82 // Returns true if it found a 'same symbol' initializer (initializer that references the variable it's initting)
83 bool writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression);
84 void writeDeferredGlobalInitializers(TInfoSinkBase &out);
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +020085
Jamie Madill55e79e02015-02-09 15:35:00 -050086 // Returns the function name
87 TString addStructEqualityFunction(const TStructure &structure);
88
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020089 sh::GLenum mShaderType;
90 int mShaderVersion;
91 const TExtensionBehavior &mExtensionBehavior;
92 const char *mSourcePath;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000093 const ShShaderOutput mOutputType;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020094 int mCompileOptions;
95
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000096 UnfoldShortCircuit *mUnfoldShortCircuit;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000097 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +000098
99 // Output streams
100 TInfoSinkBase mHeader;
101 TInfoSinkBase mBody;
102 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000103
Jamie Madill32aab012015-01-27 14:12:26 -0500104 // A stack is useful when we want to traverse in the header, or in helper functions, but not always
105 // write to the body. Instead use an InfoSink stack to keep our current state intact.
106 std::stack<TInfoSinkBase *> mInfoSinkStack;
107
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000108 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000109 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000110 ReferencedSymbols mReferencedAttributes;
111 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -0400112 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000113
Jamie Madill8daaba12014-06-13 10:04:33 -0400114 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -0400115 UniformHLSL *mUniformHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400116
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400117 struct TextureFunction
118 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400119 enum Method
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400120 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400121 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400122 BIAS,
123 LOD,
Nicolas Capens75fb4752013-07-10 15:14:47 -0400124 LOD0,
Nicolas Capens84cfa122014-04-14 13:48:45 -0400125 LOD0BIAS,
Nicolas Capensfc014542014-02-18 14:47:13 -0500126 SIZE, // textureSize()
Nicolas Capensd11d5492014-02-19 17:06:10 -0500127 FETCH,
128 GRAD
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400129 };
130
131 TBasicType sampler;
132 int coords;
133 bool proj;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500134 bool offset;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400135 Method method;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400136
137 TString name() const;
138
139 bool operator<(const TextureFunction &rhs) const;
140 };
141
142 typedef std::set<TextureFunction> TextureFunctionSet;
143
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000144 // Parameters determining what goes in the header output
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400145 TextureFunctionSet mUsesTexture;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000146 bool mUsesFragColor;
147 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000148 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000149 bool mUsesFragCoord;
150 bool mUsesPointCoord;
151 bool mUsesFrontFacing;
152 bool mUsesPointSize;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000153 bool mUsesInstanceID;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400154 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000155 bool mUsesXor;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500156 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400157 bool mUsesNestedBreak;
Arun Patole44efa0b2015-03-04 17:11:05 +0530158 bool mRequiresIEEEStrictCompiling;
159
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000160
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000161 int mNumRenderTargets;
162
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000163 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000164
165 bool mContainsLoopDiscontinuity;
Corentin Wallez80bacde2014-11-10 12:07:37 -0800166 bool mContainsAnyLoop;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000167 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000168 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400169 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000170
171 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000172
Jamie Madill98493dd2013-07-08 14:39:03 -0400173 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000174
Jamie Madill570e04d2013-06-21 09:15:33 -0400175 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
176 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
177
Jamie Madill37997142015-01-28 10:06:34 -0500178 // Some initializers use varyings, uniforms or attributes, thus we can't evaluate some variables
179 // at global static scope in HLSL. These variables depend on values which we retrieve from the
180 // shader input structure, which we set in the D3D main function. Instead, we can initialize
181 // these static globals after we initialize our other globals.
182 std::vector<std::pair<TIntermSymbol*, TIntermTyped*>> mDeferredGlobalInitializers;
Jamie Madill55e79e02015-02-09 15:35:00 -0500183
184 // A list of structure equality comparison functions. It's important to preserve the order at
185 // which we add the functions, since nested structures call each other recursively.
186 struct StructEqualityFunction
187 {
188 const TStructure *structure;
189 TString functionName;
190 TString functionDefinition;
191 };
192
193 std::vector<StructEqualityFunction> mStructEqualityFunctions;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194};
Jamie Madill8daaba12014-06-13 10:04:33 -0400195
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196}
197
Geoff Lang0a73dd82014-11-19 16:18:08 -0500198#endif // COMPILER_TRANSLATOR_OUTPUTHLSL_H_