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