blob: 3eccbf09091af52a748b6a916a94f9a262b4fccc [file] [log] [blame]
Ethan Nicholascc305772017-10-13 16:17:45 -04001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_METALCODEGENERATOR
9#define SKSL_METALCODEGENERATOR
10
11#include <stack>
12#include <tuple>
13#include <unordered_map>
14
15#include "SkSLCodeGenerator.h"
16#include "SkSLStringStream.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/SkSLSetting.h"
37#include "ir/SkSLStatement.h"
38#include "ir/SkSLSwitchStatement.h"
39#include "ir/SkSLSwizzle.h"
40#include "ir/SkSLTernaryExpression.h"
41#include "ir/SkSLVarDeclarations.h"
42#include "ir/SkSLVarDeclarationsStatement.h"
43#include "ir/SkSLVariableReference.h"
44#include "ir/SkSLWhileStatement.h"
45
46namespace SkSL {
47
48#define kLast_Capability SpvCapabilityMultiViewport
49
50/**
51 * Converts a Program into Metal code.
52 */
53class MetalCodeGenerator : public CodeGenerator {
54public:
55 enum Precedence {
56 kParentheses_Precedence = 1,
57 kPostfix_Precedence = 2,
58 kPrefix_Precedence = 3,
59 kMultiplicative_Precedence = 4,
60 kAdditive_Precedence = 5,
61 kShift_Precedence = 6,
62 kRelational_Precedence = 7,
63 kEquality_Precedence = 8,
64 kBitwiseAnd_Precedence = 9,
65 kBitwiseXor_Precedence = 10,
66 kBitwiseOr_Precedence = 11,
67 kLogicalAnd_Precedence = 12,
68 kLogicalXor_Precedence = 13,
69 kLogicalOr_Precedence = 14,
70 kTernary_Precedence = 15,
71 kAssignment_Precedence = 16,
72 kSequence_Precedence = 17,
73 kTopLevel_Precedence = kSequence_Precedence
74 };
75
76 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
77 OutputStream* out)
78 : INHERITED(program, errors, out)
79 , fLineEnding("\n")
80 , fContext(*context) {}
81
82 bool generateCode() override;
83
84protected:
85 typedef int Requirements;
86 static constexpr Requirements kNo_Requirements = 0;
87 static constexpr Requirements kInputs_Requirement = 1 << 0;
88 static constexpr Requirements kOutputs_Requirement = 1 << 1;
89 static constexpr Requirements kUniforms_Requirement = 1 << 2;
90
91 void write(const char* s);
92
93 void writeLine();
94
95 void writeLine(const char* s);
96
97 void write(const String& s);
98
99 void writeLine(const String& s);
100
101 void writeHeader();
102
103 void writeUniformStruct();
104
105 void writeInputStruct();
106
107 void writeOutputStruct();
108
109 void writePrecisionModifier();
110
111 void writeType(const Type& type);
112
113 void writeExtension(const Extension& ext);
114
115 void writeInterfaceBlock(const InterfaceBlock& intf);
116
117 void writeFunctionStart(const FunctionDeclaration& f);
118
119 void writeFunctionDeclaration(const FunctionDeclaration& f);
120
121 void writeFunction(const FunctionDefinition& f);
122
123 void writeLayout(const Layout& layout);
124
125 void writeModifiers(const Modifiers& modifiers, bool globalContext);
126
127 void writeGlobalVars(const VarDeclaration& vs);
128
129 void writeVarInitializer(const Variable& var, const Expression& value);
130
131 void writeVarDeclarations(const VarDeclarations& decl, bool global);
132
133 void writeFragCoord();
134
135 void writeVariableReference(const VariableReference& ref);
136
137 void writeExpression(const Expression& expr, Precedence parentPrecedence);
138
139 void writeIntrinsicCall(const FunctionCall& c);
140
141 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
142
143 void writeFunctionCall(const FunctionCall& c);
144
145 void writeConstructor(const Constructor& c);
146
147 void writeFieldAccess(const FieldAccess& f);
148
149 void writeSwizzle(const Swizzle& swizzle);
150
151 static Precedence GetBinaryPrecedence(Token::Kind op);
152
153 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
154
155 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
156
157 void writeIndexExpression(const IndexExpression& expr);
158
159 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
160
161 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
162
163 void writeBoolLiteral(const BoolLiteral& b);
164
165 void writeIntLiteral(const IntLiteral& i);
166
167 void writeFloatLiteral(const FloatLiteral& f);
168
169 void writeSetting(const Setting& s);
170
171 void writeStatement(const Statement& s);
172
173 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
174
175 void writeBlock(const Block& b);
176
177 void writeIfStatement(const IfStatement& stmt);
178
179 void writeForStatement(const ForStatement& f);
180
181 void writeWhileStatement(const WhileStatement& w);
182
183 void writeDoStatement(const DoStatement& d);
184
185 void writeSwitchStatement(const SwitchStatement& s);
186
187 void writeReturnStatement(const ReturnStatement& r);
188
189 void writeProgramElement(const ProgramElement& e);
190
191 Requirements requirements(const FunctionDeclaration& f);
192
193 Requirements requirements(const Expression& e);
194
195 Requirements requirements(const Statement& e);
196
197 const char* fLineEnding;
198 const Context& fContext;
199 StringStream fHeader;
200 String fFunctionHeader;
201 Program::Kind fProgramKind;
202 int fVarCount = 0;
203 int fIndentation = 0;
204 bool fAtLineStart = false;
205 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
206 // more than one or two structs per shader, a simple linear search will be faster than anything
207 // fancier.
208 std::vector<const Type*> fWrittenStructs;
209 // true if we have run into usages of dFdx / dFdy
210 bool fFoundDerivatives = false;
211 bool fFoundImageDecl = false;
212 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
213 bool fSetupFragPositionGlobal = false;
214 bool fSetupFragPositionLocal = false;
215 int fUniformBuffer = -1;
216
217 typedef CodeGenerator INHERITED;
218};
219
220}
221
222#endif