xref: /aosp_15_r20/external/angle/src/compiler/translator/glsl/OutputGLSLBase.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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_GLSL_OUTPUTGLSLBASE_H_
8 #define COMPILER_TRANSLATOR_GLSL_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,
26                     TInfoSinkBase &objSink,
27                     const ShCompileOptions &compileOptions);
28 
getShaderOutput()29     ShShaderOutput getShaderOutput() const { return mOutput; }
30 
31     // Return the original name if hash function pointer is NULL;
32     // otherwise return the hashed name. Has special handling for internal names and built-ins,
33     // which are not hashed.
34     ImmutableString hashName(const TSymbol *symbol);
35 
36   protected:
objSink()37     TInfoSinkBase &objSink() { return mObjSink; }
38     void writeFloat(TInfoSinkBase &out, float f);
39     void writeTriplet(Visit visit, const char *preStr, const char *inStr, const char *postStr);
40     std::string getCommonLayoutQualifiers(TIntermSymbol *variable);
41     std::string getMemoryQualifiers(const TType &type);
42     virtual void writeLayoutQualifier(TIntermSymbol *variable);
43     void writeFieldLayoutQualifier(const TField *field);
44     void writeInvariantQualifier(const TType &type);
45     void writePreciseQualifier(const TType &type);
46     virtual void writeVariableType(const TType &type,
47                                    const TSymbol *symbol,
48                                    bool isFunctionArgument);
49     virtual bool writeVariablePrecision(TPrecision precision) = 0;
50     void writeFunctionParameters(const TFunction *func);
51     const TConstantUnion *writeConstantUnion(const TType &type, const TConstantUnion *pConstUnion);
52     void writeConstructorTriplet(Visit visit, const TType &type);
53     ImmutableString getTypeName(const TType &type);
54 
55     void visitSymbol(TIntermSymbol *node) override;
56     void visitConstantUnion(TIntermConstantUnion *node) override;
57     bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
58     bool visitBinary(Visit visit, TIntermBinary *node) override;
59     bool visitUnary(Visit visit, TIntermUnary *node) override;
60     bool visitTernary(Visit visit, TIntermTernary *node) override;
61     bool visitIfElse(Visit visit, TIntermIfElse *node) override;
62     bool visitSwitch(Visit visit, TIntermSwitch *node) override;
63     bool visitCase(Visit visit, TIntermCase *node) override;
64     void visitFunctionPrototype(TIntermFunctionPrototype *node) override;
65     bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
66     bool visitAggregate(Visit visit, TIntermAggregate *node) override;
67     bool visitBlock(Visit visit, TIntermBlock *node) override;
68     bool visitGlobalQualifierDeclaration(Visit visit,
69                                          TIntermGlobalQualifierDeclaration *node) override;
70     bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
71     bool visitLoop(Visit visit, TIntermLoop *node) override;
72     bool visitBranch(Visit visit, TIntermBranch *node) override;
73     void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override;
74 
75     void visitCodeBlock(TIntermBlock *node);
76 
77     ImmutableString hashFieldName(const TField *field);
78     // Same as hashName(), but without hashing "main".
79     ImmutableString hashFunctionNameIfNeeded(const TFunction *func);
80     // Used to translate function names for differences between ESSL and GLSL
translateTextureFunction(const ImmutableString & name,const ShCompileOptions & option)81     virtual ImmutableString translateTextureFunction(const ImmutableString &name,
82                                                      const ShCompileOptions &option)
83     {
84         return name;
85     }
86 
87     void declareStruct(const TStructure *structure);
88     void writeQualifier(TQualifier qualifier, const TType &type, const TSymbol *symbol);
89 
90     const char *mapQualifierToString(TQualifier qualifier);
91 
getShaderType()92     sh::GLenum getShaderType() const { return mShaderType; }
isHighPrecisionSupported()93     bool isHighPrecisionSupported() const { return mHighPrecisionSupported; }
94     const char *getIndentPrefix(int extraIndentDepth = 0);
95 
96     bool needsToWriteLayoutQualifier(const TType &type);
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     NameMap &mNameMap;
112 
113     sh::GLenum mShaderType;
114     const int mShaderVersion;
115     ShShaderOutput mOutput;
116 
117     bool mHighPrecisionSupported;
118 
119     // Emit "layout(locaton = 0)" for fragment outputs whose location is unspecified. This is for
120     // transformations like pixel local storage, where new outputs are introduced to the shader, and
121     // previously valid fragment outputs with an implicit location of 0 are now required to specify
122     // their location.
123     bool mAlwaysSpecifyFragOutLocation;
124 
125     const ShCompileOptions &mCompileOptions;
126 };
127 
128 void WritePragma(TInfoSinkBase &out, const ShCompileOptions &compileOptions, const TPragma &pragma);
129 
130 void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out,
131                                          sh::TLayoutPrimitiveType inputPrimitive,
132                                          int invocations,
133                                          sh::TLayoutPrimitiveType outputPrimitive,
134                                          int maxVertices);
135 
136 void WriteTessControlShaderLayoutQualifiers(TInfoSinkBase &out, int inputVertices);
137 
138 void WriteTessEvaluationShaderLayoutQualifiers(TInfoSinkBase &out,
139                                                sh::TLayoutTessEvaluationType inputPrimitive,
140                                                sh::TLayoutTessEvaluationType inputVertexSpacing,
141                                                sh::TLayoutTessEvaluationType inputOrdering,
142                                                sh::TLayoutTessEvaluationType inputPoint);
143 
144 void WriteFragmentShaderLayoutQualifiers(TInfoSinkBase &out,
145                                          const AdvancedBlendEquations &advancedBlendEquations);
146 
147 void EmitEarlyFragmentTestsGLSL(const TCompiler &, TInfoSinkBase &sink);
148 void EmitWorkGroupSizeGLSL(const TCompiler &, TInfoSinkBase &sink);
149 void EmitMultiviewGLSL(const TCompiler &,
150                        const ShCompileOptions &,
151                        const TExtension,
152                        const TBehavior,
153                        TInfoSinkBase &sink);
154 
155 }  // namespace sh
156 
157 #endif  // COMPILER_TRANSLATOR_GLSL_OUTPUTGLSLBASE_H_
158