blob: 49a5bd6ed707b90685c9fcc46a3a85a85c9d7d35 [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
Timothy Liang7d637782018-06-05 09:58:07 -040089 bool generateCode(int shaderNum); // FIXME - remove when done inserting MSL
Ethan Nicholascc305772017-10-13 16:17:45 -040090 bool generateCode() override;
91
92protected:
93 typedef int Requirements;
94 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;
Timothy Liangee84fe12018-05-18 14:38:19 -040098 static constexpr Requirements kGlobals_Requirement = 1 << 3;
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 {
106 kTexture_SpecialIntrinsic,
Timothy Liang651286f2018-06-07 09:55:33 -0400107 kMod_SpecialIntrinsic,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400108 };
109
Timothy Lianga06f2152018-05-24 15:33:31 -0400110 enum MetalIntrinsic {
111 kLessThan_MetalIntrinsic,
112 kLessThanEqual_MetalIntrinsic,
113 kGreaterThan_MetalIntrinsic,
114 kGreaterThanEqual_MetalIntrinsic,
115 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400116
Timothy Lianga06f2152018-05-24 15:33:31 -0400117 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400118
Ethan Nicholascc305772017-10-13 16:17:45 -0400119 void write(const char* s);
120
121 void writeLine();
122
123 void writeLine(const char* s);
124
125 void write(const String& s);
126
127 void writeLine(const String& s);
128
129 void writeHeader();
130
131 void writeUniformStruct();
132
133 void writeInputStruct();
134
135 void writeOutputStruct();
136
Timothy Liang7d637782018-06-05 09:58:07 -0400137 void writeInterfaceBlocks();
138
Timothy Liangdc89f192018-06-13 09:20:31 -0400139 void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
140 const InterfaceBlock* parentIntf = nullptr);
141
Timothy Liang7d637782018-06-05 09:58:07 -0400142 int size(const Type* type, bool isPacked) const;
143
144 int alignment(const Type* type, bool isPacked) const;
145
Timothy Liangee84fe12018-05-18 14:38:19 -0400146 void writeGlobalStruct();
147
Ethan Nicholascc305772017-10-13 16:17:45 -0400148 void writePrecisionModifier();
149
150 void writeType(const Type& type);
151
152 void writeExtension(const Extension& ext);
153
154 void writeInterfaceBlock(const InterfaceBlock& intf);
155
156 void writeFunctionStart(const FunctionDeclaration& f);
157
158 void writeFunctionDeclaration(const FunctionDeclaration& f);
159
160 void writeFunction(const FunctionDefinition& f);
161
162 void writeLayout(const Layout& layout);
163
164 void writeModifiers(const Modifiers& modifiers, bool globalContext);
165
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000166 void writeGlobalVars(const VarDeclaration& vs);
167
Ethan Nicholascc305772017-10-13 16:17:45 -0400168 void writeVarInitializer(const Variable& var, const Expression& value);
169
Timothy Liang651286f2018-06-07 09:55:33 -0400170 void writeName(const String& name);
171
Ethan Nicholascc305772017-10-13 16:17:45 -0400172 void writeVarDeclarations(const VarDeclarations& decl, bool global);
173
174 void writeFragCoord();
175
176 void writeVariableReference(const VariableReference& ref);
177
178 void writeExpression(const Expression& expr, Precedence parentPrecedence);
179
180 void writeIntrinsicCall(const FunctionCall& c);
181
182 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
183
184 void writeFunctionCall(const FunctionCall& c);
185
Timothy Liang6403b0e2018-05-17 10:40:04 -0400186 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
187
Ethan Nicholascc305772017-10-13 16:17:45 -0400188 void writeConstructor(const Constructor& c);
189
190 void writeFieldAccess(const FieldAccess& f);
191
192 void writeSwizzle(const Swizzle& swizzle);
193
194 static Precedence GetBinaryPrecedence(Token::Kind op);
195
196 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
197
198 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
199
200 void writeIndexExpression(const IndexExpression& expr);
201
202 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
203
204 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
205
206 void writeBoolLiteral(const BoolLiteral& b);
207
208 void writeIntLiteral(const IntLiteral& i);
209
210 void writeFloatLiteral(const FloatLiteral& f);
211
212 void writeSetting(const Setting& s);
213
214 void writeStatement(const Statement& s);
215
216 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
217
218 void writeBlock(const Block& b);
219
220 void writeIfStatement(const IfStatement& stmt);
221
222 void writeForStatement(const ForStatement& f);
223
224 void writeWhileStatement(const WhileStatement& w);
225
226 void writeDoStatement(const DoStatement& d);
227
228 void writeSwitchStatement(const SwitchStatement& s);
229
230 void writeReturnStatement(const ReturnStatement& r);
231
232 void writeProgramElement(const ProgramElement& e);
233
234 Requirements requirements(const FunctionDeclaration& f);
235
236 Requirements requirements(const Expression& e);
237
238 Requirements requirements(const Statement& e);
239
Timothy Liang7d637782018-06-05 09:58:07 -0400240 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400241 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400242 std::unordered_set<String> fReservedWords;
Timothy Liangee84fe12018-05-18 14:38:19 -0400243 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
Timothy Lianga06f2152018-05-24 15:33:31 -0400244 std::vector<const Variable*> fTextures;
245 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
246 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
247 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400248 int fPaddingCount = 0;
Timothy Liangee84fe12018-05-18 14:38:19 -0400249 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400250 const char* fLineEnding;
251 const Context& fContext;
252 StringStream fHeader;
253 String fFunctionHeader;
254 Program::Kind fProgramKind;
255 int fVarCount = 0;
256 int fIndentation = 0;
257 bool fAtLineStart = false;
258 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
259 // more than one or two structs per shader, a simple linear search will be faster than anything
260 // fancier.
261 std::vector<const Type*> fWrittenStructs;
262 // true if we have run into usages of dFdx / dFdy
263 bool fFoundDerivatives = false;
264 bool fFoundImageDecl = false;
265 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
266 bool fSetupFragPositionGlobal = false;
267 bool fSetupFragPositionLocal = false;
268 int fUniformBuffer = -1;
269
270 typedef CodeGenerator INHERITED;
271};
272
273}
274
275#endif