1 // 2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. 3 // 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 10 #include <list> 11 #include <set> 12 #include <map> 13 14 #include "angle_gl.h" 15 16 #include "compiler/translator/IntermNode.h" 17 #include "compiler/translator/ParseContext.h" 18 19 namespace sh 20 { 21 class UnfoldShortCircuit; 22 class StructureHLSL; 23 class UniformHLSL; 24 25 typedef std::map<TString, TIntermSymbol*> ReferencedSymbols; 26 27 class OutputHLSL : public TIntermTraverser 28 { 29 public: 30 OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator); 31 ~OutputHLSL(); 32 33 void output(); 34 35 TInfoSinkBase &getBodyStream(); 36 37 const std::map<std::string, unsigned int> &getInterfaceBlockRegisterMap() const; 38 const std::map<std::string, unsigned int> &getUniformRegisterMap() const; 39 40 static TString initializer(const TType &type); 41 42 protected: 43 void header(); 44 45 // Visit AST nodes and output their code to the body stream 46 void visitSymbol(TIntermSymbol*); 47 void visitRaw(TIntermRaw*); 48 void visitConstantUnion(TIntermConstantUnion*); 49 bool visitBinary(Visit visit, TIntermBinary*); 50 bool visitUnary(Visit visit, TIntermUnary*); 51 bool visitSelection(Visit visit, TIntermSelection*); 52 bool visitAggregate(Visit visit, TIntermAggregate*); 53 bool visitLoop(Visit visit, TIntermLoop*); 54 bool visitBranch(Visit visit, TIntermBranch*); 55 56 void traverseStatements(TIntermNode *node); 57 bool isSingleStatement(TIntermNode *node); 58 bool handleExcessiveLoop(TIntermLoop *node); 59 void outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString); 60 void outputLineDirective(int line); 61 TString argumentString(const TIntermSymbol *symbol); 62 int vectorSize(const TType &type) const; 63 64 void outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters); 65 const ConstantUnion *writeConstantUnion(const TType &type, const ConstantUnion *constUnion); 66 67 TParseContext &mContext; 68 const ShShaderOutput mOutputType; 69 UnfoldShortCircuit *mUnfoldShortCircuit; 70 bool mInsideFunction; 71 72 // Output streams 73 TInfoSinkBase mHeader; 74 TInfoSinkBase mBody; 75 TInfoSinkBase mFooter; 76 77 ReferencedSymbols mReferencedUniforms; 78 ReferencedSymbols mReferencedInterfaceBlocks; 79 ReferencedSymbols mReferencedAttributes; 80 ReferencedSymbols mReferencedVaryings; 81 ReferencedSymbols mReferencedOutputVariables; 82 83 StructureHLSL *mStructureHLSL; 84 UniformHLSL *mUniformHLSL; 85 86 struct TextureFunction 87 { 88 enum Method 89 { 90 IMPLICIT, // Mipmap LOD determined implicitly (standard lookup) 91 BIAS, 92 LOD, 93 LOD0, 94 LOD0BIAS, 95 SIZE, // textureSize() 96 FETCH, 97 GRAD 98 }; 99 100 TBasicType sampler; 101 int coords; 102 bool proj; 103 bool offset; 104 Method method; 105 106 TString name() const; 107 108 bool operator<(const TextureFunction &rhs) const; 109 }; 110 111 typedef std::set<TextureFunction> TextureFunctionSet; 112 113 // Parameters determining what goes in the header output 114 TextureFunctionSet mUsesTexture; 115 bool mUsesFragColor; 116 bool mUsesFragData; 117 bool mUsesDepthRange; 118 bool mUsesFragCoord; 119 bool mUsesPointCoord; 120 bool mUsesFrontFacing; 121 bool mUsesPointSize; 122 bool mUsesFragDepth; 123 bool mUsesXor; 124 bool mUsesMod1; 125 bool mUsesMod2v; 126 bool mUsesMod2f; 127 bool mUsesMod3v; 128 bool mUsesMod3f; 129 bool mUsesMod4v; 130 bool mUsesMod4f; 131 bool mUsesFaceforward1; 132 bool mUsesFaceforward2; 133 bool mUsesFaceforward3; 134 bool mUsesFaceforward4; 135 bool mUsesAtan2_1; 136 bool mUsesAtan2_2; 137 bool mUsesAtan2_3; 138 bool mUsesAtan2_4; 139 bool mUsesDiscardRewriting; 140 bool mUsesNestedBreak; 141 142 int mNumRenderTargets; 143 144 int mUniqueIndex; // For creating unique names 145 146 bool mContainsLoopDiscontinuity; 147 bool mOutputLod0Function; 148 bool mInsideDiscontinuousLoop; 149 int mNestedLoopDepth; 150 151 TIntermSymbol *mExcessiveLoopIndex; 152 153 TString structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName); 154 155 std::map<TIntermTyped*, TString> mFlaggedStructMappedNames; 156 std::map<TIntermTyped*, TString> mFlaggedStructOriginalNames; 157 158 void makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs); 159 }; 160 161 } 162 163 #endif // COMPILER_OUTPUTHLSL_H_ 164