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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLMemoryLayout.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 13 | #include "src/sksl/ir/SkSLExtension.h" |
| 14 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 15 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 16 | #include "src/sksl/ir/SkSLNop.h" |
| 17 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 18 | |
Brian Osman | c262a12 | 2020-08-06 16:34:34 -0400 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 21 | namespace SkSL { |
| 22 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 23 | class MetalCodeGenerator::GlobalStructVisitor { |
| 24 | public: |
| 25 | virtual ~GlobalStructVisitor() = default; |
| 26 | virtual void VisitInterfaceBlock(const InterfaceBlock& block, const String& blockName) = 0; |
| 27 | virtual void VisitTexture(const Type& type, const String& name) = 0; |
| 28 | virtual void VisitSampler(const Type& type, const String& name) = 0; |
| 29 | virtual void VisitVariable(const Variable& var, const Expression* value) = 0; |
| 30 | }; |
| 31 | |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 32 | void MetalCodeGenerator::setupIntrinsics() { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 33 | #define METAL(x) std::make_pair(kMetal_IntrinsicKind, k ## x ## _MetalIntrinsic) |
| 34 | #define SPECIAL(x) std::make_pair(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic) |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 35 | fIntrinsicMap[String("sample")] = SPECIAL(Texture); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 36 | fIntrinsicMap[String("mod")] = SPECIAL(Mod); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 37 | fIntrinsicMap[String("equal")] = METAL(Equal); |
| 38 | fIntrinsicMap[String("notEqual")] = METAL(NotEqual); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 39 | fIntrinsicMap[String("lessThan")] = METAL(LessThan); |
| 40 | fIntrinsicMap[String("lessThanEqual")] = METAL(LessThanEqual); |
| 41 | fIntrinsicMap[String("greaterThan")] = METAL(GreaterThan); |
| 42 | fIntrinsicMap[String("greaterThanEqual")] = METAL(GreaterThanEqual); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 45 | void MetalCodeGenerator::write(const char* s) { |
| 46 | if (!s[0]) { |
| 47 | return; |
| 48 | } |
| 49 | if (fAtLineStart) { |
| 50 | for (int i = 0; i < fIndentation; i++) { |
| 51 | fOut->writeText(" "); |
| 52 | } |
| 53 | } |
| 54 | fOut->writeText(s); |
| 55 | fAtLineStart = false; |
| 56 | } |
| 57 | |
| 58 | void MetalCodeGenerator::writeLine(const char* s) { |
| 59 | this->write(s); |
| 60 | fOut->writeText(fLineEnding); |
| 61 | fAtLineStart = true; |
| 62 | } |
| 63 | |
| 64 | void MetalCodeGenerator::write(const String& s) { |
| 65 | this->write(s.c_str()); |
| 66 | } |
| 67 | |
| 68 | void MetalCodeGenerator::writeLine(const String& s) { |
| 69 | this->writeLine(s.c_str()); |
| 70 | } |
| 71 | |
| 72 | void MetalCodeGenerator::writeLine() { |
| 73 | this->writeLine(""); |
| 74 | } |
| 75 | |
| 76 | void MetalCodeGenerator::writeExtension(const Extension& ext) { |
Ethan Nicholas | efb09e2 | 2020-09-30 10:17:00 -0400 | [diff] [blame] | 77 | this->writeLine("#extension " + ext.name() + " : enable"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 80 | String MetalCodeGenerator::typeName(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 81 | switch (type.typeKind()) { |
| 82 | case Type::TypeKind::kVector: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 83 | return this->typeName(type.componentType()) + to_string(type.columns()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 84 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 85 | return this->typeName(type.componentType()) + to_string(type.columns()) + "x" + |
| 86 | to_string(type.rows()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 87 | case Type::TypeKind::kSampler: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 88 | return "texture2d<float>"; // FIXME - support other texture types; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 89 | default: |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 90 | if (type == *fContext.fHalf_Type) { |
| 91 | // FIXME - Currently only supporting floats in MSL to avoid type coercion issues. |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 92 | return fContext.fFloat_Type->name(); |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 93 | } else if (type == *fContext.fByte_Type) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 94 | return "char"; |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 95 | } else if (type == *fContext.fUByte_Type) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 96 | return "uchar"; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 97 | } else { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 98 | return type.name(); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 99 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 103 | void MetalCodeGenerator::writeType(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 104 | if (type.typeKind() == Type::TypeKind::kStruct) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 105 | for (const Type* search : fWrittenStructs) { |
| 106 | if (*search == type) { |
| 107 | // already written |
| 108 | this->write(type.name()); |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | fWrittenStructs.push_back(&type); |
| 113 | this->writeLine("struct " + type.name() + " {"); |
| 114 | fIndentation++; |
| 115 | this->writeFields(type.fields(), type.fOffset); |
| 116 | fIndentation--; |
| 117 | this->write("}"); |
| 118 | } else { |
| 119 | this->write(this->typeName(type)); |
| 120 | } |
| 121 | } |
| 122 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 123 | void MetalCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 124 | switch (expr.kind()) { |
| 125 | case Expression::Kind::kBinary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 126 | this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 127 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 128 | case Expression::Kind::kBoolLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 129 | this->writeBoolLiteral(expr.as<BoolLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 130 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 131 | case Expression::Kind::kConstructor: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 132 | this->writeConstructor(expr.as<Constructor>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 133 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 134 | case Expression::Kind::kIntLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 135 | this->writeIntLiteral(expr.as<IntLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 136 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 137 | case Expression::Kind::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 138 | this->writeFieldAccess(expr.as<FieldAccess>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 139 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 140 | case Expression::Kind::kFloatLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 141 | this->writeFloatLiteral(expr.as<FloatLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 142 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 143 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 144 | this->writeFunctionCall(expr.as<FunctionCall>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 145 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 146 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 147 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 148 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 149 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 150 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 151 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 152 | case Expression::Kind::kSetting: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 153 | this->writeSetting(expr.as<Setting>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 154 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 155 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 156 | this->writeSwizzle(expr.as<Swizzle>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 157 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 158 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 159 | this->writeVariableReference(expr.as<VariableReference>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 160 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 161 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 162 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 163 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 164 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 165 | this->writeIndexExpression(expr.as<IndexExpression>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 166 | break; |
| 167 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 168 | #ifdef SK_DEBUG |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 169 | ABORT("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 170 | #endif |
| 171 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 175 | void MetalCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 176 | auto i = fIntrinsicMap.find(c.function().name()); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 177 | SkASSERT(i != fIntrinsicMap.end()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 178 | Intrinsic intrinsic = i->second; |
| 179 | int32_t intrinsicId = intrinsic.second; |
| 180 | switch (intrinsic.first) { |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 181 | case kSpecial_IntrinsicKind: |
| 182 | return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 183 | break; |
| 184 | case kMetal_IntrinsicKind: |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 185 | this->writeExpression(*c.arguments()[0], kSequence_Precedence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 186 | switch ((MetalIntrinsic) intrinsicId) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 187 | case kEqual_MetalIntrinsic: |
| 188 | this->write(" == "); |
| 189 | break; |
| 190 | case kNotEqual_MetalIntrinsic: |
| 191 | this->write(" != "); |
| 192 | break; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 193 | case kLessThan_MetalIntrinsic: |
| 194 | this->write(" < "); |
| 195 | break; |
| 196 | case kLessThanEqual_MetalIntrinsic: |
| 197 | this->write(" <= "); |
| 198 | break; |
| 199 | case kGreaterThan_MetalIntrinsic: |
| 200 | this->write(" > "); |
| 201 | break; |
| 202 | case kGreaterThanEqual_MetalIntrinsic: |
| 203 | this->write(" >= "); |
| 204 | break; |
| 205 | default: |
| 206 | ABORT("unsupported metal intrinsic kind"); |
| 207 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 208 | this->writeExpression(*c.arguments()[1], kSequence_Precedence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 209 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 210 | default: |
| 211 | ABORT("unsupported intrinsic kind"); |
| 212 | } |
| 213 | } |
| 214 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 215 | void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 216 | const FunctionDeclaration& function = c.function(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 217 | const ExpressionArray& arguments = c.arguments(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 218 | const auto& entry = fIntrinsicMap.find(function.name()); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 219 | if (entry != fIntrinsicMap.end()) { |
| 220 | this->writeIntrinsicCall(c); |
| 221 | return; |
| 222 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 223 | const StringFragment& name = function.name(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 224 | bool builtin = function.isBuiltin(); |
| 225 | if (builtin && name == "atan" && arguments.size() == 2) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 226 | this->write("atan2"); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 227 | } else if (builtin && name == "inversesqrt") { |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 228 | this->write("rsqrt"); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 229 | } else if (builtin && name == "inverse") { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 230 | SkASSERT(arguments.size() == 1); |
| 231 | this->writeInverseHack(*arguments[0]); |
John Stiles | a80a3dc | 2020-10-20 10:24:56 -0400 | [diff] [blame] | 232 | } else if (builtin && name == "frexp") { |
| 233 | // Our Metal codegen assumes that out params are pointers, but Metal's built-in frexp |
| 234 | // actually takes a reference for the exponent, not a pointer. We add in a helper function |
| 235 | // here to translate. |
| 236 | SkASSERT(arguments.size() == 2); |
| 237 | auto [iter, newlyCreated] = fHelpers.insert("frexp"); |
| 238 | if (newlyCreated) { |
| 239 | fExtraFunctions.printf( |
| 240 | "float frexp(float arg, thread int* exp) { return frexp(arg, *exp); }\n"); |
| 241 | } |
| 242 | this->write("frexp"); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 243 | } else if (builtin && name == "dFdx") { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 244 | this->write("dfdx"); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 245 | } else if (builtin && name == "dFdy") { |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 246 | // Flipping Y also negates the Y derivatives. |
| 247 | this->write((fProgram.fSettings.fFlipY) ? "-dfdy" : "dfdy"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 248 | } else { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 249 | this->writeName(name); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 250 | } |
| 251 | this->write("("); |
| 252 | const char* separator = ""; |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 253 | if (this->requirements(function) & kInputs_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 254 | this->write("_in"); |
| 255 | separator = ", "; |
| 256 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 257 | if (this->requirements(function) & kOutputs_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 258 | this->write(separator); |
| 259 | this->write("_out"); |
| 260 | separator = ", "; |
| 261 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 262 | if (this->requirements(function) & kUniforms_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 263 | this->write(separator); |
| 264 | this->write("_uniforms"); |
| 265 | separator = ", "; |
| 266 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 267 | if (this->requirements(function) & kGlobals_Requirement) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 268 | this->write(separator); |
| 269 | this->write("_globals"); |
| 270 | separator = ", "; |
| 271 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 272 | if (this->requirements(function) & kFragCoord_Requirement) { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 273 | this->write(separator); |
| 274 | this->write("_fragCoord"); |
| 275 | separator = ", "; |
| 276 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 277 | const std::vector<const Variable*>& parameters = function.parameters(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 278 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 279 | const Expression& arg = *arguments[i]; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 280 | this->write(separator); |
| 281 | separator = ", "; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 282 | if (parameters[i]->modifiers().fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 283 | this->write("&"); |
| 284 | } |
| 285 | this->writeExpression(arg, kSequence_Precedence); |
| 286 | } |
| 287 | this->write(")"); |
| 288 | } |
| 289 | |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 290 | void MetalCodeGenerator::writeInverseHack(const Expression& mat) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 291 | const Type& type = mat.type(); |
| 292 | const String& typeName = type.name(); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 293 | String name = typeName + "_inverse"; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 294 | if (type == *fContext.fFloat2x2_Type || type == *fContext.fHalf2x2_Type) { |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 295 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 296 | fWrittenIntrinsics.insert(name); |
| 297 | fExtraFunctions.writeText(( |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 298 | typeName + " " + name + "(" + typeName + " m) {" |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 299 | " return float2x2(m[1][1], -m[0][1], -m[1][0], m[0][0]) * (1/determinant(m));" |
| 300 | "}" |
| 301 | ).c_str()); |
| 302 | } |
| 303 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 304 | else if (type == *fContext.fFloat3x3_Type || type == *fContext.fHalf3x3_Type) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 305 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 306 | fWrittenIntrinsics.insert(name); |
| 307 | fExtraFunctions.writeText(( |
| 308 | typeName + " " + name + "(" + typeName + " m) {" |
| 309 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 310 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 311 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 312 | " float b01 = a22 * a11 - a12 * a21;" |
| 313 | " float b11 = -a22 * a10 + a12 * a20;" |
| 314 | " float b21 = a21 * a10 - a11 * a20;" |
| 315 | " float det = a00 * b01 + a01 * b11 + a02 * b21;" |
| 316 | " return " + typeName + |
| 317 | " (b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11)," |
| 318 | " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10)," |
| 319 | " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) * " |
| 320 | " (1/det);" |
| 321 | "}" |
| 322 | ).c_str()); |
| 323 | } |
| 324 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 325 | else if (type == *fContext.fFloat4x4_Type || type == *fContext.fHalf4x4_Type) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 326 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 327 | fWrittenIntrinsics.insert(name); |
| 328 | fExtraFunctions.writeText(( |
| 329 | typeName + " " + name + "(" + typeName + " m) {" |
| 330 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 331 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 332 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 333 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 334 | " float b00 = a00 * a11 - a01 * a10;" |
| 335 | " float b01 = a00 * a12 - a02 * a10;" |
| 336 | " float b02 = a00 * a13 - a03 * a10;" |
| 337 | " float b03 = a01 * a12 - a02 * a11;" |
| 338 | " float b04 = a01 * a13 - a03 * a11;" |
| 339 | " float b05 = a02 * a13 - a03 * a12;" |
| 340 | " float b06 = a20 * a31 - a21 * a30;" |
| 341 | " float b07 = a20 * a32 - a22 * a30;" |
| 342 | " float b08 = a20 * a33 - a23 * a30;" |
| 343 | " float b09 = a21 * a32 - a22 * a31;" |
| 344 | " float b10 = a21 * a33 - a23 * a31;" |
| 345 | " float b11 = a22 * a33 - a23 * a32;" |
| 346 | " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - " |
| 347 | " b04 * b07 + b05 * b06;" |
| 348 | " return " + typeName + "(a11 * b11 - a12 * b10 + a13 * b09," |
| 349 | " a02 * b10 - a01 * b11 - a03 * b09," |
| 350 | " a31 * b05 - a32 * b04 + a33 * b03," |
| 351 | " a22 * b04 - a21 * b05 - a23 * b03," |
| 352 | " a12 * b08 - a10 * b11 - a13 * b07," |
| 353 | " a00 * b11 - a02 * b08 + a03 * b07," |
| 354 | " a32 * b02 - a30 * b05 - a33 * b01," |
| 355 | " a20 * b05 - a22 * b02 + a23 * b01," |
| 356 | " a10 * b10 - a11 * b08 + a13 * b06," |
| 357 | " a01 * b08 - a00 * b10 - a03 * b06," |
| 358 | " a30 * b04 - a31 * b02 + a33 * b00," |
| 359 | " a21 * b02 - a20 * b04 - a23 * b00," |
| 360 | " a11 * b07 - a10 * b09 - a12 * b06," |
| 361 | " a00 * b09 - a01 * b07 + a02 * b06," |
| 362 | " a31 * b01 - a30 * b03 - a32 * b00," |
| 363 | " a20 * b03 - a21 * b01 + a22 * b00) / det;" |
| 364 | "}" |
| 365 | ).c_str()); |
| 366 | } |
| 367 | } |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 368 | this->write(name); |
| 369 | } |
| 370 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 371 | void MetalCodeGenerator::writeSpecialIntrinsic(const FunctionCall & c, SpecialIntrinsic kind) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 372 | const ExpressionArray& arguments = c.arguments(); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 373 | switch (kind) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 374 | case kTexture_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 375 | this->writeExpression(*arguments[0], kSequence_Precedence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 376 | this->write(".sample("); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 377 | this->writeExpression(*arguments[0], kSequence_Precedence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 378 | this->write(SAMPLER_SUFFIX); |
| 379 | this->write(", "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 380 | const Type& arg1Type = arguments[1]->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 381 | if (arg1Type == *fContext.fFloat3_Type) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 382 | // have to store the vector in a temp variable to avoid double evaluating it |
| 383 | String tmpVar = "tmpCoord" + to_string(fVarCount++); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 384 | this->fFunctionHeader += " " + this->typeName(arg1Type) + " " + tmpVar + ";\n"; |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 385 | this->write("(" + tmpVar + " = "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 386 | this->writeExpression(*arguments[1], kSequence_Precedence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 387 | this->write(", " + tmpVar + ".xy / " + tmpVar + ".z))"); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 388 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 389 | SkASSERT(arg1Type == *fContext.fFloat2_Type); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 390 | this->writeExpression(*arguments[1], kSequence_Precedence); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 391 | this->write(")"); |
| 392 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 393 | break; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 394 | } |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 395 | case kMod_SpecialIntrinsic: { |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 396 | // fmod(x, y) in metal calculates x - y * trunc(x / y) instead of x - y * floor(x / y) |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 397 | String tmpX = "tmpX" + to_string(fVarCount++); |
| 398 | String tmpY = "tmpY" + to_string(fVarCount++); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 399 | this->fFunctionHeader += " " + this->typeName(arguments[0]->type()) + |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 400 | " " + tmpX + ", " + tmpY + ";\n"; |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 401 | this->write("(" + tmpX + " = "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 402 | this->writeExpression(*arguments[0], kSequence_Precedence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 403 | this->write(", " + tmpY + " = "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 404 | this->writeExpression(*arguments[1], kSequence_Precedence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 405 | this->write(", " + tmpX + " - " + tmpY + " * floor(" + tmpX + " / " + tmpY + "))"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 406 | break; |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 407 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 408 | default: |
| 409 | ABORT("unsupported special intrinsic kind"); |
| 410 | } |
| 411 | } |
| 412 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 413 | // Assembles a matrix of type floatRxC by resizing another matrix named `x0`. |
| 414 | // Cells that don't exist in the source matrix will be populated with identity-matrix values. |
| 415 | void MetalCodeGenerator::assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns) { |
| 416 | SkASSERT(rows <= 4); |
| 417 | SkASSERT(columns <= 4); |
| 418 | |
| 419 | const char* columnSeparator = ""; |
| 420 | for (int c = 0; c < columns; ++c) { |
| 421 | fExtraFunctions.printf("%sfloat%d(", columnSeparator, rows); |
| 422 | columnSeparator = "), "; |
| 423 | |
| 424 | // Determine how many values to take from the source matrix for this row. |
| 425 | int swizzleLength = 0; |
| 426 | if (c < sourceMatrix.columns()) { |
| 427 | swizzleLength = std::min<>(rows, sourceMatrix.rows()); |
| 428 | } |
| 429 | |
| 430 | // Emit all the values from the source matrix row. |
| 431 | bool firstItem; |
| 432 | switch (swizzleLength) { |
| 433 | case 0: firstItem = true; break; |
| 434 | case 1: firstItem = false; fExtraFunctions.printf("x0[%d].x", c); break; |
| 435 | case 2: firstItem = false; fExtraFunctions.printf("x0[%d].xy", c); break; |
| 436 | case 3: firstItem = false; fExtraFunctions.printf("x0[%d].xyz", c); break; |
| 437 | case 4: firstItem = false; fExtraFunctions.printf("x0[%d].xyzw", c); break; |
| 438 | default: SkUNREACHABLE; |
| 439 | } |
| 440 | |
| 441 | // Emit the placeholder identity-matrix cells. |
| 442 | for (int r = swizzleLength; r < rows; ++r) { |
| 443 | fExtraFunctions.printf("%s%s", firstItem ? "" : ", ", (r == c) ? "1.0" : "0.0"); |
| 444 | firstItem = false; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fExtraFunctions.writeText(")"); |
| 449 | } |
| 450 | |
| 451 | // Assembles a matrix of type floatRxC by concatenating an arbitrary mix of values, named `x0`, |
| 452 | // `x1`, etc. An error is written if the expression list don't contain exactly R*C scalars. |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 453 | void MetalCodeGenerator::assembleMatrixFromExpressions(const ExpressionArray& args, |
| 454 | int rows, int columns) { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 455 | size_t argIndex = 0; |
| 456 | int argPosition = 0; |
| 457 | |
| 458 | const char* columnSeparator = ""; |
| 459 | for (int c = 0; c < columns; ++c) { |
| 460 | fExtraFunctions.printf("%sfloat%d(", columnSeparator, rows); |
| 461 | columnSeparator = "), "; |
| 462 | |
| 463 | const char* rowSeparator = ""; |
| 464 | for (int r = 0; r < rows; ++r) { |
| 465 | fExtraFunctions.writeText(rowSeparator); |
| 466 | rowSeparator = ", "; |
| 467 | |
| 468 | if (argIndex < args.size()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 469 | const Type& argType = args[argIndex]->type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 470 | switch (argType.typeKind()) { |
| 471 | case Type::TypeKind::kScalar: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 472 | fExtraFunctions.printf("x%zu", argIndex); |
| 473 | break; |
| 474 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 475 | case Type::TypeKind::kVector: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 476 | fExtraFunctions.printf("x%zu[%d]", argIndex, argPosition); |
| 477 | break; |
| 478 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 479 | case Type::TypeKind::kMatrix: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 480 | fExtraFunctions.printf("x%zu[%d][%d]", argIndex, |
| 481 | argPosition / argType.rows(), |
| 482 | argPosition % argType.rows()); |
| 483 | break; |
| 484 | } |
| 485 | default: { |
| 486 | SkDEBUGFAIL("incorrect type of argument for matrix constructor"); |
| 487 | fExtraFunctions.writeText("<error>"); |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | ++argPosition; |
| 493 | if (argPosition >= argType.columns() * argType.rows()) { |
| 494 | ++argIndex; |
| 495 | argPosition = 0; |
| 496 | } |
| 497 | } else { |
| 498 | SkDEBUGFAIL("not enough arguments for matrix constructor"); |
| 499 | fExtraFunctions.writeText("<error>"); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (argPosition != 0 || argIndex != args.size()) { |
| 505 | SkDEBUGFAIL("incorrect number of arguments for matrix constructor"); |
| 506 | fExtraFunctions.writeText(", <error>"); |
| 507 | } |
| 508 | |
| 509 | fExtraFunctions.writeText(")"); |
| 510 | } |
| 511 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 512 | // Generates a constructor for 'matrix' which reorganizes the input arguments into the proper shape. |
| 513 | // Keeps track of previously generated constructors so that we won't generate more than one |
| 514 | // constructor for any given permutation of input argument types. Returns the name of the |
| 515 | // generated constructor method. |
| 516 | String MetalCodeGenerator::getMatrixConstructHelper(const Constructor& c) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 517 | const Type& matrix = c.type(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 518 | int columns = matrix.columns(); |
| 519 | int rows = matrix.rows(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 520 | const ExpressionArray& args = c.arguments(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 521 | |
| 522 | // Create the helper-method name and use it as our lookup key. |
| 523 | String name; |
| 524 | name.appendf("float%dx%d_from", columns, rows); |
| 525 | for (const std::unique_ptr<Expression>& expr : args) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 526 | name.appendf("_%s", expr->type().displayName().c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // If a helper-method has already been synthesized, we don't need to synthesize it again. |
| 530 | auto [iter, newlyCreated] = fHelpers.insert(name); |
| 531 | if (!newlyCreated) { |
| 532 | return name; |
| 533 | } |
| 534 | |
| 535 | // Unlike GLSL, Metal requires that matrices are initialized with exactly R vectors of C |
| 536 | // components apiece. (In Metal 2.0, you can also supply R*C scalars, but you still cannot |
| 537 | // supply a mixture of scalars and vectors.) |
| 538 | fExtraFunctions.printf("float%dx%d %s(", columns, rows, name.c_str()); |
| 539 | |
| 540 | size_t argIndex = 0; |
| 541 | const char* argSeparator = ""; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 542 | for (const std::unique_ptr<Expression>& expr : args) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 543 | fExtraFunctions.printf("%s%s x%zu", argSeparator, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 544 | expr->type().displayName().c_str(), argIndex++); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 545 | argSeparator = ", "; |
| 546 | } |
| 547 | |
| 548 | fExtraFunctions.printf(") {\n return float%dx%d(", columns, rows); |
| 549 | |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 550 | if (args.size() == 1 && args.front()->type().typeKind() == Type::TypeKind::kMatrix) { |
| 551 | this->assembleMatrixFromMatrix(args.front()->type(), rows, columns); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 552 | } else { |
| 553 | this->assembleMatrixFromExpressions(args, rows, columns); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 554 | } |
| 555 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 556 | fExtraFunctions.writeText(");\n}\n"); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 557 | return name; |
| 558 | } |
| 559 | |
| 560 | bool MetalCodeGenerator::canCoerce(const Type& t1, const Type& t2) { |
| 561 | if (t1.columns() != t2.columns() || t1.rows() != t2.rows()) { |
| 562 | return false; |
| 563 | } |
| 564 | if (t1.columns() > 1) { |
| 565 | return this->canCoerce(t1.componentType(), t2.componentType()); |
| 566 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 567 | return t1.isFloat() && t2.isFloat(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 568 | } |
| 569 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 570 | bool MetalCodeGenerator::matrixConstructHelperIsNeeded(const Constructor& c) { |
| 571 | // A matrix construct helper is only necessary if we are, in fact, constructing a matrix. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 572 | if (c.type().typeKind() != Type::TypeKind::kMatrix) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 573 | return false; |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 574 | } |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 575 | |
| 576 | // GLSL is fairly free-form about inputs to its matrix constructors, but Metal is not; it |
| 577 | // expects exactly R vectors of C components apiece. (Metal 2.0 also allows a list of R*C |
| 578 | // scalars.) Some cases are simple to translate and so we handle those inline--e.g. a list of |
| 579 | // scalars can be constructed trivially. In more complex cases, we generate a helper function |
| 580 | // that converts our inputs into a properly-shaped matrix. |
| 581 | // A matrix construct helper method is always used if any input argument is a matrix. |
| 582 | // Helper methods are also necessary when any argument would span multiple rows. For instance: |
| 583 | // |
| 584 | // float2 x = (1, 2); |
| 585 | // float3x2(x, 3, 4, 5, 6) = | 1 3 5 | = no helper needed; conversion can be done inline |
| 586 | // | 2 4 6 | |
| 587 | // |
| 588 | // float2 x = (2, 3); |
| 589 | // float3x2(1, x, 4, 5, 6) = | 1 3 5 | = x spans multiple rows; a helper method will be used |
| 590 | // | 2 4 6 | |
| 591 | // |
| 592 | // float4 x = (1, 2, 3, 4); |
| 593 | // float2x2(x) = | 1 3 | = x spans multiple rows; a helper method will be used |
| 594 | // | 2 4 | |
| 595 | // |
| 596 | |
| 597 | int position = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 598 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 599 | // If an input argument is a matrix, we need a helper function. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 600 | if (expr->type().typeKind() == Type::TypeKind::kMatrix) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 601 | return true; |
| 602 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 603 | position += expr->type().columns(); |
| 604 | if (position > c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 605 | // An input argument would span multiple rows; a helper function is required. |
| 606 | return true; |
| 607 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 608 | if (position == c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 609 | // We've advanced to the end of a row. Wrap to the start of the next row. |
| 610 | position = 0; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | void MetalCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 618 | const Type& constructorType = c.type(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 619 | // Handle special cases for single-argument constructors. |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 620 | if (c.arguments().size() == 1) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 621 | // If the type is coercible, emit it directly. |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 622 | const Expression& arg = *c.arguments().front(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 623 | const Type& argType = arg.type(); |
| 624 | if (this->canCoerce(constructorType, argType)) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 625 | this->writeExpression(arg, parentPrecedence); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | // Metal supports creating matrices with a scalar on the diagonal via the single-argument |
| 630 | // matrix constructor. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 631 | if (constructorType.typeKind() == Type::TypeKind::kMatrix && argType.isNumber()) { |
| 632 | const Type& matrix = constructorType; |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 633 | this->write("float"); |
| 634 | this->write(to_string(matrix.columns())); |
| 635 | this->write("x"); |
| 636 | this->write(to_string(matrix.rows())); |
| 637 | this->write("("); |
| 638 | this->writeExpression(arg, parentPrecedence); |
| 639 | this->write(")"); |
| 640 | return; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // Emit and invoke a matrix-constructor helper method if one is necessary. |
| 645 | if (this->matrixConstructHelperIsNeeded(c)) { |
| 646 | this->write(this->getMatrixConstructHelper(c)); |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 647 | this->write("("); |
| 648 | const char* separator = ""; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 649 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 650 | this->write(separator); |
| 651 | separator = ", "; |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 652 | this->writeExpression(*expr, kSequence_Precedence); |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 653 | } |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 654 | this->write(")"); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 655 | return; |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 656 | } |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 657 | |
| 658 | // Explicitly invoke the constructor, passing in the necessary arguments. |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 659 | this->writeType(constructorType); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 660 | this->write("("); |
| 661 | const char* separator = ""; |
| 662 | int scalarCount = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 663 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 664 | const Type& argType = arg->type(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 665 | this->write(separator); |
| 666 | separator = ", "; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 667 | if (constructorType.typeKind() == Type::TypeKind::kMatrix && |
| 668 | argType.columns() < constructorType.rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 669 | // Merge scalars and smaller vectors together. |
| 670 | if (!scalarCount) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 671 | this->writeType(constructorType.componentType()); |
| 672 | this->write(to_string(constructorType.rows())); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 673 | this->write("("); |
| 674 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 675 | scalarCount += argType.columns(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 676 | } |
| 677 | this->writeExpression(*arg, kSequence_Precedence); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 678 | if (scalarCount && scalarCount == constructorType.rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 679 | this->write(")"); |
| 680 | scalarCount = 0; |
| 681 | } |
| 682 | } |
| 683 | this->write(")"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | void MetalCodeGenerator::writeFragCoord() { |
Ethan Nicholas | f931e40 | 2019-07-26 15:40:33 -0400 | [diff] [blame] | 687 | if (fRTHeightName.length()) { |
| 688 | this->write("float4(_fragCoord.x, "); |
| 689 | this->write(fRTHeightName.c_str()); |
| 690 | this->write(" - _fragCoord.y, 0.0, _fragCoord.w)"); |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 691 | } else { |
| 692 | this->write("float4(_fragCoord.x, _fragCoord.y, 0.0, _fragCoord.w)"); |
| 693 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void MetalCodeGenerator::writeVariableReference(const VariableReference& ref) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 697 | switch (ref.variable()->modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 698 | case SK_FRAGCOLOR_BUILTIN: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 699 | this->write("_out->sk_FragColor"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 700 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 701 | case SK_FRAGCOORD_BUILTIN: |
| 702 | this->writeFragCoord(); |
| 703 | break; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 704 | case SK_VERTEXID_BUILTIN: |
| 705 | this->write("sk_VertexID"); |
| 706 | break; |
| 707 | case SK_INSTANCEID_BUILTIN: |
| 708 | this->write("sk_InstanceID"); |
| 709 | break; |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 710 | case SK_CLOCKWISE_BUILTIN: |
| 711 | // We'd set the front facing winding in the MTLRenderCommandEncoder to be counter |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 712 | // clockwise to match Skia convention. |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 713 | this->write(fProgram.fSettings.fFlipY ? "_frontFacing" : "(!_frontFacing)"); |
| 714 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 715 | default: |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 716 | const Variable& var = *ref.variable(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 717 | if (var.storage() == Variable::Storage::kGlobal) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 718 | if (var.modifiers().fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 719 | this->write("_in."); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 720 | } else if (var.modifiers().fFlags & Modifiers::kOut_Flag) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 721 | this->write("_out->"); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 722 | } else if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
| 723 | var.type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 724 | this->write("_uniforms."); |
| 725 | } else { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 726 | this->write("_globals->"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 727 | } |
| 728 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 729 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
| 733 | void MetalCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 734 | this->writeExpression(*expr.base(), kPostfix_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 735 | this->write("["); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 736 | this->writeExpression(*expr.index(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 737 | this->write("]"); |
| 738 | } |
| 739 | |
| 740 | void MetalCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 741 | const Type::Field* field = &f.base()->type().fields()[f.fieldIndex()]; |
| 742 | if (FieldAccess::OwnerKind::kDefault == f.ownerKind()) { |
| 743 | this->writeExpression(*f.base(), kPostfix_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 744 | this->write("."); |
| 745 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 746 | switch (field->fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 747 | case SK_POSITION_BUILTIN: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 748 | this->write("_out->sk_Position"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 749 | break; |
| 750 | default: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 751 | if (field->fName == "sk_PointSize") { |
| 752 | this->write("_out->sk_PointSize"); |
| 753 | } else { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 754 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 755 | this->write("_globals->"); |
| 756 | this->write(fInterfaceBlockNameMap[fInterfaceBlockMap[field]]); |
| 757 | this->write("->"); |
| 758 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 759 | this->writeName(field->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 760 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | void MetalCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 765 | this->writeExpression(*swizzle.base(), kPostfix_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 766 | this->write("."); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 767 | for (int c : swizzle.components()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 768 | SkASSERT(c >= 0 && c <= 3); |
| 769 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | MetalCodeGenerator::Precedence MetalCodeGenerator::GetBinaryPrecedence(Token::Kind op) { |
| 774 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 775 | case Token::Kind::TK_STAR: // fall through |
| 776 | case Token::Kind::TK_SLASH: // fall through |
| 777 | case Token::Kind::TK_PERCENT: return MetalCodeGenerator::kMultiplicative_Precedence; |
| 778 | case Token::Kind::TK_PLUS: // fall through |
| 779 | case Token::Kind::TK_MINUS: return MetalCodeGenerator::kAdditive_Precedence; |
| 780 | case Token::Kind::TK_SHL: // fall through |
| 781 | case Token::Kind::TK_SHR: return MetalCodeGenerator::kShift_Precedence; |
| 782 | case Token::Kind::TK_LT: // fall through |
| 783 | case Token::Kind::TK_GT: // fall through |
| 784 | case Token::Kind::TK_LTEQ: // fall through |
| 785 | case Token::Kind::TK_GTEQ: return MetalCodeGenerator::kRelational_Precedence; |
| 786 | case Token::Kind::TK_EQEQ: // fall through |
| 787 | case Token::Kind::TK_NEQ: return MetalCodeGenerator::kEquality_Precedence; |
| 788 | case Token::Kind::TK_BITWISEAND: return MetalCodeGenerator::kBitwiseAnd_Precedence; |
| 789 | case Token::Kind::TK_BITWISEXOR: return MetalCodeGenerator::kBitwiseXor_Precedence; |
| 790 | case Token::Kind::TK_BITWISEOR: return MetalCodeGenerator::kBitwiseOr_Precedence; |
| 791 | case Token::Kind::TK_LOGICALAND: return MetalCodeGenerator::kLogicalAnd_Precedence; |
| 792 | case Token::Kind::TK_LOGICALXOR: return MetalCodeGenerator::kLogicalXor_Precedence; |
| 793 | case Token::Kind::TK_LOGICALOR: return MetalCodeGenerator::kLogicalOr_Precedence; |
| 794 | case Token::Kind::TK_EQ: // fall through |
| 795 | case Token::Kind::TK_PLUSEQ: // fall through |
| 796 | case Token::Kind::TK_MINUSEQ: // fall through |
| 797 | case Token::Kind::TK_STAREQ: // fall through |
| 798 | case Token::Kind::TK_SLASHEQ: // fall through |
| 799 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 800 | case Token::Kind::TK_SHLEQ: // fall through |
| 801 | case Token::Kind::TK_SHREQ: // fall through |
| 802 | case Token::Kind::TK_LOGICALANDEQ: // fall through |
| 803 | case Token::Kind::TK_LOGICALXOREQ: // fall through |
| 804 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 805 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 806 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
| 807 | case Token::Kind::TK_BITWISEOREQ: return MetalCodeGenerator::kAssignment_Precedence; |
| 808 | case Token::Kind::TK_COMMA: return MetalCodeGenerator::kSequence_Precedence; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 809 | default: ABORT("unsupported binary operator"); |
| 810 | } |
| 811 | } |
| 812 | |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 813 | void MetalCodeGenerator::writeMatrixTimesEqualHelper(const Type& left, const Type& right, |
| 814 | const Type& result) { |
| 815 | String key = "TimesEqual" + left.name() + right.name(); |
| 816 | if (fHelpers.find(key) == fHelpers.end()) { |
| 817 | fExtraFunctions.printf("%s operator*=(thread %s& left, thread const %s& right) {\n" |
| 818 | " left = left * right;\n" |
| 819 | " return left;\n" |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 820 | "}", String(result.name()).c_str(), String(left.name()).c_str(), |
| 821 | String(right.name()).c_str()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 825 | void MetalCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 826 | Precedence parentPrecedence) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 827 | const Expression& left = *b.left(); |
| 828 | const Expression& right = *b.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 829 | const Type& leftType = left.type(); |
| 830 | const Type& rightType = right.type(); |
| 831 | Token::Kind op = b.getOperator(); |
| 832 | Precedence precedence = GetBinaryPrecedence(b.getOperator()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 833 | bool needParens = precedence >= parentPrecedence; |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 834 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 835 | case Token::Kind::TK_EQEQ: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 836 | if (leftType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 837 | this->write("all"); |
| 838 | needParens = true; |
| 839 | } |
| 840 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 841 | case Token::Kind::TK_NEQ: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 842 | if (leftType.typeKind() == Type::TypeKind::kVector) { |
Jim Van Verth | 36477b4 | 2019-04-11 14:57:30 -0400 | [diff] [blame] | 843 | this->write("any"); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 844 | needParens = true; |
| 845 | } |
| 846 | break; |
| 847 | default: |
| 848 | break; |
| 849 | } |
| 850 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 851 | this->write("("); |
| 852 | } |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 853 | if (Compiler::IsAssignment(op) && left.is<VariableReference>() && |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 854 | left.as<VariableReference>().variable()->storage() == Variable::Storage::kParameter && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 855 | left.as<VariableReference>().variable()->modifiers().fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 856 | // writing to an out parameter. Since we have to turn those into pointers, we have to |
| 857 | // dereference it here. |
| 858 | this->write("*"); |
| 859 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 860 | if (op == Token::Kind::TK_STAREQ && leftType.typeKind() == Type::TypeKind::kMatrix && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 861 | rightType.typeKind() == Type::TypeKind::kMatrix) { |
| 862 | this->writeMatrixTimesEqualHelper(leftType, rightType, b.type()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 863 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 864 | this->writeExpression(left, precedence); |
| 865 | if (op != Token::Kind::TK_EQ && Compiler::IsAssignment(op) && |
| 866 | left.kind() == Expression::Kind::kSwizzle && !left.hasSideEffects()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 867 | // This doesn't compile in Metal: |
| 868 | // float4 x = float4(1); |
| 869 | // x.xy *= float2x2(...); |
| 870 | // with the error message "non-const reference cannot bind to vector element", |
| 871 | // but switching it to x.xy = x.xy * float2x2(...) fixes it. We perform this tranformation |
| 872 | // as long as the LHS has no side effects, and hope for the best otherwise. |
| 873 | this->write(" = "); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 874 | this->writeExpression(left, kAssignment_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 875 | this->write(" "); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 876 | String opName = Compiler::OperatorName(op); |
| 877 | SkASSERT(opName.endsWith("=")); |
| 878 | this->write(opName.substr(0, opName.size() - 1).c_str()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 879 | this->write(" "); |
| 880 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 881 | this->write(String(" ") + Compiler::OperatorName(op) + " "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 882 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 883 | this->writeExpression(right, precedence); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 884 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 885 | this->write(")"); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | void MetalCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 890 | Precedence parentPrecedence) { |
| 891 | if (kTernary_Precedence >= parentPrecedence) { |
| 892 | this->write("("); |
| 893 | } |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 894 | this->writeExpression(*t.test(), kTernary_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 895 | this->write(" ? "); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 896 | this->writeExpression(*t.ifTrue(), kTernary_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 897 | this->write(" : "); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 898 | this->writeExpression(*t.ifFalse(), kTernary_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 899 | if (kTernary_Precedence >= parentPrecedence) { |
| 900 | this->write(")"); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | void MetalCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 905 | Precedence parentPrecedence) { |
| 906 | if (kPrefix_Precedence >= parentPrecedence) { |
| 907 | this->write("("); |
| 908 | } |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 909 | this->write(Compiler::OperatorName(p.getOperator())); |
| 910 | this->writeExpression(*p.operand(), kPrefix_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 911 | if (kPrefix_Precedence >= parentPrecedence) { |
| 912 | this->write(")"); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | void MetalCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 917 | Precedence parentPrecedence) { |
| 918 | if (kPostfix_Precedence >= parentPrecedence) { |
| 919 | this->write("("); |
| 920 | } |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 921 | this->writeExpression(*p.operand(), kPostfix_Precedence); |
| 922 | this->write(Compiler::OperatorName(p.getOperator())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 923 | if (kPostfix_Precedence >= parentPrecedence) { |
| 924 | this->write(")"); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | void MetalCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 929 | this->write(b.value() ? "true" : "false"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 933 | if (i.type() == *fContext.fUInt_Type) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 934 | this->write(to_string(i.value() & 0xffffffff) + "u"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 935 | } else { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 936 | this->write(to_string((int32_t) i.value())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | void MetalCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 941 | this->write(to_string(f.value())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | void MetalCodeGenerator::writeSetting(const Setting& s) { |
| 945 | ABORT("internal error; setting was not folded to a constant during compilation\n"); |
| 946 | } |
| 947 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 948 | bool MetalCodeGenerator::writeFunctionDeclaration(const FunctionDeclaration& f) { |
Ethan Nicholas | f931e40 | 2019-07-26 15:40:33 -0400 | [diff] [blame] | 949 | fRTHeightName = fProgram.fInputs.fRTHeight ? "_globals->_anonInterface0->u_skRTHeight" : ""; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 950 | const char* separator = ""; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 951 | if ("main" == f.name()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 952 | switch (fProgram.fKind) { |
| 953 | case Program::kFragment_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 954 | this->write("fragment Outputs fragmentMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 955 | break; |
| 956 | case Program::kVertex_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 957 | this->write("vertex Outputs vertexMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 958 | break; |
| 959 | default: |
John Stiles | 3fabfc0 | 2020-09-25 15:51:28 -0400 | [diff] [blame] | 960 | fErrors.error(-1, "unsupported kind of program"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 961 | return false; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 962 | } |
| 963 | this->write("(Inputs _in [[stage_in]]"); |
| 964 | if (-1 != fUniformBuffer) { |
| 965 | this->write(", constant Uniforms& _uniforms [[buffer(" + |
| 966 | to_string(fUniformBuffer) + ")]]"); |
| 967 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 968 | for (const auto& e : fProgram.elements()) { |
| 969 | if (e->is<GlobalVarDeclaration>()) { |
| 970 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 971 | const VarDeclaration& var = decls.declaration()->as<VarDeclaration>(); |
| 972 | if (var.var().type().typeKind() == Type::TypeKind::kSampler) { |
| 973 | if (var.var().modifiers().fLayout.fBinding < 0) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 974 | fErrors.error(decls.fOffset, |
| 975 | "Metal samplers must have 'layout(binding=...)'"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 976 | return false; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 977 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 978 | if (var.var().type().dimensions() != SpvDim2D) { |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 979 | // Not yet implemented--Skia currently only uses 2D textures. |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 980 | fErrors.error(decls.fOffset, "Unsupported texture dimensions"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 981 | return false; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 982 | } |
| 983 | this->write(", texture2d<float> "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 984 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 985 | this->write("[[texture("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 986 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 987 | this->write(")]]"); |
| 988 | this->write(", sampler "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 989 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 990 | this->write(SAMPLER_SUFFIX); |
| 991 | this->write("[[sampler("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 992 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 993 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 994 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 995 | } else if (e->is<InterfaceBlock>()) { |
| 996 | const InterfaceBlock& intf = e->as<InterfaceBlock>(); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 997 | if (intf.typeName() == "sk_PerVertex") { |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 998 | continue; |
| 999 | } |
| 1000 | this->write(", constant "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1001 | this->writeType(intf.variable().type()); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1002 | this->write("& " ); |
| 1003 | this->write(fInterfaceBlockNameMap[&intf]); |
| 1004 | this->write(" [[buffer("); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1005 | this->write(to_string(intf.variable().modifiers().fLayout.fBinding)); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1006 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1007 | } |
| 1008 | } |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 1009 | if (fProgram.fKind == Program::kFragment_Kind) { |
| 1010 | if (fProgram.fInputs.fRTHeight && fInterfaceBlockNameMap.empty()) { |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1011 | this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(1)]]"); |
Ethan Nicholas | f931e40 | 2019-07-26 15:40:33 -0400 | [diff] [blame] | 1012 | fRTHeightName = "_anonInterface0.u_skRTHeight"; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1013 | } |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1014 | this->write(", bool _frontFacing [[front_facing]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1015 | this->write(", float4 _fragCoord [[position]]"); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1016 | } else if (fProgram.fKind == Program::kVertex_Kind) { |
| 1017 | this->write(", uint sk_VertexID [[vertex_id]], uint sk_InstanceID [[instance_id]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1018 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1019 | separator = ", "; |
| 1020 | } else { |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1021 | this->writeType(f.returnType()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1022 | this->write(" "); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1023 | this->writeName(f.name()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1024 | this->write("("); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1025 | Requirements requirements = this->requirements(f); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1026 | if (requirements & kInputs_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1027 | this->write("Inputs _in"); |
| 1028 | separator = ", "; |
| 1029 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1030 | if (requirements & kOutputs_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1031 | this->write(separator); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1032 | this->write("thread Outputs* _out"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1033 | separator = ", "; |
| 1034 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1035 | if (requirements & kUniforms_Requirement) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1036 | this->write(separator); |
| 1037 | this->write("Uniforms _uniforms"); |
| 1038 | separator = ", "; |
| 1039 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1040 | if (requirements & kGlobals_Requirement) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1041 | this->write(separator); |
| 1042 | this->write("thread Globals* _globals"); |
| 1043 | separator = ", "; |
| 1044 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1045 | if (requirements & kFragCoord_Requirement) { |
| 1046 | this->write(separator); |
| 1047 | this->write("float4 _fragCoord"); |
| 1048 | separator = ", "; |
| 1049 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1050 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1051 | for (const auto& param : f.parameters()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1052 | this->write(separator); |
| 1053 | separator = ", "; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1054 | this->writeModifiers(param->modifiers(), false); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1055 | std::vector<int> sizes; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1056 | const Type* type = ¶m->type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1057 | while (type->typeKind() == Type::TypeKind::kArray) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1058 | sizes.push_back(type->columns()); |
| 1059 | type = &type->componentType(); |
| 1060 | } |
| 1061 | this->writeType(*type); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1062 | if (param->modifiers().fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1063 | this->write("*"); |
| 1064 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1065 | this->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1066 | this->writeName(param->name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1067 | for (int s : sizes) { |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 1068 | if (s == Type::kUnsizedArray) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1069 | this->write("[]"); |
| 1070 | } else { |
| 1071 | this->write("[" + to_string(s) + "]"); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1075 | this->write(")"); |
| 1076 | return true; |
| 1077 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1078 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1079 | void MetalCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 1080 | this->writeFunctionDeclaration(f.declaration()); |
| 1081 | this->writeLine(";"); |
| 1082 | } |
| 1083 | |
| 1084 | void MetalCodeGenerator::writeFunction(const FunctionDefinition& f) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1085 | SkASSERT(!fProgram.fSettings.fFragColorIsInOut); |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 1086 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1087 | if (!this->writeFunctionDeclaration(f.declaration())) { |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | this->writeLine(" {"); |
| 1092 | |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1093 | if (f.declaration().name() == "main") { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1094 | this->writeGlobalInit(); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1095 | this->writeLine(" Outputs _outputStruct;"); |
| 1096 | this->writeLine(" thread Outputs* _out = &_outputStruct;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1097 | } |
John Stiles | c67b362 | 2020-05-28 17:53:13 -0400 | [diff] [blame] | 1098 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1099 | fFunctionHeader = ""; |
| 1100 | OutputStream* oldOut = fOut; |
| 1101 | StringStream buffer; |
| 1102 | fOut = &buffer; |
| 1103 | fIndentation++; |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1104 | for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1105 | if (!stmt->isEmpty()) { |
| 1106 | this->writeStatement(*stmt); |
| 1107 | this->writeLine(); |
| 1108 | } |
| 1109 | } |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1110 | if (f.declaration().name() == "main") { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1111 | switch (fProgram.fKind) { |
| 1112 | case Program::kFragment_Kind: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1113 | this->writeLine("return *_out;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1114 | break; |
| 1115 | case Program::kVertex_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1116 | this->writeLine("_out->sk_Position.y = -_out->sk_Position.y;"); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1117 | this->writeLine("return *_out;"); // FIXME - detect if function already has return |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1118 | break; |
| 1119 | default: |
John Stiles | f7d7043 | 2020-05-28 15:46:38 -0400 | [diff] [blame] | 1120 | SkDEBUGFAIL("unsupported kind of program"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | fIndentation--; |
| 1124 | this->writeLine("}"); |
| 1125 | |
| 1126 | fOut = oldOut; |
| 1127 | this->write(fFunctionHeader); |
| 1128 | this->write(buffer.str()); |
| 1129 | } |
| 1130 | |
| 1131 | void MetalCodeGenerator::writeModifiers(const Modifiers& modifiers, |
| 1132 | bool globalContext) { |
| 1133 | if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 1134 | this->write("thread "); |
| 1135 | } |
| 1136 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1137 | this->write("constant "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | void MetalCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1142 | if ("sk_PerVertex" == intf.typeName()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1143 | return; |
| 1144 | } |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1145 | this->writeModifiers(intf.variable().modifiers(), true); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1146 | this->write("struct "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1147 | this->writeLine(intf.typeName() + " {"); |
| 1148 | const Type* structType = &intf.variable().type(); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1149 | fWrittenStructs.push_back(structType); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1150 | while (structType->typeKind() == Type::TypeKind::kArray) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1151 | structType = &structType->componentType(); |
| 1152 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1153 | fIndentation++; |
| 1154 | writeFields(structType->fields(), structType->fOffset, &intf); |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 1155 | if (fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1156 | this->writeLine("float u_skRTHeight;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1157 | } |
| 1158 | fIndentation--; |
| 1159 | this->write("}"); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1160 | if (intf.instanceName().size()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1161 | this->write(" "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1162 | this->write(intf.instanceName()); |
| 1163 | for (const auto& size : intf.sizes()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1164 | this->write("["); |
| 1165 | if (size) { |
| 1166 | this->writeExpression(*size, kTopLevel_Precedence); |
| 1167 | } |
| 1168 | this->write("]"); |
| 1169 | } |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1170 | fInterfaceBlockNameMap[&intf] = intf.instanceName(); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1171 | } else { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1172 | fInterfaceBlockNameMap[&intf] = "_anonInterface" + to_string(fAnonInterfaceCount++); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1173 | } |
| 1174 | this->writeLine(";"); |
| 1175 | } |
| 1176 | |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1177 | void MetalCodeGenerator::writeFields(const std::vector<Type::Field>& fields, int parentOffset, |
| 1178 | const InterfaceBlock* parentIntf) { |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1179 | MemoryLayout memoryLayout(MemoryLayout::kMetal_Standard); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1180 | int currentOffset = 0; |
| 1181 | for (const auto& field: fields) { |
| 1182 | int fieldOffset = field.fModifiers.fLayout.fOffset; |
| 1183 | const Type* fieldType = field.fType; |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 1184 | if (!memoryLayout.layoutIsSupported(*fieldType)) { |
| 1185 | fErrors.error(parentOffset, "type '" + fieldType->name() + "' is not permitted here"); |
| 1186 | return; |
| 1187 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1188 | if (fieldOffset != -1) { |
| 1189 | if (currentOffset > fieldOffset) { |
| 1190 | fErrors.error(parentOffset, |
| 1191 | "offset of field '" + field.fName + "' must be at least " + |
| 1192 | to_string((int) currentOffset)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1193 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1194 | } else if (currentOffset < fieldOffset) { |
| 1195 | this->write("char pad"); |
| 1196 | this->write(to_string(fPaddingCount++)); |
| 1197 | this->write("["); |
| 1198 | this->write(to_string(fieldOffset - currentOffset)); |
| 1199 | this->writeLine("];"); |
| 1200 | currentOffset = fieldOffset; |
| 1201 | } |
| 1202 | int alignment = memoryLayout.alignment(*fieldType); |
| 1203 | if (fieldOffset % alignment) { |
| 1204 | fErrors.error(parentOffset, |
| 1205 | "offset of field '" + field.fName + "' must be a multiple of " + |
| 1206 | to_string((int) alignment)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1207 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1208 | } |
| 1209 | } |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1210 | size_t fieldSize = memoryLayout.size(*fieldType); |
| 1211 | if (fieldSize > static_cast<size_t>(std::numeric_limits<int>::max() - currentOffset)) { |
| 1212 | fErrors.error(parentOffset, "field offset overflow"); |
| 1213 | return; |
| 1214 | } |
| 1215 | currentOffset += fieldSize; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1216 | std::vector<int> sizes; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1217 | while (fieldType->typeKind() == Type::TypeKind::kArray) { |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1218 | sizes.push_back(fieldType->columns()); |
| 1219 | fieldType = &fieldType->componentType(); |
| 1220 | } |
| 1221 | this->writeModifiers(field.fModifiers, false); |
| 1222 | this->writeType(*fieldType); |
| 1223 | this->write(" "); |
| 1224 | this->writeName(field.fName); |
| 1225 | for (int s : sizes) { |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 1226 | if (s == Type::kUnsizedArray) { |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1227 | this->write("[]"); |
| 1228 | } else { |
| 1229 | this->write("[" + to_string(s) + "]"); |
| 1230 | } |
| 1231 | } |
| 1232 | this->writeLine(";"); |
| 1233 | if (parentIntf) { |
| 1234 | fInterfaceBlockMap[&field] = parentIntf; |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1239 | void MetalCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 1240 | this->writeExpression(value, kTopLevel_Precedence); |
| 1241 | } |
| 1242 | |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1243 | void MetalCodeGenerator::writeName(const String& name) { |
| 1244 | if (fReservedWords.find(name) != fReservedWords.end()) { |
| 1245 | this->write("_"); // adding underscore before name to avoid conflict with reserved words |
| 1246 | } |
| 1247 | this->write(name); |
| 1248 | } |
| 1249 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1250 | void MetalCodeGenerator::writeVarDeclaration(const VarDeclaration& var, bool global) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1251 | if (global && !(var.var().modifiers().fFlags & Modifiers::kConst_Flag)) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1252 | return; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1253 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1254 | this->writeModifiers(var.var().modifiers(), global); |
| 1255 | this->writeType(var.baseType()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1256 | this->write(" "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1257 | this->writeName(var.var().name()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1258 | for (const std::unique_ptr<Expression>& size : var.sizes()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1259 | this->write("["); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1260 | if (size) { |
| 1261 | this->writeExpression(*size, kTopLevel_Precedence); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1262 | } |
| 1263 | this->write("]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1264 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1265 | if (var.value()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1266 | this->write(" = "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1267 | this->writeVarInitializer(var.var(), *var.value()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1268 | } |
| 1269 | this->write(";"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | void MetalCodeGenerator::writeStatement(const Statement& s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1273 | switch (s.kind()) { |
| 1274 | case Statement::Kind::kBlock: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1275 | this->writeBlock(s.as<Block>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1276 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1277 | case Statement::Kind::kExpression: |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1278 | this->writeExpression(*s.as<ExpressionStatement>().expression(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1279 | this->write(";"); |
| 1280 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1281 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1282 | this->writeReturnStatement(s.as<ReturnStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1283 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1284 | case Statement::Kind::kVarDeclaration: |
| 1285 | this->writeVarDeclaration(s.as<VarDeclaration>(), false); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1286 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1287 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1288 | this->writeIfStatement(s.as<IfStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1289 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1290 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1291 | this->writeForStatement(s.as<ForStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1292 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1293 | case Statement::Kind::kWhile: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1294 | this->writeWhileStatement(s.as<WhileStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1295 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1296 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1297 | this->writeDoStatement(s.as<DoStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1298 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1299 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1300 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1301 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1302 | case Statement::Kind::kBreak: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1303 | this->write("break;"); |
| 1304 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1305 | case Statement::Kind::kContinue: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1306 | this->write("continue;"); |
| 1307 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1308 | case Statement::Kind::kDiscard: |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1309 | this->write("discard_fragment();"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1310 | break; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1311 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1312 | case Statement::Kind::kNop: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1313 | this->write(";"); |
| 1314 | break; |
| 1315 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1316 | #ifdef SK_DEBUG |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1317 | ABORT("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1318 | #endif |
| 1319 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1323 | void MetalCodeGenerator::writeBlock(const Block& b) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1324 | bool isScope = b.isScope(); |
| 1325 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1326 | this->writeLine("{"); |
| 1327 | fIndentation++; |
| 1328 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1329 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 1330 | if (!stmt->isEmpty()) { |
| 1331 | this->writeStatement(*stmt); |
| 1332 | this->writeLine(); |
| 1333 | } |
| 1334 | } |
| 1335 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1336 | fIndentation--; |
| 1337 | this->write("}"); |
| 1338 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | void MetalCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1342 | this->write("if ("); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1343 | this->writeExpression(*stmt.test(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1344 | this->write(") "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1345 | this->writeStatement(*stmt.ifTrue()); |
| 1346 | if (stmt.ifFalse()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1347 | this->write(" else "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1348 | this->writeStatement(*stmt.ifFalse()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | void MetalCodeGenerator::writeForStatement(const ForStatement& f) { |
| 1353 | this->write("for ("); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1354 | if (f.initializer() && !f.initializer()->isEmpty()) { |
| 1355 | this->writeStatement(*f.initializer()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1356 | } else { |
| 1357 | this->write("; "); |
| 1358 | } |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1359 | if (f.test()) { |
| 1360 | this->writeExpression(*f.test(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1361 | } |
| 1362 | this->write("; "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1363 | if (f.next()) { |
| 1364 | this->writeExpression(*f.next(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1365 | } |
| 1366 | this->write(") "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1367 | this->writeStatement(*f.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | void MetalCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 1371 | this->write("while ("); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1372 | this->writeExpression(*w.test(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1373 | this->write(") "); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1374 | this->writeStatement(*w.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | void MetalCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 1378 | this->write("do "); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1379 | this->writeStatement(*d.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1380 | this->write(" while ("); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1381 | this->writeExpression(*d.test(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1382 | this->write(");"); |
| 1383 | } |
| 1384 | |
| 1385 | void MetalCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1386 | this->write("switch ("); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1387 | this->writeExpression(*s.value(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1388 | this->writeLine(") {"); |
| 1389 | fIndentation++; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1390 | for (const std::unique_ptr<SwitchCase>& c : s.cases()) { |
| 1391 | if (c->value()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1392 | this->write("case "); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1393 | this->writeExpression(*c->value(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1394 | this->writeLine(":"); |
| 1395 | } else { |
| 1396 | this->writeLine("default:"); |
| 1397 | } |
| 1398 | fIndentation++; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1399 | for (const auto& stmt : c->statements()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1400 | this->writeStatement(*stmt); |
| 1401 | this->writeLine(); |
| 1402 | } |
| 1403 | fIndentation--; |
| 1404 | } |
| 1405 | fIndentation--; |
| 1406 | this->write("}"); |
| 1407 | } |
| 1408 | |
| 1409 | void MetalCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 1410 | this->write("return"); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1411 | if (r.expression()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1412 | this->write(" "); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1413 | this->writeExpression(*r.expression(), kTopLevel_Precedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1414 | } |
| 1415 | this->write(";"); |
| 1416 | } |
| 1417 | |
| 1418 | void MetalCodeGenerator::writeHeader() { |
| 1419 | this->write("#include <metal_stdlib>\n"); |
| 1420 | this->write("#include <simd/simd.h>\n"); |
| 1421 | this->write("using namespace metal;\n"); |
| 1422 | } |
| 1423 | |
| 1424 | void MetalCodeGenerator::writeUniformStruct() { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1425 | for (const auto& e : fProgram.elements()) { |
| 1426 | if (e->is<GlobalVarDeclaration>()) { |
| 1427 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1428 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1429 | if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1430 | var.type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1431 | if (-1 == fUniformBuffer) { |
| 1432 | this->write("struct Uniforms {\n"); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1433 | fUniformBuffer = var.modifiers().fLayout.fSet; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1434 | if (-1 == fUniformBuffer) { |
| 1435 | fErrors.error(decls.fOffset, "Metal uniforms must have 'layout(set=...)'"); |
| 1436 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1437 | } else if (var.modifiers().fLayout.fSet != fUniformBuffer) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1438 | if (-1 == fUniformBuffer) { |
| 1439 | fErrors.error(decls.fOffset, "Metal backend requires all uniforms to have " |
| 1440 | "the same 'layout(set=...)'"); |
| 1441 | } |
| 1442 | } |
| 1443 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1444 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1445 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1446 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1447 | this->write(";\n"); |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | if (-1 != fUniformBuffer) { |
| 1452 | this->write("};\n"); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | void MetalCodeGenerator::writeInputStruct() { |
| 1457 | this->write("struct Inputs {\n"); |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1458 | for (const auto& e : fProgram.elements()) { |
| 1459 | if (e->is<GlobalVarDeclaration>()) { |
| 1460 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1461 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1462 | if (var.modifiers().fFlags & Modifiers::kIn_Flag && |
| 1463 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1464 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1465 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1466 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1467 | this->writeName(var.name()); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1468 | if (-1 != var.modifiers().fLayout.fLocation) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1469 | if (fProgram.fKind == Program::kVertex_Kind) { |
| 1470 | this->write(" [[attribute(" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1471 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1472 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
| 1473 | this->write(" [[user(locn" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1474 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | this->write(";\n"); |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | this->write("};\n"); |
| 1482 | } |
| 1483 | |
| 1484 | void MetalCodeGenerator::writeOutputStruct() { |
| 1485 | this->write("struct Outputs {\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1486 | if (fProgram.fKind == Program::kVertex_Kind) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1487 | this->write(" float4 sk_Position [[position]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1488 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 1489 | this->write(" float4 sk_FragColor [[color(0)]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1490 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1491 | for (const auto& e : fProgram.elements()) { |
| 1492 | if (e->is<GlobalVarDeclaration>()) { |
| 1493 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1494 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1495 | if (var.modifiers().fFlags & Modifiers::kOut_Flag && |
| 1496 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1497 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1498 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1499 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1500 | this->writeName(var.name()); |
| 1501 | if (fProgram.fKind == Program::kVertex_Kind) { |
| 1502 | this->write(" [[user(locn" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1503 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1504 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
| 1505 | this->write(" [[color(" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1506 | to_string(var.modifiers().fLayout.fLocation) +")"); |
| 1507 | int colorIndex = var.modifiers().fLayout.fIndex; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1508 | if (colorIndex) { |
| 1509 | this->write(", index(" + to_string(colorIndex) + ")"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1510 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1511 | this->write("]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1512 | } |
| 1513 | this->write(";\n"); |
| 1514 | } |
| 1515 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1516 | } |
| 1517 | if (fProgram.fKind == Program::kVertex_Kind) { |
Jim Van Verth | 3913d3e | 2020-08-31 15:16:57 -0400 | [diff] [blame] | 1518 | this->write(" float sk_PointSize [[point_size]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1519 | } |
| 1520 | this->write("};\n"); |
| 1521 | } |
| 1522 | |
| 1523 | void MetalCodeGenerator::writeInterfaceBlocks() { |
| 1524 | bool wroteInterfaceBlock = false; |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1525 | for (const auto& e : fProgram.elements()) { |
| 1526 | if (e->is<InterfaceBlock>()) { |
| 1527 | this->writeInterfaceBlock(e->as<InterfaceBlock>()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1528 | wroteInterfaceBlock = true; |
| 1529 | } |
| 1530 | } |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 1531 | if (!wroteInterfaceBlock && fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1532 | this->writeLine("struct sksl_synthetic_uniforms {"); |
| 1533 | this->writeLine(" float u_skRTHeight;"); |
| 1534 | this->writeLine("};"); |
| 1535 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1538 | void MetalCodeGenerator::visitGlobalStruct(GlobalStructVisitor* visitor) { |
| 1539 | // Visit the interface blocks. |
| 1540 | for (const auto& [interfaceType, interfaceName] : fInterfaceBlockNameMap) { |
| 1541 | visitor->VisitInterfaceBlock(*interfaceType, interfaceName); |
| 1542 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1543 | for (const auto& element : fProgram.elements()) { |
| 1544 | if (!element->is<GlobalVarDeclaration>()) { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1545 | continue; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1546 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1547 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 1548 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 1549 | const Variable& var = decl.var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1550 | if ((!var.modifiers().fFlags && -1 == var.modifiers().fLayout.fBuiltin) || |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1551 | var.type().typeKind() == Type::TypeKind::kSampler) { |
| 1552 | if (var.type().typeKind() == Type::TypeKind::kSampler) { |
| 1553 | // Samplers are represented as a "texture/sampler" duo in the global struct. |
| 1554 | visitor->VisitTexture(var.type(), var.name()); |
| 1555 | visitor->VisitSampler(var.type(), String(var.name()) + SAMPLER_SUFFIX); |
| 1556 | } else { |
| 1557 | // Visit a regular variable. |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1558 | visitor->VisitVariable(var, decl.value().get()); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | } |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | void MetalCodeGenerator::writeGlobalStruct() { |
| 1565 | class : public GlobalStructVisitor { |
| 1566 | public: |
| 1567 | void VisitInterfaceBlock(const InterfaceBlock& block, const String& blockName) override { |
| 1568 | this->AddElement(); |
| 1569 | fCodeGen->write(" constant "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1570 | fCodeGen->write(block.typeName()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1571 | fCodeGen->write("* "); |
| 1572 | fCodeGen->writeName(blockName); |
| 1573 | fCodeGen->write(";\n"); |
| 1574 | } |
| 1575 | void VisitTexture(const Type& type, const String& name) override { |
| 1576 | this->AddElement(); |
| 1577 | fCodeGen->write(" "); |
| 1578 | fCodeGen->writeType(type); |
| 1579 | fCodeGen->write(" "); |
| 1580 | fCodeGen->writeName(name); |
| 1581 | fCodeGen->write(";\n"); |
| 1582 | } |
| 1583 | void VisitSampler(const Type&, const String& name) override { |
| 1584 | this->AddElement(); |
| 1585 | fCodeGen->write(" sampler "); |
| 1586 | fCodeGen->writeName(name); |
| 1587 | fCodeGen->write(";\n"); |
| 1588 | } |
| 1589 | void VisitVariable(const Variable& var, const Expression* value) override { |
| 1590 | this->AddElement(); |
| 1591 | fCodeGen->write(" "); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1592 | fCodeGen->writeType(var.type()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1593 | fCodeGen->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1594 | fCodeGen->writeName(var.name()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1595 | fCodeGen->write(";\n"); |
| 1596 | } |
| 1597 | void AddElement() { |
| 1598 | if (fFirst) { |
| 1599 | fCodeGen->write("struct Globals {\n"); |
| 1600 | fFirst = false; |
| 1601 | } |
| 1602 | } |
| 1603 | void Finish() { |
| 1604 | if (!fFirst) { |
| 1605 | fCodeGen->write("};"); |
| 1606 | fFirst = true; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | MetalCodeGenerator* fCodeGen = nullptr; |
| 1611 | bool fFirst = true; |
| 1612 | } visitor; |
| 1613 | |
| 1614 | visitor.fCodeGen = this; |
| 1615 | this->visitGlobalStruct(&visitor); |
| 1616 | visitor.Finish(); |
| 1617 | } |
| 1618 | |
| 1619 | void MetalCodeGenerator::writeGlobalInit() { |
| 1620 | class : public GlobalStructVisitor { |
| 1621 | public: |
| 1622 | void VisitInterfaceBlock(const InterfaceBlock& blockType, |
| 1623 | const String& blockName) override { |
| 1624 | this->AddElement(); |
| 1625 | fCodeGen->write("&"); |
| 1626 | fCodeGen->writeName(blockName); |
| 1627 | } |
| 1628 | void VisitTexture(const Type&, const String& name) override { |
| 1629 | this->AddElement(); |
| 1630 | fCodeGen->writeName(name); |
| 1631 | } |
| 1632 | void VisitSampler(const Type&, const String& name) override { |
| 1633 | this->AddElement(); |
| 1634 | fCodeGen->writeName(name); |
| 1635 | } |
| 1636 | void VisitVariable(const Variable& var, const Expression* value) override { |
| 1637 | this->AddElement(); |
| 1638 | if (value) { |
| 1639 | fCodeGen->writeVarInitializer(var, *value); |
| 1640 | } else { |
| 1641 | fCodeGen->write("{}"); |
| 1642 | } |
| 1643 | } |
| 1644 | void AddElement() { |
| 1645 | if (fFirst) { |
| 1646 | fCodeGen->write(" Globals globalStruct{"); |
| 1647 | fFirst = false; |
| 1648 | } else { |
| 1649 | fCodeGen->write(", "); |
| 1650 | } |
| 1651 | } |
| 1652 | void Finish() { |
| 1653 | if (!fFirst) { |
| 1654 | fCodeGen->writeLine("};"); |
| 1655 | fCodeGen->writeLine(" thread Globals* _globals = &globalStruct;"); |
| 1656 | fCodeGen->writeLine(" (void)_globals;"); |
| 1657 | } |
| 1658 | } |
| 1659 | MetalCodeGenerator* fCodeGen = nullptr; |
| 1660 | bool fFirst = true; |
| 1661 | } visitor; |
| 1662 | |
| 1663 | visitor.fCodeGen = this; |
| 1664 | this->visitGlobalStruct(&visitor); |
| 1665 | visitor.Finish(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1666 | } |
| 1667 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1668 | void MetalCodeGenerator::writeProgramElement(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1669 | switch (e.kind()) { |
| 1670 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1671 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1672 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1673 | const GlobalVarDeclaration& global = e.as<GlobalVarDeclaration>(); |
| 1674 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 1675 | int builtin = decl.var().modifiers().fLayout.fBuiltin; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1676 | if (-1 == builtin) { |
| 1677 | // normal var |
| 1678 | this->writeVarDeclaration(decl, true); |
| 1679 | this->writeLine(); |
| 1680 | } else if (SK_FRAGCOLOR_BUILTIN == builtin) { |
| 1681 | // ignore |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1682 | } |
| 1683 | break; |
| 1684 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1685 | case ProgramElement::Kind::kInterfaceBlock: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1686 | // handled in writeInterfaceBlocks, do nothing |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1687 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1688 | case ProgramElement::Kind::kFunction: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1689 | this->writeFunction(e.as<FunctionDefinition>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1690 | break; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1691 | case ProgramElement::Kind::kFunctionPrototype: |
| 1692 | this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
| 1693 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1694 | case ProgramElement::Kind::kModifiers: |
Ethan Nicholas | 077050b | 2020-10-13 10:30:20 -0400 | [diff] [blame] | 1695 | this->writeModifiers(e.as<ModifiersDeclaration>().modifiers(), true); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1696 | this->writeLine(";"); |
| 1697 | break; |
| 1698 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1699 | #ifdef SK_DEBUG |
| 1700 | ABORT("unsupported program element: %s\n", e.description().c_str()); |
| 1701 | #endif |
| 1702 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1706 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Expression* e) { |
| 1707 | if (!e) { |
| 1708 | return kNo_Requirements; |
| 1709 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1710 | switch (e->kind()) { |
| 1711 | case Expression::Kind::kFunctionCall: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1712 | const FunctionCall& f = e->as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1713 | Requirements result = this->requirements(f.function()); |
| 1714 | for (const auto& arg : f.arguments()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1715 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1716 | } |
| 1717 | return result; |
| 1718 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1719 | case Expression::Kind::kConstructor: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1720 | const Constructor& c = e->as<Constructor>(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1721 | Requirements result = kNo_Requirements; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1722 | for (const auto& arg : c.arguments()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1723 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1724 | } |
| 1725 | return result; |
| 1726 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1727 | case Expression::Kind::kFieldAccess: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1728 | const FieldAccess& f = e->as<FieldAccess>(); |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1729 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1730 | return kGlobals_Requirement; |
| 1731 | } |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1732 | return this->requirements(f.base().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1733 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1734 | case Expression::Kind::kSwizzle: |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1735 | return this->requirements(e->as<Swizzle>().base().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1736 | case Expression::Kind::kBinary: { |
| 1737 | const BinaryExpression& bin = e->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1738 | return this->requirements(bin.left().get()) | |
| 1739 | this->requirements(bin.right().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1740 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1741 | case Expression::Kind::kIndex: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1742 | const IndexExpression& idx = e->as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1743 | return this->requirements(idx.base().get()) | this->requirements(idx.index().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1744 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1745 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 1746 | return this->requirements(e->as<PrefixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1747 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 1748 | return this->requirements(e->as<PostfixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1749 | case Expression::Kind::kTernary: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1750 | const TernaryExpression& t = e->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 1751 | return this->requirements(t.test().get()) | this->requirements(t.ifTrue().get()) | |
| 1752 | this->requirements(t.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1753 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1754 | case Expression::Kind::kVariableReference: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1755 | const VariableReference& v = e->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1756 | const Modifiers& modifiers = v.variable()->modifiers(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1757 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1758 | if (modifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 1759 | result = kGlobals_Requirement | kFragCoord_Requirement; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1760 | } else if (Variable::Storage::kGlobal == v.variable()->storage()) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1761 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1762 | result = kInputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1763 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1764 | result = kOutputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1765 | } else if (modifiers.fFlags & Modifiers::kUniform_Flag && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1766 | v.variable()->type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1767 | result = kUniforms_Requirement; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1768 | } else { |
| 1769 | result = kGlobals_Requirement; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | return result; |
| 1773 | } |
| 1774 | default: |
| 1775 | return kNo_Requirements; |
| 1776 | } |
| 1777 | } |
| 1778 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1779 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Statement* s) { |
| 1780 | if (!s) { |
| 1781 | return kNo_Requirements; |
| 1782 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1783 | switch (s->kind()) { |
| 1784 | case Statement::Kind::kBlock: { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1785 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1786 | for (const std::unique_ptr<Statement>& child : s->as<Block>().children()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1787 | result |= this->requirements(child.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1788 | } |
| 1789 | return result; |
| 1790 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1791 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1792 | const VarDeclaration& var = s->as<VarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1793 | return this->requirements(var.value().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1794 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1795 | case Statement::Kind::kExpression: |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1796 | return this->requirements(s->as<ExpressionStatement>().expression().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1797 | case Statement::Kind::kReturn: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1798 | const ReturnStatement& r = s->as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1799 | return this->requirements(r.expression().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1800 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1801 | case Statement::Kind::kIf: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1802 | const IfStatement& i = s->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1803 | return this->requirements(i.test().get()) | |
| 1804 | this->requirements(i.ifTrue().get()) | |
| 1805 | this->requirements(i.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1806 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1807 | case Statement::Kind::kFor: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1808 | const ForStatement& f = s->as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1809 | return this->requirements(f.initializer().get()) | |
| 1810 | this->requirements(f.test().get()) | |
| 1811 | this->requirements(f.next().get()) | |
| 1812 | this->requirements(f.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1813 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1814 | case Statement::Kind::kWhile: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1815 | const WhileStatement& w = s->as<WhileStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1816 | return this->requirements(w.test().get()) | |
| 1817 | this->requirements(w.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1818 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1819 | case Statement::Kind::kDo: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1820 | const DoStatement& d = s->as<DoStatement>(); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1821 | return this->requirements(d.test().get()) | |
| 1822 | this->requirements(d.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1823 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1824 | case Statement::Kind::kSwitch: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 1825 | const SwitchStatement& sw = s->as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1826 | Requirements result = this->requirements(sw.value().get()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1827 | for (const std::unique_ptr<SwitchCase>& sc : sw.cases()) { |
| 1828 | for (const auto& st : sc->statements()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 1829 | result |= this->requirements(st.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | return result; |
| 1833 | } |
| 1834 | default: |
| 1835 | return kNo_Requirements; |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const FunctionDeclaration& f) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1840 | if (f.isBuiltin()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1841 | return kNo_Requirements; |
| 1842 | } |
| 1843 | auto found = fRequirements.find(&f); |
| 1844 | if (found == fRequirements.end()) { |
Ethan Nicholas | 65a8f56 | 2019-04-19 14:00:26 -0400 | [diff] [blame] | 1845 | fRequirements[&f] = kNo_Requirements; |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1846 | for (const auto& e : fProgram.elements()) { |
| 1847 | if (e->is<FunctionDefinition>()) { |
| 1848 | const FunctionDefinition& def = e->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1849 | if (&def.declaration() == &f) { |
| 1850 | Requirements reqs = this->requirements(def.body().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1851 | fRequirements[&f] = reqs; |
| 1852 | return reqs; |
| 1853 | } |
| 1854 | } |
| 1855 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1856 | // We never found a definition for this declared function, but it's legal to prototype a |
| 1857 | // function without ever giving a definition, as long as you don't call it. |
| 1858 | return kNo_Requirements; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1859 | } |
| 1860 | return found->second; |
| 1861 | } |
| 1862 | |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1863 | bool MetalCodeGenerator::generateCode() { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1864 | OutputStream* rawOut = fOut; |
| 1865 | fOut = &fHeader; |
| 1866 | fProgramKind = fProgram.fKind; |
| 1867 | this->writeHeader(); |
| 1868 | this->writeUniformStruct(); |
| 1869 | this->writeInputStruct(); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1870 | this->writeOutputStruct(); |
| 1871 | this->writeInterfaceBlocks(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1872 | this->writeGlobalStruct(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1873 | StringStream body; |
| 1874 | fOut = &body; |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1875 | for (const auto& e : fProgram.elements()) { |
| 1876 | this->writeProgramElement(*e); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1877 | } |
| 1878 | fOut = rawOut; |
| 1879 | |
| 1880 | write_stringstream(fHeader, *rawOut); |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 1881 | write_stringstream(fExtraFunctions, *rawOut); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1882 | write_stringstream(body, *rawOut); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1883 | return 0 == fErrors.errorCount(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1884 | } |
| 1885 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1886 | } // namespace SkSL |