blob: 3f0bc2862b731dcacb5be79c02a68931fe436759 [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"
16#include "SkSLStringStream.h"
17#include "ir/SkSLBinaryExpression.h"
18#include "ir/SkSLBoolLiteral.h"
19#include "ir/SkSLConstructor.h"
20#include "ir/SkSLDoStatement.h"
21#include "ir/SkSLExtension.h"
22#include "ir/SkSLFloatLiteral.h"
23#include "ir/SkSLIfStatement.h"
24#include "ir/SkSLIndexExpression.h"
25#include "ir/SkSLInterfaceBlock.h"
26#include "ir/SkSLIntLiteral.h"
27#include "ir/SkSLFieldAccess.h"
28#include "ir/SkSLForStatement.h"
29#include "ir/SkSLFunctionCall.h"
30#include "ir/SkSLFunctionDeclaration.h"
31#include "ir/SkSLFunctionDefinition.h"
32#include "ir/SkSLPrefixExpression.h"
33#include "ir/SkSLPostfixExpression.h"
34#include "ir/SkSLProgramElement.h"
35#include "ir/SkSLReturnStatement.h"
36#include "ir/SkSLSetting.h"
37#include "ir/SkSLStatement.h"
38#include "ir/SkSLSwitchStatement.h"
39#include "ir/SkSLSwizzle.h"
40#include "ir/SkSLTernaryExpression.h"
41#include "ir/SkSLVarDeclarations.h"
42#include "ir/SkSLVarDeclarationsStatement.h"
43#include "ir/SkSLVariableReference.h"
44#include "ir/SkSLWhileStatement.h"
45
46namespace SkSL {
47
48#define kLast_Capability SpvCapabilityMultiViewport
49
50/**
51 * Converts a Program into Metal code.
52 */
53class MetalCodeGenerator : public CodeGenerator {
54public:
Timothy Lianga06f2152018-05-24 15:33:31 -040055 static constexpr const char* SAMPLER_SUFFIX = "Smplr";
Timothy Liang7d637782018-06-05 09:58:07 -040056 static constexpr const char* PACKED_PREFIX = "packed_";
Timothy Lianga06f2152018-05-24 15:33:31 -040057
Ethan Nicholascc305772017-10-13 16:17:45 -040058 enum Precedence {
59 kParentheses_Precedence = 1,
60 kPostfix_Precedence = 2,
61 kPrefix_Precedence = 3,
62 kMultiplicative_Precedence = 4,
63 kAdditive_Precedence = 5,
64 kShift_Precedence = 6,
65 kRelational_Precedence = 7,
66 kEquality_Precedence = 8,
67 kBitwiseAnd_Precedence = 9,
68 kBitwiseXor_Precedence = 10,
69 kBitwiseOr_Precedence = 11,
70 kLogicalAnd_Precedence = 12,
71 kLogicalXor_Precedence = 13,
72 kLogicalOr_Precedence = 14,
73 kTernary_Precedence = 15,
74 kAssignment_Precedence = 16,
75 kSequence_Precedence = 17,
76 kTopLevel_Precedence = kSequence_Precedence
77 };
78
79 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
80 OutputStream* out)
81 : INHERITED(program, errors, out)
82 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040083 , fContext(*context) {
84 this->setupIntrinsics();
85 }
Ethan Nicholascc305772017-10-13 16:17:45 -040086
Timothy Liang7d637782018-06-05 09:58:07 -040087 bool generateCode(int shaderNum); // FIXME - remove when done inserting MSL
Ethan Nicholascc305772017-10-13 16:17:45 -040088 bool generateCode() override;
89
90protected:
91 typedef int Requirements;
92 static constexpr Requirements kNo_Requirements = 0;
93 static constexpr Requirements kInputs_Requirement = 1 << 0;
94 static constexpr Requirements kOutputs_Requirement = 1 << 1;
95 static constexpr Requirements kUniforms_Requirement = 1 << 2;
Timothy Liangee84fe12018-05-18 14:38:19 -040096 static constexpr Requirements kGlobals_Requirement = 1 << 3;
Ethan Nicholascc305772017-10-13 16:17:45 -040097
Timothy Liang6403b0e2018-05-17 10:40:04 -040098 enum IntrinsicKind {
Timothy Lianga06f2152018-05-24 15:33:31 -040099 kSpecial_IntrinsicKind,
100 kMetal_IntrinsicKind,
Timothy Liang6403b0e2018-05-17 10:40:04 -0400101 };
102
103 enum SpecialIntrinsic {
104 kTexture_SpecialIntrinsic,
105 };
106
Timothy Lianga06f2152018-05-24 15:33:31 -0400107 enum MetalIntrinsic {
108 kLessThan_MetalIntrinsic,
109 kLessThanEqual_MetalIntrinsic,
110 kGreaterThan_MetalIntrinsic,
111 kGreaterThanEqual_MetalIntrinsic,
112 };
Timothy Liang6403b0e2018-05-17 10:40:04 -0400113
Timothy Lianga06f2152018-05-24 15:33:31 -0400114 void setupIntrinsics();
Timothy Liangee84fe12018-05-18 14:38:19 -0400115
Ethan Nicholascc305772017-10-13 16:17:45 -0400116 void write(const char* s);
117
118 void writeLine();
119
120 void writeLine(const char* s);
121
122 void write(const String& s);
123
124 void writeLine(const String& s);
125
126 void writeHeader();
127
128 void writeUniformStruct();
129
130 void writeInputStruct();
131
132 void writeOutputStruct();
133
Timothy Liang7d637782018-06-05 09:58:07 -0400134 void writeInterfaceBlocks();
135
136 int size(const Type* type, bool isPacked) const;
137
138 int alignment(const Type* type, bool isPacked) const;
139
Timothy Liangee84fe12018-05-18 14:38:19 -0400140 void writeGlobalStruct();
141
Ethan Nicholascc305772017-10-13 16:17:45 -0400142 void writePrecisionModifier();
143
144 void writeType(const Type& type);
145
146 void writeExtension(const Extension& ext);
147
148 void writeInterfaceBlock(const InterfaceBlock& intf);
149
150 void writeFunctionStart(const FunctionDeclaration& f);
151
152 void writeFunctionDeclaration(const FunctionDeclaration& f);
153
154 void writeFunction(const FunctionDefinition& f);
155
156 void writeLayout(const Layout& layout);
157
158 void writeModifiers(const Modifiers& modifiers, bool globalContext);
159
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000160 void writeGlobalVars(const VarDeclaration& vs);
161
Ethan Nicholascc305772017-10-13 16:17:45 -0400162 void writeVarInitializer(const Variable& var, const Expression& value);
163
164 void writeVarDeclarations(const VarDeclarations& decl, bool global);
165
166 void writeFragCoord();
167
168 void writeVariableReference(const VariableReference& ref);
169
170 void writeExpression(const Expression& expr, Precedence parentPrecedence);
171
172 void writeIntrinsicCall(const FunctionCall& c);
173
174 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
175
176 void writeFunctionCall(const FunctionCall& c);
177
Timothy Liang6403b0e2018-05-17 10:40:04 -0400178 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
179
Ethan Nicholascc305772017-10-13 16:17:45 -0400180 void writeConstructor(const Constructor& c);
181
182 void writeFieldAccess(const FieldAccess& f);
183
184 void writeSwizzle(const Swizzle& swizzle);
185
186 static Precedence GetBinaryPrecedence(Token::Kind op);
187
188 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
189
190 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
191
192 void writeIndexExpression(const IndexExpression& expr);
193
194 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
195
196 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
197
198 void writeBoolLiteral(const BoolLiteral& b);
199
200 void writeIntLiteral(const IntLiteral& i);
201
202 void writeFloatLiteral(const FloatLiteral& f);
203
204 void writeSetting(const Setting& s);
205
206 void writeStatement(const Statement& s);
207
208 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
209
210 void writeBlock(const Block& b);
211
212 void writeIfStatement(const IfStatement& stmt);
213
214 void writeForStatement(const ForStatement& f);
215
216 void writeWhileStatement(const WhileStatement& w);
217
218 void writeDoStatement(const DoStatement& d);
219
220 void writeSwitchStatement(const SwitchStatement& s);
221
222 void writeReturnStatement(const ReturnStatement& r);
223
224 void writeProgramElement(const ProgramElement& e);
225
226 Requirements requirements(const FunctionDeclaration& f);
227
228 Requirements requirements(const Expression& e);
229
230 Requirements requirements(const Statement& e);
231
Timothy Liang7d637782018-06-05 09:58:07 -0400232 typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
Timothy Liang6403b0e2018-05-17 10:40:04 -0400233 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liangee84fe12018-05-18 14:38:19 -0400234 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
Timothy Lianga06f2152018-05-24 15:33:31 -0400235 std::vector<const Variable*> fTextures;
236 std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
237 std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
238 int fAnonInterfaceCount = 0;
Timothy Liang7d637782018-06-05 09:58:07 -0400239 int fPaddingCount = 0;
Timothy Liangee84fe12018-05-18 14:38:19 -0400240 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400241 const char* fLineEnding;
242 const Context& fContext;
243 StringStream fHeader;
244 String fFunctionHeader;
245 Program::Kind fProgramKind;
246 int fVarCount = 0;
247 int fIndentation = 0;
248 bool fAtLineStart = false;
249 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
250 // more than one or two structs per shader, a simple linear search will be faster than anything
251 // fancier.
252 std::vector<const Type*> fWrittenStructs;
253 // true if we have run into usages of dFdx / dFdy
254 bool fFoundDerivatives = false;
255 bool fFoundImageDecl = false;
256 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
257 bool fSetupFragPositionGlobal = false;
258 bool fSetupFragPositionLocal = false;
259 int fUniformBuffer = -1;
260
261 typedef CodeGenerator INHERITED;
262};
263
264}
265
266#endif