blob: 1668e6979da2eba41bebbf652def6188ad4a4fc9 [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"
Brian Osman00185012021-02-04 16:07:11 -050019#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/sksl/SkSLStringStream.h"
John Stiles3738ef52021-04-13 10:41:57 -040021#include "src/sksl/codegen/SkSLCodeGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/sksl/ir/SkSLBinaryExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/ir/SkSLConstructor.h"
John Stiles8cad6372021-04-07 12:31:13 -040024#include "src/sksl/ir/SkSLConstructorCompound.h"
John Stiles5abb9e12021-04-06 13:47:19 -040025#include "src/sksl/ir/SkSLConstructorMatrixResize.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#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/SkSLInterfaceBlock.h"
John Stiles7591d4b2021-09-13 13:32:06 -040038#include "src/sksl/ir/SkSLLiteral.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#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 Nicholas3abc6c62021-08-13 11:20:09 -040059 MetalCodeGenerator(const Context* context, const Program* program, OutputStream* out)
60 : INHERITED(context, program, out)
John Stiles23456532020-12-30 18:12:48 -050061 , fReservedWords({"atan2", "rsqrt", "rint", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholas3abc6c62021-08-13 11:20:09 -040062 , fLineEnding("\n") {}
Ethan Nicholascc305772017-10-13 16:17:45 -040063
64 bool generateCode() override;
65
66protected:
John Stiles45990502021-02-16 10:55:27 -050067 using Precedence = Operator::Precedence;
Brian Osman00185012021-02-04 16:07:11 -050068
Ethan Nicholascc305772017-10-13 16:17:45 -040069 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040070 static constexpr Requirements kNo_Requirements = 0;
71 static constexpr Requirements kInputs_Requirement = 1 << 0;
72 static constexpr Requirements kOutputs_Requirement = 1 << 1;
73 static constexpr Requirements kUniforms_Requirement = 1 << 2;
74 static constexpr Requirements kGlobals_Requirement = 1 << 3;
75 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040076
John Stiles45990502021-02-16 10:55:27 -050077 static const char* OperatorName(Operator op);
John Stilesd6449e92020-11-30 09:13:23 -050078
John Stilescdcdb042020-07-06 09:03:51 -040079 class GlobalStructVisitor;
80 void visitGlobalStruct(GlobalStructVisitor* visitor);
81
Ethan Nicholas962dec42021-06-10 13:06:39 -040082 void write(skstd::string_view s);
Ethan Nicholascc305772017-10-13 16:17:45 -040083
Ethan Nicholas962dec42021-06-10 13:06:39 -040084 void writeLine(skstd::string_view s = skstd::string_view());
Ethan Nicholascc305772017-10-13 16:17:45 -040085
John Stilese8b5a732021-03-12 13:25:52 -050086 void finishLine();
87
Michael Ludwigbf58add2021-03-16 10:40:11 -040088 void writeHeader();
89
Ethan Nicholascc305772017-10-13 16:17:45 -040090 void writeUniformStruct();
91
92 void writeInputStruct();
93
94 void writeOutputStruct();
95
Timothy Liang7d637782018-06-05 09:58:07 -040096 void writeInterfaceBlocks();
97
John Stilesdc75a972020-11-25 16:24:55 -050098 void writeStructDefinitions();
99
Ethan Nicholas89cfde12021-09-27 11:20:34 -0400100 void writeFields(const std::vector<Type::Field>& fields, int parentLine,
Timothy Liangdc89f192018-06-13 09:20:31 -0400101 const InterfaceBlock* parentIntf = nullptr);
102
Timothy Liang7d637782018-06-05 09:58:07 -0400103 int size(const Type* type, bool isPacked) const;
104
105 int alignment(const Type* type, bool isPacked) const;
106
Timothy Liangee84fe12018-05-18 14:38:19 -0400107 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500108
John Stilescdcdb042020-07-06 09:03:51 -0400109 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400110
Ethan Nicholascc305772017-10-13 16:17:45 -0400111 void writePrecisionModifier();
112
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500113 String typeName(const Type& type);
114
Brian Osman02bc5222021-01-28 11:00:20 -0500115 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500116
John Stilesb4418502021-02-04 10:57:08 -0500117 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400118
119 void writeExtension(const Extension& ext);
120
121 void writeInterfaceBlock(const InterfaceBlock& intf);
122
123 void writeFunctionStart(const FunctionDeclaration& f);
124
John Stiles06b84ef2020-12-09 12:35:48 -0500125 void writeFunctionRequirementParams(const FunctionDeclaration& f,
126 const char*& separator);
127
128 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
129
John Stiles569249b2020-11-03 12:18:22 -0500130 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400131
132 void writeFunction(const FunctionDefinition& f);
133
John Stiles569249b2020-11-03 12:18:22 -0500134 void writeFunctionPrototype(const FunctionPrototype& f);
135
Ethan Nicholascc305772017-10-13 16:17:45 -0400136 void writeLayout(const Layout& layout);
137
Brian Osman58134e12021-05-13 14:51:07 -0400138 void writeModifiers(const Modifiers& modifiers);
Ethan Nicholascc305772017-10-13 16:17:45 -0400139
Ethan Nicholascc305772017-10-13 16:17:45 -0400140 void writeVarInitializer(const Variable& var, const Expression& value);
141
Ethan Nicholas962dec42021-06-10 13:06:39 -0400142 void writeName(skstd::string_view name);
Timothy Liang651286f2018-06-07 09:55:33 -0400143
Brian Osman58134e12021-05-13 14:51:07 -0400144 void writeVarDeclaration(const VarDeclaration& decl);
Ethan Nicholascc305772017-10-13 16:17:45 -0400145
146 void writeFragCoord();
147
148 void writeVariableReference(const VariableReference& ref);
149
150 void writeExpression(const Expression& expr, Precedence parentPrecedence);
151
Ethan Nicholascc305772017-10-13 16:17:45 -0400152 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
153
John Stiles06b84ef2020-12-09 12:35:48 -0500154 String getOutParamHelper(const FunctionCall& c,
155 const ExpressionArray& arguments,
156 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400157
John Stilesd7199b22020-12-30 16:22:58 -0500158 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500159
John Stilesf64e4072020-12-10 10:34:27 -0500160 String getBitcastIntrinsic(const Type& outType);
161
John Stiles86424eb2020-12-11 11:17:14 -0500162 String getTempVariable(const Type& varType);
163
John Stilesb21fac22020-12-04 15:36:49 -0500164 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500165
John Stiles8cad6372021-04-07 12:31:13 -0400166 bool matrixConstructHelperIsNeeded(const ConstructorCompound& c);
John Stiles5abb9e12021-04-06 13:47:19 -0400167 String getMatrixConstructHelper(const AnyConstructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400168 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles5abb9e12021-04-06 13:47:19 -0400169 void assembleMatrixFromExpressions(const AnyConstructor& ctor, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500170
Brian Osman93aed9a2020-12-28 15:18:46 -0500171 void writeMatrixCompMult();
John Stiles01cdf012021-02-10 09:53:41 -0500172
John Stilesfeb1e122021-09-09 14:27:28 -0400173 void writeOuterProduct();
174
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500175 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
176
John Stilesc985e142021-05-13 14:01:48 -0400177 void writeMatrixDivisionHelpers(const Type& type);
178
John Stiles39346472021-04-29 14:39:35 -0400179 void writeMatrixEqualityHelpers(const Type& left, const Type& right);
John Stiles01cdf012021-02-10 09:53:41 -0500180
John Stilesc18ee4e2021-08-13 17:53:49 -0400181 String getVectorFromMat2x2ConstructorHelper(const Type& matrixType);
John Stiles6de2e1d2021-07-09 12:41:55 -0400182
John Stiles39346472021-04-29 14:39:35 -0400183 void writeArrayEqualityHelpers(const Type& type);
184
185 void writeStructEqualityHelpers(const Type& type);
186
187 void writeEqualityHelpers(const Type& leftType, const Type& rightType);
John Stiles01cdf012021-02-10 09:53:41 -0500188
John Stilesd7199b22020-12-30 16:22:58 -0500189 void writeArgumentList(const ExpressionArray& arguments);
190
John Stiles791c27d2020-12-30 14:56:57 -0500191 void writeSimpleIntrinsic(const FunctionCall& c);
192
John Stiles496b7d12021-05-06 10:26:45 -0400193 bool writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400194
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500195 bool canCoerce(const Type& t1, const Type& t2);
196
John Stiles8cad6372021-04-07 12:31:13 -0400197 void writeConstructorCompound(const ConstructorCompound& c, Precedence parentPrecedence);
John Stiles2bec8ab2021-04-06 18:40:04 -0400198
John Stiles6de2e1d2021-07-09 12:41:55 -0400199 void writeConstructorCompoundVector(const ConstructorCompound& c, Precedence parentPrecedence);
200
John Stiles8cad6372021-04-07 12:31:13 -0400201 void writeConstructorCompoundMatrix(const ConstructorCompound& c, Precedence parentPrecedence);
John Stiles2bec8ab2021-04-06 18:40:04 -0400202
John Stiles5abb9e12021-04-06 13:47:19 -0400203 void writeConstructorMatrixResize(const ConstructorMatrixResize& c,
204 Precedence parentPrecedence);
205
John Stilesb14a8192021-04-05 11:40:46 -0400206 void writeAnyConstructor(const AnyConstructor& c,
207 const char* leftBracket,
208 const char* rightBracket,
209 Precedence parentPrecedence);
John Stiles7384b372021-04-01 13:48:15 -0400210
John Stilesb14a8192021-04-05 11:40:46 -0400211 void writeCastConstructor(const AnyConstructor& c,
212 const char* leftBracket,
213 const char* rightBracket,
214 Precedence parentPrecedence);
John Stilese1182782021-03-30 22:09:37 -0400215
Ethan Nicholascc305772017-10-13 16:17:45 -0400216 void writeFieldAccess(const FieldAccess& f);
217
218 void writeSwizzle(const Swizzle& swizzle);
219
John Stiles6b131292021-05-12 17:12:21 -0400220 // Splats a scalar expression across a matrix of arbitrary size.
221 void writeNumberAsMatrix(const Expression& expr, const Type& matrixType);
222
Ethan Nicholascc305772017-10-13 16:17:45 -0400223 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
224
225 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
226
227 void writeIndexExpression(const IndexExpression& expr);
228
229 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
230
231 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
232
John Stiles7591d4b2021-09-13 13:32:06 -0400233 void writeLiteral(const Literal& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400234
235 void writeSetting(const Setting& s);
236
237 void writeStatement(const Statement& s);
238
John Stiles8f2a0cf2020-10-13 12:48:21 -0400239 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400240
241 void writeBlock(const Block& b);
242
243 void writeIfStatement(const IfStatement& stmt);
244
245 void writeForStatement(const ForStatement& f);
246
Ethan Nicholascc305772017-10-13 16:17:45 -0400247 void writeDoStatement(const DoStatement& d);
248
249 void writeSwitchStatement(const SwitchStatement& s);
250
John Stiles986c7fb2020-12-01 14:44:56 -0500251 void writeReturnStatementFromMain();
252
Ethan Nicholascc305772017-10-13 16:17:45 -0400253 void writeReturnStatement(const ReturnStatement& r);
254
255 void writeProgramElement(const ProgramElement& e);
256
257 Requirements requirements(const FunctionDeclaration& f);
258
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400259 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400260
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400261 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400262
John Stilesda5cdf62021-01-28 11:47:29 -0500263 int getUniformBinding(const Modifiers& m);
264
265 int getUniformSet(const Modifiers& m);
266
Ethan Nicholas962dec42021-06-10 13:06:39 -0400267 std::unordered_set<skstd::string_view> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400268 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
Ethan Nicholas3533ff12021-08-02 12:53:29 -0400269 std::unordered_map<const InterfaceBlock*, skstd::string_view> fInterfaceBlockNameMap;
Timothy Lianga06f2152018-05-24 15:33:31 -0400270 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400271 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400272 const char* fLineEnding;
Ethan Nicholascc305772017-10-13 16:17:45 -0400273 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500274 StringStream fExtraFunctions;
John Stiles82d4c122021-08-10 16:03:23 -0400275 StringStream fExtraFunctionPrototypes;
Ethan Nicholascc305772017-10-13 16:17:45 -0400276 int fVarCount = 0;
277 int fIndentation = 0;
278 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500279 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400280 // true if we have run into usages of dFdx / dFdy
281 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400282 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
283 bool fSetupFragPositionGlobal = false;
284 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400285 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400286 int fUniformBuffer = -1;
Brian Salomond8d85b92021-07-07 09:41:17 -0400287 String fRTFlipName;
John Stiles986c7fb2020-12-01 14:44:56 -0500288 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500289 int fSwizzleHelperCount = 0;
290 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400291
John Stiles7571f9e2020-09-02 22:42:33 -0400292 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400293};
294
John Stilesa6841be2020-08-06 14:11:56 -0400295} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400296
297#endif