blob: a0a0cae986a7040ac37b6374927ab463600a6bf0 [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;
Timothy Liangee84fe12018-05-18 14:38:19 -040088 typedef unsigned int TextureId;
Ethan Nicholascc305772017-10-13 16:17:45 -040089 static constexpr Requirements kNo_Requirements = 0;
90 static constexpr Requirements kInputs_Requirement = 1 << 0;
91 static constexpr Requirements kOutputs_Requirement = 1 << 1;
92 static constexpr Requirements kUniforms_Requirement = 1 << 2;
Timothy Liangee84fe12018-05-18 14:38:19 -040093 static constexpr Requirements kGlobals_Requirement = 1 << 3;
Ethan Nicholascc305772017-10-13 16:17:45 -040094
Timothy Liang6403b0e2018-05-17 10:40:04 -040095 enum IntrinsicKind {
96 kSpecial_IntrinsicKind
97 };
98
99 enum SpecialIntrinsic {
100 kTexture_SpecialIntrinsic,
101 };
102
103 void setupIntrinsics();
104
Timothy Liangee84fe12018-05-18 14:38:19 -0400105 TextureId nextTextureId();
106
Ethan Nicholascc305772017-10-13 16:17:45 -0400107 void write(const char* s);
108
109 void writeLine();
110
111 void writeLine(const char* s);
112
113 void write(const String& s);
114
115 void writeLine(const String& s);
116
117 void writeHeader();
118
119 void writeUniformStruct();
120
121 void writeInputStruct();
122
123 void writeOutputStruct();
124
Timothy Liangee84fe12018-05-18 14:38:19 -0400125 void writeGlobalStruct();
126
Ethan Nicholascc305772017-10-13 16:17:45 -0400127 void writePrecisionModifier();
128
129 void writeType(const Type& type);
130
131 void writeExtension(const Extension& ext);
132
133 void writeInterfaceBlock(const InterfaceBlock& intf);
134
135 void writeFunctionStart(const FunctionDeclaration& f);
136
137 void writeFunctionDeclaration(const FunctionDeclaration& f);
138
139 void writeFunction(const FunctionDefinition& f);
140
141 void writeLayout(const Layout& layout);
142
143 void writeModifiers(const Modifiers& modifiers, bool globalContext);
144
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000145 void writeGlobalVars(const VarDeclaration& vs);
146
Ethan Nicholascc305772017-10-13 16:17:45 -0400147 void writeVarInitializer(const Variable& var, const Expression& value);
148
149 void writeVarDeclarations(const VarDeclarations& decl, bool global);
150
151 void writeFragCoord();
152
153 void writeVariableReference(const VariableReference& ref);
154
155 void writeExpression(const Expression& expr, Precedence parentPrecedence);
156
157 void writeIntrinsicCall(const FunctionCall& c);
158
159 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
160
161 void writeFunctionCall(const FunctionCall& c);
162
Timothy Liang6403b0e2018-05-17 10:40:04 -0400163 void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
164
Ethan Nicholascc305772017-10-13 16:17:45 -0400165 void writeConstructor(const Constructor& c);
166
167 void writeFieldAccess(const FieldAccess& f);
168
169 void writeSwizzle(const Swizzle& swizzle);
170
171 static Precedence GetBinaryPrecedence(Token::Kind op);
172
173 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
174
175 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
176
177 void writeIndexExpression(const IndexExpression& expr);
178
179 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
180
181 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
182
183 void writeBoolLiteral(const BoolLiteral& b);
184
185 void writeIntLiteral(const IntLiteral& i);
186
187 void writeFloatLiteral(const FloatLiteral& f);
188
189 void writeSetting(const Setting& s);
190
191 void writeStatement(const Statement& s);
192
193 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
194
195 void writeBlock(const Block& b);
196
197 void writeIfStatement(const IfStatement& stmt);
198
199 void writeForStatement(const ForStatement& f);
200
201 void writeWhileStatement(const WhileStatement& w);
202
203 void writeDoStatement(const DoStatement& d);
204
205 void writeSwitchStatement(const SwitchStatement& s);
206
207 void writeReturnStatement(const ReturnStatement& r);
208
209 void writeProgramElement(const ProgramElement& e);
210
211 Requirements requirements(const FunctionDeclaration& f);
212
213 Requirements requirements(const Expression& e);
214
215 Requirements requirements(const Statement& e);
216
Timothy Liang6403b0e2018-05-17 10:40:04 -0400217 typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
218 std::unordered_map<String, Intrinsic> fIntrinsicMap;
Timothy Liangee84fe12018-05-18 14:38:19 -0400219 std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
220 TextureId fCurrentTextureId = 0;
221 std::unordered_map<String, TextureId> fTextureMap;
222 bool fNeedsGlobalStructInit = false;
Ethan Nicholascc305772017-10-13 16:17:45 -0400223 const char* fLineEnding;
224 const Context& fContext;
225 StringStream fHeader;
226 String fFunctionHeader;
227 Program::Kind fProgramKind;
228 int fVarCount = 0;
229 int fIndentation = 0;
230 bool fAtLineStart = false;
231 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
232 // more than one or two structs per shader, a simple linear search will be faster than anything
233 // fancier.
234 std::vector<const Type*> fWrittenStructs;
235 // true if we have run into usages of dFdx / dFdy
236 bool fFoundDerivatives = false;
237 bool fFoundImageDecl = false;
238 std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
239 bool fSetupFragPositionGlobal = false;
240 bool fSetupFragPositionLocal = false;
241 int fUniformBuffer = -1;
242
243 typedef CodeGenerator INHERITED;
244};
245
246}
247
248#endif