blob: d446807f5be4b557f9749815b4b081f887241a4e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/sksl/SkSLCodeGenerator.h"
Brian Osman00185012021-02-04 16:07:11 -050018#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLStringStream.h"
20#include "src/sksl/ir/SkSLBinaryExpression.h"
21#include "src/sksl/ir/SkSLBoolLiteral.h"
22#include "src/sksl/ir/SkSLConstructor.h"
23#include "src/sksl/ir/SkSLDoStatement.h"
24#include "src/sksl/ir/SkSLExtension.h"
25#include "src/sksl/ir/SkSLFieldAccess.h"
26#include "src/sksl/ir/SkSLFloatLiteral.h"
27#include "src/sksl/ir/SkSLForStatement.h"
28#include "src/sksl/ir/SkSLFunctionCall.h"
29#include "src/sksl/ir/SkSLFunctionDeclaration.h"
30#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050031#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/sksl/ir/SkSLIfStatement.h"
33#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040034#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/sksl/ir/SkSLIntLiteral.h"
36#include "src/sksl/ir/SkSLInterfaceBlock.h"
37#include "src/sksl/ir/SkSLPostfixExpression.h"
38#include "src/sksl/ir/SkSLPrefixExpression.h"
39#include "src/sksl/ir/SkSLProgramElement.h"
40#include "src/sksl/ir/SkSLReturnStatement.h"
41#include "src/sksl/ir/SkSLSetting.h"
42#include "src/sksl/ir/SkSLStatement.h"
43#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:
Brian Osman00185012021-02-04 16:07:11 -050071 using Precedence = Operators::Precedence;
72
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 Stilesd6449e92020-11-30 09:13:23 -0500112 static const char* OperatorName(Token::Kind op);
113
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
129 void writeHeader();
130
131 void writeUniformStruct();
132
133 void writeInputStruct();
134
135 void writeOutputStruct();
136
Timothy Liang7d637782018-06-05 09:58:07 -0400137 void writeInterfaceBlocks();
138
John Stilesdc75a972020-11-25 16:24:55 -0500139 void writeStructDefinitions();
140
Timothy Liangdc89f192018-06-13 09:20:31 -0400141 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
142 const InterfaceBlock* parentIntf = nullptr);
143
Timothy Liang7d637782018-06-05 09:58:07 -0400144 int size(const Type* type, bool isPacked) const;
145
146 int alignment(const Type* type, bool isPacked) const;
147
Timothy Liangee84fe12018-05-18 14:38:19 -0400148 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500149
John Stilescdcdb042020-07-06 09:03:51 -0400150 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400151
Ethan Nicholascc305772017-10-13 16:17:45 -0400152 void writePrecisionModifier();
153
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500154 String typeName(const Type& type);
155
Brian Osman02bc5222021-01-28 11:00:20 -0500156 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500157
John Stilesb4418502021-02-04 10:57:08 -0500158 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400159
160 void writeExtension(const Extension& ext);
161
162 void writeInterfaceBlock(const InterfaceBlock& intf);
163
164 void writeFunctionStart(const FunctionDeclaration& f);
165
John Stiles06b84ef2020-12-09 12:35:48 -0500166 void writeFunctionRequirementParams(const FunctionDeclaration& f,
167 const char*& separator);
168
169 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
170
John Stiles569249b2020-11-03 12:18:22 -0500171 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400172
173 void writeFunction(const FunctionDefinition& f);
174
John Stiles569249b2020-11-03 12:18:22 -0500175 void writeFunctionPrototype(const FunctionPrototype& f);
176
Ethan Nicholascc305772017-10-13 16:17:45 -0400177 void writeLayout(const Layout& layout);
178
179 void writeModifiers(const Modifiers& modifiers, bool globalContext);
180
Ethan Nicholascc305772017-10-13 16:17:45 -0400181 void writeVarInitializer(const Variable& var, const Expression& value);
182
Timothy Liang651286f2018-06-07 09:55:33 -0400183 void writeName(const String& name);
184
Brian Osmanc0213602020-10-06 14:43:32 -0400185 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400186
187 void writeFragCoord();
188
189 void writeVariableReference(const VariableReference& ref);
190
191 void writeExpression(const Expression& expr, Precedence parentPrecedence);
192
Ethan Nicholascc305772017-10-13 16:17:45 -0400193 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
194
John Stiles06b84ef2020-12-09 12:35:48 -0500195 String getOutParamHelper(const FunctionCall& c,
196 const ExpressionArray& arguments,
197 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400198
John Stilesd7199b22020-12-30 16:22:58 -0500199 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500200
John Stilesf64e4072020-12-10 10:34:27 -0500201 String getBitcastIntrinsic(const Type& outType);
202
John Stiles86424eb2020-12-11 11:17:14 -0500203 String getTempVariable(const Type& varType);
204
John Stilesb21fac22020-12-04 15:36:49 -0500205 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500206
John Stiles1bdafbf2020-05-28 12:17:20 -0400207 bool matrixConstructHelperIsNeeded(const Constructor& c);
208 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400209 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400210 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500211
Brian Osman93aed9a2020-12-28 15:18:46 -0500212 void writeMatrixCompMult();
John Stiles01cdf012021-02-10 09:53:41 -0500213
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500214 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
215
John Stiles01cdf012021-02-10 09:53:41 -0500216 void writeMatrixEqualityHelper(const Type& left, const Type& right);
217
218 void writeMatrixInequalityHelper(const Type& left, const Type& right);
219
John Stilesd7199b22020-12-30 16:22:58 -0500220 void writeArgumentList(const ExpressionArray& arguments);
221
John Stiles791c27d2020-12-30 14:56:57 -0500222 void writeSimpleIntrinsic(const FunctionCall& c);
223
John Stilesd7199b22020-12-30 16:22:58 -0500224 void writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400225
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500226 bool canCoerce(const Type& t1, const Type& t2);
227
228 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400229
230 void writeFieldAccess(const FieldAccess& f);
231
232 void writeSwizzle(const Swizzle& swizzle);
233
Ethan Nicholascc305772017-10-13 16:17:45 -0400234 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
235
236 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
237
238 void writeIndexExpression(const IndexExpression& expr);
239
240 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
241
242 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
243
244 void writeBoolLiteral(const BoolLiteral& b);
245
246 void writeIntLiteral(const IntLiteral& i);
247
248 void writeFloatLiteral(const FloatLiteral& f);
249
250 void writeSetting(const Setting& s);
251
252 void writeStatement(const Statement& s);
253
John Stiles8f2a0cf2020-10-13 12:48:21 -0400254 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400255
256 void writeBlock(const Block& b);
257
258 void writeIfStatement(const IfStatement& stmt);
259
260 void writeForStatement(const ForStatement& f);
261
Ethan Nicholascc305772017-10-13 16:17:45 -0400262 void writeDoStatement(const DoStatement& d);
263
264 void writeSwitchStatement(const SwitchStatement& s);
265
John Stiles986c7fb2020-12-01 14:44:56 -0500266 void writeReturnStatementFromMain();
267
Ethan Nicholascc305772017-10-13 16:17:45 -0400268 void writeReturnStatement(const ReturnStatement& r);
269
270 void writeProgramElement(const ProgramElement& e);
271
272 Requirements requirements(const FunctionDeclaration& f);
273
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400274 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400275
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400276 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400277
John Stilesda5cdf62021-01-28 11:47:29 -0500278 int getUniformBinding(const Modifiers& m);
279
280 int getUniformSet(const Modifiers& m);
281
John Stilesd7199b22020-12-30 16:22:58 -0500282 std::unordered_map<String, IntrinsicKind> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400283 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400284 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
285 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
286 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400287 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400288 const char* fLineEnding;
289 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400290 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500291 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400292 Program::Kind fProgramKind;
293 int fVarCount = 0;
294 int fIndentation = 0;
295 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500296 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400297 // true if we have run into usages of dFdx / dFdy
298 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400299 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
300 bool fSetupFragPositionGlobal = false;
301 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400302 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400303 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400304 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500305 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500306 int fSwizzleHelperCount = 0;
307 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400308
John Stiles7571f9e2020-09-02 22:42:33 -0400309 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400310};
311
John Stilesa6841be2020-08-06 14:11:56 -0400312} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400313
314#endif