blob: 3276f0d88ea697e22ef758308c2e49f6e87ca1c8 [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>
John Stiles1bdafbf2020-05-28 12:17:20 -040014#include <unordered_set>
Ethan Nicholascc305772017-10-13 16:17:45 -040015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/sksl/SkSLCodeGenerator.h"
17#include "src/sksl/SkSLMemoryLayout.h"
18#include "src/sksl/SkSLStringStream.h"
19#include "src/sksl/ir/SkSLBinaryExpression.h"
20#include "src/sksl/ir/SkSLBoolLiteral.h"
21#include "src/sksl/ir/SkSLConstructor.h"
22#include "src/sksl/ir/SkSLDoStatement.h"
23#include "src/sksl/ir/SkSLExtension.h"
24#include "src/sksl/ir/SkSLFieldAccess.h"
25#include "src/sksl/ir/SkSLFloatLiteral.h"
26#include "src/sksl/ir/SkSLForStatement.h"
27#include "src/sksl/ir/SkSLFunctionCall.h"
28#include "src/sksl/ir/SkSLFunctionDeclaration.h"
29#include "src/sksl/ir/SkSLFunctionDefinition.h"
30#include "src/sksl/ir/SkSLIfStatement.h"
31#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040032#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLIntLiteral.h"
34#include "src/sksl/ir/SkSLInterfaceBlock.h"
35#include "src/sksl/ir/SkSLPostfixExpression.h"
36#include "src/sksl/ir/SkSLPrefixExpression.h"
37#include "src/sksl/ir/SkSLProgramElement.h"
38#include "src/sksl/ir/SkSLReturnStatement.h"
39#include "src/sksl/ir/SkSLSetting.h"
40#include "src/sksl/ir/SkSLStatement.h"
41#include "src/sksl/ir/SkSLSwitchStatement.h"
42#include "src/sksl/ir/SkSLSwizzle.h"
43#include "src/sksl/ir/SkSLTernaryExpression.h"
44#include "src/sksl/ir/SkSLVarDeclarations.h"
45#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
46#include "src/sksl/ir/SkSLVariableReference.h"
47#include "src/sksl/ir/SkSLWhileStatement.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040048
49namespace SkSL {
50
51#define kLast_Capability SpvCapabilityMultiViewport
52
53/**
54 * Converts a Program into Metal code.
55 */
56class MetalCodeGenerator : public CodeGenerator {
57public:
Timothy Lianga06f2152018-05-24 15:33:31 -040058 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040059 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040060
Ethan Nicholascc305772017-10-13 16:17:45 -040061 enum Precedence {
62 kParentheses_Precedence = 1,
63 kPostfix_Precedence = 2,
64 kPrefix_Precedence = 3,
65 kMultiplicative_Precedence = 4,
66 kAdditive_Precedence = 5,
67 kShift_Precedence = 6,
68 kRelational_Precedence = 7,
69 kEquality_Precedence = 8,
70 kBitwiseAnd_Precedence = 9,
71 kBitwiseXor_Precedence = 10,
72 kBitwiseOr_Precedence = 11,
73 kLogicalAnd_Precedence = 12,
74 kLogicalXor_Precedence = 13,
75 kLogicalOr_Precedence = 14,
76 kTernary_Precedence = 15,
77 kAssignment_Precedence = 16,
78 kSequence_Precedence = 17,
79 kTopLevel_Precedence = kSequence_Precedence
80 };
81
82 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040083 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040084 : INHERITED(program, errors, out)
Timothy Liangdc89f192018-06-13 09:20:31 -040085 , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040086 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040087 , fContext(*context) {
88 this->setupIntrinsics();
89 }
Ethan Nicholascc305772017-10-13 16:17:45 -040090
91 bool generateCode() override;
92
93protected:
94 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040095 static constexpr Requirements kNo_Requirements = 0;
96 static constexpr Requirements kInputs_Requirement = 1 << 0;
97 static constexpr Requirements kOutputs_Requirement = 1 << 1;
98 static constexpr Requirements kUniforms_Requirement = 1 << 2;
99 static constexpr Requirements kGlobals_Requirement = 1 << 3;
100 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -0400101
Timothy Liang6403b0e2018-05-17 10:40:04 -0400102 enum IntrinsicKind {
Timothy Lianga06f2152018-05-24 15:33:31 -0400103 kSpecial_IntrinsicKind,
104 kMetal_IntrinsicKind,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400105 };
106
107 enum SpecialIntrinsic {
108 kTexture_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400109 kMod_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400110 };
111
Timothy Lianga06f2152018-05-24 15:33:31 -0400112 enum MetalIntrinsic {
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500113 kEqual_MetalIntrinsic,
114 kNotEqual_MetalIntrinsic,
Timothy Lianga06f2152018-05-24 15:33:31 -0400115 kLessThan_MetalIntrinsic,
116 kLessThanEqual_MetalIntrinsic,
117 kGreaterThan_MetalIntrinsic,
118 kGreaterThanEqual_MetalIntrinsic,
119 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400120
John Stilescdcdb042020-07-06 09:03:51 -0400121 class GlobalStructVisitor;
122 void visitGlobalStruct(GlobalStructVisitor* visitor);
123
Timothy Lianga06f2152018-05-24 15:33:31 -0400124 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400125
Ethan Nicholascc305772017-10-13 16:17:45 -0400126 void write(const char* s);
127
128 void writeLine();
129
130 void writeLine(const char* s);
131
132 void write(const String& s);
133
134 void writeLine(const String& s);
135
136 void writeHeader();
137
138 void writeUniformStruct();
139
140 void writeInputStruct();
141
142 void writeOutputStruct();
143
Timothy Liang7d637782018-06-05 09:58:07 -0400144 void writeInterfaceBlocks();
145
Timothy Liangdc89f192018-06-13 09:20:31 -0400146 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
147 const InterfaceBlock* parentIntf = nullptr);
148
Timothy Liang7d637782018-06-05 09:58:07 -0400149 int size(const Type* type, bool isPacked) const;
150
151 int alignment(const Type* type, bool isPacked) const;
152
Timothy Liangee84fe12018-05-18 14:38:19 -0400153 void writeGlobalStruct();
John Stilescdcdb042020-07-06 09:03:51 -0400154 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400155
Ethan Nicholascc305772017-10-13 16:17:45 -0400156 void writePrecisionModifier();
157
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500158 String typeName(const Type& type);
159
Ethan Nicholascc305772017-10-13 16:17:45 -0400160 void writeType(const Type& type);
161
162 void writeExtension(const Extension& ext);
163
164 void writeInterfaceBlock(const InterfaceBlock& intf);
165
166 void writeFunctionStart(const FunctionDeclaration& f);
167
168 void writeFunctionDeclaration(const FunctionDeclaration& f);
169
170 void writeFunction(const FunctionDefinition& f);
171
172 void writeLayout(const Layout& layout);
173
174 void writeModifiers(const Modifiers& modifiers, bool globalContext);
175
Ethan Nicholascc305772017-10-13 16:17:45 -0400176 void writeVarInitializer(const Variable& var, const Expression& value);
177
Timothy Liang651286f2018-06-07 09:55:33 -0400178 void writeName(const String& name);
179
Ethan Nicholascc305772017-10-13 16:17:45 -0400180 void writeVarDeclarations(const VarDeclarations& decl, bool global);
181
182 void writeFragCoord();
183
184 void writeVariableReference(const VariableReference& ref);
185
186 void writeExpression(const Expression& expr, Precedence parentPrecedence);
187
188 void writeIntrinsicCall(const FunctionCall& c);
189
190 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
191
192 void writeFunctionCall(const FunctionCall& c);
193
Chris Daltondba7aab2018-11-15 10:57:49 -0500194 void writeInverseHack(const Expression& mat);
195
John Stiles1bdafbf2020-05-28 12:17:20 -0400196 bool matrixConstructHelperIsNeeded(const Constructor& c);
197 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400198 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
199 void assembleMatrixFromExpressions(const std::vector<std::unique_ptr<Expression>>& args,
200 int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500201
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500202 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
203
Timothy Liang6403b0e2018-05-17 10:40:04 -0400204 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
205
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500206 bool canCoerce(const Type& t1, const Type& t2);
207
208 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400209
210 void writeFieldAccess(const FieldAccess& f);
211
212 void writeSwizzle(const Swizzle& swizzle);
213
214 static Precedence GetBinaryPrecedence(Token::Kind op);
215
216 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
217
218 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
219
220 void writeIndexExpression(const IndexExpression& expr);
221
222 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
223
224 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
225
226 void writeBoolLiteral(const BoolLiteral& b);
227
228 void writeIntLiteral(const IntLiteral& i);
229
230 void writeFloatLiteral(const FloatLiteral& f);
231
232 void writeSetting(const Setting& s);
233
234 void writeStatement(const Statement& s);
235
236 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
237
238 void writeBlock(const Block& b);
239
240 void writeIfStatement(const IfStatement& stmt);
241
242 void writeForStatement(const ForStatement& f);
243
244 void writeWhileStatement(const WhileStatement& w);
245
246 void writeDoStatement(const DoStatement& d);
247
248 void writeSwitchStatement(const SwitchStatement& s);
249
250 void writeReturnStatement(const ReturnStatement& r);
251
252 void writeProgramElement(const ProgramElement& e);
253
254 Requirements requirements(const FunctionDeclaration& f);
255
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400256 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400257
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400258 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400259
Timothy Liang7d637782018-06-05 09:58:07 -0400260 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400261 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400262 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400263 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
264 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
265 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400266 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400267 const char* fLineEnding;
268 const Context& fContext;
269 StringStream fHeader;
270 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500271 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400272 Program::Kind fProgramKind;
273 int fVarCount = 0;
274 int fIndentation = 0;
275 bool fAtLineStart = false;
276 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
277 // more than one or two structs per shader, a simple linear search will be faster than anything
278 // fancier.
279 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500280 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400281 // true if we have run into usages of dFdx / dFdy
282 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400283 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
284 bool fSetupFragPositionGlobal = false;
285 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400286 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400287 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400288 String fRTHeightName;
Ethan Nicholascc305772017-10-13 16:17:45 -0400289
John Stiles7571f9e2020-09-02 22:42:33 -0400290 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400291};
292
John Stilesa6841be2020-08-06 14:11:56 -0400293} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400294
295#endif