blob: 13c18fdb7300baab21a378afd72be4dd0cd6ad4f [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"
John Stiles569249b2020-11-03 12:18:22 -050030#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/sksl/ir/SkSLIfStatement.h"
32#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040033#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLIntLiteral.h"
35#include "src/sksl/ir/SkSLInterfaceBlock.h"
36#include "src/sksl/ir/SkSLPostfixExpression.h"
37#include "src/sksl/ir/SkSLPrefixExpression.h"
38#include "src/sksl/ir/SkSLProgramElement.h"
39#include "src/sksl/ir/SkSLReturnStatement.h"
40#include "src/sksl/ir/SkSLSetting.h"
41#include "src/sksl/ir/SkSLStatement.h"
42#include "src/sksl/ir/SkSLSwitchStatement.h"
43#include "src/sksl/ir/SkSLSwizzle.h"
44#include "src/sksl/ir/SkSLTernaryExpression.h"
45#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#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
John Stiles569249b2020-11-03 12:18:22 -0500168 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400169
170 void writeFunction(const FunctionDefinition& f);
171
John Stiles569249b2020-11-03 12:18:22 -0500172 void writeFunctionPrototype(const FunctionPrototype& f);
173
Ethan Nicholascc305772017-10-13 16:17:45 -0400174 void writeLayout(const Layout& layout);
175
176 void writeModifiers(const Modifiers& modifiers, bool globalContext);
177
Ethan Nicholascc305772017-10-13 16:17:45 -0400178 void writeVarInitializer(const Variable& var, const Expression& value);
179
Timothy Liang651286f2018-06-07 09:55:33 -0400180 void writeName(const String& name);
181
Brian Osmanc0213602020-10-06 14:43:32 -0400182 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400183
184 void writeFragCoord();
185
186 void writeVariableReference(const VariableReference& ref);
187
188 void writeExpression(const Expression& expr, Precedence parentPrecedence);
189
190 void writeIntrinsicCall(const FunctionCall& c);
191
192 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
193
194 void writeFunctionCall(const FunctionCall& c);
195
Chris Daltondba7aab2018-11-15 10:57:49 -0500196 void writeInverseHack(const Expression& mat);
197
John Stiles1bdafbf2020-05-28 12:17:20 -0400198 bool matrixConstructHelperIsNeeded(const Constructor& c);
199 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400200 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400201 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500202
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500203 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
204
Timothy Liang6403b0e2018-05-17 10:40:04 -0400205 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
206
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500207 bool canCoerce(const Type& t1, const Type& t2);
208
209 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400210
211 void writeFieldAccess(const FieldAccess& f);
212
213 void writeSwizzle(const Swizzle& swizzle);
214
215 static Precedence GetBinaryPrecedence(Token::Kind op);
216
217 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
218
219 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
220
221 void writeIndexExpression(const IndexExpression& expr);
222
223 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
224
225 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
226
227 void writeBoolLiteral(const BoolLiteral& b);
228
229 void writeIntLiteral(const IntLiteral& i);
230
231 void writeFloatLiteral(const FloatLiteral& f);
232
233 void writeSetting(const Setting& s);
234
235 void writeStatement(const Statement& s);
236
John Stiles8f2a0cf2020-10-13 12:48:21 -0400237 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400238
239 void writeBlock(const Block& b);
240
241 void writeIfStatement(const IfStatement& stmt);
242
243 void writeForStatement(const ForStatement& f);
244
245 void writeWhileStatement(const WhileStatement& w);
246
247 void writeDoStatement(const DoStatement& d);
248
249 void writeSwitchStatement(const SwitchStatement& s);
250
251 void writeReturnStatement(const ReturnStatement& r);
252
253 void writeProgramElement(const ProgramElement& e);
254
255 Requirements requirements(const FunctionDeclaration& f);
256
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400257 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400258
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400259 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400260
Timothy Liang7d637782018-06-05 09:58:07 -0400261 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400262 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400263 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400264 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
265 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
266 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400267 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400268 const char* fLineEnding;
269 const Context& fContext;
270 StringStream fHeader;
271 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500272 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400273 Program::Kind fProgramKind;
274 int fVarCount = 0;
275 int fIndentation = 0;
276 bool fAtLineStart = false;
277 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
278 // more than one or two structs per shader, a simple linear search will be faster than anything
279 // fancier.
280 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500281 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400282 // true if we have run into usages of dFdx / dFdy
283 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400284 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
285 bool fSetupFragPositionGlobal = false;
286 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400287 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400288 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400289 String fRTHeightName;
Ethan Nicholascc305772017-10-13 16:17:45 -0400290
John Stiles7571f9e2020-09-02 22:42:33 -0400291 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400292};
293
John Stilesa6841be2020-08-06 14:11:56 -0400294} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400295
296#endif