blob: 1a3a790a63163b81b4efc7ad762130a3a63c70e1 [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>
14
15#include "SkSLCodeGenerator.h"
Timothy Liang651286f2018-06-07 09:55:33 -040016#include "SkSLMemoryLayout.h"
Ethan Nicholascc305772017-10-13 16:17:45 -040017#include "SkSLStringStream.h"
18#include "ir/SkSLBinaryExpression.h"
19#include "ir/SkSLBoolLiteral.h"
20#include "ir/SkSLConstructor.h"
21#include "ir/SkSLDoStatement.h"
22#include "ir/SkSLExtension.h"
23#include "ir/SkSLFloatLiteral.h"
24#include "ir/SkSLIfStatement.h"
25#include "ir/SkSLIndexExpression.h"
26#include "ir/SkSLInterfaceBlock.h"
27#include "ir/SkSLIntLiteral.h"
28#include "ir/SkSLFieldAccess.h"
29#include "ir/SkSLForStatement.h"
30#include "ir/SkSLFunctionCall.h"
31#include "ir/SkSLFunctionDeclaration.h"
32#include "ir/SkSLFunctionDefinition.h"
33#include "ir/SkSLPrefixExpression.h"
34#include "ir/SkSLPostfixExpression.h"
35#include "ir/SkSLProgramElement.h"
36#include "ir/SkSLReturnStatement.h"
37#include "ir/SkSLSetting.h"
38#include "ir/SkSLStatement.h"
39#include "ir/SkSLSwitchStatement.h"
40#include "ir/SkSLSwizzle.h"
41#include "ir/SkSLTernaryExpression.h"
42#include "ir/SkSLVarDeclarations.h"
43#include "ir/SkSLVarDeclarationsStatement.h"
44#include "ir/SkSLVariableReference.h"
45#include "ir/SkSLWhileStatement.h"
46
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,
81 OutputStream* out)
82 : 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;
93 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;
Timothy Liangee84fe12018-05-18 14:38:19 -040097 static constexpr Requirements kGlobals_Requirement = 1 << 3;
Ethan Nicholascc305772017-10-13 16:17:45 -040098
Timothy Liang6403b0e2018-05-17 10:40:04 -040099 enum IntrinsicKind {
Timothy Lianga06f2152018-05-24 15:33:31 -0400100 kSpecial_IntrinsicKind,
101 kMetal_IntrinsicKind,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400102 };
103
104 enum SpecialIntrinsic {
105 kTexture_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400106 kMod_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400107 };
108
Timothy Lianga06f2152018-05-24 15:33:31 -0400109 enum MetalIntrinsic {
110 kLessThan_MetalIntrinsic,
111 kLessThanEqual_MetalIntrinsic,
112 kGreaterThan_MetalIntrinsic,
113 kGreaterThanEqual_MetalIntrinsic,
114 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400115
Timothy Lianga06f2152018-05-24 15:33:31 -0400116 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400117
Ethan Nicholascc305772017-10-13 16:17:45 -0400118 void write(const char* s);
119
120 void writeLine();
121
122 void writeLine(const char* s);
123
124 void write(const String& s);
125
126 void writeLine(const String& s);
127
128 void writeHeader();
129
130 void writeUniformStruct();
131
132 void writeInputStruct();
133
134 void writeOutputStruct();
135
Timothy Liang7d637782018-06-05 09:58:07 -0400136 void writeInterfaceBlocks();
137
Timothy Liangdc89f192018-06-13 09:20:31 -0400138 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
139 const InterfaceBlock* parentIntf = nullptr);
140
Timothy Liang7d637782018-06-05 09:58:07 -0400141 int size(const Type* type, bool isPacked) const;
142
143 int alignment(const Type* type, bool isPacked) const;
144
Timothy Liangee84fe12018-05-18 14:38:19 -0400145 void writeGlobalStruct();
146
Ethan Nicholascc305772017-10-13 16:17:45 -0400147 void writePrecisionModifier();
148
149 void writeType(const Type& type);
150
151 void writeExtension(const Extension& ext);
152
153 void writeInterfaceBlock(const InterfaceBlock& intf);
154
155 void writeFunctionStart(const FunctionDeclaration& f);
156
157 void writeFunctionDeclaration(const FunctionDeclaration& f);
158
159 void writeFunction(const FunctionDefinition& f);
160
161 void writeLayout(const Layout& layout);
162
163 void writeModifiers(const Modifiers& modifiers, bool globalContext);
164
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000165 void writeGlobalVars(const VarDeclaration& vs);
166
Ethan Nicholascc305772017-10-13 16:17:45 -0400167 void writeVarInitializer(const Variable& var, const Expression& value);
168
Timothy Liang651286f2018-06-07 09:55:33 -0400169 void writeName(const String& name);
170
Ethan Nicholascc305772017-10-13 16:17:45 -0400171 void writeVarDeclarations(const VarDeclarations& decl, bool global);
172
173 void writeFragCoord();
174
175 void writeVariableReference(const VariableReference& ref);
176
177 void writeExpression(const Expression& expr, Precedence parentPrecedence);
178
179 void writeIntrinsicCall(const FunctionCall& c);
180
181 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
182
183 void writeFunctionCall(const FunctionCall& c);
184
Chris Daltondba7aab2018-11-15 10:57:49 -0500185 void writeInverseHack(const Expression& mat);
186
Timothy Liang6403b0e2018-05-17 10:40:04 -0400187 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
188
Ethan Nicholascc305772017-10-13 16:17:45 -0400189 void writeConstructor(const Constructor& c);
190
191 void writeFieldAccess(const FieldAccess& f);
192
193 void writeSwizzle(const Swizzle& swizzle);
194
195 static Precedence GetBinaryPrecedence(Token::Kind op);
196
197 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
198
199 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
200
201 void writeIndexExpression(const IndexExpression& expr);
202
203 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
204
205 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
206
207 void writeBoolLiteral(const BoolLiteral& b);
208
209 void writeIntLiteral(const IntLiteral& i);
210
211 void writeFloatLiteral(const FloatLiteral& f);
212
213 void writeSetting(const Setting& s);
214
215 void writeStatement(const Statement& s);
216
217 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
218
219 void writeBlock(const Block& b);
220
221 void writeIfStatement(const IfStatement& stmt);
222
223 void writeForStatement(const ForStatement& f);
224
225 void writeWhileStatement(const WhileStatement& w);
226
227 void writeDoStatement(const DoStatement& d);
228
229 void writeSwitchStatement(const SwitchStatement& s);
230
231 void writeReturnStatement(const ReturnStatement& r);
232
233 void writeProgramElement(const ProgramElement& e);
234
235 Requirements requirements(const FunctionDeclaration& f);
236
237 Requirements requirements(const Expression& e);
238
239 Requirements requirements(const Statement& e);
240
Timothy Liang7d637782018-06-05 09:58:07 -0400241 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400242 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400243 std::unordered_set<String> fReservedWords;
Timothy Liangee84fe12018-05-18 14:38:19 -0400244 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
Timothy Lianga06f2152018-05-24 15:33:31 -0400245 std::vector<const Variable*> fTextures;
246 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
247 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
248 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400249 int fPaddingCount = 0;
Timothy Liangee84fe12018-05-18 14:38:19 -0400250 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400251 const char* fLineEnding;
252 const Context& fContext;
253 StringStream fHeader;
254 String fFunctionHeader;
Chris Daltondba7aab2018-11-15 10:57:49 -0500255 StringStream fExtraFunctions;
Ethan Nicholascc305772017-10-13 16:17:45 -0400256 Program::Kind fProgramKind;
257 int fVarCount = 0;
258 int fIndentation = 0;
259 bool fAtLineStart = false;
260 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
261 // more than one or two structs per shader, a simple linear search will be faster than anything
262 // fancier.
263 std::vector<const Type*> fWrittenStructs;
Chris Daltondba7aab2018-11-15 10:57:49 -0500264 std::set<String> fWrittenIntrinsics;
Ethan Nicholascc305772017-10-13 16:17:45 -0400265 // true if we have run into usages of dFdx / dFdy
266 bool fFoundDerivatives = false;
267 bool fFoundImageDecl = false;
268 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
269 bool fSetupFragPositionGlobal = false;
270 bool fSetupFragPositionLocal = false;
271 int fUniformBuffer = -1;
272
273 typedef CodeGenerator INHERITED;
274};
275
276}
277
278#endif