blob: 68c9899161f80a33bad736498d3d31d6450b3583 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002// Copyright (c) 2002-2012 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
15#include <GLES2/gl2.h>
alokp@chromium.org4e89d232010-05-14 19:37:21 +000016
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017#include "compiler/intermediate.h"
18#include "compiler/ParseHelper.h"
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +000019#include "compiler/Uniform.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
21namespace sh
22{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000023class UnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025class OutputHLSL : public TIntermTraverser
26{
27 public:
daniel@transgaming.comb5875982010-04-15 20:44:53 +000028 explicit OutputHLSL(TParseContext &context);
29 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com950f9932010-04-13 03:26:14 +000031 void output();
32
daniel@transgaming.comb5875982010-04-15 20:44:53 +000033 TInfoSinkBase &getBodyStream();
daniel@transgaming.com043da132012-12-20 21:12:22 +000034 const ActiveUniforms &getUniforms();
daniel@transgaming.comb5875982010-04-15 20:44:53 +000035
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000036 TString typeString(const TType &type);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000037 static TString qualifierString(TQualifier qualifier);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000038 static TString arrayString(const TType &type);
39 static TString initializer(const TType &type);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +000040 static TString decorate(const TString &string); // Prepends an underscore to avoid naming clashes
apatrick@chromium.org65756022012-01-17 21:45:38 +000041 static TString decorateUniform(const TString &string, const TType &type);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +000042 static TString decorateField(const TString &string, const TType &structure);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000043
daniel@transgaming.com950f9932010-04-13 03:26:14 +000044 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 void header();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046
daniel@transgaming.com950f9932010-04-13 03:26:14 +000047 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 void visitSymbol(TIntermSymbol*);
49 void visitConstantUnion(TIntermConstantUnion*);
50 bool visitBinary(Visit visit, TIntermBinary*);
51 bool visitUnary(Visit visit, TIntermUnary*);
52 bool visitSelection(Visit visit, TIntermSelection*);
53 bool visitAggregate(Visit visit, TIntermAggregate*);
54 bool visitLoop(Visit visit, TIntermLoop*);
55 bool visitBranch(Visit visit, TIntermBranch*);
56
daniel@transgaming.com44fffee2012-04-28 00:34:20 +000057 void traverseStatements(TIntermNode *node);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000058 bool isSingleStatement(TIntermNode *node);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +000059 bool handleExcessiveLoop(TIntermLoop *node);
daniel@transgaming.com67de6d62010-04-29 03:35:30 +000060 void outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000061 void outputLineDirective(int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000062 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000063 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064
daniel@transgaming.com67de6d62010-04-29 03:35:30 +000065 void addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +000066 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000067
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000068 TString scopeString(unsigned int depthLimit);
69 TString scopedStruct(const TString &typeName);
70 TString structLookup(const TString &typeName);
71
daniel@transgaming.com950f9932010-04-13 03:26:14 +000072 TParseContext &mContext;
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000073 UnfoldShortCircuit *mUnfoldShortCircuit;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000074 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +000075
76 // Output streams
77 TInfoSinkBase mHeader;
78 TInfoSinkBase mBody;
79 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000080
daniel@transgaming.com8803b852012-12-20 21:11:47 +000081 typedef std::map<TString, TIntermSymbol*> ReferencedSymbols;
82 ReferencedSymbols mReferencedUniforms;
83 ReferencedSymbols mReferencedAttributes;
84 ReferencedSymbols mReferencedVaryings;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +000085
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000086 // Parameters determining what goes in the header output
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000087 bool mUsesTexture2D;
88 bool mUsesTexture2D_bias;
daniel@transgaming.com15795192011-05-11 15:36:20 +000089 bool mUsesTexture2DLod;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000090 bool mUsesTexture2DProj;
91 bool mUsesTexture2DProj_bias;
daniel@transgaming.com15795192011-05-11 15:36:20 +000092 bool mUsesTexture2DProjLod;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000093 bool mUsesTextureCube;
94 bool mUsesTextureCube_bias;
daniel@transgaming.com15795192011-05-11 15:36:20 +000095 bool mUsesTextureCubeLod;
daniel@transgaming.coma54f5182012-05-31 01:20:16 +000096 bool mUsesTexture2DLod0;
97 bool mUsesTexture2DLod0_bias;
98 bool mUsesTexture2DProjLod0;
99 bool mUsesTexture2DProjLod0_bias;
100 bool mUsesTextureCubeLod0;
101 bool mUsesTextureCubeLod0_bias;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000102 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000103 bool mUsesFragCoord;
104 bool mUsesPointCoord;
105 bool mUsesFrontFacing;
106 bool mUsesPointSize;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000107 bool mUsesXor;
108 bool mUsesMod1;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000109 bool mUsesMod2v;
110 bool mUsesMod2f;
111 bool mUsesMod3v;
112 bool mUsesMod3f;
113 bool mUsesMod4v;
114 bool mUsesMod4f;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000115 bool mUsesFaceforward1;
116 bool mUsesFaceforward2;
117 bool mUsesFaceforward3;
118 bool mUsesFaceforward4;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000119 bool mUsesEqualMat2;
120 bool mUsesEqualMat3;
121 bool mUsesEqualMat4;
122 bool mUsesEqualVec2;
123 bool mUsesEqualVec3;
124 bool mUsesEqualVec4;
125 bool mUsesEqualIVec2;
126 bool mUsesEqualIVec3;
127 bool mUsesEqualIVec4;
128 bool mUsesEqualBVec2;
129 bool mUsesEqualBVec3;
130 bool mUsesEqualBVec4;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000131 bool mUsesAtan2_1;
132 bool mUsesAtan2_2;
133 bool mUsesAtan2_3;
134 bool mUsesAtan2_4;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000135
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000136 typedef std::set<TString> Constructors;
137 Constructors mConstructors;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000138
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000139 typedef std::set<TString> StructNames;
140 StructNames mStructNames;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000141
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000142 typedef std::list<TString> StructDeclarations;
143 StructDeclarations mStructDeclarations;
daniel@transgaming.com63691862010-04-29 03:32:42 +0000144
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000145 typedef std::vector<int> ScopeBracket;
146 ScopeBracket mScopeBracket;
147 unsigned int mScopeDepth;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +0000148
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000149 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000150
151 bool mContainsLoopDiscontinuity;
152 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000153 bool mInsideDiscontinuousLoop;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000154
155 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000156
157 int mUniformRegister;
158 int mSamplerRegister;
159
160 TString registerString(TIntermSymbol *operand);
161 int samplerRegister(TIntermSymbol *sampler);
162 int uniformRegister(TIntermSymbol *uniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000163 void declareUniform(const TType &type, const TString &name, int index);
164 static GLenum glVariableType(const TType &type);
165
166 ActiveUniforms mActiveUniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167};
168}
169
170#endif // COMPILER_OUTPUTHLSL_H_