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