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