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