1 // 2 // Copyright 2002 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_TRANSLATOR_OUTPUTGLSLBASE_H_ 8 #define COMPILER_TRANSLATOR_OUTPUTGLSLBASE_H_ 9 10 #include <set> 11 12 #include "compiler/translator/HashNames.h" 13 #include "compiler/translator/InfoSink.h" 14 #include "compiler/translator/tree_util/IntermTraverse.h" 15 16 namespace sh 17 { 18 19 class TOutputGLSLBase : public TIntermTraverser 20 { 21 public: 22 TOutputGLSLBase(TInfoSinkBase &objSink, 23 ShArrayIndexClampingStrategy clampingStrategy, 24 ShHashFunction64 hashFunction, 25 NameMap &nameMap, 26 TSymbolTable *symbolTable, 27 sh::GLenum shaderType, 28 int shaderVersion, 29 ShShaderOutput output, 30 ShCompileOptions compileOptions); 31 getShaderOutput()32 ShShaderOutput getShaderOutput() const { return mOutput; } 33 34 // Return the original name if hash function pointer is NULL; 35 // otherwise return the hashed name. Has special handling for internal names and built-ins, 36 // which are not hashed. 37 ImmutableString hashName(const TSymbol *symbol); 38 39 protected: objSink()40 TInfoSinkBase &objSink() { return mObjSink; } 41 void writeFloat(TInfoSinkBase &out, float f); 42 void writeTriplet(Visit visit, const char *preStr, const char *inStr, const char *postStr); 43 std::string getCommonLayoutQualifiers(TIntermTyped *variable); 44 std::string getMemoryQualifiers(const TType &type); 45 virtual void writeLayoutQualifier(TIntermTyped *variable); 46 void writeFieldLayoutQualifier(const TField *field); 47 void writeInvariantQualifier(const TType &type); 48 void writePreciseQualifier(const TType &type); 49 virtual void writeVariableType(const TType &type, 50 const TSymbol *symbol, 51 bool isFunctionArgument); 52 virtual bool writeVariablePrecision(TPrecision precision) = 0; 53 void writeFunctionParameters(const TFunction *func); 54 const TConstantUnion *writeConstantUnion(const TType &type, const TConstantUnion *pConstUnion); 55 void writeConstructorTriplet(Visit visit, const TType &type); 56 ImmutableString getTypeName(const TType &type); 57 58 void visitSymbol(TIntermSymbol *node) override; 59 void visitConstantUnion(TIntermConstantUnion *node) override; 60 bool visitSwizzle(Visit visit, TIntermSwizzle *node) override; 61 bool visitBinary(Visit visit, TIntermBinary *node) override; 62 bool visitUnary(Visit visit, TIntermUnary *node) override; 63 bool visitTernary(Visit visit, TIntermTernary *node) override; 64 bool visitIfElse(Visit visit, TIntermIfElse *node) override; 65 bool visitSwitch(Visit visit, TIntermSwitch *node) override; 66 bool visitCase(Visit visit, TIntermCase *node) override; 67 void visitFunctionPrototype(TIntermFunctionPrototype *node) override; 68 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override; 69 bool visitAggregate(Visit visit, TIntermAggregate *node) override; 70 bool visitBlock(Visit visit, TIntermBlock *node) override; 71 bool visitGlobalQualifierDeclaration(Visit visit, 72 TIntermGlobalQualifierDeclaration *node) override; 73 bool visitDeclaration(Visit visit, TIntermDeclaration *node) override; 74 bool visitLoop(Visit visit, TIntermLoop *node) override; 75 bool visitBranch(Visit visit, TIntermBranch *node) override; 76 void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override; 77 78 void visitCodeBlock(TIntermBlock *node); 79 80 ImmutableString hashFieldName(const TField *field); 81 // Same as hashName(), but without hashing "main". 82 ImmutableString hashFunctionNameIfNeeded(const TFunction *func); 83 // Used to translate function names for differences between ESSL and GLSL translateTextureFunction(const ImmutableString & name,const ShCompileOptions & option)84 virtual ImmutableString translateTextureFunction(const ImmutableString &name, 85 const ShCompileOptions &option) 86 { 87 return name; 88 } 89 90 void declareStruct(const TStructure *structure); 91 void writeQualifier(TQualifier qualifier, const TType &type, const TSymbol *symbol); 92 bool structDeclared(const TStructure *structure) const; 93 94 const char *mapQualifierToString(TQualifier qualifier); 95 getShaderType()96 sh::GLenum getShaderType() { return mShaderType; } 97 98 private: 99 void declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock); 100 void declareInterfaceBlock(const TInterfaceBlock *interfaceBlock); 101 102 void writeBuiltInFunctionTriplet(Visit visit, TOperator op, bool useEmulatedFunction); 103 104 TInfoSinkBase &mObjSink; 105 bool mDeclaringVariable; 106 107 // This set contains all the ids of the structs from every scope. 108 std::set<int> mDeclaredStructs; 109 110 ShArrayIndexClampingStrategy mClampingStrategy; 111 112 // name hashing. 113 ShHashFunction64 mHashFunction; 114 115 NameMap &mNameMap; 116 117 sh::GLenum mShaderType; 118 119 const int mShaderVersion; 120 121 ShShaderOutput mOutput; 122 123 ShCompileOptions mCompileOptions; 124 }; 125 126 void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out, 127 sh::TLayoutPrimitiveType inputPrimitive, 128 int invocations, 129 sh::TLayoutPrimitiveType outputPrimitive, 130 int maxVertices); 131 132 bool NeedsToWriteLayoutQualifier(const TType &type); 133 134 } // namespace sh 135 136 #endif // COMPILER_TRANSLATOR_OUTPUTGLSLBASE_H_ 137