blob: 6b38c037aeae6b0bc2a61dd3ca82543a261160f3 [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"
25#include "src/sksl/ir/SkSLDoStatement.h"
26#include "src/sksl/ir/SkSLExtension.h"
27#include "src/sksl/ir/SkSLFieldAccess.h"
28#include "src/sksl/ir/SkSLFloatLiteral.h"
29#include "src/sksl/ir/SkSLForStatement.h"
30#include "src/sksl/ir/SkSLFunctionCall.h"
31#include "src/sksl/ir/SkSLFunctionDeclaration.h"
32#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050033#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/sksl/ir/SkSLIfStatement.h"
35#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040036#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/sksl/ir/SkSLIntLiteral.h"
38#include "src/sksl/ir/SkSLInterfaceBlock.h"
39#include "src/sksl/ir/SkSLPostfixExpression.h"
40#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLReturnStatement.h"
42#include "src/sksl/ir/SkSLSetting.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLSwitchStatement.h"
44#include "src/sksl/ir/SkSLSwizzle.h"
45#include "src/sksl/ir/SkSLTernaryExpression.h"
46#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040048
49namespace SkSL {
50
Ethan Nicholascc305772017-10-13 16:17:45 -040051/**
52 * Converts a Program into Metal code.
53 */
54class MetalCodeGenerator : public CodeGenerator {
55public:
Timothy Lianga06f2152018-05-24 15:33:31 -040056 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040057 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040058
Ethan Nicholascc305772017-10-13 16:17:45 -040059 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040060 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040061 : INHERITED(program, errors, out)
John Stiles23456532020-12-30 18:12:48 -050062 , fReservedWords({"atan2", "rsqrt", "rint", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040063 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040064 , fContext(*context) {
65 this->setupIntrinsics();
66 }
Ethan Nicholascc305772017-10-13 16:17:45 -040067
68 bool generateCode() override;
69
70protected:
John Stiles45990502021-02-16 10:55:27 -050071 using Precedence = Operator::Precedence;
Brian Osman00185012021-02-04 16:07:11 -050072
Ethan Nicholascc305772017-10-13 16:17:45 -040073 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040074 static constexpr Requirements kNo_Requirements = 0;
75 static constexpr Requirements kInputs_Requirement = 1 << 0;
76 static constexpr Requirements kOutputs_Requirement = 1 << 1;
77 static constexpr Requirements kUniforms_Requirement = 1 << 2;
78 static constexpr Requirements kGlobals_Requirement = 1 << 3;
79 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040080
Timothy Liang6403b0e2018-05-17 10:40:04 -040081 enum IntrinsicKind {
John Stilesd7199b22020-12-30 16:22:58 -050082 kAtan_IntrinsicKind,
83 kBitcast_IntrinsicKind,
84 kBitCount_IntrinsicKind,
85 kCompareEqual_IntrinsicKind,
86 kCompareGreaterThan_IntrinsicKind,
87 kCompareGreaterThanEqual_IntrinsicKind,
88 kCompareLessThan_IntrinsicKind,
89 kCompareLessThanEqual_IntrinsicKind,
90 kCompareNotEqual_IntrinsicKind,
91 kDegrees_IntrinsicKind,
92 kDFdx_IntrinsicKind,
93 kDFdy_IntrinsicKind,
94 kDistance_IntrinsicKind,
95 kDot_IntrinsicKind,
96 kFaceforward_IntrinsicKind,
97 kFindLSB_IntrinsicKind,
98 kFindMSB_IntrinsicKind,
99 kInverse_IntrinsicKind,
100 kInversesqrt_IntrinsicKind,
101 kLength_IntrinsicKind,
102 kMatrixCompMult_IntrinsicKind,
103 kMod_IntrinsicKind,
104 kNormalize_IntrinsicKind,
105 kRadians_IntrinsicKind,
106 kReflect_IntrinsicKind,
107 kRefract_IntrinsicKind,
John Stiles23456532020-12-30 18:12:48 -0500108 kRoundEven_IntrinsicKind,
John Stilesd7199b22020-12-30 16:22:58 -0500109 kTexture_IntrinsicKind,
Timothy Lianga06f2152018-05-24 15:33:31 -0400110 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400111
John Stiles45990502021-02-16 10:55:27 -0500112 static const char* OperatorName(Operator op);
John Stilesd6449e92020-11-30 09:13:23 -0500113
John Stilescdcdb042020-07-06 09:03:51 -0400114 class GlobalStructVisitor;
115 void visitGlobalStruct(GlobalStructVisitor* visitor);
116
Timothy Lianga06f2152018-05-24 15:33:31 -0400117 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400118
Ethan Nicholascc305772017-10-13 16:17:45 -0400119 void write(const char* s);
120
121 void writeLine();
122
123 void writeLine(const char* s);
124
125 void write(const String& s);
126
127 void writeLine(const String& s);
128
John Stilese8b5a732021-03-12 13:25:52 -0500129 void finishLine();
130
Michael Ludwigbf58add2021-03-16 10:40:11 -0400131 void writeHeader();
132
Ethan Nicholascc305772017-10-13 16:17:45 -0400133 void writeUniformStruct();
134
135 void writeInputStruct();
136
137 void writeOutputStruct();
138
Timothy Liang7d637782018-06-05 09:58:07 -0400139 void writeInterfaceBlocks();
140
John Stilesdc75a972020-11-25 16:24:55 -0500141 void writeStructDefinitions();
142
Timothy Liangdc89f192018-06-13 09:20:31 -0400143 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
144 const InterfaceBlock* parentIntf = nullptr);
145
Timothy Liang7d637782018-06-05 09:58:07 -0400146 int size(const Type* type, bool isPacked) const;
147
148 int alignment(const Type* type, bool isPacked) const;
149
Timothy Liangee84fe12018-05-18 14:38:19 -0400150 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500151
John Stilescdcdb042020-07-06 09:03:51 -0400152 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400153
Ethan Nicholascc305772017-10-13 16:17:45 -0400154 void writePrecisionModifier();
155
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500156 String typeName(const Type& type);
157
Brian Osman02bc5222021-01-28 11:00:20 -0500158 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500159
John Stilesb4418502021-02-04 10:57:08 -0500160 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400161
162 void writeExtension(const Extension& ext);
163
164 void writeInterfaceBlock(const InterfaceBlock& intf);
165
166 void writeFunctionStart(const FunctionDeclaration& f);
167
John Stiles06b84ef2020-12-09 12:35:48 -0500168 void writeFunctionRequirementParams(const FunctionDeclaration& f,
169 const char*& separator);
170
171 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
172
John Stiles569249b2020-11-03 12:18:22 -0500173 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400174
175 void writeFunction(const FunctionDefinition& f);
176
John Stiles569249b2020-11-03 12:18:22 -0500177 void writeFunctionPrototype(const FunctionPrototype& f);
178
Ethan Nicholascc305772017-10-13 16:17:45 -0400179 void writeLayout(const Layout& layout);
180
181 void writeModifiers(const Modifiers& modifiers, bool globalContext);
182
Ethan Nicholascc305772017-10-13 16:17:45 -0400183 void writeVarInitializer(const Variable& var, const Expression& value);
184
Timothy Liang651286f2018-06-07 09:55:33 -0400185 void writeName(const String& name);
186
Brian Osmanc0213602020-10-06 14:43:32 -0400187 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400188
189 void writeFragCoord();
190
191 void writeVariableReference(const VariableReference& ref);
192
193 void writeExpression(const Expression& expr, Precedence parentPrecedence);
194
Ethan Nicholascc305772017-10-13 16:17:45 -0400195 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
196
John Stiles06b84ef2020-12-09 12:35:48 -0500197 String getOutParamHelper(const FunctionCall& c,
198 const ExpressionArray& arguments,
199 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400200
John Stilesd7199b22020-12-30 16:22:58 -0500201 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500202
John Stilesf64e4072020-12-10 10:34:27 -0500203 String getBitcastIntrinsic(const Type& outType);
204
John Stiles86424eb2020-12-11 11:17:14 -0500205 String getTempVariable(const Type& varType);
206
John Stilesb21fac22020-12-04 15:36:49 -0500207 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500208
John Stiles1bdafbf2020-05-28 12:17:20 -0400209 bool matrixConstructHelperIsNeeded(const Constructor& c);
210 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400211 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400212 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500213
Brian Osman93aed9a2020-12-28 15:18:46 -0500214 void writeMatrixCompMult();
John Stiles01cdf012021-02-10 09:53:41 -0500215
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500216 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
217
John Stiles01cdf012021-02-10 09:53:41 -0500218 void writeMatrixEqualityHelper(const Type& left, const Type& right);
219
220 void writeMatrixInequalityHelper(const Type& left, const Type& right);
221
John Stilesd7199b22020-12-30 16:22:58 -0500222 void writeArgumentList(const ExpressionArray& arguments);
223
John Stiles791c27d2020-12-30 14:56:57 -0500224 void writeSimpleIntrinsic(const FunctionCall& c);
225
John Stilesd7199b22020-12-30 16:22:58 -0500226 void writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400227
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500228 bool canCoerce(const Type& t1, const Type& t2);
229
230 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400231
232 void writeFieldAccess(const FieldAccess& f);
233
234 void writeSwizzle(const Swizzle& swizzle);
235
Ethan Nicholascc305772017-10-13 16:17:45 -0400236 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
237
238 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
239
240 void writeIndexExpression(const IndexExpression& expr);
241
242 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
243
244 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
245
246 void writeBoolLiteral(const BoolLiteral& b);
247
248 void writeIntLiteral(const IntLiteral& i);
249
250 void writeFloatLiteral(const FloatLiteral& f);
251
252 void writeSetting(const Setting& s);
253
254 void writeStatement(const Statement& s);
255
John Stiles8f2a0cf2020-10-13 12:48:21 -0400256 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400257
258 void writeBlock(const Block& b);
259
260 void writeIfStatement(const IfStatement& stmt);
261
262 void writeForStatement(const ForStatement& f);
263
Ethan Nicholascc305772017-10-13 16:17:45 -0400264 void writeDoStatement(const DoStatement& d);
265
266 void writeSwitchStatement(const SwitchStatement& s);
267
John Stiles986c7fb2020-12-01 14:44:56 -0500268 void writeReturnStatementFromMain();
269
Ethan Nicholascc305772017-10-13 16:17:45 -0400270 void writeReturnStatement(const ReturnStatement& r);
271
272 void writeProgramElement(const ProgramElement& e);
273
274 Requirements requirements(const FunctionDeclaration& f);
275
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400276 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400277
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400278 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400279
John Stilesda5cdf62021-01-28 11:47:29 -0500280 int getUniformBinding(const Modifiers& m);
281
282 int getUniformSet(const Modifiers& m);
283
John Stilesd7199b22020-12-30 16:22:58 -0500284 std::unordered_map<String, IntrinsicKind> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400285 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400286 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
287 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
288 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400289 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400290 const char* fLineEnding;
291 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400292 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500293 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400294 int fVarCount = 0;
295 int fIndentation = 0;
296 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500297 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400298 // true if we have run into usages of dFdx / dFdy
299 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400300 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
301 bool fSetupFragPositionGlobal = false;
302 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400303 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400304 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400305 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500306 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500307 int fSwizzleHelperCount = 0;
308 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400309
John Stiles7571f9e2020-09-02 22:42:33 -0400310 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400311};
312
John Stilesa6841be2020-08-06 14:11:56 -0400313} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400314
315#endif