blob: d5a8ca12fa80ec5168a7247da32c80175efc4cff [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 Liang651286f2018-06-07 09:55:33 -040083 , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy"})
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
139 int size(const Type* type, bool isPacked) const;
140
141 int alignment(const Type* type, bool isPacked) const;
142
Timothy Liangee84fe12018-05-18 14:38:19 -0400143 void writeGlobalStruct();
144
Ethan Nicholascc305772017-10-13 16:17:45 -0400145 void writePrecisionModifier();
146
147 void writeType(const Type& type);
148
149 void writeExtension(const Extension& ext);
150
151 void writeInterfaceBlock(const InterfaceBlock& intf);
152
153 void writeFunctionStart(const FunctionDeclaration& f);
154
155 void writeFunctionDeclaration(const FunctionDeclaration& f);
156
157 void writeFunction(const FunctionDefinition& f);
158
159 void writeLayout(const Layout& layout);
160
161 void writeModifiers(const Modifiers& modifiers, bool globalContext);
162
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000163 void writeGlobalVars(const VarDeclaration& vs);
164
Ethan Nicholascc305772017-10-13 16:17:45 -0400165 void writeVarInitializer(const Variable& var, const Expression& value);
166
Timothy Liang651286f2018-06-07 09:55:33 -0400167 void writeName(const String& name);
168
Ethan Nicholascc305772017-10-13 16:17:45 -0400169 void writeVarDeclarations(const VarDeclarations& decl, bool global);
170
171 void writeFragCoord();
172
173 void writeVariableReference(const VariableReference& ref);
174
175 void writeExpression(const Expression& expr, Precedence parentPrecedence);
176
177 void writeIntrinsicCall(const FunctionCall& c);
178
179 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
180
181 void writeFunctionCall(const FunctionCall& c);
182
Timothy Liang6403b0e2018-05-17 10:40:04 -0400183 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
184
Ethan Nicholascc305772017-10-13 16:17:45 -0400185 void writeConstructor(const Constructor& c);
186
187 void writeFieldAccess(const FieldAccess& f);
188
189 void writeSwizzle(const Swizzle& swizzle);
190
191 static Precedence GetBinaryPrecedence(Token::Kind op);
192
193 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
194
195 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
196
197 void writeIndexExpression(const IndexExpression& expr);
198
199 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
200
201 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
202
203 void writeBoolLiteral(const BoolLiteral& b);
204
205 void writeIntLiteral(const IntLiteral& i);
206
207 void writeFloatLiteral(const FloatLiteral& f);
208
209 void writeSetting(const Setting& s);
210
211 void writeStatement(const Statement& s);
212
213 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
214
215 void writeBlock(const Block& b);
216
217 void writeIfStatement(const IfStatement& stmt);
218
219 void writeForStatement(const ForStatement& f);
220
221 void writeWhileStatement(const WhileStatement& w);
222
223 void writeDoStatement(const DoStatement& d);
224
225 void writeSwitchStatement(const SwitchStatement& s);
226
227 void writeReturnStatement(const ReturnStatement& r);
228
229 void writeProgramElement(const ProgramElement& e);
230
231 Requirements requirements(const FunctionDeclaration& f);
232
233 Requirements requirements(const Expression& e);
234
235 Requirements requirements(const Statement& e);
236
Timothy Liang7d637782018-06-05 09:58:07 -0400237 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400238 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liang651286f2018-06-07 09:55:33 -0400239 std::unordered_set<String> fReservedWords;
Timothy Liangee84fe12018-05-18 14:38:19 -0400240 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
Timothy Lianga06f2152018-05-24 15:33:31 -0400241 std::vector<const Variable*> fTextures;
242 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
243 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
244 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400245 int fPaddingCount = 0;
Timothy Liangee84fe12018-05-18 14:38:19 -0400246 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400247 const char* fLineEnding;
248 const Context& fContext;
249 StringStream fHeader;
250 String fFunctionHeader;
251 Program::Kind fProgramKind;
252 int fVarCount = 0;
253 int fIndentation = 0;
254 bool fAtLineStart = false;
255 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
256 // more than one or two structs per shader, a simple linear search will be faster than anything
257 // fancier.
258 std::vector<const Type*> fWrittenStructs;
259 // true if we have run into usages of dFdx / dFdy
260 bool fFoundDerivatives = false;
261 bool fFoundImageDecl = false;
262 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
263 bool fSetupFragPositionGlobal = false;
264 bool fSetupFragPositionLocal = false;
265 int fUniformBuffer = -1;
266
267 typedef CodeGenerator INHERITED;
268};
269
270}
271
272#endif