1 /*
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 "src/sksl/SkSLCodeGenerator.h"
16 #include "src/sksl/SkSLMemoryLayout.h"
17 #include "src/sksl/SkSLStringStream.h"
18 #include "src/sksl/ir/SkSLBinaryExpression.h"
19 #include "src/sksl/ir/SkSLBoolLiteral.h"
20 #include "src/sksl/ir/SkSLConstructor.h"
21 #include "src/sksl/ir/SkSLDoStatement.h"
22 #include "src/sksl/ir/SkSLExtension.h"
23 #include "src/sksl/ir/SkSLFieldAccess.h"
24 #include "src/sksl/ir/SkSLFloatLiteral.h"
25 #include "src/sksl/ir/SkSLForStatement.h"
26 #include "src/sksl/ir/SkSLFunctionCall.h"
27 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
28 #include "src/sksl/ir/SkSLFunctionDefinition.h"
29 #include "src/sksl/ir/SkSLIfStatement.h"
30 #include "src/sksl/ir/SkSLIndexExpression.h"
31 #include "src/sksl/ir/SkSLIntLiteral.h"
32 #include "src/sksl/ir/SkSLInterfaceBlock.h"
33 #include "src/sksl/ir/SkSLPostfixExpression.h"
34 #include "src/sksl/ir/SkSLPrefixExpression.h"
35 #include "src/sksl/ir/SkSLProgramElement.h"
36 #include "src/sksl/ir/SkSLReturnStatement.h"
37 #include "src/sksl/ir/SkSLSetting.h"
38 #include "src/sksl/ir/SkSLStatement.h"
39 #include "src/sksl/ir/SkSLSwitchStatement.h"
40 #include "src/sksl/ir/SkSLSwizzle.h"
41 #include "src/sksl/ir/SkSLTernaryExpression.h"
42 #include "src/sksl/ir/SkSLVarDeclarations.h"
43 #include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
44 #include "src/sksl/ir/SkSLVariableReference.h"
45 #include "src/sksl/ir/SkSLWhileStatement.h"
46 
47 namespace SkSL {
48 
49 #define kLast_Capability SpvCapabilityMultiViewport
50 
51 /**
52  * Converts a Program into Metal code.
53  */
54 class MetalCodeGenerator : public CodeGenerator {
55 public:
56     static constexpr const char* SAMPLER_SUFFIX = "Smplr";
57     static constexpr const char* PACKED_PREFIX = "packed_";
58 
59     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 
MetalCodeGenerator(const Context * context,const Program * program,ErrorReporter * errors,OutputStream * out)80     MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
81                       OutputStream* out)
82     : INHERITED(program, errors, out)
83     , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy", "vertex", "fragment"})
84     , fLineEnding("\n")
85     , fContext(*context) {
86         this->setupIntrinsics();
87     }
88 
89     bool generateCode() override;
90 
91 protected:
92     typedef int Requirements;
93     static constexpr Requirements kNo_Requirements       = 0;
94     static constexpr Requirements kInputs_Requirement    = 1 << 0;
95     static constexpr Requirements kOutputs_Requirement   = 1 << 1;
96     static constexpr Requirements kUniforms_Requirement  = 1 << 2;
97     static constexpr Requirements kGlobals_Requirement   = 1 << 3;
98     static constexpr Requirements kFragCoord_Requirement = 1 << 4;
99 
100     enum IntrinsicKind {
101         kSpecial_IntrinsicKind,
102         kMetal_IntrinsicKind,
103     };
104 
105     enum SpecialIntrinsic {
106         kTexture_SpecialIntrinsic,
107         kMod_SpecialIntrinsic,
108     };
109 
110     enum MetalIntrinsic {
111         kEqual_MetalIntrinsic,
112         kNotEqual_MetalIntrinsic,
113         kLessThan_MetalIntrinsic,
114         kLessThanEqual_MetalIntrinsic,
115         kGreaterThan_MetalIntrinsic,
116         kGreaterThanEqual_MetalIntrinsic,
117     };
118 
119     void setupIntrinsics();
120 
121     void write(const char* s);
122 
123     void writeLine();
124 
125     void writeLine(const char* s);
126 
127     void write(const String& s);
128 
129     void writeLine(const String& s);
130 
131     void writeHeader();
132 
133     void writeUniformStruct();
134 
135     void writeInputStruct();
136 
137     void writeOutputStruct();
138 
139     void writeInterfaceBlocks();
140 
141     void writeFields(const std::vector<Type::Field>& fields, int parentOffset,
142                      const InterfaceBlock* parentIntf = nullptr);
143 
144     int size(const Type* type, bool isPacked) const;
145 
146     int alignment(const Type* type, bool isPacked) const;
147 
148     void writeGlobalStruct();
149 
150     void writePrecisionModifier();
151 
152     void writeType(const Type& type);
153 
154     void writeExtension(const Extension& ext);
155 
156     void writeInterfaceBlock(const InterfaceBlock& intf);
157 
158     void writeFunctionStart(const FunctionDeclaration& f);
159 
160     void writeFunctionDeclaration(const FunctionDeclaration& f);
161 
162     void writeFunction(const FunctionDefinition& f);
163 
164     void writeLayout(const Layout& layout);
165 
166     void writeModifiers(const Modifiers& modifiers, bool globalContext);
167 
168     void writeGlobalVars(const VarDeclaration& vs);
169 
170     void writeVarInitializer(const Variable& var, const Expression& value);
171 
172     void writeName(const String& name);
173 
174     void writeVarDeclarations(const VarDeclarations& decl, bool global);
175 
176     void writeFragCoord();
177 
178     void writeVariableReference(const VariableReference& ref);
179 
180     void writeExpression(const Expression& expr, Precedence parentPrecedence);
181 
182     void writeIntrinsicCall(const FunctionCall& c);
183 
184     void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
185 
186     void writeFunctionCall(const FunctionCall& c);
187 
188     void writeInverseHack(const Expression& mat);
189 
190     String getMatrixConstructHelper(const Type& matrix, const Type& arg);
191 
192     void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result);
193 
194     void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
195 
196     bool canCoerce(const Type& t1, const Type& t2);
197 
198     void writeConstructor(const Constructor& c, Precedence parentPrecedence);
199 
200     void writeFieldAccess(const FieldAccess& f);
201 
202     void writeSwizzle(const Swizzle& swizzle);
203 
204     static Precedence GetBinaryPrecedence(Token::Kind op);
205 
206     void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
207 
208     void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
209 
210     void writeIndexExpression(const IndexExpression& expr);
211 
212     void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
213 
214     void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
215 
216     void writeBoolLiteral(const BoolLiteral& b);
217 
218     void writeIntLiteral(const IntLiteral& i);
219 
220     void writeFloatLiteral(const FloatLiteral& f);
221 
222     void writeSetting(const Setting& s);
223 
224     void writeStatement(const Statement& s);
225 
226     void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
227 
228     void writeBlock(const Block& b);
229 
230     void writeIfStatement(const IfStatement& stmt);
231 
232     void writeForStatement(const ForStatement& f);
233 
234     void writeWhileStatement(const WhileStatement& w);
235 
236     void writeDoStatement(const DoStatement& d);
237 
238     void writeSwitchStatement(const SwitchStatement& s);
239 
240     void writeReturnStatement(const ReturnStatement& r);
241 
242     void writeProgramElement(const ProgramElement& e);
243 
244     Requirements requirements(const FunctionDeclaration& f);
245 
246     Requirements requirements(const Expression& e);
247 
248     Requirements requirements(const Statement& e);
249 
250     typedef std::pair<IntrinsicKind, int32_t> Intrinsic;
251     std::unordered_map<String, Intrinsic> fIntrinsicMap;
252     std::unordered_set<String> fReservedWords;
253     std::vector<const VarDeclaration*> fInitNonConstGlobalVars;
254     std::vector<const Variable*> fTextures;
255     std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap;
256     std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap;
257     int fAnonInterfaceCount = 0;
258     int fPaddingCount = 0;
259     bool fNeedsGlobalStructInit = false;
260     const char* fLineEnding;
261     const Context& fContext;
262     StringStream fHeader;
263     String fFunctionHeader;
264     StringStream fExtraFunctions;
265     Program::Kind fProgramKind;
266     int fVarCount = 0;
267     int fIndentation = 0;
268     bool fAtLineStart = false;
269     // Keeps track of which struct types we have written. Given that we are unlikely to ever write
270     // more than one or two structs per shader, a simple linear search will be faster than anything
271     // fancier.
272     std::vector<const Type*> fWrittenStructs;
273     std::set<String> fWrittenIntrinsics;
274     // true if we have run into usages of dFdx / dFdy
275     bool fFoundDerivatives = false;
276     std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements;
277     bool fSetupFragPositionGlobal = false;
278     bool fSetupFragPositionLocal = false;
279     std::unordered_map<String, String> fHelpers;
280     int fUniformBuffer = -1;
281     String fRTHeightName;
282 
283     typedef CodeGenerator INHERITED;
284 };
285 
286 }
287 
288 #endif
289