blob: 596ef9ea40f55d6e95d4be640cc90f401638b38f [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
11#include <stack>
12#include <tuple>
13#include <unordered_map>
John Stiles1bdafbf2020-05-28 12:17:20 -040014#include <unordered_set>
Ethan Nicholascc305772017-10-13 16:17:45 -040015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/sksl/SkSLCodeGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/sksl/SkSLStringStream.h"
18#include "src/sksl/ir/SkSLBinaryExpression.h"
19#include "src/sksl/ir/SkSLBoolLiteral.h"
20#include "src/sksl/ir/SkSLConstructor.h"
21#include "src/sksl/ir/SkSLDoStatement.h"
22#include "src/sksl/ir/SkSLExtension.h"
23#include "src/sksl/ir/SkSLFieldAccess.h"
24#include "src/sksl/ir/SkSLFloatLiteral.h"
25#include "src/sksl/ir/SkSLForStatement.h"
26#include "src/sksl/ir/SkSLFunctionCall.h"
27#include "src/sksl/ir/SkSLFunctionDeclaration.h"
28#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050029#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/sksl/ir/SkSLIfStatement.h"
31#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040032#include "src/sksl/ir/SkSLInlineMarker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLIntLiteral.h"
34#include "src/sksl/ir/SkSLInterfaceBlock.h"
35#include "src/sksl/ir/SkSLPostfixExpression.h"
36#include "src/sksl/ir/SkSLPrefixExpression.h"
37#include "src/sksl/ir/SkSLProgramElement.h"
38#include "src/sksl/ir/SkSLReturnStatement.h"
39#include "src/sksl/ir/SkSLSetting.h"
40#include "src/sksl/ir/SkSLStatement.h"
41#include "src/sksl/ir/SkSLSwitchStatement.h"
42#include "src/sksl/ir/SkSLSwizzle.h"
43#include "src/sksl/ir/SkSLTernaryExpression.h"
44#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040046
47namespace SkSL {
48
49#define kLast_Capability SpvCapabilityMultiViewport
50
51/**
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 enum Precedence {
60 kParentheses_Precedence = 1,
61 kPostfix_Precedence = 2,
62 kPrefix_Precedence = 3,
63 kMultiplicative_Precedence = 4,
64 kAdditive_Precedence = 5,
65 kShift_Precedence = 6,
66 kRelational_Precedence = 7,
67 kEquality_Precedence = 8,
68 kBitwiseAnd_Precedence = 9,
69 kBitwiseXor_Precedence = 10,
70 kBitwiseOr_Precedence = 11,
71 kLogicalAnd_Precedence = 12,
72 kLogicalXor_Precedence = 13,
73 kLogicalOr_Precedence = 14,
74 kTernary_Precedence = 15,
75 kAssignment_Precedence = 16,
76 kSequence_Precedence = 17,
77 kTopLevel_Precedence = kSequence_Precedence
78 };
79
80 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040081 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040082 : INHERITED(program, errors, out)
Timothy Liangdc89f192018-06-13 09:20:31 -040083 , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040084 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040085 , fContext(*context) {
86 this->setupIntrinsics();
87 }
Ethan Nicholascc305772017-10-13 16:17:45 -040088
89 bool generateCode() override;
90
91protected:
92 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040093 static constexpr Requirements kNo_Requirements = 0;
94 static constexpr Requirements kInputs_Requirement = 1 << 0;
95 static constexpr Requirements kOutputs_Requirement = 1 << 1;
96 static constexpr Requirements kUniforms_Requirement = 1 << 2;
97 static constexpr Requirements kGlobals_Requirement = 1 << 3;
98 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -040099
Timothy Liang6403b0e2018-05-17 10:40:04 -0400100 enum IntrinsicKind {
Timothy Lianga06f2152018-05-24 15:33:31 -0400101 kSpecial_IntrinsicKind,
102 kMetal_IntrinsicKind,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400103 };
104
105 enum SpecialIntrinsic {
John Stiles0063a9f2020-12-10 18:01:45 -0500106 kBitcast_SpecialIntrinsic,
John Stilese7dc7cb2020-12-22 15:37:14 -0500107 kBitCount_SpecialIntrinsic,
John Stilese2d34f82020-12-10 18:02:02 -0500108 kDegrees_SpecialIntrinsic,
Brian Osman46787d52020-11-24 14:18:23 -0500109 kDistance_SpecialIntrinsic,
110 kDot_SpecialIntrinsic,
John Stilesad0571f2020-12-10 18:03:10 -0500111 kFaceforward_SpecialIntrinsic,
John Stiles86424eb2020-12-11 11:17:14 -0500112 kFindLSB_SpecialIntrinsic,
John Stiles9685e522020-12-14 11:52:14 -0500113 kFindMSB_SpecialIntrinsic,
Brian Osman46787d52020-11-24 14:18:23 -0500114 kLength_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400115 kMod_SpecialIntrinsic,
Brian Osman46787d52020-11-24 14:18:23 -0500116 kNormalize_SpecialIntrinsic,
John Stilese2d34f82020-12-10 18:02:02 -0500117 kRadians_SpecialIntrinsic,
John Stiles791c27d2020-12-30 14:56:57 -0500118 kReflect_SpecialIntrinsic,
John Stiles4b783d62020-12-30 14:58:07 -0500119 kRefract_SpecialIntrinsic,
Brian Osman46787d52020-11-24 14:18:23 -0500120 kTexture_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400121 };
122
Timothy Lianga06f2152018-05-24 15:33:31 -0400123 enum MetalIntrinsic {
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500124 kEqual_MetalIntrinsic,
125 kNotEqual_MetalIntrinsic,
Timothy Lianga06f2152018-05-24 15:33:31 -0400126 kLessThan_MetalIntrinsic,
127 kLessThanEqual_MetalIntrinsic,
128 kGreaterThan_MetalIntrinsic,
129 kGreaterThanEqual_MetalIntrinsic,
130 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400131
John Stilesd6449e92020-11-30 09:13:23 -0500132 static const char* OperatorName(Token::Kind op);
133
John Stilescdcdb042020-07-06 09:03:51 -0400134 class GlobalStructVisitor;
135 void visitGlobalStruct(GlobalStructVisitor* visitor);
136
Timothy Lianga06f2152018-05-24 15:33:31 -0400137 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400138
Ethan Nicholascc305772017-10-13 16:17:45 -0400139 void write(const char* s);
140
141 void writeLine();
142
143 void writeLine(const char* s);
144
145 void write(const String& s);
146
147 void writeLine(const String& s);
148
149 void writeHeader();
150
151 void writeUniformStruct();
152
153 void writeInputStruct();
154
155 void writeOutputStruct();
156
Timothy Liang7d637782018-06-05 09:58:07 -0400157 void writeInterfaceBlocks();
158
John Stilesdc75a972020-11-25 16:24:55 -0500159 void writeStructDefinitions();
160
Timothy Liangdc89f192018-06-13 09:20:31 -0400161 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
162 const InterfaceBlock* parentIntf = nullptr);
163
Timothy Liang7d637782018-06-05 09:58:07 -0400164 int size(const Type* type, bool isPacked) const;
165
166 int alignment(const Type* type, bool isPacked) const;
167
Timothy Liangee84fe12018-05-18 14:38:19 -0400168 void writeGlobalStruct();
John Stilescdcdb042020-07-06 09:03:51 -0400169 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400170
Ethan Nicholascc305772017-10-13 16:17:45 -0400171 void writePrecisionModifier();
172
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500173 String typeName(const Type& type);
174
John Stilesdc75a972020-11-25 16:24:55 -0500175 bool writeStructDefinition(const Type& type);
176
John Stilesea166702020-12-11 17:30:47 -0500177 void disallowArrayTypes(const Type& type, int offset);
John Stiles3dba3ee2020-12-02 23:35:49 -0500178
179 void writeBaseType(const Type& type);
180
181 void writeArrayDimensions(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400182
183 void writeExtension(const Extension& ext);
184
185 void writeInterfaceBlock(const InterfaceBlock& intf);
186
187 void writeFunctionStart(const FunctionDeclaration& f);
188
John Stiles06b84ef2020-12-09 12:35:48 -0500189 void writeFunctionRequirementParams(const FunctionDeclaration& f,
190 const char*& separator);
191
192 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
193
John Stiles569249b2020-11-03 12:18:22 -0500194 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400195
196 void writeFunction(const FunctionDefinition& f);
197
John Stiles569249b2020-11-03 12:18:22 -0500198 void writeFunctionPrototype(const FunctionPrototype& f);
199
Ethan Nicholascc305772017-10-13 16:17:45 -0400200 void writeLayout(const Layout& layout);
201
202 void writeModifiers(const Modifiers& modifiers, bool globalContext);
203
Ethan Nicholascc305772017-10-13 16:17:45 -0400204 void writeVarInitializer(const Variable& var, const Expression& value);
205
Timothy Liang651286f2018-06-07 09:55:33 -0400206 void writeName(const String& name);
207
Brian Osmanc0213602020-10-06 14:43:32 -0400208 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400209
210 void writeFragCoord();
211
212 void writeVariableReference(const VariableReference& ref);
213
214 void writeExpression(const Expression& expr, Precedence parentPrecedence);
215
216 void writeIntrinsicCall(const FunctionCall& c);
217
218 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
219
John Stiles06b84ef2020-12-09 12:35:48 -0500220 String getOutParamHelper(const FunctionCall& c,
221 const ExpressionArray& arguments,
222 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400223
John Stilesb21fac22020-12-04 15:36:49 -0500224 String getInverseHack(const Expression& mat);
225
John Stilesf64e4072020-12-10 10:34:27 -0500226 String getBitcastIntrinsic(const Type& outType);
227
John Stiles86424eb2020-12-11 11:17:14 -0500228 String getTempVariable(const Type& varType);
229
John Stilesb21fac22020-12-04 15:36:49 -0500230 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500231
John Stiles1bdafbf2020-05-28 12:17:20 -0400232 bool matrixConstructHelperIsNeeded(const Constructor& c);
233 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400234 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400235 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500236
Brian Osman93aed9a2020-12-28 15:18:46 -0500237 void writeMatrixCompMult();
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500238 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
239
John Stiles791c27d2020-12-30 14:56:57 -0500240 void writeSimpleIntrinsic(const FunctionCall& c);
241
Timothy Liang6403b0e2018-05-17 10:40:04 -0400242 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
243
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500244 bool canCoerce(const Type& t1, const Type& t2);
245
246 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400247
248 void writeFieldAccess(const FieldAccess& f);
249
250 void writeSwizzle(const Swizzle& swizzle);
251
252 static Precedence GetBinaryPrecedence(Token::Kind op);
253
254 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
255
256 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
257
258 void writeIndexExpression(const IndexExpression& expr);
259
260 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
261
262 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
263
264 void writeBoolLiteral(const BoolLiteral& b);
265
266 void writeIntLiteral(const IntLiteral& i);
267
268 void writeFloatLiteral(const FloatLiteral& f);
269
270 void writeSetting(const Setting& s);
271
272 void writeStatement(const Statement& s);
273
John Stiles8f2a0cf2020-10-13 12:48:21 -0400274 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400275
276 void writeBlock(const Block& b);
277
278 void writeIfStatement(const IfStatement& stmt);
279
280 void writeForStatement(const ForStatement& f);
281
Ethan Nicholascc305772017-10-13 16:17:45 -0400282 void writeDoStatement(const DoStatement& d);
283
284 void writeSwitchStatement(const SwitchStatement& s);
285
John Stiles986c7fb2020-12-01 14:44:56 -0500286 void writeReturnStatementFromMain();
287
Ethan Nicholascc305772017-10-13 16:17:45 -0400288 void writeReturnStatement(const ReturnStatement& r);
289
290 void writeProgramElement(const ProgramElement& e);
291
292 Requirements requirements(const FunctionDeclaration& f);
293
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400294 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400295
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400296 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400297
Timothy Liang7d637782018-06-05 09:58:07 -0400298 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400299 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400300 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400301 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
302 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
303 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400304 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400305 const char* fLineEnding;
306 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400307 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500308 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400309 Program::Kind fProgramKind;
310 int fVarCount = 0;
311 int fIndentation = 0;
312 bool fAtLineStart = false;
313 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
314 // more than one or two structs per shader, a simple linear search will be faster than anything
315 // fancier.
316 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500317 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400318 // true if we have run into usages of dFdx / dFdy
319 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400320 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
321 bool fSetupFragPositionGlobal = false;
322 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400323 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400324 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400325 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500326 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500327 int fSwizzleHelperCount = 0;
328 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400329
John Stiles7571f9e2020-09-02 22:42:33 -0400330 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400331};
332
John Stilesa6841be2020-08-06 14:11:56 -0400333} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400334
335#endif