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