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
13 #include <unordered_map>
16 #include "SkSLCodeGenerator.h"
17 #include "ir/SkSLBinaryExpression.h"
18 #include "ir/SkSLBoolLiteral.h"
19 #include "ir/SkSLConstructor.h"
20 #include "ir/SkSLDoStatement.h"
21 #include "ir/SkSLExtension.h"
22 #include "ir/SkSLFloatLiteral.h"
23 #include "ir/SkSLIfStatement.h"
24 #include "ir/SkSLIndexExpression.h"
25 #include "ir/SkSLInterfaceBlock.h"
26 #include "ir/SkSLIntLiteral.h"
27 #include "ir/SkSLFieldAccess.h"
28 #include "ir/SkSLForStatement.h"
29 #include "ir/SkSLFunctionCall.h"
30 #include "ir/SkSLFunctionDeclaration.h"
31 #include "ir/SkSLFunctionDefinition.h"
32 #include "ir/SkSLPrefixExpression.h"
33 #include "ir/SkSLPostfixExpression.h"
34 #include "ir/SkSLProgramElement.h"
35 #include "ir/SkSLReturnStatement.h"
36 #include "ir/SkSLStatement.h"
37 #include "ir/SkSLSwitchStatement.h"
38 #include "ir/SkSLSwizzle.h"
39 #include "ir/SkSLTernaryExpression.h"
40 #include "ir/SkSLVarDeclarations.h"
41 #include "ir/SkSLVarDeclarationsStatement.h"
42 #include "ir/SkSLVariableReference.h"
43 #include "ir/SkSLWhileStatement.h"
47 #define kLast_Capability SpvCapabilityMultiViewport
50 * Converts a Program into GLSL code.
52 class GLSLCodeGenerator : public CodeGenerator {
55 kParentheses_Precedence = 1,
56 kPostfix_Precedence = 2,
57 kPrefix_Precedence = 3,
58 kMultiplicative_Precedence = 4,
59 kAdditive_Precedence = 5,
60 kShift_Precedence = 6,
61 kRelational_Precedence = 7,
62 kEquality_Precedence = 8,
63 kBitwiseAnd_Precedence = 9,
64 kBitwiseXor_Precedence = 10,
65 kBitwiseOr_Precedence = 11,
66 kLogicalAnd_Precedence = 12,
67 kLogicalXor_Precedence = 13,
68 kLogicalOr_Precedence = 14,
69 kTernary_Precedence = 15,
70 kAssignment_Precedence = 16,
71 kSequence_Precedence = 17,
72 kTopLevel_Precedence = 18
75 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
77 : INHERITED(program, errors, out)
78 , fContext(*context) {}
80 virtual bool generateCode() override;
83 void write(const char* s);
87 void writeLine(const char* s);
89 void write(const SkString& s);
91 void writeLine(const SkString& s);
93 void writeType(const Type& type);
95 void writeExtension(const Extension& ext);
97 void writeInterfaceBlock(const InterfaceBlock& intf);
99 void writeFunctionStart(const FunctionDeclaration& f);
101 void writeFunctionDeclaration(const FunctionDeclaration& f);
103 void writeFunction(const FunctionDefinition& f);
105 void writeLayout(const Layout& layout);
107 void writeModifiers(const Modifiers& modifiers, bool globalContext);
109 void writeGlobalVars(const VarDeclaration& vs);
111 void writeVarDeclarations(const VarDeclarations& decl, bool global);
113 void writeFragCoord();
115 void writeVariableReference(const VariableReference& ref);
117 void writeExpression(const Expression& expr, Precedence parentPrecedence);
119 void writeIntrinsicCall(const FunctionCall& c);
121 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
123 void writeFunctionCall(const FunctionCall& c);
125 void writeConstructor(const Constructor& c);
127 void writeFieldAccess(const FieldAccess& f);
129 void writeSwizzle(const Swizzle& swizzle);
131 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
133 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
135 void writeIndexExpression(const IndexExpression& expr);
137 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
139 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
141 void writeBoolLiteral(const BoolLiteral& b);
143 void writeIntLiteral(const IntLiteral& i);
145 void writeFloatLiteral(const FloatLiteral& f);
147 void writeStatement(const Statement& s);
149 void writeBlock(const Block& b);
151 void writeIfStatement(const IfStatement& stmt);
153 void writeForStatement(const ForStatement& f);
155 void writeWhileStatement(const WhileStatement& w);
157 void writeDoStatement(const DoStatement& d);
159 void writeSwitchStatement(const SwitchStatement& s);
161 void writeReturnStatement(const ReturnStatement& r);
163 const Context& fContext;
164 SkDynamicMemoryWStream fHeader;
165 SkString fFunctionHeader;
166 Program::Kind fProgramKind;
168 int fIndentation = 0;
169 bool fAtLineStart = false;
170 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
171 // more than one or two structs per shader, a simple linear search will be faster than anything
173 std::vector<const Type*> fWrittenStructs;
174 // true if we have run into usages of dFdx / dFdy
175 bool fFoundDerivatives = false;
176 bool fFoundImageDecl = false;
177 bool fSetupFragPositionGlobal = false;
178 bool fSetupFragPositionLocal = false;
180 typedef CodeGenerator INHERITED;