blob: 75b57fe8aa35c5473e6d8197f11773f2ecb665ec [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
Brian Osman00185012021-02-04 16:07:11 -050011#include <set>
Ethan Nicholascc305772017-10-13 16:17:45 -040012#include <stack>
13#include <tuple>
14#include <unordered_map>
John Stiles1bdafbf2020-05-28 12:17:20 -040015#include <unordered_set>
Ethan Nicholascc305772017-10-13 16:17:45 -040016
Ethan Nicholas24c17722021-03-09 13:10:59 -050017#include "include/private/SkSLProgramElement.h"
18#include "include/private/SkSLStatement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLCodeGenerator.h"
Brian Osman00185012021-02-04 16:07:11 -050020#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/sksl/SkSLStringStream.h"
22#include "src/sksl/ir/SkSLBinaryExpression.h"
23#include "src/sksl/ir/SkSLBoolLiteral.h"
24#include "src/sksl/ir/SkSLConstructor.h"
John Stiles7384b372021-04-01 13:48:15 -040025#include "src/sksl/ir/SkSLConstructorArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/sksl/ir/SkSLDoStatement.h"
27#include "src/sksl/ir/SkSLExtension.h"
28#include "src/sksl/ir/SkSLFieldAccess.h"
29#include "src/sksl/ir/SkSLFloatLiteral.h"
30#include "src/sksl/ir/SkSLForStatement.h"
31#include "src/sksl/ir/SkSLFunctionCall.h"
32#include "src/sksl/ir/SkSLFunctionDeclaration.h"
33#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050034#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/sksl/ir/SkSLIfStatement.h"
36#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040037#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/sksl/ir/SkSLIntLiteral.h"
39#include "src/sksl/ir/SkSLInterfaceBlock.h"
40#include "src/sksl/ir/SkSLPostfixExpression.h"
41#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/sksl/ir/SkSLReturnStatement.h"
43#include "src/sksl/ir/SkSLSetting.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/ir/SkSLSwitchStatement.h"
45#include "src/sksl/ir/SkSLSwizzle.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040049
50namespace SkSL {
51
Ethan Nicholascc305772017-10-13 16:17:45 -040052/**
53 * Converts a Program into Metal code.
54 */
55class MetalCodeGenerator : public CodeGenerator {
56public:
Timothy Lianga06f2152018-05-24 15:33:31 -040057 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040058 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040059
Ethan Nicholascc305772017-10-13 16:17:45 -040060 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040061 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040062 : INHERITED(program, errors, out)
John Stiles23456532020-12-30 18:12:48 -050063 , fReservedWords({"atan2", "rsqrt", "rint", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040064 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040065 , fContext(*context) {
66 this->setupIntrinsics();
67 }
Ethan Nicholascc305772017-10-13 16:17:45 -040068
69 bool generateCode() override;
70
71protected:
John Stiles45990502021-02-16 10:55:27 -050072 using Precedence = Operator::Precedence;
Brian Osman00185012021-02-04 16:07:11 -050073
Ethan Nicholascc305772017-10-13 16:17:45 -040074 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040075 static constexpr Requirements kNo_Requirements = 0;
76 static constexpr Requirements kInputs_Requirement = 1 << 0;
77 static constexpr Requirements kOutputs_Requirement = 1 << 1;
78 static constexpr Requirements kUniforms_Requirement = 1 << 2;
79 static constexpr Requirements kGlobals_Requirement = 1 << 3;
80 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040081
Timothy Liang6403b0e2018-05-17 10:40:04 -040082 enum IntrinsicKind {
John Stilesd7199b22020-12-30 16:22:58 -050083 kAtan_IntrinsicKind,
84 kBitcast_IntrinsicKind,
85 kBitCount_IntrinsicKind,
86 kCompareEqual_IntrinsicKind,
87 kCompareGreaterThan_IntrinsicKind,
88 kCompareGreaterThanEqual_IntrinsicKind,
89 kCompareLessThan_IntrinsicKind,
90 kCompareLessThanEqual_IntrinsicKind,
91 kCompareNotEqual_IntrinsicKind,
92 kDegrees_IntrinsicKind,
93 kDFdx_IntrinsicKind,
94 kDFdy_IntrinsicKind,
95 kDistance_IntrinsicKind,
96 kDot_IntrinsicKind,
97 kFaceforward_IntrinsicKind,
98 kFindLSB_IntrinsicKind,
99 kFindMSB_IntrinsicKind,
100 kInverse_IntrinsicKind,
101 kInversesqrt_IntrinsicKind,
102 kLength_IntrinsicKind,
103 kMatrixCompMult_IntrinsicKind,
104 kMod_IntrinsicKind,
105 kNormalize_IntrinsicKind,
106 kRadians_IntrinsicKind,
107 kReflect_IntrinsicKind,
108 kRefract_IntrinsicKind,
John Stiles23456532020-12-30 18:12:48 -0500109 kRoundEven_IntrinsicKind,
John Stilesd7199b22020-12-30 16:22:58 -0500110 kTexture_IntrinsicKind,
Timothy Lianga06f2152018-05-24 15:33:31 -0400111 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400112
John Stiles45990502021-02-16 10:55:27 -0500113 static const char* OperatorName(Operator op);
John Stilesd6449e92020-11-30 09:13:23 -0500114
John Stilescdcdb042020-07-06 09:03:51 -0400115 class GlobalStructVisitor;
116 void visitGlobalStruct(GlobalStructVisitor* visitor);
117
Timothy Lianga06f2152018-05-24 15:33:31 -0400118 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400119
Ethan Nicholascc305772017-10-13 16:17:45 -0400120 void write(const char* s);
121
122 void writeLine();
123
124 void writeLine(const char* s);
125
126 void write(const String& s);
127
128 void writeLine(const String& s);
129
John Stilese8b5a732021-03-12 13:25:52 -0500130 void finishLine();
131
Michael Ludwigbf58add2021-03-16 10:40:11 -0400132 void writeHeader();
133
Ethan Nicholascc305772017-10-13 16:17:45 -0400134 void writeUniformStruct();
135
136 void writeInputStruct();
137
138 void writeOutputStruct();
139
Timothy Liang7d637782018-06-05 09:58:07 -0400140 void writeInterfaceBlocks();
141
John Stilesdc75a972020-11-25 16:24:55 -0500142 void writeStructDefinitions();
143
Timothy Liangdc89f192018-06-13 09:20:31 -0400144 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
145 const InterfaceBlock* parentIntf = nullptr);
146
Timothy Liang7d637782018-06-05 09:58:07 -0400147 int size(const Type* type, bool isPacked) const;
148
149 int alignment(const Type* type, bool isPacked) const;
150
Timothy Liangee84fe12018-05-18 14:38:19 -0400151 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500152
John Stilescdcdb042020-07-06 09:03:51 -0400153 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400154
Ethan Nicholascc305772017-10-13 16:17:45 -0400155 void writePrecisionModifier();
156
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500157 String typeName(const Type& type);
158
Brian Osman02bc5222021-01-28 11:00:20 -0500159 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500160
John Stilesb4418502021-02-04 10:57:08 -0500161 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400162
163 void writeExtension(const Extension& ext);
164
165 void writeInterfaceBlock(const InterfaceBlock& intf);
166
167 void writeFunctionStart(const FunctionDeclaration& f);
168
John Stiles06b84ef2020-12-09 12:35:48 -0500169 void writeFunctionRequirementParams(const FunctionDeclaration& f,
170 const char*& separator);
171
172 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
173
John Stiles569249b2020-11-03 12:18:22 -0500174 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400175
176 void writeFunction(const FunctionDefinition& f);
177
John Stiles569249b2020-11-03 12:18:22 -0500178 void writeFunctionPrototype(const FunctionPrototype& f);
179
Ethan Nicholascc305772017-10-13 16:17:45 -0400180 void writeLayout(const Layout& layout);
181
182 void writeModifiers(const Modifiers& modifiers, bool globalContext);
183
Ethan Nicholascc305772017-10-13 16:17:45 -0400184 void writeVarInitializer(const Variable& var, const Expression& value);
185
Timothy Liang651286f2018-06-07 09:55:33 -0400186 void writeName(const String& name);
187
Brian Osmanc0213602020-10-06 14:43:32 -0400188 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400189
190 void writeFragCoord();
191
192 void writeVariableReference(const VariableReference& ref);
193
194 void writeExpression(const Expression& expr, Precedence parentPrecedence);
195
Ethan Nicholascc305772017-10-13 16:17:45 -0400196 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
197
John Stiles06b84ef2020-12-09 12:35:48 -0500198 String getOutParamHelper(const FunctionCall& c,
199 const ExpressionArray& arguments,
200 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400201
John Stilesd7199b22020-12-30 16:22:58 -0500202 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500203
John Stilesf64e4072020-12-10 10:34:27 -0500204 String getBitcastIntrinsic(const Type& outType);
205
John Stiles86424eb2020-12-11 11:17:14 -0500206 String getTempVariable(const Type& varType);
207
John Stilesb21fac22020-12-04 15:36:49 -0500208 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500209
John Stiles1bdafbf2020-05-28 12:17:20 -0400210 bool matrixConstructHelperIsNeeded(const Constructor& c);
211 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400212 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400213 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500214
Brian Osman93aed9a2020-12-28 15:18:46 -0500215 void writeMatrixCompMult();
John Stiles01cdf012021-02-10 09:53:41 -0500216
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500217 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
218
John Stiles01cdf012021-02-10 09:53:41 -0500219 void writeMatrixEqualityHelper(const Type& left, const Type& right);
220
221 void writeMatrixInequalityHelper(const Type& left, const Type& right);
222
John Stilesd7199b22020-12-30 16:22:58 -0500223 void writeArgumentList(const ExpressionArray& arguments);
224
John Stiles791c27d2020-12-30 14:56:57 -0500225 void writeSimpleIntrinsic(const FunctionCall& c);
226
John Stilesd7199b22020-12-30 16:22:58 -0500227 void writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400228
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500229 bool canCoerce(const Type& t1, const Type& t2);
230
231 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400232
John Stiles7384b372021-04-01 13:48:15 -0400233 void writeConstructorArray(const ConstructorArray& c, Precedence parentPrecedence);
234
John Stiles933043b2021-03-31 15:23:16 -0400235 void writeSingleArgumentConstructor(const SingleArgumentConstructor& c,
John Stilese1182782021-03-30 22:09:37 -0400236 Precedence parentPrecedence);
237
Ethan Nicholascc305772017-10-13 16:17:45 -0400238 void writeFieldAccess(const FieldAccess& f);
239
240 void writeSwizzle(const Swizzle& swizzle);
241
Ethan Nicholascc305772017-10-13 16:17:45 -0400242 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
243
244 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
245
246 void writeIndexExpression(const IndexExpression& expr);
247
248 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
249
250 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
251
252 void writeBoolLiteral(const BoolLiteral& b);
253
254 void writeIntLiteral(const IntLiteral& i);
255
256 void writeFloatLiteral(const FloatLiteral& f);
257
258 void writeSetting(const Setting& s);
259
260 void writeStatement(const Statement& s);
261
John Stiles8f2a0cf2020-10-13 12:48:21 -0400262 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400263
264 void writeBlock(const Block& b);
265
266 void writeIfStatement(const IfStatement& stmt);
267
268 void writeForStatement(const ForStatement& f);
269
Ethan Nicholascc305772017-10-13 16:17:45 -0400270 void writeDoStatement(const DoStatement& d);
271
272 void writeSwitchStatement(const SwitchStatement& s);
273
John Stiles986c7fb2020-12-01 14:44:56 -0500274 void writeReturnStatementFromMain();
275
Ethan Nicholascc305772017-10-13 16:17:45 -0400276 void writeReturnStatement(const ReturnStatement& r);
277
278 void writeProgramElement(const ProgramElement& e);
279
280 Requirements requirements(const FunctionDeclaration& f);
281
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400282 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400283
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400284 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400285
John Stilesda5cdf62021-01-28 11:47:29 -0500286 int getUniformBinding(const Modifiers& m);
287
288 int getUniformSet(const Modifiers& m);
289
John Stilesd7199b22020-12-30 16:22:58 -0500290 std::unordered_map<String, IntrinsicKind> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400291 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400292 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
293 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
294 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400295 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400296 const char* fLineEnding;
297 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400298 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500299 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400300 int fVarCount = 0;
301 int fIndentation = 0;
302 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500303 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400304 // true if we have run into usages of dFdx / dFdy
305 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400306 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
307 bool fSetupFragPositionGlobal = false;
308 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400309 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400310 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400311 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500312 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500313 int fSwizzleHelperCount = 0;
314 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400315
John Stiles7571f9e2020-09-02 22:42:33 -0400316 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400317};
318
John Stilesa6841be2020-08-06 14:11:56 -0400319} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400320
321#endif