blob: 6f8933c3818a47489c746050a555b2f313d05f4c [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:
55 enum Precedence {
56 kParentheses_Precedence = 1,
57 kPostfix_Precedence = 2,
58 kPrefix_Precedence = 3,
59 kMultiplicative_Precedence = 4,
60 kAdditive_Precedence = 5,
61 kShift_Precedence = 6,
62 kRelational_Precedence = 7,
63 kEquality_Precedence = 8,
64 kBitwiseAnd_Precedence = 9,
65 kBitwiseXor_Precedence = 10,
66 kBitwiseOr_Precedence = 11,
67 kLogicalAnd_Precedence = 12,
68 kLogicalXor_Precedence = 13,
69 kLogicalOr_Precedence = 14,
70 kTernary_Precedence = 15,
71 kAssignment_Precedence = 16,
72 kSequence_Precedence = 17,
73 kTopLevel_Precedence = kSequence_Precedence
74 };
75
76 MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
77 OutputStream* out)
78 : INHERITED(program, errors, out)
79 , fLineEnding("\n")
Timothy Liang6403b0e2018-05-17 10:40:04 -040080 , fContext(*context) {
81 this->setupIntrinsics();
82 }
Ethan Nicholascc305772017-10-13 16:17:45 -040083
84 bool generateCode() override;
85
86protected:
87 typedef int Requirements;
88 static constexpr Requirements kNo_Requirements = 0;
89 static constexpr Requirements kInputs_Requirement = 1 << 0;
90 static constexpr Requirements kOutputs_Requirement = 1 << 1;
91 static constexpr Requirements kUniforms_Requirement = 1 << 2;
92
Timothy Liang6403b0e2018-05-17 10:40:04 -040093 enum IntrinsicKind {
94 kSpecial_IntrinsicKind
95 };
96
97 enum SpecialIntrinsic {
98 kTexture_SpecialIntrinsic,
99 };
100
101 void setupIntrinsics();
102
Ethan Nicholascc305772017-10-13 16:17:45 -0400103 void write(const char* s);
104
105 void writeLine();
106
107 void writeLine(const char* s);
108
109 void write(const String& s);
110
111 void writeLine(const String& s);
112
113 void writeHeader();
114
115 void writeUniformStruct();
116
117 void writeInputStruct();
118
119 void writeOutputStruct();
120
121 void writePrecisionModifier();
122
123 void writeType(const Type& type);
124
125 void writeExtension(const Extension& ext);
126
127 void writeInterfaceBlock(const InterfaceBlock& intf);
128
129 void writeFunctionStart(const FunctionDeclaration& f);
130
131 void writeFunctionDeclaration(const FunctionDeclaration& f);
132
133 void writeFunction(const FunctionDefinition& f);
134
135 void writeLayout(const Layout& layout);
136
137 void writeModifiers(const Modifiers& modifiers, bool globalContext);
138
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000139 void writeGlobalVars(const VarDeclaration& vs);
140
Ethan Nicholascc305772017-10-13 16:17:45 -0400141 void writeVarInitializer(const Variable& var, const Expression& value);
142
143 void writeVarDeclarations(const VarDeclarations& decl, bool global);
144
145 void writeFragCoord();
146
147 void writeVariableReference(const VariableReference& ref);
148
149 void writeExpression(const Expression& expr, Precedence parentPrecedence);
150
151 void writeIntrinsicCall(const FunctionCall& c);
152
153 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
154
155 void writeFunctionCall(const FunctionCall& c);
156
Timothy Liang6403b0e2018-05-17 10:40:04 -0400157 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
158
Ethan Nicholascc305772017-10-13 16:17:45 -0400159 void writeConstructor(const Constructor& c);
160
161 void writeFieldAccess(const FieldAccess& f);
162
163 void writeSwizzle(const Swizzle& swizzle);
164
165 static Precedence GetBinaryPrecedence(Token::Kind op);
166
167 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
168
169 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
170
171 void writeIndexExpression(const IndexExpression& expr);
172
173 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
174
175 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
176
177 void writeBoolLiteral(const BoolLiteral& b);
178
179 void writeIntLiteral(const IntLiteral& i);
180
181 void writeFloatLiteral(const FloatLiteral& f);
182
183 void writeSetting(const Setting& s);
184
185 void writeStatement(const Statement& s);
186
187 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
188
189 void writeBlock(const Block& b);
190
191 void writeIfStatement(const IfStatement& stmt);
192
193 void writeForStatement(const ForStatement& f);
194
195 void writeWhileStatement(const WhileStatement& w);
196
197 void writeDoStatement(const DoStatement& d);
198
199 void writeSwitchStatement(const SwitchStatement& s);
200
201 void writeReturnStatement(const ReturnStatement& r);
202
203 void writeProgramElement(const ProgramElement& e);
204
205 Requirements requirements(const FunctionDeclaration& f);
206
207 Requirements requirements(const Expression& e);
208
209 Requirements requirements(const Statement& e);
210
Timothy Liang6403b0e2018-05-17 10:40:04 -0400211 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
212 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Ethan Nicholascc305772017-10-13 16:17:45 -0400213 const char* fLineEnding;
214 const Context& fContext;
215 StringStream fHeader;
216 String fFunctionHeader;
217 Program::Kind fProgramKind;
218 int fVarCount = 0;
219 int fIndentation = 0;
220 bool fAtLineStart = false;
221 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
222 // more than one or two structs per shader, a simple linear search will be faster than anything
223 // fancier.
224 std::vector<const Type*> fWrittenStructs;
225 // true if we have run into usages of dFdx / dFdy
226 bool fFoundDerivatives = false;
227 bool fFoundImageDecl = false;
228 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
229 bool fSetupFragPositionGlobal = false;
230 bool fSetupFragPositionLocal = false;
231 int fUniformBuffer = -1;
232
233 typedef CodeGenerator INHERITED;
234};
235
236}
237
238#endif