blob: 9cc0f46a1231cc81d672173fd225aa6ca7fb1d9d [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/sksl/SkSLStringStream.h"
18#include "src/sksl/ir/SkSLBinaryExpression.h"
19#include "src/sksl/ir/SkSLBoolLiteral.h"
20#include "src/sksl/ir/SkSLConstructor.h"
21#include "src/sksl/ir/SkSLDoStatement.h"
22#include "src/sksl/ir/SkSLExtension.h"
23#include "src/sksl/ir/SkSLFieldAccess.h"
24#include "src/sksl/ir/SkSLFloatLiteral.h"
25#include "src/sksl/ir/SkSLForStatement.h"
26#include "src/sksl/ir/SkSLFunctionCall.h"
27#include "src/sksl/ir/SkSLFunctionDeclaration.h"
28#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050029#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040046
47namespace SkSL {
48
Ethan Nicholascc305772017-10-13 16:17:45 -040049/**
50 * Converts a Program into Metal code.
51 */
52class MetalCodeGenerator : public CodeGenerator {
53public:
Timothy Lianga06f2152018-05-24 15:33:31 -040054 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040055 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040056
Ethan Nicholascc305772017-10-13 16:17:45 -040057 enum Precedence {
58 kParentheses_Precedence = 1,
59 kPostfix_Precedence = 2,
60 kPrefix_Precedence = 3,
61 kMultiplicative_Precedence = 4,
62 kAdditive_Precedence = 5,
63 kShift_Precedence = 6,
64 kRelational_Precedence = 7,
65 kEquality_Precedence = 8,
66 kBitwiseAnd_Precedence = 9,
67 kBitwiseXor_Precedence = 10,
68 kBitwiseOr_Precedence = 11,
69 kLogicalAnd_Precedence = 12,
70 kLogicalXor_Precedence = 13,
71 kLogicalOr_Precedence = 14,
72 kTernary_Precedence = 15,
73 kAssignment_Precedence = 16,
74 kSequence_Precedence = 17,
75 kTopLevel_Precedence = kSequence_Precedence
76 };
77
78 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040079 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040080 : INHERITED(program, errors, out)
John Stiles23456532020-12-30 18:12:48 -050081 , fReservedWords({"atan2", "rsqrt", "rint", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040082 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040083 , fContext(*context) {
84 this->setupIntrinsics();
85 }
Ethan Nicholascc305772017-10-13 16:17:45 -040086
87 bool generateCode() override;
88
89protected:
90 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040091 static constexpr Requirements kNo_Requirements = 0;
92 static constexpr Requirements kInputs_Requirement = 1 << 0;
93 static constexpr Requirements kOutputs_Requirement = 1 << 1;
94 static constexpr Requirements kUniforms_Requirement = 1 << 2;
95 static constexpr Requirements kGlobals_Requirement = 1 << 3;
96 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040097
Timothy Liang6403b0e2018-05-17 10:40:04 -040098 enum IntrinsicKind {
John Stilesd7199b22020-12-30 16:22:58 -050099 kAtan_IntrinsicKind,
100 kBitcast_IntrinsicKind,
101 kBitCount_IntrinsicKind,
102 kCompareEqual_IntrinsicKind,
103 kCompareGreaterThan_IntrinsicKind,
104 kCompareGreaterThanEqual_IntrinsicKind,
105 kCompareLessThan_IntrinsicKind,
106 kCompareLessThanEqual_IntrinsicKind,
107 kCompareNotEqual_IntrinsicKind,
108 kDegrees_IntrinsicKind,
109 kDFdx_IntrinsicKind,
110 kDFdy_IntrinsicKind,
111 kDistance_IntrinsicKind,
112 kDot_IntrinsicKind,
113 kFaceforward_IntrinsicKind,
114 kFindLSB_IntrinsicKind,
115 kFindMSB_IntrinsicKind,
116 kInverse_IntrinsicKind,
117 kInversesqrt_IntrinsicKind,
118 kLength_IntrinsicKind,
119 kMatrixCompMult_IntrinsicKind,
120 kMod_IntrinsicKind,
121 kNormalize_IntrinsicKind,
122 kRadians_IntrinsicKind,
123 kReflect_IntrinsicKind,
124 kRefract_IntrinsicKind,
John Stiles23456532020-12-30 18:12:48 -0500125 kRoundEven_IntrinsicKind,
John Stilesd7199b22020-12-30 16:22:58 -0500126 kTexture_IntrinsicKind,
Timothy Lianga06f2152018-05-24 15:33:31 -0400127 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400128
John Stilesd6449e92020-11-30 09:13:23 -0500129 static const char* OperatorName(Token::Kind op);
130
John Stilescdcdb042020-07-06 09:03:51 -0400131 class GlobalStructVisitor;
132 void visitGlobalStruct(GlobalStructVisitor* visitor);
133
Timothy Lianga06f2152018-05-24 15:33:31 -0400134 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400135
Ethan Nicholascc305772017-10-13 16:17:45 -0400136 void write(const char* s);
137
138 void writeLine();
139
140 void writeLine(const char* s);
141
142 void write(const String& s);
143
144 void writeLine(const String& s);
145
146 void writeHeader();
147
148 void writeUniformStruct();
149
150 void writeInputStruct();
151
152 void writeOutputStruct();
153
Timothy Liang7d637782018-06-05 09:58:07 -0400154 void writeInterfaceBlocks();
155
John Stilesdc75a972020-11-25 16:24:55 -0500156 void writeStructDefinitions();
157
Timothy Liangdc89f192018-06-13 09:20:31 -0400158 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
159 const InterfaceBlock* parentIntf = nullptr);
160
Timothy Liang7d637782018-06-05 09:58:07 -0400161 int size(const Type* type, bool isPacked) const;
162
163 int alignment(const Type* type, bool isPacked) const;
164
Timothy Liangee84fe12018-05-18 14:38:19 -0400165 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500166
John Stilescdcdb042020-07-06 09:03:51 -0400167 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400168
Ethan Nicholascc305772017-10-13 16:17:45 -0400169 void writePrecisionModifier();
170
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500171 String typeName(const Type& type);
172
Brian Osman02bc5222021-01-28 11:00:20 -0500173 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500174
John Stilesb4418502021-02-04 10:57:08 -0500175 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400176
177 void writeExtension(const Extension& ext);
178
179 void writeInterfaceBlock(const InterfaceBlock& intf);
180
181 void writeFunctionStart(const FunctionDeclaration& f);
182
John Stiles06b84ef2020-12-09 12:35:48 -0500183 void writeFunctionRequirementParams(const FunctionDeclaration& f,
184 const char*& separator);
185
186 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
187
John Stiles569249b2020-11-03 12:18:22 -0500188 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400189
190 void writeFunction(const FunctionDefinition& f);
191
John Stiles569249b2020-11-03 12:18:22 -0500192 void writeFunctionPrototype(const FunctionPrototype& f);
193
Ethan Nicholascc305772017-10-13 16:17:45 -0400194 void writeLayout(const Layout& layout);
195
196 void writeModifiers(const Modifiers& modifiers, bool globalContext);
197
Ethan Nicholascc305772017-10-13 16:17:45 -0400198 void writeVarInitializer(const Variable& var, const Expression& value);
199
Timothy Liang651286f2018-06-07 09:55:33 -0400200 void writeName(const String& name);
201
Brian Osmanc0213602020-10-06 14:43:32 -0400202 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400203
204 void writeFragCoord();
205
206 void writeVariableReference(const VariableReference& ref);
207
208 void writeExpression(const Expression& expr, Precedence parentPrecedence);
209
Ethan Nicholascc305772017-10-13 16:17:45 -0400210 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
211
John Stiles06b84ef2020-12-09 12:35:48 -0500212 String getOutParamHelper(const FunctionCall& c,
213 const ExpressionArray& arguments,
214 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400215
John Stilesd7199b22020-12-30 16:22:58 -0500216 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500217
John Stilesf64e4072020-12-10 10:34:27 -0500218 String getBitcastIntrinsic(const Type& outType);
219
John Stiles86424eb2020-12-11 11:17:14 -0500220 String getTempVariable(const Type& varType);
221
John Stilesb21fac22020-12-04 15:36:49 -0500222 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500223
John Stiles1bdafbf2020-05-28 12:17:20 -0400224 bool matrixConstructHelperIsNeeded(const Constructor& c);
225 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400226 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400227 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500228
Brian Osman93aed9a2020-12-28 15:18:46 -0500229 void writeMatrixCompMult();
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500230 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
231
John Stilesd7199b22020-12-30 16:22:58 -0500232 void writeArgumentList(const ExpressionArray& arguments);
233
John Stiles791c27d2020-12-30 14:56:57 -0500234 void writeSimpleIntrinsic(const FunctionCall& c);
235
John Stilesd7199b22020-12-30 16:22:58 -0500236 void writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400237
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500238 bool canCoerce(const Type& t1, const Type& t2);
239
240 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400241
242 void writeFieldAccess(const FieldAccess& f);
243
244 void writeSwizzle(const Swizzle& swizzle);
245
246 static Precedence GetBinaryPrecedence(Token::Kind op);
247
248 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
249
250 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
251
252 void writeIndexExpression(const IndexExpression& expr);
253
254 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
255
256 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
257
258 void writeBoolLiteral(const BoolLiteral& b);
259
260 void writeIntLiteral(const IntLiteral& i);
261
262 void writeFloatLiteral(const FloatLiteral& f);
263
264 void writeSetting(const Setting& s);
265
266 void writeStatement(const Statement& s);
267
John Stiles8f2a0cf2020-10-13 12:48:21 -0400268 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400269
270 void writeBlock(const Block& b);
271
272 void writeIfStatement(const IfStatement& stmt);
273
274 void writeForStatement(const ForStatement& f);
275
Ethan Nicholascc305772017-10-13 16:17:45 -0400276 void writeDoStatement(const DoStatement& d);
277
278 void writeSwitchStatement(const SwitchStatement& s);
279
John Stiles986c7fb2020-12-01 14:44:56 -0500280 void writeReturnStatementFromMain();
281
Ethan Nicholascc305772017-10-13 16:17:45 -0400282 void writeReturnStatement(const ReturnStatement& r);
283
284 void writeProgramElement(const ProgramElement& e);
285
286 Requirements requirements(const FunctionDeclaration& f);
287
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400288 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400289
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400290 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400291
John Stilesda5cdf62021-01-28 11:47:29 -0500292 int getUniformBinding(const Modifiers& m);
293
294 int getUniformSet(const Modifiers& m);
295
John Stilesd7199b22020-12-30 16:22:58 -0500296 std::unordered_map<String, IntrinsicKind> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400297 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400298 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
299 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
300 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400301 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400302 const char* fLineEnding;
303 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400304 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500305 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400306 Program::Kind fProgramKind;
307 int fVarCount = 0;
308 int fIndentation = 0;
309 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500310 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400311 // true if we have run into usages of dFdx / dFdy
312 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400313 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
314 bool fSetupFragPositionGlobal = false;
315 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400316 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400317 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400318 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500319 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500320 int fSwizzleHelperCount = 0;
321 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400322
John Stiles7571f9e2020-09-02 22:42:33 -0400323 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400324};
325
John Stilesa6841be2020-08-06 14:11:56 -0400326} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400327
328#endif