Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 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 "SkSLCodeGenerator.h" |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 16 | #include "SkSLMemoryLayout.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 17 | #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 | |
| 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: |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 56 | static constexpr const char* SAMPLER_SUFFIX = "Smplr"; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 57 | static constexpr const char* PACKED_PREFIX = "packed_"; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 58 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 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 | |
| 80 | MetalCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 81 | OutputStream* out) |
| 82 | : INHERITED(program, errors, out) |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 83 | , fReservedWords({"atan2", "rsqrt", "dfdx", "dfdy", "vertex", "fragment"}) |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 84 | , fLineEnding("\n") |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 85 | , fContext(*context) { |
| 86 | this->setupIntrinsics(); |
| 87 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 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; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 97 | static constexpr Requirements kGlobals_Requirement = 1 << 3; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 98 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 99 | enum IntrinsicKind { |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 100 | kSpecial_IntrinsicKind, |
| 101 | kMetal_IntrinsicKind, |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | enum SpecialIntrinsic { |
| 105 | kTexture_SpecialIntrinsic, |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 106 | kMod_SpecialIntrinsic, |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 109 | enum MetalIntrinsic { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 110 | kEqual_MetalIntrinsic, |
| 111 | kNotEqual_MetalIntrinsic, |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 112 | kLessThan_MetalIntrinsic, |
| 113 | kLessThanEqual_MetalIntrinsic, |
| 114 | kGreaterThan_MetalIntrinsic, |
| 115 | kGreaterThanEqual_MetalIntrinsic, |
| 116 | }; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 117 | |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 118 | void setupIntrinsics(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 119 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 120 | void write(const char* s); |
| 121 | |
| 122 | void writeLine(); |
| 123 | |
| 124 | void writeLine(const char* s); |
| 125 | |
| 126 | void write(const String& s); |
| 127 | |
| 128 | void writeLine(const String& s); |
| 129 | |
| 130 | void writeHeader(); |
| 131 | |
| 132 | void writeUniformStruct(); |
| 133 | |
| 134 | void writeInputStruct(); |
| 135 | |
| 136 | void writeOutputStruct(); |
| 137 | |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 138 | void writeInterfaceBlocks(); |
| 139 | |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 140 | void writeFields(const std::vector<Type::Field>& fields, int parentOffset, |
| 141 | const InterfaceBlock* parentIntf = nullptr); |
| 142 | |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 143 | int size(const Type* type, bool isPacked) const; |
| 144 | |
| 145 | int alignment(const Type* type, bool isPacked) const; |
| 146 | |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 147 | void writeGlobalStruct(); |
| 148 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 149 | void writePrecisionModifier(); |
| 150 | |
| 151 | void writeType(const Type& type); |
| 152 | |
| 153 | void writeExtension(const Extension& ext); |
| 154 | |
| 155 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 156 | |
| 157 | void writeFunctionStart(const FunctionDeclaration& f); |
| 158 | |
| 159 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 160 | |
| 161 | void writeFunction(const FunctionDefinition& f); |
| 162 | |
| 163 | void writeLayout(const Layout& layout); |
| 164 | |
| 165 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
| 166 | |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 167 | void writeGlobalVars(const VarDeclaration& vs); |
| 168 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 169 | void writeVarInitializer(const Variable& var, const Expression& value); |
| 170 | |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 171 | void writeName(const String& name); |
| 172 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 173 | void writeVarDeclarations(const VarDeclarations& decl, bool global); |
| 174 | |
| 175 | void writeFragCoord(); |
| 176 | |
| 177 | void writeVariableReference(const VariableReference& ref); |
| 178 | |
| 179 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
| 180 | |
| 181 | void writeIntrinsicCall(const FunctionCall& c); |
| 182 | |
| 183 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 184 | |
| 185 | void writeFunctionCall(const FunctionCall& c); |
| 186 | |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 187 | void writeInverseHack(const Expression& mat); |
| 188 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 189 | String getMatrixConstructHelper(const Type& matrix, const Type& arg); |
| 190 | |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 191 | void writeMatrixTimesEqualHelper(const Type& left, const Type& right, const Type& result); |
| 192 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 193 | void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind); |
| 194 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 195 | bool canCoerce(const Type& t1, const Type& t2); |
| 196 | |
| 197 | void writeConstructor(const Constructor& c, Precedence parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 198 | |
| 199 | void writeFieldAccess(const FieldAccess& f); |
| 200 | |
| 201 | void writeSwizzle(const Swizzle& swizzle); |
| 202 | |
| 203 | static Precedence GetBinaryPrecedence(Token::Kind op); |
| 204 | |
| 205 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 206 | |
| 207 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 208 | |
| 209 | void writeIndexExpression(const IndexExpression& expr); |
| 210 | |
| 211 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 212 | |
| 213 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 214 | |
| 215 | void writeBoolLiteral(const BoolLiteral& b); |
| 216 | |
| 217 | void writeIntLiteral(const IntLiteral& i); |
| 218 | |
| 219 | void writeFloatLiteral(const FloatLiteral& f); |
| 220 | |
| 221 | void writeSetting(const Setting& s); |
| 222 | |
| 223 | void writeStatement(const Statement& s); |
| 224 | |
| 225 | void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements); |
| 226 | |
| 227 | void writeBlock(const Block& b); |
| 228 | |
| 229 | void writeIfStatement(const IfStatement& stmt); |
| 230 | |
| 231 | void writeForStatement(const ForStatement& f); |
| 232 | |
| 233 | void writeWhileStatement(const WhileStatement& w); |
| 234 | |
| 235 | void writeDoStatement(const DoStatement& d); |
| 236 | |
| 237 | void writeSwitchStatement(const SwitchStatement& s); |
| 238 | |
| 239 | void writeReturnStatement(const ReturnStatement& r); |
| 240 | |
| 241 | void writeProgramElement(const ProgramElement& e); |
| 242 | |
| 243 | Requirements requirements(const FunctionDeclaration& f); |
| 244 | |
| 245 | Requirements requirements(const Expression& e); |
| 246 | |
| 247 | Requirements requirements(const Statement& e); |
| 248 | |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 249 | typedef std::pair<IntrinsicKind, int32_t> Intrinsic; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 250 | std::unordered_map<String, Intrinsic> fIntrinsicMap; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 251 | std::unordered_set<String> fReservedWords; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 252 | std::vector<const VarDeclaration*> fInitNonConstGlobalVars; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 253 | std::vector<const Variable*> fTextures; |
| 254 | std::unordered_map<const Type::Field*, const InterfaceBlock*> fInterfaceBlockMap; |
| 255 | std::unordered_map<const InterfaceBlock*, String> fInterfaceBlockNameMap; |
| 256 | int fAnonInterfaceCount = 0; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 257 | int fPaddingCount = 0; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 258 | bool fNeedsGlobalStructInit = false; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 259 | const char* fLineEnding; |
| 260 | const Context& fContext; |
| 261 | StringStream fHeader; |
| 262 | String fFunctionHeader; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 263 | StringStream fExtraFunctions; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 264 | Program::Kind fProgramKind; |
| 265 | int fVarCount = 0; |
| 266 | int fIndentation = 0; |
| 267 | bool fAtLineStart = false; |
| 268 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 269 | // more than one or two structs per shader, a simple linear search will be faster than anything |
| 270 | // fancier. |
| 271 | std::vector<const Type*> fWrittenStructs; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 272 | std::set<String> fWrittenIntrinsics; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 273 | // true if we have run into usages of dFdx / dFdy |
| 274 | bool fFoundDerivatives = false; |
| 275 | bool fFoundImageDecl = false; |
| 276 | std::unordered_map<const FunctionDeclaration*, Requirements> fRequirements; |
| 277 | bool fSetupFragPositionGlobal = false; |
| 278 | bool fSetupFragPositionLocal = false; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 279 | std::unordered_map<String, String> fHelpers; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 280 | int fUniformBuffer = -1; |
| 281 | |
| 282 | typedef CodeGenerator INHERITED; |
| 283 | }; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | #endif |