blob: 2c946361496f1ff2294120c3999337b860f33c54 [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
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
shannon.woods%transgaming.com@gtempaccount.comf26ddae2013-04-13 03:29:13 +000014#include <GLES3/gl3.h>
daniel@transgaming.com652468c2012-12-20 21:11:57 +000015#include <GLES2/gl2.h>
alokp@chromium.org4e89d232010-05-14 19:37:21 +000016
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/intermediate.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040018#include "compiler/translator/ParseContext.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040019#include "common/shadervars.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
21namespace 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:
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000032 OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000033 ~OutputHLSL();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034
daniel@transgaming.com950f9932010-04-13 03:26:14 +000035 void output();
36
daniel@transgaming.comb5875982010-04-15 20:44:53 +000037 TInfoSinkBase &getBodyStream();
Jamie Madillf2575982014-06-25 16:04:54 -040038 const std::vector<sh::Uniform> &getUniforms();
39 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const;
40 const std::vector<sh::Attribute> &getOutputVariables() const;
41 const std::vector<sh::Attribute> &getAttributes() const;
42 const std::vector<sh::Varying> &getVaryings() const;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000043
daniel@transgaming.comb5875982010-04-15 20:44:53 +000044 static TString initializer(const TType &type);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000045
daniel@transgaming.com950f9932010-04-13 03:26:14 +000046 protected:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047 void header();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048
daniel@transgaming.com950f9932010-04-13 03:26:14 +000049 // Visit AST nodes and output their code to the body stream
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 void visitSymbol(TIntermSymbol*);
51 void visitConstantUnion(TIntermConstantUnion*);
52 bool visitBinary(Visit visit, TIntermBinary*);
53 bool visitUnary(Visit visit, TIntermUnary*);
54 bool visitSelection(Visit visit, TIntermSelection*);
55 bool visitAggregate(Visit visit, TIntermAggregate*);
56 bool visitLoop(Visit visit, TIntermLoop*);
57 bool visitBranch(Visit visit, TIntermBranch*);
58
daniel@transgaming.com44fffee2012-04-28 00:34:20 +000059 void traverseStatements(TIntermNode *node);
daniel@transgaming.comb5875982010-04-15 20:44:53 +000060 bool isSingleStatement(TIntermNode *node);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +000061 bool handleExcessiveLoop(TIntermLoop *node);
daniel@transgaming.com67de6d62010-04-29 03:35:30 +000062 void outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +000063 void outputLineDirective(int line);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000064 TString argumentString(const TIntermSymbol *symbol);
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000065 int vectorSize(const TType &type) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066
Nicolas Capens1af18dc2014-06-11 11:07:32 -040067 void outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +000068 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion);
daniel@transgaming.com63691862010-04-29 03:32:42 +000069
daniel@transgaming.com950f9932010-04-13 03:26:14 +000070 TParseContext &mContext;
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000071 const ShShaderOutput mOutputType;
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000072 UnfoldShortCircuit *mUnfoldShortCircuit;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000073 bool mInsideFunction;
daniel@transgaming.com950f9932010-04-13 03:26:14 +000074
75 // Output streams
76 TInfoSinkBase mHeader;
77 TInfoSinkBase mBody;
78 TInfoSinkBase mFooter;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000079
daniel@transgaming.com8803b852012-12-20 21:11:47 +000080 ReferencedSymbols mReferencedUniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000081 ReferencedSymbols mReferencedInterfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +000082 ReferencedSymbols mReferencedAttributes;
83 ReferencedSymbols mReferencedVaryings;
Jamie Madill46131a32013-06-20 11:55:50 -040084 ReferencedSymbols mReferencedOutputVariables;
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +000085
Jamie Madill8daaba12014-06-13 10:04:33 -040086 StructureHLSL *mStructureHLSL;
Jamie Madillf91ce812014-06-13 10:04:34 -040087 UniformHLSL *mUniformHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -040088
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089 struct TextureFunction
90 {
Nicolas Capens75fb4752013-07-10 15:14:47 -040091 enum Method
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 {
Nicolas Capens75fb4752013-07-10 15:14:47 -040093 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040094 BIAS,
95 LOD,
Nicolas Capens75fb4752013-07-10 15:14:47 -040096 LOD0,
Nicolas Capens84cfa122014-04-14 13:48:45 -040097 LOD0BIAS,
Nicolas Capensfc014542014-02-18 14:47:13 -050098 SIZE, // textureSize()
Nicolas Capensd11d5492014-02-19 17:06:10 -050099 FETCH,
100 GRAD
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400101 };
102
103 TBasicType sampler;
104 int coords;
105 bool proj;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500106 bool offset;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400107 Method method;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400108
109 TString name() const;
110
111 bool operator<(const TextureFunction &rhs) const;
112 };
113
114 typedef std::set<TextureFunction> TextureFunctionSet;
115
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000116 // Parameters determining what goes in the header output
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400117 TextureFunctionSet mUsesTexture;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000118 bool mUsesFragColor;
119 bool mUsesFragData;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000120 bool mUsesDepthRange;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000121 bool mUsesFragCoord;
122 bool mUsesPointCoord;
123 bool mUsesFrontFacing;
124 bool mUsesPointSize;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400125 bool mUsesFragDepth;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000126 bool mUsesXor;
127 bool mUsesMod1;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000128 bool mUsesMod2v;
129 bool mUsesMod2f;
130 bool mUsesMod3v;
131 bool mUsesMod3f;
132 bool mUsesMod4v;
133 bool mUsesMod4f;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000134 bool mUsesFaceforward1;
135 bool mUsesFaceforward2;
136 bool mUsesFaceforward3;
137 bool mUsesFaceforward4;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000138 bool mUsesAtan2_1;
139 bool mUsesAtan2_2;
140 bool mUsesAtan2_3;
141 bool mUsesAtan2_4;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500142 bool mUsesDiscardRewriting;
Nicolas Capens655fe362014-04-11 13:12:34 -0400143 bool mUsesNestedBreak;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000144
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000145 int mNumRenderTargets;
146
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000147 int mUniqueIndex; // For creating unique names
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000148
149 bool mContainsLoopDiscontinuity;
150 bool mOutputLod0Function;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000151 bool mInsideDiscontinuousLoop;
Nicolas Capens655fe362014-04-11 13:12:34 -0400152 int mNestedLoopDepth;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000153
154 TIntermSymbol *mExcessiveLoopIndex;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000155
Jamie Madillf2575982014-06-25 16:04:54 -0400156 void declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<sh::Varying>& fieldsOut);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000157
Jamie Madill98493dd2013-07-08 14:39:03 -0400158 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +0000159
Jamie Madillf2575982014-06-25 16:04:54 -0400160 std::vector<sh::Attribute> mActiveOutputVariables;
161 std::vector<sh::Attribute> mActiveAttributes;
162 std::vector<sh::Varying> mActiveVaryings;
Jamie Madill570e04d2013-06-21 09:15:33 -0400163 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames;
164 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames;
165
166 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167};
Jamie Madill8daaba12014-06-13 10:04:33 -0400168
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169}
170
171#endif // COMPILER_OUTPUTHLSL_H_