blob: c2839eebaf70ee8cef44064edc62be9ebf12ed4e [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 Stiles8cad6372021-04-07 12:31:13 -040025#include "src/sksl/ir/SkSLConstructorCompound.h"
John Stiles5abb9e12021-04-06 13:47:19 -040026#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/sksl/ir/SkSLDoStatement.h"
28#include "src/sksl/ir/SkSLExtension.h"
29#include "src/sksl/ir/SkSLFieldAccess.h"
30#include "src/sksl/ir/SkSLFloatLiteral.h"
31#include "src/sksl/ir/SkSLForStatement.h"
32#include "src/sksl/ir/SkSLFunctionCall.h"
33#include "src/sksl/ir/SkSLFunctionDeclaration.h"
34#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050035#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/sksl/ir/SkSLIfStatement.h"
37#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040038#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/sksl/ir/SkSLIntLiteral.h"
40#include "src/sksl/ir/SkSLInterfaceBlock.h"
41#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLReturnStatement.h"
44#include "src/sksl/ir/SkSLSetting.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLSwitchStatement.h"
46#include "src/sksl/ir/SkSLSwizzle.h"
47#include "src/sksl/ir/SkSLTernaryExpression.h"
48#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040050
51namespace SkSL {
52
Ethan Nicholascc305772017-10-13 16:17:45 -040053/**
54 * Converts a Program into Metal code.
55 */
56class MetalCodeGenerator : public CodeGenerator {
57public:
Timothy Lianga06f2152018-05-24 15:33:31 -040058 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040059 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040060
Ethan Nicholascc305772017-10-13 16:17:45 -040061 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040062 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040063 : INHERITED(program, errors, out)
John Stiles23456532020-12-30 18:12:48 -050064 , fReservedWords({"atan2", "rsqrt", "rint", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040065 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040066 , fContext(*context) {
67 this->setupIntrinsics();
68 }
Ethan Nicholascc305772017-10-13 16:17:45 -040069
70 bool generateCode() override;
71
72protected:
John Stiles45990502021-02-16 10:55:27 -050073 using Precedence = Operator::Precedence;
Brian Osman00185012021-02-04 16:07:11 -050074
Ethan Nicholascc305772017-10-13 16:17:45 -040075 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040076 static constexpr Requirements kNo_Requirements = 0;
77 static constexpr Requirements kInputs_Requirement = 1 << 0;
78 static constexpr Requirements kOutputs_Requirement = 1 << 1;
79 static constexpr Requirements kUniforms_Requirement = 1 << 2;
80 static constexpr Requirements kGlobals_Requirement = 1 << 3;
81 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040082
Timothy Liang6403b0e2018-05-17 10:40:04 -040083 enum IntrinsicKind {
John Stilesd7199b22020-12-30 16:22:58 -050084 kAtan_IntrinsicKind,
85 kBitcast_IntrinsicKind,
86 kBitCount_IntrinsicKind,
87 kCompareEqual_IntrinsicKind,
88 kCompareGreaterThan_IntrinsicKind,
89 kCompareGreaterThanEqual_IntrinsicKind,
90 kCompareLessThan_IntrinsicKind,
91 kCompareLessThanEqual_IntrinsicKind,
92 kCompareNotEqual_IntrinsicKind,
93 kDegrees_IntrinsicKind,
94 kDFdx_IntrinsicKind,
95 kDFdy_IntrinsicKind,
96 kDistance_IntrinsicKind,
97 kDot_IntrinsicKind,
98 kFaceforward_IntrinsicKind,
99 kFindLSB_IntrinsicKind,
100 kFindMSB_IntrinsicKind,
101 kInverse_IntrinsicKind,
102 kInversesqrt_IntrinsicKind,
103 kLength_IntrinsicKind,
104 kMatrixCompMult_IntrinsicKind,
105 kMod_IntrinsicKind,
106 kNormalize_IntrinsicKind,
107 kRadians_IntrinsicKind,
108 kReflect_IntrinsicKind,
109 kRefract_IntrinsicKind,
John Stiles23456532020-12-30 18:12:48 -0500110 kRoundEven_IntrinsicKind,
John Stilesd7199b22020-12-30 16:22:58 -0500111 kTexture_IntrinsicKind,
Timothy Lianga06f2152018-05-24 15:33:31 -0400112 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400113
John Stiles45990502021-02-16 10:55:27 -0500114 static const char* OperatorName(Operator op);
John Stilesd6449e92020-11-30 09:13:23 -0500115
John Stilescdcdb042020-07-06 09:03:51 -0400116 class GlobalStructVisitor;
117 void visitGlobalStruct(GlobalStructVisitor* visitor);
118
Timothy Lianga06f2152018-05-24 15:33:31 -0400119 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400120
Ethan Nicholascc305772017-10-13 16:17:45 -0400121 void write(const char* s);
122
123 void writeLine();
124
125 void writeLine(const char* s);
126
127 void write(const String& s);
128
129 void writeLine(const String& s);
130
John Stilese8b5a732021-03-12 13:25:52 -0500131 void finishLine();
132
Michael Ludwigbf58add2021-03-16 10:40:11 -0400133 void writeHeader();
134
Ethan Nicholascc305772017-10-13 16:17:45 -0400135 void writeUniformStruct();
136
137 void writeInputStruct();
138
139 void writeOutputStruct();
140
Timothy Liang7d637782018-06-05 09:58:07 -0400141 void writeInterfaceBlocks();
142
John Stilesdc75a972020-11-25 16:24:55 -0500143 void writeStructDefinitions();
144
Timothy Liangdc89f192018-06-13 09:20:31 -0400145 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
146 const InterfaceBlock* parentIntf = nullptr);
147
Timothy Liang7d637782018-06-05 09:58:07 -0400148 int size(const Type* type, bool isPacked) const;
149
150 int alignment(const Type* type, bool isPacked) const;
151
Timothy Liangee84fe12018-05-18 14:38:19 -0400152 void writeGlobalStruct();
John Stilesd7199b22020-12-30 16:22:58 -0500153
John Stilescdcdb042020-07-06 09:03:51 -0400154 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400155
Ethan Nicholascc305772017-10-13 16:17:45 -0400156 void writePrecisionModifier();
157
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500158 String typeName(const Type& type);
159
Brian Osman02bc5222021-01-28 11:00:20 -0500160 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500161
John Stilesb4418502021-02-04 10:57:08 -0500162 void writeType(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400163
164 void writeExtension(const Extension& ext);
165
166 void writeInterfaceBlock(const InterfaceBlock& intf);
167
168 void writeFunctionStart(const FunctionDeclaration& f);
169
John Stiles06b84ef2020-12-09 12:35:48 -0500170 void writeFunctionRequirementParams(const FunctionDeclaration& f,
171 const char*& separator);
172
173 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
174
John Stiles569249b2020-11-03 12:18:22 -0500175 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400176
177 void writeFunction(const FunctionDefinition& f);
178
John Stiles569249b2020-11-03 12:18:22 -0500179 void writeFunctionPrototype(const FunctionPrototype& f);
180
Ethan Nicholascc305772017-10-13 16:17:45 -0400181 void writeLayout(const Layout& layout);
182
183 void writeModifiers(const Modifiers& modifiers, bool globalContext);
184
Ethan Nicholascc305772017-10-13 16:17:45 -0400185 void writeVarInitializer(const Variable& var, const Expression& value);
186
Timothy Liang651286f2018-06-07 09:55:33 -0400187 void writeName(const String& name);
188
Brian Osmanc0213602020-10-06 14:43:32 -0400189 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400190
191 void writeFragCoord();
192
193 void writeVariableReference(const VariableReference& ref);
194
195 void writeExpression(const Expression& expr, Precedence parentPrecedence);
196
Ethan Nicholascc305772017-10-13 16:17:45 -0400197 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
198
John Stiles06b84ef2020-12-09 12:35:48 -0500199 String getOutParamHelper(const FunctionCall& c,
200 const ExpressionArray& arguments,
201 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400202
John Stilesd7199b22020-12-30 16:22:58 -0500203 String getInversePolyfill(const ExpressionArray& arguments);
John Stilesb21fac22020-12-04 15:36:49 -0500204
John Stilesf64e4072020-12-10 10:34:27 -0500205 String getBitcastIntrinsic(const Type& outType);
206
John Stiles86424eb2020-12-11 11:17:14 -0500207 String getTempVariable(const Type& varType);
208
John Stilesb21fac22020-12-04 15:36:49 -0500209 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500210
John Stiles8cad6372021-04-07 12:31:13 -0400211 bool matrixConstructHelperIsNeeded(const ConstructorCompound& c);
John Stiles5abb9e12021-04-06 13:47:19 -0400212 String getMatrixConstructHelper(const AnyConstructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400213 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles5abb9e12021-04-06 13:47:19 -0400214 void assembleMatrixFromExpressions(const AnyConstructor& ctor, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500215
Brian Osman93aed9a2020-12-28 15:18:46 -0500216 void writeMatrixCompMult();
John Stiles01cdf012021-02-10 09:53:41 -0500217
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500218 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
219
John Stiles01cdf012021-02-10 09:53:41 -0500220 void writeMatrixEqualityHelper(const Type& left, const Type& right);
221
222 void writeMatrixInequalityHelper(const Type& left, const Type& right);
223
John Stilesd7199b22020-12-30 16:22:58 -0500224 void writeArgumentList(const ExpressionArray& arguments);
225
John Stiles791c27d2020-12-30 14:56:57 -0500226 void writeSimpleIntrinsic(const FunctionCall& c);
227
John Stilesd7199b22020-12-30 16:22:58 -0500228 void writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind);
Timothy Liang6403b0e2018-05-17 10:40:04 -0400229
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500230 bool canCoerce(const Type& t1, const Type& t2);
231
John Stiles8cad6372021-04-07 12:31:13 -0400232 void writeConstructorCompound(const ConstructorCompound& c, Precedence parentPrecedence);
John Stiles2bec8ab2021-04-06 18:40:04 -0400233
John Stiles8cad6372021-04-07 12:31:13 -0400234 void writeConstructorCompoundMatrix(const ConstructorCompound& c, Precedence parentPrecedence);
John Stiles2bec8ab2021-04-06 18:40:04 -0400235
John Stiles5abb9e12021-04-06 13:47:19 -0400236 void writeConstructorMatrixResize(const ConstructorMatrixResize& c,
237 Precedence parentPrecedence);
238
John Stilesb14a8192021-04-05 11:40:46 -0400239 void writeAnyConstructor(const AnyConstructor& c,
240 const char* leftBracket,
241 const char* rightBracket,
242 Precedence parentPrecedence);
John Stiles7384b372021-04-01 13:48:15 -0400243
John Stilesb14a8192021-04-05 11:40:46 -0400244 void writeCastConstructor(const AnyConstructor& c,
245 const char* leftBracket,
246 const char* rightBracket,
247 Precedence parentPrecedence);
John Stilese1182782021-03-30 22:09:37 -0400248
Ethan Nicholascc305772017-10-13 16:17:45 -0400249 void writeFieldAccess(const FieldAccess& f);
250
251 void writeSwizzle(const Swizzle& swizzle);
252
Ethan Nicholascc305772017-10-13 16:17:45 -0400253 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
254
255 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
256
257 void writeIndexExpression(const IndexExpression& expr);
258
259 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
260
261 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
262
263 void writeBoolLiteral(const BoolLiteral& b);
264
265 void writeIntLiteral(const IntLiteral& i);
266
267 void writeFloatLiteral(const FloatLiteral& f);
268
269 void writeSetting(const Setting& s);
270
271 void writeStatement(const Statement& s);
272
John Stiles8f2a0cf2020-10-13 12:48:21 -0400273 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400274
275 void writeBlock(const Block& b);
276
277 void writeIfStatement(const IfStatement& stmt);
278
279 void writeForStatement(const ForStatement& f);
280
Ethan Nicholascc305772017-10-13 16:17:45 -0400281 void writeDoStatement(const DoStatement& d);
282
283 void writeSwitchStatement(const SwitchStatement& s);
284
John Stiles986c7fb2020-12-01 14:44:56 -0500285 void writeReturnStatementFromMain();
286
Ethan Nicholascc305772017-10-13 16:17:45 -0400287 void writeReturnStatement(const ReturnStatement& r);
288
289 void writeProgramElement(const ProgramElement& e);
290
291 Requirements requirements(const FunctionDeclaration& f);
292
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400293 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400294
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400295 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400296
John Stilesda5cdf62021-01-28 11:47:29 -0500297 int getUniformBinding(const Modifiers& m);
298
299 int getUniformSet(const Modifiers& m);
300
John Stilesd7199b22020-12-30 16:22:58 -0500301 std::unordered_map<String, IntrinsicKind> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400302 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400303 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
304 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
305 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400306 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400307 const char* fLineEnding;
308 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400309 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500310 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400311 int fVarCount = 0;
312 int fIndentation = 0;
313 bool fAtLineStart = false;
Chris Daltondba7aab2018-11-15 10:57:49 -0500314 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400315 // true if we have run into usages of dFdx / dFdy
316 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400317 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
318 bool fSetupFragPositionGlobal = false;
319 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400320 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400321 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400322 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500323 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500324 int fSwizzleHelperCount = 0;
325 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400326
John Stiles7571f9e2020-09-02 22:42:33 -0400327 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400328};
329
John Stilesa6841be2020-08-06 14:11:56 -0400330} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400331
332#endif