blob: 65216dc3aa23c70c0c96ea6f0b05dbd7e2749bd1 [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"
17#include "src/sksl/SkSLMemoryLayout.h"
18#include "src/sksl/SkSLStringStream.h"
19#include "src/sksl/ir/SkSLBinaryExpression.h"
20#include "src/sksl/ir/SkSLBoolLiteral.h"
21#include "src/sksl/ir/SkSLConstructor.h"
22#include "src/sksl/ir/SkSLDoStatement.h"
23#include "src/sksl/ir/SkSLExtension.h"
24#include "src/sksl/ir/SkSLFieldAccess.h"
25#include "src/sksl/ir/SkSLFloatLiteral.h"
26#include "src/sksl/ir/SkSLForStatement.h"
27#include "src/sksl/ir/SkSLFunctionCall.h"
28#include "src/sksl/ir/SkSLFunctionDeclaration.h"
29#include "src/sksl/ir/SkSLFunctionDefinition.h"
30#include "src/sksl/ir/SkSLIfStatement.h"
31#include "src/sksl/ir/SkSLIndexExpression.h"
32#include "src/sksl/ir/SkSLIntLiteral.h"
33#include "src/sksl/ir/SkSLInterfaceBlock.h"
34#include "src/sksl/ir/SkSLPostfixExpression.h"
35#include "src/sksl/ir/SkSLPrefixExpression.h"
36#include "src/sksl/ir/SkSLProgramElement.h"
37#include "src/sksl/ir/SkSLReturnStatement.h"
38#include "src/sksl/ir/SkSLSetting.h"
39#include "src/sksl/ir/SkSLStatement.h"
40#include "src/sksl/ir/SkSLSwitchStatement.h"
41#include "src/sksl/ir/SkSLSwizzle.h"
42#include "src/sksl/ir/SkSLTernaryExpression.h"
43#include "src/sksl/ir/SkSLVarDeclarations.h"
44#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
45#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 {
107 kTexture_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400108 kMod_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400109 };
110
Timothy Lianga06f2152018-05-24 15:33:31 -0400111 enum MetalIntrinsic {
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500112 kEqual_MetalIntrinsic,
113 kNotEqual_MetalIntrinsic,
Timothy Lianga06f2152018-05-24 15:33:31 -0400114 kLessThan_MetalIntrinsic,
115 kLessThanEqual_MetalIntrinsic,
116 kGreaterThan_MetalIntrinsic,
117 kGreaterThanEqual_MetalIntrinsic,
118 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400119
Timothy Lianga06f2152018-05-24 15:33:31 -0400120 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400121
Ethan Nicholascc305772017-10-13 16:17:45 -0400122 void write(const char* s);
123
124 void writeLine();
125
126 void writeLine(const char* s);
127
128 void write(const String& s);
129
130 void writeLine(const String& s);
131
132 void writeHeader();
133
134 void writeUniformStruct();
135
136 void writeInputStruct();
137
138 void writeOutputStruct();
139
Timothy Liang7d637782018-06-05 09:58:07 -0400140 void writeInterfaceBlocks();
141
Timothy Liangdc89f192018-06-13 09:20:31 -0400142 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
143 const InterfaceBlock* parentIntf = nullptr);
144
Timothy Liang7d637782018-06-05 09:58:07 -0400145 int size(const Type* type, bool isPacked) const;
146
147 int alignment(const Type* type, bool isPacked) const;
148
Timothy Liangee84fe12018-05-18 14:38:19 -0400149 void writeGlobalStruct();
150
Ethan Nicholascc305772017-10-13 16:17:45 -0400151 void writePrecisionModifier();
152
Ethan Nicholas45fa8102020-01-13 10:58:49 -0500153 String typeName(const Type& type);
154
Ethan Nicholascc305772017-10-13 16:17:45 -0400155 void writeType(const Type& type);
156
157 void writeExtension(const Extension& ext);
158
159 void writeInterfaceBlock(const InterfaceBlock& intf);
160
161 void writeFunctionStart(const FunctionDeclaration& f);
162
163 void writeFunctionDeclaration(const FunctionDeclaration& f);
164
165 void writeFunction(const FunctionDefinition& f);
166
167 void writeLayout(const Layout& layout);
168
169 void writeModifiers(const Modifiers& modifiers, bool globalContext);
170
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000171 void writeGlobalVars(const VarDeclaration& vs);
172
Ethan Nicholascc305772017-10-13 16:17:45 -0400173 void writeVarInitializer(const Variable& var, const Expression& value);
174
Timothy Liang651286f2018-06-07 09:55:33 -0400175 void writeName(const String& name);
176
Ethan Nicholascc305772017-10-13 16:17:45 -0400177 void writeVarDeclarations(const VarDeclarations& decl, bool global);
178
179 void writeFragCoord();
180
181 void writeVariableReference(const VariableReference& ref);
182
183 void writeExpression(const Expression& expr, Precedence parentPrecedence);
184
185 void writeIntrinsicCall(const FunctionCall& c);
186
187 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
188
189 void writeFunctionCall(const FunctionCall& c);
190
Chris Daltondba7aab2018-11-15 10:57:49 -0500191 void writeInverseHack(const Expression& mat);
192
John Stiles1bdafbf2020-05-28 12:17:20 -0400193 bool matrixConstructHelperIsNeeded(const Constructor& c);
194 String getMatrixConstructHelper(const Constructor& c);
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500195
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500196 void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
197
Timothy Liang6403b0e2018-05-17 10:40:04 -0400198 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
199
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500200 bool canCoerce(const Type& t1, const Type& t2);
201
202 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
Ethan Nicholascc305772017-10-13 16:17:45 -0400203
204 void writeFieldAccess(const FieldAccess& f);
205
206 void writeSwizzle(const Swizzle& swizzle);
207
208 static Precedence GetBinaryPrecedence(Token::Kind op);
209
210 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
211
212 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
213
214 void writeIndexExpression(const IndexExpression& expr);
215
216 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
217
218 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
219
220 void writeBoolLiteral(const BoolLiteral& b);
221
222 void writeIntLiteral(const IntLiteral& i);
223
224 void writeFloatLiteral(const FloatLiteral& f);
225
226 void writeSetting(const Setting& s);
227
228 void writeStatement(const Statement& s);
229
230 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
231
232 void writeBlock(const Block& b);
233
234 void writeIfStatement(const IfStatement& stmt);
235
236 void writeForStatement(const ForStatement& f);
237
238 void writeWhileStatement(const WhileStatement& w);
239
240 void writeDoStatement(const DoStatement& d);
241
242 void writeSwitchStatement(const SwitchStatement& s);
243
244 void writeReturnStatement(const ReturnStatement& r);
245
246 void writeProgramElement(const ProgramElement& e);
247
248 Requirements requirements(const FunctionDeclaration& f);
249
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400250 Requirements requirements(const Expression* e);
Ethan Nicholascc305772017-10-13 16:17:45 -0400251
Ethan Nicholasff350cb2020-05-14 14:05:13 -0400252 Requirements requirements(const Statement* s);
Ethan Nicholascc305772017-10-13 16:17:45 -0400253
Timothy Liang7d637782018-06-05 09:58:07 -0400254 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400255 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400256 std::unordered_set<String> fReservedWords;
Timothy Liangee84fe12018-05-18 14:38:19 -0400257 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
Timothy Lianga06f2152018-05-24 15:33:31 -0400258 std::vector<const Variable*> fTextures;
259 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
260 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
261 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400262 int fPaddingCount = 0;
Timothy Liangee84fe12018-05-18 14:38:19 -0400263 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400264 const char* fLineEnding;
265 const Context& fContext;
266 StringStream fHeader;
267 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500268 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400269 Program::Kind fProgramKind;
270 int fVarCount = 0;
271 int fIndentation = 0;
272 bool fAtLineStart = false;
273 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
274 // more than one or two structs per shader, a simple linear search will be faster than anything
275 // fancier.
276 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500277 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400278 // true if we have run into usages of dFdx / dFdy
279 bool fFoundDerivatives = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400280 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
281 bool fSetupFragPositionGlobal = false;
282 bool fSetupFragPositionLocal = false;
John Stiles1bdafbf2020-05-28 12:17:20 -0400283 std::unordered_set<String> fHelpers;
Ethan Nicholascc305772017-10-13 16:17:45 -0400284 int fUniformBuffer = -1;
Ethan Nicholasf931e402019-07-26 15:40:33 -0400285 String fRTHeightName;
Ethan Nicholascc305772017-10-13 16:17:45 -0400286
287 typedef CodeGenerator INHERITED;
288};
289
290}
291
292#endif