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