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