blob: 73409e8acb5ee908b37549893fb4eeb0eef911d5 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 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
7#ifndef COMPILER_OUTPUTHLSL_H_
8#define COMPILER_OUTPUTHLSL_H_
9
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>
13
14#define GL_APICALL
shannon.woods%transgaming.com@gtempaccount.comf26ddae2013-04-13 03:29:13 +000015#include <GLES3/gl3.h>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000016#include <GLES2/gl2.h>
alokp@chromium.org4e89d232010-05-14 19:37:21 +000017
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018#include "compiler/intermediate.h"
19#include "compiler/ParseHelper.h"
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +000020#include "compiler/Uniform.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021
22namespace sh
23{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000024class UnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000025
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026class OutputHLSL : public TIntermTraverser
27{
28 public:
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000029 OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000030 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com950f9932010-04-13 03:26:14 +000032 void output();
33
daniel@transgaming.comb5875982010-04-15 20:44:53 +000034 TInfoSinkBase &getBodyStream();
daniel@transgaming.com043da132012-12-20 21:12:22 +000035 const ActiveUniforms &getUniforms();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000036 const ActiveInterfaceBlocks &getInterfaceBlocks() const;
Jamie Madill46131a32013-06-20 11:55:50 -040037 const ActiveShaderVariables &getOutputVariables() const;
Jamie Madilldefb6742013-06-20 11:55:51 -040038 const ActiveShaderVariables &getAttributes() const;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000039
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000040 TString typeString(const TType &type);
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +000041 TString textureString(const TType &type);
Nicolas Capenscb127d32013-07-15 17:26:18 -040042 TString samplerString(const TType &type);
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +000043 TString interpolationString(TQualifier qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -040044 TString structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing);
45 TString structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000046 static TString qualifierString(TQualifier qualifier);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000047 static TString arrayString(const TType &type);
48 static TString initializer(const TType &type);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +000049 static TString decorate(const TString &string); // Prepends an underscore to avoid naming clashes
apatrick@chromium.org65756022012-01-17 21:45:38 +000050 static TString decorateUniform(const TString &string, const TType &type);
Jamie Madill98493dd2013-07-08 14:39:03 -040051 static TString decorateField(const TString &string, const TStructure &structure);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000052
daniel@transgaming.com950f9932010-04-13 03:26:14 +000053 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054 void header();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055
daniel@transgaming.com950f9932010-04-13 03:26:14 +000056 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057 void visitSymbol(TIntermSymbol*);
58 void visitConstantUnion(TIntermConstantUnion*);
59 bool visitBinary(Visit visit, TIntermBinary*);
60 bool visitUnary(Visit visit, TIntermUnary*);
61 bool visitSelection(Visit visit, TIntermSelection*);
62 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@transgaming.com67de6d62010-04-29 03:35:30 +000069 void outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000070 void outputLineDirective(int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000071 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000072 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073
daniel@transgaming.com67de6d62010-04-29 03:35:30 +000074 void addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +000075 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000076
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000077 TString scopeString(unsigned int depthLimit);
78 TString scopedStruct(const TString &typeName);
79 TString structLookup(const TString &typeName);
80
daniel@transgaming.com950f9932010-04-13 03:26:14 +000081 TParseContext &mContext;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000082 const ShShaderOutput mOutputType;
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000083 UnfoldShortCircuit *mUnfoldShortCircuit;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000084 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +000085
86 // Output streams
87 TInfoSinkBase mHeader;
88 TInfoSinkBase mBody;
89 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000090
daniel@transgaming.com8803b852012-12-20 21:11:47 +000091 typedef std::map<TString, TIntermSymbol*> ReferencedSymbols;
92 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000093 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +000094 ReferencedSymbols mReferencedAttributes;
95 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -040096 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +000097
Nicolas Capense0ba27a2013-06-24 16:10:52 -040098 struct TextureFunction
99 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400100 enum Method
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400101 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400102 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400103 BIAS,
104 LOD,
Nicolas Capens75fb4752013-07-10 15:14:47 -0400105 LOD0,
106 SIZE // textureSize()
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400107 };
108
109 TBasicType sampler;
110 int coords;
111 bool proj;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400112 Method method;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400113
114 TString name() const;
115
116 bool operator<(const TextureFunction &rhs) const;
117 };
118
119 typedef std::set<TextureFunction> TextureFunctionSet;
120
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000121 // Parameters determining what goes in the header output
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400122 TextureFunctionSet mUsesTexture;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000123 bool mUsesFragColor;
124 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000125 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000126 bool mUsesFragCoord;
127 bool mUsesPointCoord;
128 bool mUsesFrontFacing;
129 bool mUsesPointSize;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400130 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000131 bool mUsesXor;
132 bool mUsesMod1;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000133 bool mUsesMod2v;
134 bool mUsesMod2f;
135 bool mUsesMod3v;
136 bool mUsesMod3f;
137 bool mUsesMod4v;
138 bool mUsesMod4f;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000139 bool mUsesFaceforward1;
140 bool mUsesFaceforward2;
141 bool mUsesFaceforward3;
142 bool mUsesFaceforward4;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000143 bool mUsesEqualMat[5][5];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000144 bool mUsesEqualVec2;
145 bool mUsesEqualVec3;
146 bool mUsesEqualVec4;
147 bool mUsesEqualIVec2;
148 bool mUsesEqualIVec3;
149 bool mUsesEqualIVec4;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000150 bool mUsesEqualUVec2;
151 bool mUsesEqualUVec3;
152 bool mUsesEqualUVec4;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000153 bool mUsesEqualBVec2;
154 bool mUsesEqualBVec3;
155 bool mUsesEqualBVec4;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000156 bool mUsesAtan2_1;
157 bool mUsesAtan2_2;
158 bool mUsesAtan2_3;
159 bool mUsesAtan2_4;
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.coma2a95e72010-05-20 19:17:55 +0000163 typedef std::set<TString> Constructors;
164 Constructors mConstructors;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000165
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000166 typedef std::set<TString> StructNames;
167 StructNames mStructNames;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000168
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000169 typedef std::list<TString> StructDeclarations;
170 StructDeclarations mStructDeclarations;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000171
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000172 typedef std::vector<int> ScopeBracket;
173 ScopeBracket mScopeBracket;
174 unsigned int mScopeDepth;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +0000175
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000176 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000177
178 bool mContainsLoopDiscontinuity;
179 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000180 bool mInsideDiscontinuousLoop;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000181
182 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000183
184 int mUniformRegister;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000185 int mInterfaceBlockRegister;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000186 int mSamplerRegister;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400187 int mPaddingCounter;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000188
189 TString registerString(TIntermSymbol *operand);
190 int samplerRegister(TIntermSymbol *sampler);
191 int uniformRegister(TIntermSymbol *uniform);
Jamie Madill98493dd2013-07-08 14:39:03 -0400192 void declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000193 void declareUniform(const TType &type, const TString &name, int index);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000194
Jamie Madill98493dd2013-07-08 14:39:03 -0400195 TString interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000196 TString decoratePrivate(const TString &privateText);
Jamie Madill98493dd2013-07-08 14:39:03 -0400197 TString interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlockType);
198 TString interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex);
199 TString interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage);
200 TString interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage);
201 TString interfaceBlockStructString(const TInterfaceBlock &interfaceBlock);
202 TString interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400203 TString std140PrePaddingString(const TType &type, int *elementIndex);
Jamie Madille4075c92013-06-21 09:15:32 -0400204 TString std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking);
Jamie Madill98493dd2013-07-08 14:39:03 -0400205 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +0000206
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000207 static GLenum glVariableType(const TType &type);
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +0000208 static GLenum glVariablePrecision(const TType &type);
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +0000209 static bool isVaryingIn(TQualifier qualifier);
210 static bool isVaryingOut(TQualifier qualifier);
211 static bool isVarying(TQualifier qualifier);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000212
213 ActiveUniforms mActiveUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000214 ActiveInterfaceBlocks mActiveInterfaceBlocks;
Jamie Madill46131a32013-06-20 11:55:50 -0400215 ActiveShaderVariables mActiveOutputVariables;
Jamie Madilldefb6742013-06-20 11:55:51 -0400216 ActiveShaderVariables mActiveAttributes;
Jamie Madille4075c92013-06-21 09:15:32 -0400217 std::map<TString, int> mStd140StructElementIndexes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400218 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
219 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
220
221 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222};
223}
224
225#endif // COMPILER_OUTPUTHLSL_H_