1//
2// Copyright (c) 2002-2013 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
16namespace sh
17{
18
19class 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
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:
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 virtual void writeLayoutQualifier(TIntermTyped *variable);
44 void writeInvariantQualifier(const TType &type);
45 virtual void writeVariableType(const TType &type, const TSymbol *symbol);
46 virtual bool writeVariablePrecision(TPrecision precision) = 0;
47 void writeFunctionParameters(const TFunction *func);
48 const TConstantUnion *writeConstantUnion(const TType &type, const TConstantUnion *pConstUnion);
49 void writeConstructorTriplet(Visit visit, const TType &type);
50 ImmutableString getTypeName(const TType &type);
51
52 void visitSymbol(TIntermSymbol *node) override;
53 void visitConstantUnion(TIntermConstantUnion *node) override;
54 bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
55 bool visitBinary(Visit visit, TIntermBinary *node) override;
56 bool visitUnary(Visit visit, TIntermUnary *node) override;
57 bool visitTernary(Visit visit, TIntermTernary *node) override;
58 bool visitIfElse(Visit visit, TIntermIfElse *node) override;
59 bool visitSwitch(Visit visit, TIntermSwitch *node) override;
60 bool visitCase(Visit visit, TIntermCase *node) override;
61 void visitFunctionPrototype(TIntermFunctionPrototype *node) override;
62 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
63 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
64 bool visitBlock(Visit visit, TIntermBlock *node) override;
65 bool visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node) override;
66 bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
67 bool visitLoop(Visit visit, TIntermLoop *node) override;
68 bool visitBranch(Visit visit, TIntermBranch *node) override;
69 void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override;
70
71 void visitCodeBlock(TIntermBlock *node);
72
73 ImmutableString hashFieldName(const TField *field);
74 // Same as hashName(), but without hashing "main".
75 ImmutableString hashFunctionNameIfNeeded(const TFunction *func);
76 // Used to translate function names for differences between ESSL and GLSL
77 virtual ImmutableString translateTextureFunction(const ImmutableString &name) { return name; }
78
79 void declareStruct(const TStructure *structure);
80 virtual void writeQualifier(TQualifier qualifier, const TSymbol *symbol);
81 bool structDeclared(const TStructure *structure) const;
82
83 private:
84 void declareInterfaceBlockLayout(const TInterfaceBlock *interfaceBlock);
85 void declareInterfaceBlock(const TInterfaceBlock *interfaceBlock);
86
87 void writeBuiltInFunctionTriplet(Visit visit, TOperator op, bool useEmulatedFunction);
88
89 const char *mapQualifierToString(TQualifier qualifier);
90
91 TInfoSinkBase &mObjSink;
92 bool mDeclaringVariable;
93
94 // This set contains all the ids of the structs from every scope.
95 std::set<int> mDeclaredStructs;
96
97 ShArrayIndexClampingStrategy mClampingStrategy;
98
99 // name hashing.
100 ShHashFunction64 mHashFunction;
101
102 NameMap &mNameMap;
103
104 sh::GLenum mShaderType;
105
106 const int mShaderVersion;
107
108 ShShaderOutput mOutput;
109
110 ShCompileOptions mCompileOptions;
111};
112
113void WriteGeometryShaderLayoutQualifiers(TInfoSinkBase &out,
114 sh::TLayoutPrimitiveType inputPrimitive,
115 int invocations,
116 sh::TLayoutPrimitiveType outputPrimitive,
117 int maxVertices);
118
119bool NeedsToWriteLayoutQualifier(const TType &type);
120
121} // namespace sh
122
123#endif // COMPILER_TRANSLATOR_OUTPUTGLSLBASE_H_
124