blob: 7fde7c45bd4a04e4a91e195dc396fe11500df32a [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"
46#include "src/sksl/ir/SkSLWhileStatement.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040047
48namespace SkSL {
49
50#define kLast_Capability SpvCapabilityMultiViewport
51
52/**
53 * Converts a Program into Metal code.
54 */
55class MetalCodeGenerator : public CodeGenerator {
56public:
Timothy Lianga06f2152018-05-24 15:33:31 -040057 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040058 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040059
Ethan Nicholascc305772017-10-13 16:17:45 -040060 enum Precedence {
61 kParentheses_Precedence = 1,
62 kPostfix_Precedence = 2,
63 kPrefix_Precedence = 3,
64 kMultiplicative_Precedence = 4,
65 kAdditive_Precedence = 5,
66 kShift_Precedence = 6,
67 kRelational_Precedence = 7,
68 kEquality_Precedence = 8,
69 kBitwiseAnd_Precedence = 9,
70 kBitwiseXor_Precedence = 10,
71 kBitwiseOr_Precedence = 11,
72 kLogicalAnd_Precedence = 12,
73 kLogicalXor_Precedence = 13,
74 kLogicalOr_Precedence = 14,
75 kTernary_Precedence = 15,
76 kAssignment_Precedence = 16,
77 kSequence_Precedence = 17,
78 kTopLevel_Precedence = kSequence_Precedence
79 };
80
81 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -040082 OutputStream* out)
Ethan Nicholascc305772017-10-13 16:17:45 -040083 : INHERITED(program, errors, out)
Timothy Liangdc89f192018-06-13 09:20:31 -040084 , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy", "vertex", "fragment"})
Ethan Nicholascc305772017-10-13 16:17:45 -040085 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040086 , fContext(*context) {
87 this->setupIntrinsics();
88 }
Ethan Nicholascc305772017-10-13 16:17:45 -040089
90 bool generateCode() override;
91
92protected:
93 typedef int Requirements;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -040094 static constexpr Requirements kNo_Requirements = 0;
95 static constexpr Requirements kInputs_Requirement = 1 << 0;
96 static constexpr Requirements kOutputs_Requirement = 1 << 1;
97 static constexpr Requirements kUniforms_Requirement = 1 << 2;
98 static constexpr Requirements kGlobals_Requirement = 1 << 3;
99 static constexpr Requirements kFragCoord_Requirement = 1 << 4;
Ethan Nicholascc305772017-10-13 16:17:45 -0400100
Timothy Liang6403b0e2018-05-17 10:40:04 -0400101 enum IntrinsicKind {
Timothy Lianga06f2152018-05-24 15:33:31 -0400102 kSpecial_IntrinsicKind,
103 kMetal_IntrinsicKind,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400104 };
105
106 enum SpecialIntrinsic {
Brian Osman46787d52020-11-24 14:18:23 -0500107 kDistance_SpecialIntrinsic,
108 kDot_SpecialIntrinsic,
109 kLength_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400110 kMod_SpecialIntrinsic,
Brian Osman46787d52020-11-24 14:18:23 -0500111 kNormalize_SpecialIntrinsic,
112 kTexture_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400113 };
114
Timothy Lianga06f2152018-05-24 15:33:31 -0400115 enum MetalIntrinsic {
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500116 kEqual_MetalIntrinsic,
117 kNotEqual_MetalIntrinsic,
Timothy Lianga06f2152018-05-24 15:33:31 -0400118 kLessThan_MetalIntrinsic,
119 kLessThanEqual_MetalIntrinsic,
120 kGreaterThan_MetalIntrinsic,
121 kGreaterThanEqual_MetalIntrinsic,
122 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400123
John Stilesd6449e92020-11-30 09:13:23 -0500124 static const char* OperatorName(Token::Kind op);
125
John Stilescdcdb042020-07-06 09:03:51 -0400126 class GlobalStructVisitor;
127 void visitGlobalStruct(GlobalStructVisitor* visitor);
128
Timothy Lianga06f2152018-05-24 15:33:31 -0400129 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400130
Ethan Nicholascc305772017-10-13 16:17:45 -0400131 void write(const char* s);
132
133 void writeLine();
134
135 void writeLine(const char* s);
136
137 void write(const String& s);
138
139 void writeLine(const String& s);
140
141 void writeHeader();
142
143 void writeUniformStruct();
144
145 void writeInputStruct();
146
147 void writeOutputStruct();
148
Timothy Liang7d637782018-06-05 09:58:07 -0400149 void writeInterfaceBlocks();
150
John Stilesdc75a972020-11-25 16:24:55 -0500151 void writeStructDefinitions();
152
Timothy Liangdc89f192018-06-13 09:20:31 -0400153 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
154 const InterfaceBlock* parentIntf = nullptr);
155
Timothy Liang7d637782018-06-05 09:58:07 -0400156 int size(const Type* type, bool isPacked) const;
157
158 int alignment(const Type* type, bool isPacked) const;
159
Timothy Liangee84fe12018-05-18 14:38:19 -0400160 void writeGlobalStruct();
John Stilescdcdb042020-07-06 09:03:51 -0400161 void writeGlobalInit();
Timothy Liangee84fe12018-05-18 14:38:19 -0400162
Ethan Nicholascc305772017-10-13 16:17:45 -0400163 void writePrecisionModifier();
164
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500165 String typeName(const Type& type);
166
John Stilesdc75a972020-11-25 16:24:55 -0500167 bool writeStructDefinition(const Type& type);
168
John Stiles3dba3ee2020-12-02 23:35:49 -0500169 void disallowArrayTypes(const Type& type);
170
171 void writeBaseType(const Type& type);
172
173 void writeArrayDimensions(const Type& type);
Ethan Nicholascc305772017-10-13 16:17:45 -0400174
175 void writeExtension(const Extension& ext);
176
177 void writeInterfaceBlock(const InterfaceBlock& intf);
178
179 void writeFunctionStart(const FunctionDeclaration& f);
180
John Stiles06b84ef2020-12-09 12:35:48 -0500181 void writeFunctionRequirementParams(const FunctionDeclaration& f,
182 const char*& separator);
183
184 void writeFunctionRequirementArgs(const FunctionDeclaration& f, const char*& separator);
185
John Stiles569249b2020-11-03 12:18:22 -0500186 bool writeFunctionDeclaration(const FunctionDeclaration& f);
Ethan Nicholascc305772017-10-13 16:17:45 -0400187
188 void writeFunction(const FunctionDefinition& f);
189
John Stiles569249b2020-11-03 12:18:22 -0500190 void writeFunctionPrototype(const FunctionPrototype& f);
191
Ethan Nicholascc305772017-10-13 16:17:45 -0400192 void writeLayout(const Layout& layout);
193
194 void writeModifiers(const Modifiers& modifiers, bool globalContext);
195
Ethan Nicholascc305772017-10-13 16:17:45 -0400196 void writeVarInitializer(const Variable& var, const Expression& value);
197
Timothy Liang651286f2018-06-07 09:55:33 -0400198 void writeName(const String& name);
199
Brian Osmanc0213602020-10-06 14:43:32 -0400200 void writeVarDeclaration(const VarDeclaration& decl, bool global);
Ethan Nicholascc305772017-10-13 16:17:45 -0400201
202 void writeFragCoord();
203
204 void writeVariableReference(const VariableReference& ref);
205
206 void writeExpression(const Expression& expr, Precedence parentPrecedence);
207
208 void writeIntrinsicCall(const FunctionCall& c);
209
210 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
211
John Stiles06b84ef2020-12-09 12:35:48 -0500212 String getOutParamHelper(const FunctionCall& c,
213 const ExpressionArray& arguments,
214 const SkTArray<VariableReference*>& outVars);
Ethan Nicholascc305772017-10-13 16:17:45 -0400215
John Stilesb21fac22020-12-04 15:36:49 -0500216 String getInverseHack(const Expression& mat);
217
John Stilesf64e4072020-12-10 10:34:27 -0500218 String getBitcastIntrinsic(const Type& outType);
219
John Stilesb21fac22020-12-04 15:36:49 -0500220 void writeFunctionCall(const FunctionCall& c);
Chris Daltondba7aab2018-11-15 10:57:49 -0500221
John Stiles1bdafbf2020-05-28 12:17:20 -0400222 bool matrixConstructHelperIsNeeded(const Constructor& c);
223 String getMatrixConstructHelper(const Constructor& c);
John Stilesfcf8cb22020-08-06 14:29:22 -0400224 void assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns);
John Stiles8e3b6be2020-10-13 11:14:08 -0400225 void assembleMatrixFromExpressions(const ExpressionArray& args, int rows, int columns);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500226
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500227 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
228
Timothy Liang6403b0e2018-05-17 10:40:04 -0400229 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
230
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500231 bool canCoerce(const Type& t1, const Type& t2);
232
233 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400234
235 void writeFieldAccess(const FieldAccess& f);
236
237 void writeSwizzle(const Swizzle& swizzle);
238
239 static Precedence GetBinaryPrecedence(Token::Kind op);
240
241 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
242
243 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
244
245 void writeIndexExpression(const IndexExpression& expr);
246
247 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
248
249 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
250
251 void writeBoolLiteral(const BoolLiteral& b);
252
253 void writeIntLiteral(const IntLiteral& i);
254
255 void writeFloatLiteral(const FloatLiteral& f);
256
257 void writeSetting(const Setting& s);
258
259 void writeStatement(const Statement& s);
260
John Stiles8f2a0cf2020-10-13 12:48:21 -0400261 void writeStatements(const StatementArray& statements);
Ethan Nicholascc305772017-10-13 16:17:45 -0400262
263 void writeBlock(const Block& b);
264
265 void writeIfStatement(const IfStatement& stmt);
266
267 void writeForStatement(const ForStatement& f);
268
269 void writeWhileStatement(const WhileStatement& w);
270
271 void writeDoStatement(const DoStatement& d);
272
273 void writeSwitchStatement(const SwitchStatement& s);
274
John Stiles986c7fb2020-12-01 14:44:56 -0500275 void writeReturnStatementFromMain();
276
Ethan Nicholascc305772017-10-13 16:17:45 -0400277 void writeReturnStatement(const ReturnStatement& r);
278
279 void writeProgramElement(const ProgramElement& e);
280
281 Requirements requirements(const FunctionDeclaration& f);
282
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400283 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400284
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400285 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400286
Timothy Liang7d637782018-06-05 09:58:07 -0400287 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400288 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400289 std::unordered_set<String> fReservedWords;
Timothy Lianga06f2152018-05-24 15:33:31 -0400290 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
291 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
292 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400293 int fPaddingCount = 0;
Ethan Nicholascc305772017-10-13 16:17:45 -0400294 const char* fLineEnding;
295 const Context& fContext;
Ethan Nicholascc305772017-10-13 16:17:45 -0400296 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500297 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400298 Program::Kind fProgramKind;
299 int fVarCount = 0;
300 int fIndentation = 0;
301 bool fAtLineStart = false;
302 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
303 // more than one or two structs per shader, a simple linear search will be faster than anything
304 // fancier.
305 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500306 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400307 // true if we have run into usages of dFdx / dFdy
308 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400309 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
310 bool fSetupFragPositionGlobal = false;
311 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400312 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400313 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400314 String fRTHeightName;
John Stiles986c7fb2020-12-01 14:44:56 -0500315 const FunctionDeclaration* fCurrentFunction = nullptr;
John Stiles06b84ef2020-12-09 12:35:48 -0500316 int fSwizzleHelperCount = 0;
317 bool fIgnoreVariableReferenceModifiers = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400318
John Stiles7571f9e2020-09-02 22:42:33 -0400319 using INHERITED = CodeGenerator;
Ethan Nicholascc305772017-10-13 16:17:45 -0400320};
321
John Stilesa6841be2020-08-06 14:11:56 -0400322} // namespace SkSL
Ethan Nicholascc305772017-10-13 16:17:45 -0400323
324#endif