2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SKSL_GLSLCODEGENERATOR
9 #define SKSL_GLSLCODEGENERATOR
11 #include "include/sksl/SkSLOperator.h"
12 #include "src/sksl/SkSLContext.h"
13 #include "src/sksl/SkSLStringStream.h"
14 #include "src/sksl/codegen/SkSLCodeGenerator.h"
18 #include <string_view>
23 class BinaryExpression;
25 class ConstructorDiagonalMatrix;
28 class ExpressionStatement;
32 class FunctionDeclaration;
33 class FunctionDefinition;
34 class FunctionPrototype;
39 class PostfixExpression;
40 class PrefixExpression;
42 class ReturnStatement;
45 class StructDefinition;
46 class SwitchStatement;
47 class TernaryExpression;
51 class VariableReference;
52 struct IndexExpression;
60 * Converts a Program into GLSL code.
62 class GLSLCodeGenerator : public CodeGenerator {
64 GLSLCodeGenerator(const Context* context, const Program* program, OutputStream* out)
65 : INHERITED(context, program, out) {}
67 bool generateCode() override;
70 using Precedence = Operator::Precedence;
72 void write(std::string_view s);
74 void writeLine(std::string_view s = std::string_view());
78 virtual void writeHeader();
80 virtual bool usesPrecisionModifiers() const;
82 virtual std::string getTypeName(const Type& type);
84 void writeStructDefinition(const StructDefinition& s);
86 void writeType(const Type& type);
88 void writeExtension(std::string_view name, bool require = true);
90 void writeInterfaceBlock(const InterfaceBlock& intf);
92 void writeFunctionDeclaration(const FunctionDeclaration& f);
94 void writeFunctionPrototype(const FunctionPrototype& f);
96 virtual void writeFunction(const FunctionDefinition& f);
98 void writeLayout(const Layout& layout);
100 void writeModifiers(const Modifiers& modifiers, bool globalContext);
102 virtual void writeInputVars();
104 virtual void writeVarInitializer(const Variable& var, const Expression& value);
106 const char* getTypePrecision(const Type& type);
108 void writeTypePrecision(const Type& type);
110 void writeVarDeclaration(const VarDeclaration& var, bool global);
112 void writeFragCoord();
114 virtual void writeVariableReference(const VariableReference& ref);
116 void writeExpression(const Expression& expr, Precedence parentPrecedence);
118 void writeIntrinsicCall(const FunctionCall& c);
120 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
122 void writeDeterminantHack(const Expression& mat);
124 void writeInverseHack(const Expression& mat);
126 void writeTransposeHack(const Expression& mat);
128 void writeInverseSqrtHack(const Expression& x);
130 void writeMatrixComparisonWorkaround(const BinaryExpression& x);
132 virtual void writeFunctionCall(const FunctionCall& c);
134 void writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c,
135 Precedence parentPrecedence);
137 virtual void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence);
139 virtual void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence);
141 virtual void writeFieldAccess(const FieldAccess& f);
143 virtual void writeSwizzle(const Swizzle& swizzle);
145 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
147 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
148 Precedence parentPrecedence);
150 virtual void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
152 virtual void writeIndexExpression(const IndexExpression& expr);
154 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
156 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
158 virtual void writeLiteral(const Literal& l);
160 virtual void writeSetting(const Setting& s);
162 void writeStatement(const Statement& s);
164 void writeBlock(const Block& b);
166 virtual void writeIfStatement(const IfStatement& stmt);
168 void writeForStatement(const ForStatement& f);
170 void writeDoStatement(const DoStatement& d);
172 void writeExpressionStatement(const ExpressionStatement& s);
174 virtual void writeSwitchStatement(const SwitchStatement& s);
176 virtual void writeReturnStatement(const ReturnStatement& r);
178 virtual void writeProgramElement(const ProgramElement& e);
180 const ShaderCaps& caps() const { return fContext.fCaps; }
182 StringStream fExtensions;
183 StringStream fGlobals;
184 StringStream fExtraFunctions;
185 std::string fFunctionHeader;
187 int fIndentation = 0;
188 bool fAtLineStart = false;
189 std::set<std::string> fWrittenIntrinsics;
190 // true if we have run into usages of dFdx / dFdy
191 bool fFoundDerivatives = false;
192 bool fFoundExternalSamplerDecl = false;
193 bool fFoundRectSamplerDecl = false;
194 bool fSetupClockwise = false;
195 bool fSetupFragPosition = false;
196 bool fSetupFragCoordWorkaround = false;
198 using INHERITED = CodeGenerator;