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" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLConstructorArray.h" |
| 14 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLConstructorVectorCast.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 19 | #include "src/sksl/ir/SkSLExtension.h" |
| 20 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 21 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 22 | #include "src/sksl/ir/SkSLNop.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 25 | |
Brian Osman | c262a12 | 2020-08-06 16:34:34 -0400 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 28 | namespace SkSL { |
| 29 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 30 | const char* MetalCodeGenerator::OperatorName(Operator op) { |
| 31 | switch (op.kind()) { |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 32 | case Token::Kind::TK_LOGICALXOR: return "!="; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 33 | default: return op.operatorName(); |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 37 | class MetalCodeGenerator::GlobalStructVisitor { |
| 38 | public: |
| 39 | virtual ~GlobalStructVisitor() = default; |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 40 | virtual void visitInterfaceBlock(const InterfaceBlock& block, const String& blockName) = 0; |
| 41 | virtual void visitTexture(const Type& type, const String& name) = 0; |
| 42 | virtual void visitSampler(const Type& type, const String& name) = 0; |
| 43 | virtual void visitVariable(const Variable& var, const Expression* value) = 0; |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 46 | void MetalCodeGenerator::setupIntrinsics() { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 47 | fIntrinsicMap[String("atan")] = kAtan_IntrinsicKind; |
| 48 | fIntrinsicMap[String("floatBitsToInt")] = kBitcast_IntrinsicKind; |
| 49 | fIntrinsicMap[String("floatBitsToUint")] = kBitcast_IntrinsicKind; |
| 50 | fIntrinsicMap[String("intBitsToFloat")] = kBitcast_IntrinsicKind; |
| 51 | fIntrinsicMap[String("uintBitsToFloat")] = kBitcast_IntrinsicKind; |
| 52 | fIntrinsicMap[String("equal")] = kCompareEqual_IntrinsicKind; |
| 53 | fIntrinsicMap[String("notEqual")] = kCompareNotEqual_IntrinsicKind; |
| 54 | fIntrinsicMap[String("lessThan")] = kCompareLessThan_IntrinsicKind; |
| 55 | fIntrinsicMap[String("lessThanEqual")] = kCompareLessThanEqual_IntrinsicKind; |
| 56 | fIntrinsicMap[String("greaterThan")] = kCompareGreaterThan_IntrinsicKind; |
| 57 | fIntrinsicMap[String("greaterThanEqual")] = kCompareGreaterThanEqual_IntrinsicKind; |
| 58 | fIntrinsicMap[String("degrees")] = kDegrees_IntrinsicKind; |
| 59 | fIntrinsicMap[String("dFdx")] = kDFdx_IntrinsicKind; |
| 60 | fIntrinsicMap[String("dFdy")] = kDFdy_IntrinsicKind; |
| 61 | fIntrinsicMap[String("distance")] = kDistance_IntrinsicKind; |
| 62 | fIntrinsicMap[String("dot")] = kDot_IntrinsicKind; |
| 63 | fIntrinsicMap[String("faceforward")] = kFaceforward_IntrinsicKind; |
| 64 | fIntrinsicMap[String("bitCount")] = kBitCount_IntrinsicKind; |
| 65 | fIntrinsicMap[String("findLSB")] = kFindLSB_IntrinsicKind; |
| 66 | fIntrinsicMap[String("findMSB")] = kFindMSB_IntrinsicKind; |
| 67 | fIntrinsicMap[String("inverse")] = kInverse_IntrinsicKind; |
| 68 | fIntrinsicMap[String("inversesqrt")] = kInversesqrt_IntrinsicKind; |
| 69 | fIntrinsicMap[String("length")] = kLength_IntrinsicKind; |
| 70 | fIntrinsicMap[String("matrixCompMult")] = kMatrixCompMult_IntrinsicKind; |
| 71 | fIntrinsicMap[String("mod")] = kMod_IntrinsicKind; |
| 72 | fIntrinsicMap[String("normalize")] = kNormalize_IntrinsicKind; |
| 73 | fIntrinsicMap[String("radians")] = kRadians_IntrinsicKind; |
| 74 | fIntrinsicMap[String("reflect")] = kReflect_IntrinsicKind; |
| 75 | fIntrinsicMap[String("refract")] = kRefract_IntrinsicKind; |
John Stiles | 2345653 | 2020-12-30 18:12:48 -0500 | [diff] [blame] | 76 | fIntrinsicMap[String("roundEven")] = kRoundEven_IntrinsicKind; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 77 | fIntrinsicMap[String("sample")] = kTexture_IntrinsicKind; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 80 | void MetalCodeGenerator::write(const char* s) { |
| 81 | if (!s[0]) { |
| 82 | return; |
| 83 | } |
| 84 | if (fAtLineStart) { |
| 85 | for (int i = 0; i < fIndentation; i++) { |
| 86 | fOut->writeText(" "); |
| 87 | } |
| 88 | } |
| 89 | fOut->writeText(s); |
| 90 | fAtLineStart = false; |
| 91 | } |
| 92 | |
| 93 | void MetalCodeGenerator::writeLine(const char* s) { |
| 94 | this->write(s); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 95 | this->writeLine(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void MetalCodeGenerator::write(const String& s) { |
| 99 | this->write(s.c_str()); |
| 100 | } |
| 101 | |
| 102 | void MetalCodeGenerator::writeLine(const String& s) { |
| 103 | this->writeLine(s.c_str()); |
| 104 | } |
| 105 | |
| 106 | void MetalCodeGenerator::writeLine() { |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 107 | fOut->writeText(fLineEnding); |
| 108 | fAtLineStart = true; |
| 109 | } |
| 110 | |
| 111 | void MetalCodeGenerator::finishLine() { |
| 112 | if (!fAtLineStart) { |
| 113 | this->writeLine(); |
| 114 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void MetalCodeGenerator::writeExtension(const Extension& ext) { |
Ethan Nicholas | efb09e2 | 2020-09-30 10:17:00 -0400 | [diff] [blame] | 118 | this->writeLine("#extension " + ext.name() + " : enable"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 121 | String MetalCodeGenerator::typeName(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 122 | switch (type.typeKind()) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 123 | case Type::TypeKind::kArray: |
| 124 | SkASSERTF(type.columns() > 0, "invalid array size: %s", type.description().c_str()); |
| 125 | return String::printf("array<%s, %d>", |
| 126 | this->typeName(type.componentType()).c_str(), type.columns()); |
| 127 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 128 | case Type::TypeKind::kVector: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 129 | return this->typeName(type.componentType()) + to_string(type.columns()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 130 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 131 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 132 | return this->typeName(type.componentType()) + to_string(type.columns()) + "x" + |
| 133 | to_string(type.rows()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 134 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 135 | case Type::TypeKind::kSampler: |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 136 | return "texture2d<float>"; // FIXME - support other texture types |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 137 | |
John Stiles | fd41d87 | 2020-11-25 22:39:45 -0500 | [diff] [blame] | 138 | case Type::TypeKind::kEnum: |
| 139 | return "int"; |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 140 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 141 | default: |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 142 | if (type == *fContext.fTypes.fHalf) { |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 143 | // FIXME - Currently only supporting floats in MSL to avoid type coercion issues. |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 144 | return fContext.fTypes.fFloat->name(); |
| 145 | } else if (type == *fContext.fTypes.fByte) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 146 | return "char"; |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 147 | } else if (type == *fContext.fTypes.fUByte) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 148 | return "uchar"; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 149 | } else { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 150 | return type.name(); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 151 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 155 | void MetalCodeGenerator::writeStructDefinition(const StructDefinition& s) { |
| 156 | const Type& type = s.type(); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 157 | this->writeLine("struct " + type.name() + " {"); |
| 158 | fIndentation++; |
| 159 | this->writeFields(type.fields(), type.fOffset); |
| 160 | fIndentation--; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 161 | this->writeLine("};"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 162 | } |
| 163 | |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 164 | void MetalCodeGenerator::writeType(const Type& type) { |
| 165 | this->write(this->typeName(type)); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 168 | void MetalCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 169 | switch (expr.kind()) { |
| 170 | case Expression::Kind::kBinary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 171 | this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 172 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 173 | case Expression::Kind::kBoolLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 174 | this->writeBoolLiteral(expr.as<BoolLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 175 | break; |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 176 | case Expression::Kind::kConstructorArray: |
| 177 | this->writeAnyConstructor(expr.asAnyConstructor(), "{", "}", parentPrecedence); |
| 178 | break; |
| 179 | case Expression::Kind::kConstructorComposite: |
| 180 | this->writeConstructorComposite(expr.as<ConstructorComposite>(), parentPrecedence); |
| 181 | break; |
| 182 | case Expression::Kind::kConstructorDiagonalMatrix: |
| 183 | case Expression::Kind::kConstructorSplat: |
| 184 | this->writeAnyConstructor(expr.asAnyConstructor(), "(", ")", parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 185 | break; |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 186 | case Expression::Kind::kConstructorMatrixResize: |
| 187 | this->writeConstructorMatrixResize(expr.as<ConstructorMatrixResize>(), |
| 188 | parentPrecedence); |
| 189 | break; |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 190 | case Expression::Kind::kConstructor: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 191 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 192 | case Expression::Kind::kConstructorVectorCast: |
| 193 | this->writeCastConstructor(expr.asAnyConstructor(), "(", ")", parentPrecedence); |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 194 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 195 | case Expression::Kind::kIntLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 196 | this->writeIntLiteral(expr.as<IntLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 197 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 198 | case Expression::Kind::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 199 | this->writeFieldAccess(expr.as<FieldAccess>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 200 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 201 | case Expression::Kind::kFloatLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 202 | this->writeFloatLiteral(expr.as<FloatLiteral>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 203 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 204 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 205 | this->writeFunctionCall(expr.as<FunctionCall>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 206 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 207 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 208 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 209 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 210 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 211 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 212 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 213 | case Expression::Kind::kSetting: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 214 | this->writeSetting(expr.as<Setting>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 215 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 216 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 217 | this->writeSwizzle(expr.as<Swizzle>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 218 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 219 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 220 | this->writeVariableReference(expr.as<VariableReference>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 221 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 222 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 223 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 224 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 225 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 226 | this->writeIndexExpression(expr.as<IndexExpression>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 227 | break; |
| 228 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 229 | SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 230 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 234 | String MetalCodeGenerator::getOutParamHelper(const FunctionCall& call, |
| 235 | const ExpressionArray& arguments, |
| 236 | const SkTArray<VariableReference*>& outVars) { |
| 237 | AutoOutputStream outputToExtraFunctions(this, &fExtraFunctions, &fIndentation); |
| 238 | const FunctionDeclaration& function = call.function(); |
| 239 | |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 240 | String name = "_skOutParamHelper" + to_string(fSwizzleHelperCount++) + |
| 241 | "_" + function.mangledName(); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 242 | const char* separator = ""; |
| 243 | |
| 244 | // Emit a prototype for the function we'll be calling through to in our helper. |
| 245 | if (!function.isBuiltin()) { |
| 246 | this->writeFunctionDeclaration(function); |
| 247 | this->writeLine(";"); |
| 248 | } |
| 249 | |
| 250 | // Synthesize a helper function that takes the same inputs as `function`, except in places where |
| 251 | // `outVars` is non-null; in those places, we take the type of the VariableReference. |
| 252 | // |
| 253 | // float _skOutParamHelper0_originalFuncName(float _var0, float _var1, float& outParam) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 254 | this->writeType(call.type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 255 | this->write(" "); |
| 256 | this->write(name); |
| 257 | this->write("("); |
| 258 | this->writeFunctionRequirementParams(function, separator); |
| 259 | |
| 260 | SkASSERT(outVars.size() == arguments.size()); |
| 261 | SkASSERT(outVars.size() == function.parameters().size()); |
| 262 | |
John Stiles | 73e2c89 | 2021-02-13 13:58:01 -0500 | [diff] [blame] | 263 | // We need to detect cases where the caller passes the same variable as an out-param more than |
| 264 | // once, and avoid reusing the variable name. (In those cases we can actually just ignore the |
| 265 | // redundant input parameter entirely, and not give it any name.) |
| 266 | std::unordered_set<const Variable*> writtenVars; |
| 267 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 268 | for (int index = 0; index < arguments.count(); ++index) { |
| 269 | this->write(separator); |
| 270 | separator = ", "; |
| 271 | |
| 272 | const Variable* param = function.parameters()[index]; |
| 273 | this->writeModifiers(param->modifiers(), /*globalContext=*/false); |
| 274 | |
| 275 | const Type* type = outVars[index] ? &outVars[index]->type() : &arguments[index]->type(); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 276 | this->writeType(*type); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 277 | |
| 278 | if (param->modifiers().fFlags & Modifiers::kOut_Flag) { |
| 279 | this->write("&"); |
| 280 | } |
| 281 | if (outVars[index]) { |
John Stiles | 73e2c89 | 2021-02-13 13:58:01 -0500 | [diff] [blame] | 282 | auto [iter, didInsert] = writtenVars.insert(outVars[index]->variable()); |
| 283 | if (didInsert) { |
| 284 | this->write(" "); |
| 285 | fIgnoreVariableReferenceModifiers = true; |
| 286 | this->writeVariableReference(*outVars[index]); |
| 287 | fIgnoreVariableReferenceModifiers = false; |
| 288 | } |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 289 | } else { |
| 290 | this->write(" _var"); |
| 291 | this->write(to_string(index)); |
| 292 | } |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 293 | } |
| 294 | this->writeLine(") {"); |
| 295 | |
| 296 | ++fIndentation; |
| 297 | for (int index = 0; index < outVars.count(); ++index) { |
| 298 | if (!outVars[index]) { |
| 299 | continue; |
| 300 | } |
| 301 | // float3 _var2[ = outParam.zyx]; |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 302 | this->writeType(arguments[index]->type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 303 | this->write(" _var"); |
| 304 | this->write(to_string(index)); |
| 305 | |
| 306 | const Variable* param = function.parameters()[index]; |
| 307 | if (param->modifiers().fFlags & Modifiers::kIn_Flag) { |
| 308 | this->write(" = "); |
| 309 | fIgnoreVariableReferenceModifiers = true; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 310 | this->writeExpression(*arguments[index], Precedence::kAssignment); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 311 | fIgnoreVariableReferenceModifiers = false; |
| 312 | } |
| 313 | |
| 314 | this->writeLine(";"); |
| 315 | } |
| 316 | |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 317 | // [int _skResult = ] myFunction(inputs, outputs, _globals, _var0, _var1, _var2, _var3); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 318 | bool hasResult = (call.type().name() != "void"); |
| 319 | if (hasResult) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 320 | this->writeType(call.type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 321 | this->write(" _skResult = "); |
| 322 | } |
| 323 | |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 324 | this->writeName(function.mangledName()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 325 | this->write("("); |
| 326 | separator = ""; |
| 327 | this->writeFunctionRequirementArgs(function, separator); |
| 328 | |
| 329 | for (int index = 0; index < arguments.count(); ++index) { |
| 330 | this->write(separator); |
| 331 | separator = ", "; |
| 332 | |
| 333 | this->write("_var"); |
| 334 | this->write(to_string(index)); |
| 335 | } |
| 336 | this->writeLine(");"); |
| 337 | |
| 338 | for (int index = 0; index < outVars.count(); ++index) { |
| 339 | if (!outVars[index]) { |
| 340 | continue; |
| 341 | } |
| 342 | // outParam.zyx = _var2; |
| 343 | fIgnoreVariableReferenceModifiers = true; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 344 | this->writeExpression(*arguments[index], Precedence::kAssignment); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 345 | fIgnoreVariableReferenceModifiers = false; |
| 346 | this->write(" = _var"); |
| 347 | this->write(to_string(index)); |
| 348 | this->writeLine(";"); |
| 349 | } |
| 350 | |
| 351 | if (hasResult) { |
| 352 | this->writeLine("return _skResult;"); |
| 353 | } |
| 354 | |
| 355 | --fIndentation; |
| 356 | this->writeLine("}"); |
| 357 | |
| 358 | return name; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 359 | } |
| 360 | |
John Stiles | f64e407 | 2020-12-10 10:34:27 -0500 | [diff] [blame] | 361 | String MetalCodeGenerator::getBitcastIntrinsic(const Type& outType) { |
| 362 | return "as_type<" + outType.displayName() + ">"; |
| 363 | } |
| 364 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 365 | void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 366 | const FunctionDeclaration& function = c.function(); |
John Stiles | 89ac7c2 | 2020-12-30 17:47:31 -0500 | [diff] [blame] | 367 | // If this function is a built-in with no declaration, it's probably an intrinsic and might need |
| 368 | // special handling. |
| 369 | if (function.isBuiltin() && !function.definition()) { |
| 370 | auto iter = fIntrinsicMap.find(function.name()); |
| 371 | if (iter != fIntrinsicMap.end()) { |
| 372 | this->writeIntrinsicCall(c, iter->second); |
| 373 | return; |
| 374 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 375 | } |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 376 | |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 377 | // Determine whether or not we need to emulate GLSL's out-param semantics for Metal using a |
| 378 | // helper function. (Specifically, out-parameters in GLSL are only written back to the original |
| 379 | // variable at the end of the function call; also, swizzles are supported, whereas Metal doesn't |
| 380 | // allow a swizzle to be passed to a `floatN&`.) |
| 381 | const ExpressionArray& arguments = c.arguments(); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 382 | const std::vector<const Variable*>& parameters = function.parameters(); |
| 383 | SkASSERT(arguments.size() == parameters.size()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 384 | |
| 385 | bool foundOutParam = false; |
| 386 | SkSTArray<16, VariableReference*> outVars; |
John Stiles | f64e407 | 2020-12-10 10:34:27 -0500 | [diff] [blame] | 387 | outVars.push_back_n(arguments.count(), (VariableReference*)nullptr); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 388 | |
| 389 | for (int index = 0; index < arguments.count(); ++index) { |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 390 | // If this is an out parameter... |
| 391 | if (parameters[index]->modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 392 | // Find the expression's inner variable being written to. |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 393 | Analysis::AssignmentInfo info; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 394 | // Assignability was verified at IRGeneration time, so this should always succeed. |
| 395 | SkAssertResult(Analysis::IsAssignable(*arguments[index], &info)); |
| 396 | outVars[index] = info.fAssignedVar; |
| 397 | foundOutParam = true; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 401 | if (foundOutParam) { |
| 402 | // Out parameters need to be written back to at the end of the function. To do this, we |
| 403 | // synthesize a helper function which evaluates the out-param expression into a temporary |
| 404 | // variable, calls the original function, then writes the temp var back into the out param |
| 405 | // using the original out-param expression. (This lets us support things like swizzles and |
| 406 | // array indices.) |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 407 | this->write(getOutParamHelper(c, arguments, outVars)); |
| 408 | } else { |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 409 | this->write(function.mangledName()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 412 | this->write("("); |
| 413 | const char* separator = ""; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 414 | this->writeFunctionRequirementArgs(function, separator); |
| 415 | for (int i = 0; i < arguments.count(); ++i) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 416 | this->write(separator); |
| 417 | separator = ", "; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 418 | |
| 419 | if (outVars[i]) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 420 | this->writeExpression(*outVars[i], Precedence::kSequence); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 421 | } else { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 422 | this->writeExpression(*arguments[i], Precedence::kSequence); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 423 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 424 | } |
| 425 | this->write(")"); |
| 426 | } |
| 427 | |
Brian Osman | 964f0a0 | 2020-12-29 10:15:22 -0500 | [diff] [blame] | 428 | static constexpr char kInverse2x2[] = R"( |
| 429 | float2x2 float2x2_inverse(float2x2 m) { |
| 430 | return float2x2(m[1][1], -m[0][1], -m[1][0], m[0][0]) * (1/determinant(m)); |
| 431 | } |
| 432 | )"; |
| 433 | |
| 434 | static constexpr char kInverse3x3[] = R"( |
| 435 | float3x3 float3x3_inverse(float3x3 m) { |
| 436 | float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; |
| 437 | float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; |
| 438 | float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; |
| 439 | float b01 = a22*a11 - a12*a21; |
| 440 | float b11 = -a22*a10 + a12*a20; |
| 441 | float b21 = a21*a10 - a11*a20; |
| 442 | float det = a00*b01 + a01*b11 + a02*b21; |
| 443 | return float3x3(b01, (-a22*a01 + a02*a21), ( a12*a01 - a02*a11), |
| 444 | b11, ( a22*a00 - a02*a20), (-a12*a00 + a02*a10), |
| 445 | b21, (-a21*a00 + a01*a20), ( a11*a00 - a01*a10)) * (1/det); |
| 446 | } |
| 447 | )"; |
| 448 | |
| 449 | static constexpr char kInverse4x4[] = R"( |
| 450 | float4x4 float4x4_inverse(float4x4 m) { |
| 451 | float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3]; |
| 452 | float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3]; |
| 453 | float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3]; |
| 454 | float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3]; |
| 455 | float b00 = a00*a11 - a01*a10; |
| 456 | float b01 = a00*a12 - a02*a10; |
| 457 | float b02 = a00*a13 - a03*a10; |
| 458 | float b03 = a01*a12 - a02*a11; |
| 459 | float b04 = a01*a13 - a03*a11; |
| 460 | float b05 = a02*a13 - a03*a12; |
| 461 | float b06 = a20*a31 - a21*a30; |
| 462 | float b07 = a20*a32 - a22*a30; |
| 463 | float b08 = a20*a33 - a23*a30; |
| 464 | float b09 = a21*a32 - a22*a31; |
| 465 | float b10 = a21*a33 - a23*a31; |
| 466 | float b11 = a22*a33 - a23*a32; |
| 467 | float det = b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06; |
| 468 | return float4x4(a11*b11 - a12*b10 + a13*b09, |
| 469 | a02*b10 - a01*b11 - a03*b09, |
| 470 | a31*b05 - a32*b04 + a33*b03, |
| 471 | a22*b04 - a21*b05 - a23*b03, |
| 472 | a12*b08 - a10*b11 - a13*b07, |
| 473 | a00*b11 - a02*b08 + a03*b07, |
| 474 | a32*b02 - a30*b05 - a33*b01, |
| 475 | a20*b05 - a22*b02 + a23*b01, |
| 476 | a10*b10 - a11*b08 + a13*b06, |
| 477 | a01*b08 - a00*b10 - a03*b06, |
| 478 | a30*b04 - a31*b02 + a33*b00, |
| 479 | a21*b02 - a20*b04 - a23*b00, |
| 480 | a11*b07 - a10*b09 - a12*b06, |
| 481 | a00*b09 - a01*b07 + a02*b06, |
| 482 | a31*b01 - a30*b03 - a32*b00, |
| 483 | a20*b03 - a21*b01 + a22*b00) * (1/det); |
| 484 | } |
| 485 | )"; |
| 486 | |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 487 | String MetalCodeGenerator::getInversePolyfill(const ExpressionArray& arguments) { |
| 488 | // Only use polyfills for a function taking a single-argument square matrix. |
| 489 | if (arguments.size() == 1) { |
| 490 | const Type& type = arguments.front()->type(); |
| 491 | if (type.isMatrix() && type.rows() == type.columns()) { |
| 492 | // Inject the correct polyfill based on the matrix size. |
| 493 | String name = this->typeName(type) + "_inverse"; |
| 494 | auto [iter, didInsert] = fWrittenIntrinsics.insert(name); |
| 495 | if (didInsert) { |
| 496 | switch (type.rows()) { |
| 497 | case 2: |
| 498 | fExtraFunctions.writeText(kInverse2x2); |
| 499 | break; |
| 500 | case 3: |
| 501 | fExtraFunctions.writeText(kInverse3x3); |
| 502 | break; |
| 503 | case 4: |
| 504 | fExtraFunctions.writeText(kInverse4x4); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | return name; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 509 | } |
| 510 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 511 | // This isn't the built-in `inverse`. We don't want to polyfill it at all. |
| 512 | return "inverse"; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 513 | } |
| 514 | |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 515 | static constexpr char kMatrixCompMult[] = R"( |
| 516 | template <int C, int R> |
| 517 | matrix<float, C, R> matrixCompMult(matrix<float, C, R> a, matrix<float, C, R> b) { |
| 518 | matrix<float, C, R> result; |
| 519 | for (int c = 0; c < C; ++c) { |
| 520 | result[c] = a[c] * b[c]; |
| 521 | } |
| 522 | return result; |
| 523 | } |
| 524 | )"; |
| 525 | |
| 526 | void MetalCodeGenerator::writeMatrixCompMult() { |
| 527 | String name = "matrixCompMult"; |
| 528 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 529 | fWrittenIntrinsics.insert(name); |
| 530 | fExtraFunctions.writeText(kMatrixCompMult); |
| 531 | } |
| 532 | } |
| 533 | |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 534 | String MetalCodeGenerator::getTempVariable(const Type& type) { |
| 535 | String tempVar = "_skTemp" + to_string(fVarCount++); |
| 536 | this->fFunctionHeader += " " + this->typeName(type) + " " + tempVar + ";\n"; |
| 537 | return tempVar; |
| 538 | } |
| 539 | |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 540 | void MetalCodeGenerator::writeSimpleIntrinsic(const FunctionCall& c) { |
| 541 | // Write out an intrinsic function call exactly as-is. No muss no fuss. |
| 542 | this->write(c.function().name()); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 543 | this->writeArgumentList(c.arguments()); |
| 544 | } |
| 545 | |
| 546 | void MetalCodeGenerator::writeArgumentList(const ExpressionArray& arguments) { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 547 | this->write("("); |
| 548 | const char* separator = ""; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 549 | for (const std::unique_ptr<Expression>& arg : arguments) { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 550 | this->write(separator); |
| 551 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 552 | this->writeExpression(*arg, Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 553 | } |
| 554 | this->write(")"); |
| 555 | } |
| 556 | |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 557 | void MetalCodeGenerator::writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 558 | const ExpressionArray& arguments = c.arguments(); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 559 | switch (kind) { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 560 | case kTexture_IntrinsicKind: { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 561 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 562 | this->write(".sample("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 563 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 564 | this->write(SAMPLER_SUFFIX); |
| 565 | this->write(", "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 566 | const Type& arg1Type = arguments[1]->type(); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 567 | if (arg1Type.columns() == 3) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 568 | // have to store the vector in a temp variable to avoid double evaluating it |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 569 | String tmpVar = this->getTempVariable(arg1Type); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 570 | this->write("(" + tmpVar + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 571 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 572 | this->write(", " + tmpVar + ".xy / " + tmpVar + ".z))"); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 573 | } else { |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 574 | SkASSERT(arg1Type.columns() == 2); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 575 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 576 | this->write(")"); |
| 577 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 578 | break; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 579 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 580 | case kMod_IntrinsicKind: { |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 581 | // fmod(x, y) in metal calculates x - y * trunc(x / y) instead of x - y * floor(x / y) |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 582 | String tmpX = this->getTempVariable(arguments[0]->type()); |
John Stiles | 61b2e81 | 2020-12-30 18:27:31 -0500 | [diff] [blame] | 583 | String tmpY = this->getTempVariable(arguments[1]->type()); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 584 | this->write("(" + tmpX + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 585 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 586 | this->write(", " + tmpY + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 587 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 588 | this->write(", " + tmpX + " - " + tmpY + " * floor(" + tmpX + " / " + tmpY + "))"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 589 | break; |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 590 | } |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 591 | // GLSL declares scalar versions of most geometric intrinsics, but these don't exist in MSL |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 592 | case kDistance_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 593 | if (arguments[0]->type().columns() == 1) { |
| 594 | this->write("abs("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 595 | this->writeExpression(*arguments[0], Precedence::kAdditive); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 596 | this->write(" - "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 597 | this->writeExpression(*arguments[1], Precedence::kAdditive); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 598 | this->write(")"); |
| 599 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 600 | this->writeSimpleIntrinsic(c); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 601 | } |
| 602 | break; |
| 603 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 604 | case kDot_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 605 | if (arguments[0]->type().columns() == 1) { |
| 606 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 607 | this->writeExpression(*arguments[0], Precedence::kMultiplicative); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 608 | this->write(" * "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 609 | this->writeExpression(*arguments[1], Precedence::kMultiplicative); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 610 | this->write(")"); |
| 611 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 612 | this->writeSimpleIntrinsic(c); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 613 | } |
| 614 | break; |
| 615 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 616 | case kFaceforward_IntrinsicKind: { |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 617 | if (arguments[0]->type().columns() == 1) { |
| 618 | // ((((Nref) * (I) < 0) ? 1 : -1) * (N)) |
| 619 | this->write("(((("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 620 | this->writeExpression(*arguments[2], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 621 | this->write(") * ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 622 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 623 | this->write(") < 0) ? 1 : -1) * ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 624 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 625 | this->write("))"); |
| 626 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 627 | this->writeSimpleIntrinsic(c); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 628 | } |
| 629 | break; |
| 630 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 631 | case kLength_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 632 | this->write(arguments[0]->type().columns() == 1 ? "abs(" : "length("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 633 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 634 | this->write(")"); |
| 635 | break; |
| 636 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 637 | case kNormalize_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 638 | this->write(arguments[0]->type().columns() == 1 ? "sign(" : "normalize("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 639 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 640 | this->write(")"); |
| 641 | break; |
| 642 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 643 | case kBitcast_IntrinsicKind: { |
John Stiles | 0063a9f | 2020-12-10 18:01:45 -0500 | [diff] [blame] | 644 | this->write(this->getBitcastIntrinsic(c.type())); |
| 645 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 646 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 0063a9f | 2020-12-10 18:01:45 -0500 | [diff] [blame] | 647 | this->write(")"); |
| 648 | break; |
| 649 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 650 | case kDegrees_IntrinsicKind: { |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 651 | this->write("(("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 652 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 653 | this->write(") * 57.2957795)"); |
| 654 | break; |
| 655 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 656 | case kRadians_IntrinsicKind: { |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 657 | this->write("(("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 658 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 659 | this->write(") * 0.0174532925)"); |
| 660 | break; |
| 661 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 662 | case kDFdx_IntrinsicKind: { |
| 663 | this->write("dfdx"); |
| 664 | this->writeArgumentList(c.arguments()); |
| 665 | break; |
| 666 | } |
| 667 | case kDFdy_IntrinsicKind: { |
| 668 | // Flipping Y also negates the Y derivatives. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 669 | if (fProgram.fConfig->fSettings.fFlipY) { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 670 | this->write("-"); |
| 671 | } |
| 672 | this->write("dfdy"); |
| 673 | this->writeArgumentList(c.arguments()); |
| 674 | break; |
| 675 | } |
| 676 | case kInverse_IntrinsicKind: { |
| 677 | this->write(this->getInversePolyfill(arguments)); |
| 678 | this->writeArgumentList(c.arguments()); |
| 679 | break; |
| 680 | } |
| 681 | case kInversesqrt_IntrinsicKind: { |
| 682 | this->write("rsqrt"); |
| 683 | this->writeArgumentList(c.arguments()); |
| 684 | break; |
| 685 | } |
| 686 | case kAtan_IntrinsicKind: { |
| 687 | this->write(c.arguments().size() == 2 ? "atan2" : "atan"); |
| 688 | this->writeArgumentList(c.arguments()); |
| 689 | break; |
| 690 | } |
| 691 | case kReflect_IntrinsicKind: { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 692 | if (arguments[0]->type().columns() == 1) { |
| 693 | // We need to synthesize `I - 2 * N * I * N`. |
| 694 | String tmpI = this->getTempVariable(arguments[0]->type()); |
| 695 | String tmpN = this->getTempVariable(arguments[1]->type()); |
| 696 | |
| 697 | // (_skTempI = ... |
| 698 | this->write("(" + tmpI + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 699 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 700 | |
| 701 | // , _skTempN = ... |
| 702 | this->write(", " + tmpN + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 703 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 704 | |
| 705 | // , _skTempI - 2 * _skTempN * _skTempI * _skTempN) |
| 706 | this->write(", " + tmpI + " - 2 * " + tmpN + " * " + tmpI + " * " + tmpN + ")"); |
| 707 | } else { |
| 708 | this->writeSimpleIntrinsic(c); |
| 709 | } |
| 710 | break; |
| 711 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 712 | case kRefract_IntrinsicKind: { |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 713 | if (arguments[0]->type().columns() == 1) { |
| 714 | // Metal does implement refract for vectors; rather than reimplementing refract from |
| 715 | // scratch, we can replace the call with `refract(float2(I,0), float2(N,0), eta).x`. |
| 716 | this->write("(refract(float2("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 717 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 718 | this->write(", 0), float2("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 719 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 720 | this->write(", 0), "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 721 | this->writeExpression(*arguments[2], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 722 | this->write(").x)"); |
| 723 | } else { |
| 724 | this->writeSimpleIntrinsic(c); |
| 725 | } |
| 726 | break; |
| 727 | } |
John Stiles | 2345653 | 2020-12-30 18:12:48 -0500 | [diff] [blame] | 728 | case kRoundEven_IntrinsicKind: { |
| 729 | this->write("rint"); |
| 730 | this->writeArgumentList(c.arguments()); |
| 731 | break; |
| 732 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 733 | case kBitCount_IntrinsicKind: { |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame] | 734 | this->write("popcount("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 735 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame] | 736 | this->write(")"); |
| 737 | break; |
| 738 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 739 | case kFindLSB_IntrinsicKind: { |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 740 | // Create a temp variable to store the expression, to avoid double-evaluating it. |
| 741 | String skTemp = this->getTempVariable(arguments[0]->type()); |
| 742 | String exprType = this->typeName(arguments[0]->type()); |
| 743 | |
| 744 | // ctz returns numbits(type) on zero inputs; GLSL documents it as generating -1 instead. |
| 745 | // Use select to detect zero inputs and force a -1 result. |
| 746 | |
| 747 | // (_skTemp1 = (.....), select(ctz(_skTemp1), int4(-1), _skTemp1 == int4(0))) |
| 748 | this->write("("); |
| 749 | this->write(skTemp); |
| 750 | this->write(" = ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 751 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 752 | this->write("), select(ctz("); |
| 753 | this->write(skTemp); |
| 754 | this->write("), "); |
| 755 | this->write(exprType); |
| 756 | this->write("(-1), "); |
| 757 | this->write(skTemp); |
| 758 | this->write(" == "); |
| 759 | this->write(exprType); |
| 760 | this->write("(0)))"); |
| 761 | break; |
| 762 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 763 | case kFindMSB_IntrinsicKind: { |
John Stiles | 9685e52 | 2020-12-14 11:52:14 -0500 | [diff] [blame] | 764 | // Create a temp variable to store the expression, to avoid double-evaluating it. |
| 765 | String skTemp1 = this->getTempVariable(arguments[0]->type()); |
| 766 | String exprType = this->typeName(arguments[0]->type()); |
| 767 | |
| 768 | // GLSL findMSB is actually quite different from Metal's clz: |
| 769 | // - For signed negative numbers, it returns the first zero bit, not the first one bit! |
| 770 | // - For an empty input (0/~0 depending on sign), findMSB gives -1; clz is numbits(type) |
| 771 | |
| 772 | // (_skTemp1 = (.....), |
| 773 | this->write("("); |
| 774 | this->write(skTemp1); |
| 775 | this->write(" = ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 776 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 9685e52 | 2020-12-14 11:52:14 -0500 | [diff] [blame] | 777 | this->write("), "); |
| 778 | |
| 779 | // Signed input types might be negative; we need another helper variable to negate the |
| 780 | // input (since we can only find one bits, not zero bits). |
| 781 | String skTemp2; |
| 782 | if (arguments[0]->type().isSigned()) { |
| 783 | // ... _skTemp2 = (select(_skTemp1, ~_skTemp1, _skTemp1 < 0)), |
| 784 | skTemp2 = this->getTempVariable(arguments[0]->type()); |
| 785 | this->write(skTemp2); |
| 786 | this->write(" = (select("); |
| 787 | this->write(skTemp1); |
| 788 | this->write(", ~"); |
| 789 | this->write(skTemp1); |
| 790 | this->write(", "); |
| 791 | this->write(skTemp1); |
| 792 | this->write(" < 0)), "); |
| 793 | } else { |
| 794 | skTemp2 = skTemp1; |
| 795 | } |
| 796 | |
| 797 | // ... select(int4(clz(_skTemp2)), int4(-1), _skTemp2 == int4(0))) |
| 798 | this->write("select("); |
| 799 | this->write(this->typeName(c.type())); |
| 800 | this->write("(clz("); |
| 801 | this->write(skTemp2); |
| 802 | this->write(")), "); |
| 803 | this->write(this->typeName(c.type())); |
| 804 | this->write("(-1), "); |
| 805 | this->write(skTemp2); |
| 806 | this->write(" == "); |
| 807 | this->write(exprType); |
| 808 | this->write("(0)))"); |
| 809 | break; |
| 810 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 811 | case kMatrixCompMult_IntrinsicKind: { |
| 812 | this->writeMatrixCompMult(); |
| 813 | this->writeSimpleIntrinsic(c); |
| 814 | break; |
| 815 | } |
| 816 | case kCompareEqual_IntrinsicKind: |
| 817 | case kCompareGreaterThan_IntrinsicKind: |
| 818 | case kCompareGreaterThanEqual_IntrinsicKind: |
| 819 | case kCompareLessThan_IntrinsicKind: |
| 820 | case kCompareLessThanEqual_IntrinsicKind: |
| 821 | case kCompareNotEqual_IntrinsicKind: { |
| 822 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 823 | this->writeExpression(*c.arguments()[0], Precedence::kRelational); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 824 | switch (kind) { |
| 825 | case kCompareEqual_IntrinsicKind: |
| 826 | this->write(" == "); |
| 827 | break; |
| 828 | case kCompareNotEqual_IntrinsicKind: |
| 829 | this->write(" != "); |
| 830 | break; |
| 831 | case kCompareLessThan_IntrinsicKind: |
| 832 | this->write(" < "); |
| 833 | break; |
| 834 | case kCompareLessThanEqual_IntrinsicKind: |
| 835 | this->write(" <= "); |
| 836 | break; |
| 837 | case kCompareGreaterThan_IntrinsicKind: |
| 838 | this->write(" > "); |
| 839 | break; |
| 840 | case kCompareGreaterThanEqual_IntrinsicKind: |
| 841 | this->write(" >= "); |
| 842 | break; |
| 843 | default: |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 844 | SK_ABORT("unsupported comparison intrinsic kind"); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 845 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 846 | this->writeExpression(*c.arguments()[1], Precedence::kRelational); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 847 | this->write(")"); |
| 848 | break; |
| 849 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 850 | default: |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 851 | SK_ABORT("unsupported intrinsic kind"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 855 | // Assembles a matrix of type floatRxC by resizing another matrix named `x0`. |
| 856 | // Cells that don't exist in the source matrix will be populated with identity-matrix values. |
| 857 | void MetalCodeGenerator::assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns) { |
| 858 | SkASSERT(rows <= 4); |
| 859 | SkASSERT(columns <= 4); |
| 860 | |
| 861 | const char* columnSeparator = ""; |
| 862 | for (int c = 0; c < columns; ++c) { |
| 863 | fExtraFunctions.printf("%sfloat%d(", columnSeparator, rows); |
| 864 | columnSeparator = "), "; |
| 865 | |
| 866 | // Determine how many values to take from the source matrix for this row. |
| 867 | int swizzleLength = 0; |
| 868 | if (c < sourceMatrix.columns()) { |
| 869 | swizzleLength = std::min<>(rows, sourceMatrix.rows()); |
| 870 | } |
| 871 | |
| 872 | // Emit all the values from the source matrix row. |
| 873 | bool firstItem; |
| 874 | switch (swizzleLength) { |
| 875 | case 0: firstItem = true; break; |
| 876 | case 1: firstItem = false; fExtraFunctions.printf("x0[%d].x", c); break; |
| 877 | case 2: firstItem = false; fExtraFunctions.printf("x0[%d].xy", c); break; |
| 878 | case 3: firstItem = false; fExtraFunctions.printf("x0[%d].xyz", c); break; |
| 879 | case 4: firstItem = false; fExtraFunctions.printf("x0[%d].xyzw", c); break; |
| 880 | default: SkUNREACHABLE; |
| 881 | } |
| 882 | |
| 883 | // Emit the placeholder identity-matrix cells. |
| 884 | for (int r = swizzleLength; r < rows; ++r) { |
| 885 | fExtraFunctions.printf("%s%s", firstItem ? "" : ", ", (r == c) ? "1.0" : "0.0"); |
| 886 | firstItem = false; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | fExtraFunctions.writeText(")"); |
| 891 | } |
| 892 | |
| 893 | // Assembles a matrix of type floatRxC by concatenating an arbitrary mix of values, named `x0`, |
| 894 | // `x1`, etc. An error is written if the expression list don't contain exactly R*C scalars. |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 895 | void MetalCodeGenerator::assembleMatrixFromExpressions(const AnyConstructor& ctor, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 896 | int rows, int columns) { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 897 | size_t argIndex = 0; |
| 898 | int argPosition = 0; |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 899 | auto args = ctor.argumentSpan(); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 900 | |
| 901 | const char* columnSeparator = ""; |
| 902 | for (int c = 0; c < columns; ++c) { |
| 903 | fExtraFunctions.printf("%sfloat%d(", columnSeparator, rows); |
| 904 | columnSeparator = "), "; |
| 905 | |
| 906 | const char* rowSeparator = ""; |
| 907 | for (int r = 0; r < rows; ++r) { |
| 908 | fExtraFunctions.writeText(rowSeparator); |
| 909 | rowSeparator = ", "; |
| 910 | |
| 911 | if (argIndex < args.size()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 912 | const Type& argType = args[argIndex]->type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 913 | switch (argType.typeKind()) { |
| 914 | case Type::TypeKind::kScalar: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 915 | fExtraFunctions.printf("x%zu", argIndex); |
| 916 | break; |
| 917 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 918 | case Type::TypeKind::kVector: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 919 | fExtraFunctions.printf("x%zu[%d]", argIndex, argPosition); |
| 920 | break; |
| 921 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 922 | case Type::TypeKind::kMatrix: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 923 | fExtraFunctions.printf("x%zu[%d][%d]", argIndex, |
| 924 | argPosition / argType.rows(), |
| 925 | argPosition % argType.rows()); |
| 926 | break; |
| 927 | } |
| 928 | default: { |
| 929 | SkDEBUGFAIL("incorrect type of argument for matrix constructor"); |
| 930 | fExtraFunctions.writeText("<error>"); |
| 931 | break; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | ++argPosition; |
| 936 | if (argPosition >= argType.columns() * argType.rows()) { |
| 937 | ++argIndex; |
| 938 | argPosition = 0; |
| 939 | } |
| 940 | } else { |
| 941 | SkDEBUGFAIL("not enough arguments for matrix constructor"); |
| 942 | fExtraFunctions.writeText("<error>"); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | if (argPosition != 0 || argIndex != args.size()) { |
| 948 | SkDEBUGFAIL("incorrect number of arguments for matrix constructor"); |
| 949 | fExtraFunctions.writeText(", <error>"); |
| 950 | } |
| 951 | |
| 952 | fExtraFunctions.writeText(")"); |
| 953 | } |
| 954 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 955 | // Generates a constructor for 'matrix' which reorganizes the input arguments into the proper shape. |
| 956 | // Keeps track of previously generated constructors so that we won't generate more than one |
| 957 | // constructor for any given permutation of input argument types. Returns the name of the |
| 958 | // generated constructor method. |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 959 | String MetalCodeGenerator::getMatrixConstructHelper(const AnyConstructor& c) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 960 | const Type& matrix = c.type(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 961 | int columns = matrix.columns(); |
| 962 | int rows = matrix.rows(); |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 963 | auto args = c.argumentSpan(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 964 | |
| 965 | // Create the helper-method name and use it as our lookup key. |
| 966 | String name; |
| 967 | name.appendf("float%dx%d_from", columns, rows); |
| 968 | for (const std::unique_ptr<Expression>& expr : args) { |
John Stiles | 3612913 | 2020-12-29 12:28:04 -0500 | [diff] [blame] | 969 | name.appendf("_%s", this->typeName(expr->type()).c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | // If a helper-method has already been synthesized, we don't need to synthesize it again. |
| 973 | auto [iter, newlyCreated] = fHelpers.insert(name); |
| 974 | if (!newlyCreated) { |
| 975 | return name; |
| 976 | } |
| 977 | |
| 978 | // Unlike GLSL, Metal requires that matrices are initialized with exactly R vectors of C |
| 979 | // components apiece. (In Metal 2.0, you can also supply R*C scalars, but you still cannot |
| 980 | // supply a mixture of scalars and vectors.) |
| 981 | fExtraFunctions.printf("float%dx%d %s(", columns, rows, name.c_str()); |
| 982 | |
| 983 | size_t argIndex = 0; |
| 984 | const char* argSeparator = ""; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 985 | for (const std::unique_ptr<Expression>& expr : args) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 986 | fExtraFunctions.printf("%s%s x%zu", argSeparator, |
John Stiles | 3612913 | 2020-12-29 12:28:04 -0500 | [diff] [blame] | 987 | this->typeName(expr->type()).c_str(), argIndex++); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 988 | argSeparator = ", "; |
| 989 | } |
| 990 | |
| 991 | fExtraFunctions.printf(") {\n return float%dx%d(", columns, rows); |
| 992 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 993 | if (args.size() == 1 && args.front()->type().isMatrix()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 994 | this->assembleMatrixFromMatrix(args.front()->type(), rows, columns); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 995 | } else { |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 996 | this->assembleMatrixFromExpressions(c, rows, columns); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 997 | } |
| 998 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 999 | fExtraFunctions.writeText(");\n}\n"); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1000 | return name; |
| 1001 | } |
| 1002 | |
| 1003 | bool MetalCodeGenerator::canCoerce(const Type& t1, const Type& t2) { |
| 1004 | if (t1.columns() != t2.columns() || t1.rows() != t2.rows()) { |
| 1005 | return false; |
| 1006 | } |
| 1007 | if (t1.columns() > 1) { |
| 1008 | return this->canCoerce(t1.componentType(), t2.componentType()); |
| 1009 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1010 | return t1.isFloat() && t2.isFloat(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1013 | bool MetalCodeGenerator::matrixConstructHelperIsNeeded(const ConstructorComposite& c) { |
| 1014 | SkASSERT(c.type().isMatrix()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1015 | |
| 1016 | // GLSL is fairly free-form about inputs to its matrix constructors, but Metal is not; it |
| 1017 | // expects exactly R vectors of C components apiece. (Metal 2.0 also allows a list of R*C |
| 1018 | // scalars.) Some cases are simple to translate and so we handle those inline--e.g. a list of |
| 1019 | // scalars can be constructed trivially. In more complex cases, we generate a helper function |
| 1020 | // that converts our inputs into a properly-shaped matrix. |
| 1021 | // A matrix construct helper method is always used if any input argument is a matrix. |
| 1022 | // Helper methods are also necessary when any argument would span multiple rows. For instance: |
| 1023 | // |
| 1024 | // float2 x = (1, 2); |
| 1025 | // float3x2(x, 3, 4, 5, 6) = | 1 3 5 | = no helper needed; conversion can be done inline |
| 1026 | // | 2 4 6 | |
| 1027 | // |
| 1028 | // float2 x = (2, 3); |
| 1029 | // float3x2(1, x, 4, 5, 6) = | 1 3 5 | = x spans multiple rows; a helper method will be used |
| 1030 | // | 2 4 6 | |
| 1031 | // |
| 1032 | // float4 x = (1, 2, 3, 4); |
| 1033 | // float2x2(x) = | 1 3 | = x spans multiple rows; a helper method will be used |
| 1034 | // | 2 4 | |
| 1035 | // |
| 1036 | |
| 1037 | int position = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1038 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1039 | // If an input argument is a matrix, we need a helper function. |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1040 | if (expr->type().isMatrix()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1041 | return true; |
| 1042 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1043 | position += expr->type().columns(); |
| 1044 | if (position > c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1045 | // An input argument would span multiple rows; a helper function is required. |
| 1046 | return true; |
| 1047 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1048 | if (position == c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1049 | // We've advanced to the end of a row. Wrap to the start of the next row. |
| 1050 | position = 0; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1057 | void MetalCodeGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& c, |
| 1058 | Precedence parentPrecedence) { |
| 1059 | // Matrix-resize via casting doesn't natively exist in Metal at all, so we always need to use a |
| 1060 | // matrix-construct helper here. |
| 1061 | this->write(this->getMatrixConstructHelper(c)); |
| 1062 | this->write("("); |
| 1063 | this->writeExpression(*c.argument(), Precedence::kSequence); |
| 1064 | this->write(")"); |
| 1065 | } |
| 1066 | |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1067 | void MetalCodeGenerator::writeConstructorComposite(const ConstructorComposite& c, |
| 1068 | Precedence parentPrecedence) { |
| 1069 | if (c.type().isMatrix()) { |
| 1070 | this->writeConstructorCompositeMatrix(c, parentPrecedence); |
| 1071 | } else { |
| 1072 | this->writeAnyConstructor(c, "(", ")", parentPrecedence); |
| 1073 | } |
| 1074 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1075 | |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1076 | void MetalCodeGenerator::writeConstructorCompositeMatrix(const ConstructorComposite& c, |
| 1077 | Precedence parentPrecedence) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1078 | // Emit and invoke a matrix-constructor helper method if one is necessary. |
| 1079 | if (this->matrixConstructHelperIsNeeded(c)) { |
| 1080 | this->write(this->getMatrixConstructHelper(c)); |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1081 | this->write("("); |
| 1082 | const char* separator = ""; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1083 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1084 | this->write(separator); |
| 1085 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1086 | this->writeExpression(*expr, Precedence::kSequence); |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1087 | } |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1088 | this->write(")"); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1089 | return; |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1090 | } |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1091 | |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1092 | // Metal doesn't allow creating matrices by passing in scalars and vectors in a jumble; it |
| 1093 | // requires your scalars to be grouped up into columns. Because `matrixConstructHelperIsNeeded` |
| 1094 | // returned false, we know that none of our scalars/vectors "wrap" across across a column, so we |
| 1095 | // can group our inputs up and synthesize a constructor for each column. |
| 1096 | const Type& matrixType = c.type(); |
| 1097 | const Type& columnType = matrixType.componentType().toCompound( |
| 1098 | fContext, /*columns=*/matrixType.rows(), /*rows=*/1); |
| 1099 | |
| 1100 | this->writeType(matrixType); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1101 | this->write("("); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1102 | const char* separator = ""; |
| 1103 | int scalarCount = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1104 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1105 | this->write(separator); |
| 1106 | separator = ", "; |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1107 | if (arg->type().columns() < matrixType.rows()) { |
| 1108 | // Write a `floatN(` constructor to group scalars and smaller vectors together. |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1109 | if (!scalarCount) { |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1110 | this->writeType(columnType); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1111 | this->write("("); |
| 1112 | } |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1113 | scalarCount += arg->type().columns(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1114 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1115 | this->writeExpression(*arg, Precedence::kSequence); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 1116 | if (scalarCount && scalarCount == matrixType.rows()) { |
| 1117 | // Close our `floatN(...` constructor block from above. |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1118 | this->write(")"); |
| 1119 | scalarCount = 0; |
| 1120 | } |
| 1121 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1122 | this->write(")"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1123 | } |
| 1124 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1125 | void MetalCodeGenerator::writeAnyConstructor(const AnyConstructor& c, |
| 1126 | const char* leftBracket, |
| 1127 | const char* rightBracket, |
| 1128 | Precedence parentPrecedence) { |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 1129 | this->writeType(c.type()); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1130 | this->write(leftBracket); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1131 | const char* separator = ""; |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1132 | for (const std::unique_ptr<Expression>& arg : c.argumentSpan()) { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1133 | this->write(separator); |
| 1134 | separator = ", "; |
| 1135 | this->writeExpression(*arg, Precedence::kSequence); |
| 1136 | } |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1137 | this->write(rightBracket); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1138 | } |
| 1139 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1140 | void MetalCodeGenerator::writeCastConstructor(const AnyConstructor& c, |
| 1141 | const char* leftBracket, |
| 1142 | const char* rightBracket, |
| 1143 | Precedence parentPrecedence) { |
| 1144 | // If the type is coercible, emit it directly without the cast. |
| 1145 | auto args = c.argumentSpan(); |
| 1146 | if (args.size() == 1) { |
| 1147 | if (this->canCoerce(c.type(), args.front()->type())) { |
| 1148 | this->writeExpression(*args.front(), parentPrecedence); |
| 1149 | return; |
| 1150 | } |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1153 | return this->writeAnyConstructor(c, leftBracket, rightBracket, parentPrecedence); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1156 | void MetalCodeGenerator::writeFragCoord() { |
Ethan Nicholas | f931e40 | 2019-07-26 15:40:33 -0400 | [diff] [blame] | 1157 | if (fRTHeightName.length()) { |
| 1158 | this->write("float4(_fragCoord.x, "); |
| 1159 | this->write(fRTHeightName.c_str()); |
| 1160 | this->write(" - _fragCoord.y, 0.0, _fragCoord.w)"); |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 1161 | } else { |
| 1162 | this->write("float4(_fragCoord.x, _fragCoord.y, 0.0, _fragCoord.w)"); |
| 1163 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | void MetalCodeGenerator::writeVariableReference(const VariableReference& ref) { |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1167 | // When assembling out-param helper functions, we copy variables into local clones with matching |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1168 | // names. We never want to prepend "_in." or "_globals." when writing these variables since |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1169 | // we're actually targeting the clones. |
| 1170 | if (fIgnoreVariableReferenceModifiers) { |
| 1171 | this->writeName(ref.variable()->name()); |
| 1172 | return; |
| 1173 | } |
| 1174 | |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1175 | switch (ref.variable()->modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1176 | case SK_FRAGCOLOR_BUILTIN: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1177 | this->write("_out.sk_FragColor"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1178 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1179 | case SK_FRAGCOORD_BUILTIN: |
| 1180 | this->writeFragCoord(); |
| 1181 | break; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1182 | case SK_VERTEXID_BUILTIN: |
| 1183 | this->write("sk_VertexID"); |
| 1184 | break; |
| 1185 | case SK_INSTANCEID_BUILTIN: |
| 1186 | this->write("sk_InstanceID"); |
| 1187 | break; |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1188 | case SK_CLOCKWISE_BUILTIN: |
| 1189 | // 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] | 1190 | // clockwise to match Skia convention. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1191 | this->write(fProgram.fConfig->fSettings.fFlipY ? "_frontFacing" : "(!_frontFacing)"); |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1192 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1193 | default: |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1194 | const Variable& var = *ref.variable(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1195 | if (var.storage() == Variable::Storage::kGlobal) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1196 | if (var.modifiers().fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1197 | this->write("_in."); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1198 | } else if (var.modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1199 | this->write("_out."); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1200 | } else if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
| 1201 | var.type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1202 | this->write("_uniforms."); |
| 1203 | } else { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1204 | this->write("_globals."); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1205 | } |
| 1206 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1207 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | void MetalCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1212 | this->writeExpression(*expr.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1213 | this->write("["); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1214 | this->writeExpression(*expr.index(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1215 | this->write("]"); |
| 1216 | } |
| 1217 | |
| 1218 | void MetalCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1219 | const Type::Field* field = &f.base()->type().fields()[f.fieldIndex()]; |
| 1220 | if (FieldAccess::OwnerKind::kDefault == f.ownerKind()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1221 | this->writeExpression(*f.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1222 | this->write("."); |
| 1223 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1224 | switch (field->fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1225 | case SK_POSITION_BUILTIN: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1226 | this->write("_out.sk_Position"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1227 | break; |
| 1228 | default: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1229 | if (field->fName == "sk_PointSize") { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1230 | this->write("_out.sk_PointSize"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1231 | } else { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1232 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1233 | this->write("_globals."); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1234 | this->write(fInterfaceBlockNameMap[fInterfaceBlockMap[field]]); |
| 1235 | this->write("->"); |
| 1236 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1237 | this->writeName(field->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1238 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | void MetalCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1243 | this->writeExpression(*swizzle.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1244 | this->write("."); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1245 | for (int c : swizzle.components()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 1246 | SkASSERT(c >= 0 && c <= 3); |
| 1247 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1251 | void MetalCodeGenerator::writeMatrixTimesEqualHelper(const Type& left, const Type& right, |
| 1252 | const Type& result) { |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1253 | String key = "TimesEqual" + this->typeName(left) + ":" + this->typeName(right); |
John Stiles | 56233d1 | 2021-02-03 13:03:29 -0500 | [diff] [blame] | 1254 | |
| 1255 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1256 | if (wasInserted) { |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 1257 | fExtraFunctions.printf("thread %s& operator*=(thread %s& left, thread const %s& right) {\n" |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1258 | " left = left * right;\n" |
| 1259 | " return left;\n" |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 1260 | "}\n", |
| 1261 | this->typeName(result).c_str(), this->typeName(left).c_str(), |
| 1262 | this->typeName(right).c_str()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1266 | void MetalCodeGenerator::writeMatrixEqualityHelper(const Type& left, const Type& right) { |
| 1267 | SkASSERTF(left.rows() == right.rows() && left.columns() == right.columns(), "left=%s, right=%s", |
| 1268 | left.description().c_str(), right.description().c_str()); |
| 1269 | |
| 1270 | String key = "Equality" + this->typeName(left) + ":" + this->typeName(right); |
| 1271 | |
| 1272 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1273 | if (wasInserted) { |
| 1274 | fExtraFunctions.printf( |
| 1275 | "thread bool operator==(const %s left, const %s right) {\n" |
| 1276 | " return", |
| 1277 | this->typeName(left).c_str(), this->typeName(right).c_str()); |
| 1278 | |
| 1279 | for (int index=0; index<left.columns(); ++index) { |
| 1280 | fExtraFunctions.printf("%s all(left[%d] == right[%d])", |
| 1281 | index == 0 ? "" : " &&", index, index); |
| 1282 | } |
| 1283 | fExtraFunctions.printf(";\n" |
| 1284 | "}\n"); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | void MetalCodeGenerator::writeMatrixInequalityHelper(const Type& left, const Type& right) { |
| 1289 | SkASSERTF(left.rows() == right.rows() && left.columns() == right.columns(), "left=%s, right=%s", |
| 1290 | left.description().c_str(), right.description().c_str()); |
| 1291 | |
| 1292 | String key = "Inequality" + this->typeName(left) + ":" + this->typeName(right); |
| 1293 | |
| 1294 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1295 | if (wasInserted) { |
| 1296 | fExtraFunctions.printf( |
| 1297 | "thread bool operator!=(const %s left, const %s right) {\n" |
| 1298 | " return", |
| 1299 | this->typeName(left).c_str(), this->typeName(right).c_str()); |
| 1300 | |
| 1301 | for (int index=0; index<left.columns(); ++index) { |
| 1302 | fExtraFunctions.printf("%s any(left[%d] != right[%d])", |
| 1303 | index == 0 ? "" : " ||", index, index); |
| 1304 | } |
| 1305 | fExtraFunctions.printf(";\n" |
| 1306 | "}\n"); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1310 | void MetalCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 1311 | Precedence parentPrecedence) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1312 | const Expression& left = *b.left(); |
| 1313 | const Expression& right = *b.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1314 | const Type& leftType = left.type(); |
| 1315 | const Type& rightType = right.type(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1316 | Operator op = b.getOperator(); |
| 1317 | Precedence precedence = op.getBinaryPrecedence(); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1318 | bool needParens = precedence >= parentPrecedence; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1319 | switch (op.kind()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1320 | case Token::Kind::TK_EQEQ: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1321 | if (leftType.isVector()) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1322 | this->write("all"); |
| 1323 | needParens = true; |
| 1324 | } |
| 1325 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1326 | case Token::Kind::TK_NEQ: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1327 | if (leftType.isVector()) { |
Jim Van Verth | 36477b4 | 2019-04-11 14:57:30 -0400 | [diff] [blame] | 1328 | this->write("any"); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1329 | needParens = true; |
| 1330 | } |
| 1331 | break; |
| 1332 | default: |
| 1333 | break; |
| 1334 | } |
| 1335 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1336 | this->write("("); |
| 1337 | } |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1338 | if (leftType.isMatrix() && rightType.isMatrix()) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1339 | if (op.kind() == Token::Kind::TK_STAREQ) { |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1340 | this->writeMatrixTimesEqualHelper(leftType, rightType, b.type()); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1341 | } else if (op.kind() == Token::Kind::TK_EQEQ) { |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1342 | this->writeMatrixEqualityHelper(leftType, rightType); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1343 | } else if (op.kind() == Token::Kind::TK_NEQ) { |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1344 | this->writeMatrixInequalityHelper(leftType, rightType); |
| 1345 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1346 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1347 | this->writeExpression(left, precedence); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1348 | if (op.kind() != Token::Kind::TK_EQ && op.isAssignment() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1349 | left.kind() == Expression::Kind::kSwizzle && !left.hasSideEffects()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1350 | // This doesn't compile in Metal: |
| 1351 | // float4 x = float4(1); |
| 1352 | // x.xy *= float2x2(...); |
| 1353 | // with the error message "non-const reference cannot bind to vector element", |
| 1354 | // but switching it to x.xy = x.xy * float2x2(...) fixes it. We perform this tranformation |
| 1355 | // as long as the LHS has no side effects, and hope for the best otherwise. |
| 1356 | this->write(" = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1357 | this->writeExpression(left, Precedence::kAssignment); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1358 | this->write(" "); |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1359 | String opName = OperatorName(op); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1360 | SkASSERT(opName.endsWith("=")); |
| 1361 | this->write(opName.substr(0, opName.size() - 1).c_str()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1362 | this->write(" "); |
| 1363 | } else { |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1364 | this->write(String(" ") + OperatorName(op) + " "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1365 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1366 | this->writeExpression(right, precedence); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1367 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1368 | this->write(")"); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | void MetalCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 1373 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1374 | if (Precedence::kTernary >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1375 | this->write("("); |
| 1376 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1377 | this->writeExpression(*t.test(), Precedence::kTernary); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1378 | this->write(" ? "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1379 | this->writeExpression(*t.ifTrue(), Precedence::kTernary); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1380 | this->write(" : "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1381 | this->writeExpression(*t.ifFalse(), Precedence::kTernary); |
| 1382 | if (Precedence::kTernary >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1383 | this->write(")"); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | void MetalCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 1388 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1389 | if (Precedence::kPrefix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1390 | this->write("("); |
| 1391 | } |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1392 | this->write(OperatorName(p.getOperator())); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1393 | this->writeExpression(*p.operand(), Precedence::kPrefix); |
| 1394 | if (Precedence::kPrefix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1395 | this->write(")"); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | void MetalCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 1400 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1401 | if (Precedence::kPostfix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1402 | this->write("("); |
| 1403 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1404 | this->writeExpression(*p.operand(), Precedence::kPostfix); |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1405 | this->write(OperatorName(p.getOperator())); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1406 | if (Precedence::kPostfix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1407 | this->write(")"); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | void MetalCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 1412 | this->write(b.value() ? "true" : "false"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
John Stiles | 12739df | 2020-12-29 09:47:20 -0500 | [diff] [blame] | 1416 | const Type& type = i.type(); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1417 | if (type == *fContext.fTypes.fUInt) { |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 1418 | this->write(to_string(i.value() & 0xffffffff) + "u"); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1419 | } else if (type == *fContext.fTypes.fUShort) { |
John Stiles | 12739df | 2020-12-29 09:47:20 -0500 | [diff] [blame] | 1420 | this->write(to_string(i.value() & 0xffff) + "u"); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1421 | } else if (type == *fContext.fTypes.fUByte) { |
John Stiles | 12739df | 2020-12-29 09:47:20 -0500 | [diff] [blame] | 1422 | this->write(to_string(i.value() & 0xff) + "u"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1423 | } else { |
John Stiles | 12739df | 2020-12-29 09:47:20 -0500 | [diff] [blame] | 1424 | this->write(to_string(i.value())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | void MetalCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 1429 | this->write(to_string(f.value())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | void MetalCodeGenerator::writeSetting(const Setting& s) { |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 1433 | SK_ABORT("internal error; setting was not folded to a constant during compilation\n"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1436 | void MetalCodeGenerator::writeFunctionRequirementArgs(const FunctionDeclaration& f, |
| 1437 | const char*& separator) { |
| 1438 | Requirements requirements = this->requirements(f); |
| 1439 | if (requirements & kInputs_Requirement) { |
| 1440 | this->write(separator); |
| 1441 | this->write("_in"); |
| 1442 | separator = ", "; |
| 1443 | } |
| 1444 | if (requirements & kOutputs_Requirement) { |
| 1445 | this->write(separator); |
| 1446 | this->write("_out"); |
| 1447 | separator = ", "; |
| 1448 | } |
| 1449 | if (requirements & kUniforms_Requirement) { |
| 1450 | this->write(separator); |
| 1451 | this->write("_uniforms"); |
| 1452 | separator = ", "; |
| 1453 | } |
| 1454 | if (requirements & kGlobals_Requirement) { |
| 1455 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1456 | this->write("_globals"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1457 | separator = ", "; |
| 1458 | } |
| 1459 | if (requirements & kFragCoord_Requirement) { |
| 1460 | this->write(separator); |
| 1461 | this->write("_fragCoord"); |
| 1462 | separator = ", "; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | void MetalCodeGenerator::writeFunctionRequirementParams(const FunctionDeclaration& f, |
| 1467 | const char*& separator) { |
| 1468 | Requirements requirements = this->requirements(f); |
| 1469 | if (requirements & kInputs_Requirement) { |
| 1470 | this->write(separator); |
| 1471 | this->write("Inputs _in"); |
| 1472 | separator = ", "; |
| 1473 | } |
| 1474 | if (requirements & kOutputs_Requirement) { |
| 1475 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1476 | this->write("thread Outputs& _out"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1477 | separator = ", "; |
| 1478 | } |
| 1479 | if (requirements & kUniforms_Requirement) { |
| 1480 | this->write(separator); |
| 1481 | this->write("Uniforms _uniforms"); |
| 1482 | separator = ", "; |
| 1483 | } |
| 1484 | if (requirements & kGlobals_Requirement) { |
| 1485 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1486 | this->write("thread Globals& _globals"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1487 | separator = ", "; |
| 1488 | } |
| 1489 | if (requirements & kFragCoord_Requirement) { |
| 1490 | this->write(separator); |
| 1491 | this->write("float4 _fragCoord"); |
| 1492 | separator = ", "; |
| 1493 | } |
| 1494 | } |
| 1495 | |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1496 | int MetalCodeGenerator::getUniformBinding(const Modifiers& m) { |
| 1497 | return (m.fLayout.fBinding >= 0) ? m.fLayout.fBinding |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1498 | : fProgram.fConfig->fSettings.fDefaultUniformBinding; |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | int MetalCodeGenerator::getUniformSet(const Modifiers& m) { |
| 1502 | return (m.fLayout.fSet >= 0) ? m.fLayout.fSet |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1503 | : fProgram.fConfig->fSettings.fDefaultUniformSet; |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1504 | } |
| 1505 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1506 | bool MetalCodeGenerator::writeFunctionDeclaration(const FunctionDeclaration& f) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1507 | fRTHeightName = fProgram.fInputs.fRTHeight ? "_globals._anonInterface0->u_skRTHeight" : ""; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1508 | const char* separator = ""; |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1509 | if (f.isMain()) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1510 | switch (fProgram.fConfig->fKind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1511 | case ProgramKind::kFragment: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1512 | this->write("fragment Outputs fragmentMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1513 | break; |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1514 | case ProgramKind::kVertex: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1515 | this->write("vertex Outputs vertexMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1516 | break; |
| 1517 | default: |
John Stiles | 3fabfc0 | 2020-09-25 15:51:28 -0400 | [diff] [blame] | 1518 | fErrors.error(-1, "unsupported kind of program"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1519 | return false; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1520 | } |
| 1521 | this->write("(Inputs _in [[stage_in]]"); |
| 1522 | if (-1 != fUniformBuffer) { |
| 1523 | this->write(", constant Uniforms& _uniforms [[buffer(" + |
| 1524 | to_string(fUniformBuffer) + ")]]"); |
| 1525 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1526 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1527 | if (e->is<GlobalVarDeclaration>()) { |
| 1528 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1529 | const VarDeclaration& var = decls.declaration()->as<VarDeclaration>(); |
| 1530 | if (var.var().type().typeKind() == Type::TypeKind::kSampler) { |
| 1531 | if (var.var().modifiers().fLayout.fBinding < 0) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1532 | fErrors.error(decls.fOffset, |
John Stiles | b41d5bb | 2021-01-26 16:28:12 -0500 | [diff] [blame] | 1533 | "Metal samplers must have 'layout(binding=...)'"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1534 | return false; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1535 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1536 | if (var.var().type().dimensions() != SpvDim2D) { |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1537 | // Not yet implemented--Skia currently only uses 2D textures. |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1538 | fErrors.error(decls.fOffset, "Unsupported texture dimensions"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1539 | return false; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1540 | } |
| 1541 | this->write(", texture2d<float> "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1542 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1543 | this->write("[[texture("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1544 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1545 | this->write(")]]"); |
| 1546 | this->write(", sampler "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1547 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1548 | this->write(SAMPLER_SUFFIX); |
| 1549 | this->write("[[sampler("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1550 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1551 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1552 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1553 | } else if (e->is<InterfaceBlock>()) { |
| 1554 | const InterfaceBlock& intf = e->as<InterfaceBlock>(); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1555 | if (intf.typeName() == "sk_PerVertex") { |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1556 | continue; |
| 1557 | } |
| 1558 | this->write(", constant "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1559 | this->writeType(intf.variable().type()); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1560 | this->write("& " ); |
| 1561 | this->write(fInterfaceBlockNameMap[&intf]); |
| 1562 | this->write(" [[buffer("); |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1563 | this->write(to_string(this->getUniformBinding(intf.variable().modifiers()))); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1564 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1565 | } |
| 1566 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1567 | if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 1568 | if (fProgram.fInputs.fRTHeight && fInterfaceBlockNameMap.empty()) { |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1569 | this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(1)]]"); |
Ethan Nicholas | f931e40 | 2019-07-26 15:40:33 -0400 | [diff] [blame] | 1570 | fRTHeightName = "_anonInterface0.u_skRTHeight"; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1571 | } |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1572 | this->write(", bool _frontFacing [[front_facing]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1573 | this->write(", float4 _fragCoord [[position]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1574 | } else if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1575 | this->write(", uint sk_VertexID [[vertex_id]], uint sk_InstanceID [[instance_id]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1576 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1577 | separator = ", "; |
| 1578 | } else { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1579 | this->writeType(f.returnType()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1580 | this->write(" "); |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 1581 | this->writeName(f.mangledName()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1582 | this->write("("); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1583 | this->writeFunctionRequirementParams(f, separator); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1584 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1585 | for (const auto& param : f.parameters()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1586 | this->write(separator); |
| 1587 | separator = ", "; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1588 | this->writeModifiers(param->modifiers(), /*globalContext=*/false); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1589 | const Type* type = ¶m->type(); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1590 | this->writeType(*type); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1591 | if (param->modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | f2bd501 | 2020-12-04 11:58:26 -0500 | [diff] [blame] | 1592 | this->write("&"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1593 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1594 | this->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1595 | this->writeName(param->name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1596 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1597 | this->write(")"); |
| 1598 | return true; |
| 1599 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1600 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1601 | void MetalCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 1602 | this->writeFunctionDeclaration(f.declaration()); |
| 1603 | this->writeLine(";"); |
| 1604 | } |
| 1605 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1606 | static bool is_block_ending_with_return(const Statement* stmt) { |
| 1607 | // This function detects (potentially nested) blocks that end in a return statement. |
| 1608 | if (!stmt->is<Block>()) { |
| 1609 | return false; |
| 1610 | } |
| 1611 | const StatementArray& block = stmt->as<Block>().children(); |
| 1612 | for (int index = block.count(); index--; ) { |
| 1613 | const Statement& stmt = *block[index]; |
| 1614 | if (stmt.is<ReturnStatement>()) { |
| 1615 | return true; |
| 1616 | } |
| 1617 | if (stmt.is<Block>()) { |
| 1618 | return is_block_ending_with_return(&stmt); |
| 1619 | } |
| 1620 | if (!stmt.is<Nop>()) { |
| 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1627 | void MetalCodeGenerator::writeFunction(const FunctionDefinition& f) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1628 | SkASSERT(!fProgram.fConfig->fSettings.fFragColorIsInOut); |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 1629 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1630 | if (!this->writeFunctionDeclaration(f.declaration())) { |
| 1631 | return; |
| 1632 | } |
| 1633 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1634 | fCurrentFunction = &f.declaration(); |
| 1635 | SkScopeExit clearCurrentFunction([&] { fCurrentFunction = nullptr; }); |
| 1636 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1637 | this->writeLine(" {"); |
| 1638 | |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1639 | if (f.declaration().isMain()) { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1640 | this->writeGlobalInit(); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1641 | this->writeLine(" Outputs _out;"); |
John Stiles | 3727917 | 2021-01-21 22:24:28 -0500 | [diff] [blame] | 1642 | this->writeLine(" (void)_out;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1643 | } |
John Stiles | c67b362 | 2020-05-28 17:53:13 -0400 | [diff] [blame] | 1644 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1645 | fFunctionHeader = ""; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1646 | StringStream buffer; |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1647 | { |
| 1648 | AutoOutputStream outputToBuffer(this, &buffer); |
| 1649 | fIndentation++; |
| 1650 | for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
| 1651 | if (!stmt->isEmpty()) { |
| 1652 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1653 | this->finishLine(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1654 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1655 | } |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1656 | if (f.declaration().isMain()) { |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1657 | // If the main function doesn't end with a return, we need to synthesize one here. |
| 1658 | if (!is_block_ending_with_return(f.body().get())) { |
| 1659 | this->writeReturnStatementFromMain(); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1660 | this->finishLine(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1661 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1662 | } |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1663 | fIndentation--; |
| 1664 | this->writeLine("}"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1665 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1666 | this->write(fFunctionHeader); |
| 1667 | this->write(buffer.str()); |
| 1668 | } |
| 1669 | |
| 1670 | void MetalCodeGenerator::writeModifiers(const Modifiers& modifiers, |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1671 | bool globalContext) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1672 | if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 1673 | this->write("thread "); |
| 1674 | } |
| 1675 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
John Stiles | 0bd5578 | 2021-02-09 18:29:40 -0500 | [diff] [blame] | 1676 | this->write("const "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | void MetalCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1681 | if ("sk_PerVertex" == intf.typeName()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1682 | return; |
| 1683 | } |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1684 | this->writeModifiers(intf.variable().modifiers(), /*globalContext=*/true); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1685 | this->write("struct "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1686 | this->writeLine(intf.typeName() + " {"); |
| 1687 | const Type* structType = &intf.variable().type(); |
John Stiles | bb43b7e | 2020-12-03 17:36:32 -0500 | [diff] [blame] | 1688 | if (structType->isArray()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1689 | structType = &structType->componentType(); |
| 1690 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1691 | fIndentation++; |
John Stiles | 3dba3ee | 2020-12-02 23:35:49 -0500 | [diff] [blame] | 1692 | this->writeFields(structType->fields(), structType->fOffset, &intf); |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 1693 | if (fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1694 | this->writeLine("float u_skRTHeight;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1695 | } |
| 1696 | fIndentation--; |
| 1697 | this->write("}"); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1698 | if (intf.instanceName().size()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1699 | this->write(" "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1700 | this->write(intf.instanceName()); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1701 | if (intf.arraySize() > 0) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1702 | this->write("["); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1703 | this->write(to_string(intf.arraySize())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1704 | this->write("]"); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1705 | } else if (intf.arraySize() == Type::kUnsizedArray){ |
| 1706 | this->write("[]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1707 | } |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1708 | fInterfaceBlockNameMap[&intf] = intf.instanceName(); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1709 | } else { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1710 | fInterfaceBlockNameMap[&intf] = "_anonInterface" + to_string(fAnonInterfaceCount++); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1711 | } |
| 1712 | this->writeLine(";"); |
| 1713 | } |
| 1714 | |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1715 | void MetalCodeGenerator::writeFields(const std::vector<Type::Field>& fields, int parentOffset, |
| 1716 | const InterfaceBlock* parentIntf) { |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1717 | MemoryLayout memoryLayout(MemoryLayout::kMetal_Standard); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1718 | int currentOffset = 0; |
| 1719 | for (const auto& field: fields) { |
| 1720 | int fieldOffset = field.fModifiers.fLayout.fOffset; |
| 1721 | const Type* fieldType = field.fType; |
John Stiles | 21f5f45 | 2020-11-30 09:57:59 -0500 | [diff] [blame] | 1722 | if (!MemoryLayout::LayoutIsSupported(*fieldType)) { |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 1723 | fErrors.error(parentOffset, "type '" + fieldType->name() + "' is not permitted here"); |
| 1724 | return; |
| 1725 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1726 | if (fieldOffset != -1) { |
| 1727 | if (currentOffset > fieldOffset) { |
| 1728 | fErrors.error(parentOffset, |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 1729 | "offset of field '" + field.fName + "' must be at least " + |
| 1730 | to_string((int) currentOffset)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1731 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1732 | } else if (currentOffset < fieldOffset) { |
| 1733 | this->write("char pad"); |
| 1734 | this->write(to_string(fPaddingCount++)); |
| 1735 | this->write("["); |
| 1736 | this->write(to_string(fieldOffset - currentOffset)); |
| 1737 | this->writeLine("];"); |
| 1738 | currentOffset = fieldOffset; |
| 1739 | } |
| 1740 | int alignment = memoryLayout.alignment(*fieldType); |
| 1741 | if (fieldOffset % alignment) { |
| 1742 | fErrors.error(parentOffset, |
| 1743 | "offset of field '" + field.fName + "' must be a multiple of " + |
| 1744 | to_string((int) alignment)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1745 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1746 | } |
| 1747 | } |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 1748 | size_t fieldSize = memoryLayout.size(*fieldType); |
| 1749 | if (fieldSize > static_cast<size_t>(std::numeric_limits<int>::max() - currentOffset)) { |
| 1750 | fErrors.error(parentOffset, "field offset overflow"); |
| 1751 | return; |
| 1752 | } |
| 1753 | currentOffset += fieldSize; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1754 | this->writeModifiers(field.fModifiers, /*globalContext=*/false); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1755 | this->writeType(*fieldType); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1756 | this->write(" "); |
| 1757 | this->writeName(field.fName); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1758 | this->writeLine(";"); |
| 1759 | if (parentIntf) { |
| 1760 | fInterfaceBlockMap[&field] = parentIntf; |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1765 | void MetalCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1766 | this->writeExpression(value, Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1769 | void MetalCodeGenerator::writeName(const String& name) { |
| 1770 | if (fReservedWords.find(name) != fReservedWords.end()) { |
| 1771 | this->write("_"); // adding underscore before name to avoid conflict with reserved words |
| 1772 | } |
| 1773 | this->write(name); |
| 1774 | } |
| 1775 | |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1776 | void MetalCodeGenerator::writeVarDeclaration(const VarDeclaration& varDecl, bool global) { |
| 1777 | if (global && !(varDecl.var().modifiers().fFlags & Modifiers::kConst_Flag)) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1778 | return; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1779 | } |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1780 | this->writeModifiers(varDecl.var().modifiers(), global); |
| 1781 | this->writeType(varDecl.var().type()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1782 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1783 | this->writeName(varDecl.var().name()); |
| 1784 | if (varDecl.value()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1785 | this->write(" = "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1786 | this->writeVarInitializer(varDecl.var(), *varDecl.value()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1787 | } |
| 1788 | this->write(";"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | void MetalCodeGenerator::writeStatement(const Statement& s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1792 | switch (s.kind()) { |
| 1793 | case Statement::Kind::kBlock: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1794 | this->writeBlock(s.as<Block>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1795 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1796 | case Statement::Kind::kExpression: |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1797 | this->writeExpression(*s.as<ExpressionStatement>().expression(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1798 | this->write(";"); |
| 1799 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1800 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1801 | this->writeReturnStatement(s.as<ReturnStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1802 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1803 | case Statement::Kind::kVarDeclaration: |
| 1804 | this->writeVarDeclaration(s.as<VarDeclaration>(), false); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1805 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1806 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1807 | this->writeIfStatement(s.as<IfStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1808 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1809 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1810 | this->writeForStatement(s.as<ForStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1811 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1812 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1813 | this->writeDoStatement(s.as<DoStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1814 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1815 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 1816 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1817 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1818 | case Statement::Kind::kBreak: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1819 | this->write("break;"); |
| 1820 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1821 | case Statement::Kind::kContinue: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1822 | this->write("continue;"); |
| 1823 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1824 | case Statement::Kind::kDiscard: |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1825 | this->write("discard_fragment();"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1826 | break; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1827 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1828 | case Statement::Kind::kNop: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1829 | this->write(";"); |
| 1830 | break; |
| 1831 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 1832 | SkDEBUGFAILF("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1833 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1837 | void MetalCodeGenerator::writeBlock(const Block& b) { |
John Stiles | 3744bd6 | 2021-01-26 13:49:43 -0500 | [diff] [blame] | 1838 | // Write scope markers if this block is a scope, or if the block is empty (since we need to emit |
| 1839 | // something here to make the code valid). |
| 1840 | bool isScope = b.isScope() || b.isEmpty(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1841 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1842 | this->writeLine("{"); |
| 1843 | fIndentation++; |
| 1844 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1845 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 1846 | if (!stmt->isEmpty()) { |
| 1847 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1848 | this->finishLine(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 1852 | fIndentation--; |
| 1853 | this->write("}"); |
| 1854 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | void MetalCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1858 | this->write("if ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1859 | this->writeExpression(*stmt.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1860 | this->write(") "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1861 | this->writeStatement(*stmt.ifTrue()); |
| 1862 | if (stmt.ifFalse()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1863 | this->write(" else "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1864 | this->writeStatement(*stmt.ifFalse()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | void MetalCodeGenerator::writeForStatement(const ForStatement& f) { |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 1869 | // Emit loops of the form 'for(;test;)' as 'while(test)', which is probably how they started |
| 1870 | if (!f.initializer() && f.test() && !f.next()) { |
| 1871 | this->write("while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1872 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 1873 | this->write(") "); |
| 1874 | this->writeStatement(*f.statement()); |
| 1875 | return; |
| 1876 | } |
| 1877 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1878 | this->write("for ("); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1879 | if (f.initializer() && !f.initializer()->isEmpty()) { |
| 1880 | this->writeStatement(*f.initializer()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1881 | } else { |
| 1882 | this->write("; "); |
| 1883 | } |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1884 | if (f.test()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1885 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1886 | } |
| 1887 | this->write("; "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1888 | if (f.next()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1889 | this->writeExpression(*f.next(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1890 | } |
| 1891 | this->write(") "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1892 | this->writeStatement(*f.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1895 | void MetalCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 1896 | this->write("do "); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 1897 | this->writeStatement(*d.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1898 | this->write(" while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1899 | this->writeExpression(*d.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1900 | this->write(");"); |
| 1901 | } |
| 1902 | |
| 1903 | void MetalCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1904 | this->write("switch ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1905 | this->writeExpression(*s.value(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1906 | this->writeLine(") {"); |
| 1907 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1908 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 1909 | const SwitchCase& c = stmt->as<SwitchCase>(); |
| 1910 | if (c.value()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1911 | this->write("case "); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1912 | this->writeExpression(*c.value(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1913 | this->writeLine(":"); |
| 1914 | } else { |
| 1915 | this->writeLine("default:"); |
| 1916 | } |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1917 | if (!c.statement()->isEmpty()) { |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 1918 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 1919 | this->writeStatement(*c.statement()); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1920 | this->finishLine(); |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 1921 | fIndentation--; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1922 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1923 | } |
| 1924 | fIndentation--; |
| 1925 | this->write("}"); |
| 1926 | } |
| 1927 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1928 | void MetalCodeGenerator::writeReturnStatementFromMain() { |
| 1929 | // main functions in Metal return a magic _out parameter that doesn't exist in SkSL. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1930 | switch (fProgram.fConfig->fKind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1931 | case ProgramKind::kFragment: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1932 | this->write("return _out;"); |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1933 | break; |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1934 | case ProgramKind::kVertex: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1935 | this->write("return (_out.sk_Position.y = -_out.sk_Position.y, _out);"); |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1936 | break; |
| 1937 | default: |
| 1938 | SkDEBUGFAIL("unsupported kind of program"); |
| 1939 | } |
| 1940 | } |
| 1941 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1942 | void MetalCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1943 | if (fCurrentFunction && fCurrentFunction->isMain()) { |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1944 | if (r.expression()) { |
John Stiles | 97d1817 | 2021-01-22 16:47:42 -0500 | [diff] [blame] | 1945 | if (r.expression()->type() == *fContext.fTypes.fHalf4) { |
| 1946 | this->write("_out.sk_FragColor = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1947 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
John Stiles | 97d1817 | 2021-01-22 16:47:42 -0500 | [diff] [blame] | 1948 | this->writeLine(";"); |
| 1949 | } else { |
| 1950 | fErrors.error(r.fOffset, "Metal does not support returning '" + |
| 1951 | r.expression()->type().description() + "' from main()"); |
| 1952 | } |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1953 | } |
| 1954 | this->writeReturnStatementFromMain(); |
| 1955 | return; |
| 1956 | } |
| 1957 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1958 | this->write("return"); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1959 | if (r.expression()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1960 | this->write(" "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1961 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1962 | } |
| 1963 | this->write(";"); |
| 1964 | } |
| 1965 | |
Michael Ludwig | bf58add | 2021-03-16 10:40:11 -0400 | [diff] [blame] | 1966 | void MetalCodeGenerator::writeHeader() { |
| 1967 | this->write("#include <metal_stdlib>\n"); |
| 1968 | this->write("#include <simd/simd.h>\n"); |
| 1969 | this->write("using namespace metal;\n"); |
| 1970 | } |
| 1971 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1972 | void MetalCodeGenerator::writeUniformStruct() { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1973 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1974 | if (e->is<GlobalVarDeclaration>()) { |
| 1975 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1976 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1977 | if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1978 | var.type().typeKind() != Type::TypeKind::kSampler) { |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1979 | int uniformSet = this->getUniformSet(var.modifiers()); |
John Stiles | d8fc95d | 2021-01-26 16:22:53 -0500 | [diff] [blame] | 1980 | // Make sure that the program's uniform-set value is consistent throughout. |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1981 | if (-1 == fUniformBuffer) { |
| 1982 | this->write("struct Uniforms {\n"); |
John Stiles | d8fc95d | 2021-01-26 16:22:53 -0500 | [diff] [blame] | 1983 | fUniformBuffer = uniformSet; |
| 1984 | } else if (uniformSet != fUniformBuffer) { |
| 1985 | fErrors.error(decls.fOffset, "Metal backend requires all uniforms to have " |
| 1986 | "the same 'layout(set=...)'"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1987 | } |
| 1988 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1989 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1990 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1991 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1992 | this->write(";\n"); |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | if (-1 != fUniformBuffer) { |
| 1997 | this->write("};\n"); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | void MetalCodeGenerator::writeInputStruct() { |
| 2002 | this->write("struct Inputs {\n"); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2003 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2004 | if (e->is<GlobalVarDeclaration>()) { |
| 2005 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2006 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2007 | if (var.modifiers().fFlags & Modifiers::kIn_Flag && |
| 2008 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2009 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2010 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2011 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2012 | this->writeName(var.name()); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2013 | if (-1 != var.modifiers().fLayout.fLocation) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2014 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2015 | this->write(" [[attribute(" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2016 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2017 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2018 | this->write(" [[user(locn" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2019 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | this->write(";\n"); |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | this->write("};\n"); |
| 2027 | } |
| 2028 | |
| 2029 | void MetalCodeGenerator::writeOutputStruct() { |
| 2030 | this->write("struct Outputs {\n"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2031 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 2032 | this->write(" float4 sk_Position [[position]];\n"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2033 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 2034 | this->write(" float4 sk_FragColor [[color(0)]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2035 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2036 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2037 | if (e->is<GlobalVarDeclaration>()) { |
| 2038 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2039 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2040 | if (var.modifiers().fFlags & Modifiers::kOut_Flag && |
| 2041 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2042 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2043 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2044 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2045 | this->writeName(var.name()); |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2046 | |
| 2047 | int location = var.modifiers().fLayout.fLocation; |
| 2048 | if (location < 0) { |
| 2049 | fErrors.error(var.fOffset, |
| 2050 | "Metal out variables must have 'layout(location=...)'"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2051 | } else if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2052 | this->write(" [[user(locn" + to_string(location) + ")]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2053 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2054 | this->write(" [[color(" + to_string(location) + ")"); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2055 | int colorIndex = var.modifiers().fLayout.fIndex; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2056 | if (colorIndex) { |
| 2057 | this->write(", index(" + to_string(colorIndex) + ")"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2058 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2059 | this->write("]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2060 | } |
| 2061 | this->write(";\n"); |
| 2062 | } |
| 2063 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2064 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2065 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Jim Van Verth | 3913d3e | 2020-08-31 15:16:57 -0400 | [diff] [blame] | 2066 | this->write(" float sk_PointSize [[point_size]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2067 | } |
| 2068 | this->write("};\n"); |
| 2069 | } |
| 2070 | |
| 2071 | void MetalCodeGenerator::writeInterfaceBlocks() { |
| 2072 | bool wroteInterfaceBlock = false; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2073 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2074 | if (e->is<InterfaceBlock>()) { |
| 2075 | this->writeInterfaceBlock(e->as<InterfaceBlock>()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2076 | wroteInterfaceBlock = true; |
| 2077 | } |
| 2078 | } |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 2079 | if (!wroteInterfaceBlock && fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2080 | this->writeLine("struct sksl_synthetic_uniforms {"); |
| 2081 | this->writeLine(" float u_skRTHeight;"); |
| 2082 | this->writeLine("};"); |
| 2083 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2084 | } |
| 2085 | |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2086 | void MetalCodeGenerator::writeStructDefinitions() { |
| 2087 | for (const ProgramElement* e : fProgram.elements()) { |
| 2088 | if (e->is<StructDefinition>()) { |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 2089 | this->writeStructDefinition(e->as<StructDefinition>()); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | } |
| 2093 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2094 | void MetalCodeGenerator::visitGlobalStruct(GlobalStructVisitor* visitor) { |
| 2095 | // Visit the interface blocks. |
| 2096 | for (const auto& [interfaceType, interfaceName] : fInterfaceBlockNameMap) { |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2097 | visitor->visitInterfaceBlock(*interfaceType, interfaceName); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2098 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2099 | for (const ProgramElement* element : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2100 | if (!element->is<GlobalVarDeclaration>()) { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2101 | continue; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2102 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2103 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 2104 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 2105 | const Variable& var = decl.var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2106 | if ((!var.modifiers().fFlags && -1 == var.modifiers().fLayout.fBuiltin) || |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2107 | var.type().typeKind() == Type::TypeKind::kSampler) { |
| 2108 | if (var.type().typeKind() == Type::TypeKind::kSampler) { |
| 2109 | // Samplers are represented as a "texture/sampler" duo in the global struct. |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2110 | visitor->visitTexture(var.type(), var.name()); |
| 2111 | visitor->visitSampler(var.type(), String(var.name()) + SAMPLER_SUFFIX); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2112 | } else { |
| 2113 | // Visit a regular variable. |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2114 | visitor->visitVariable(var, decl.value().get()); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | } |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | void MetalCodeGenerator::writeGlobalStruct() { |
| 2121 | class : public GlobalStructVisitor { |
| 2122 | public: |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2123 | void visitInterfaceBlock(const InterfaceBlock& block, const String& blockName) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2124 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2125 | fCodeGen->write(" constant "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2126 | fCodeGen->write(block.typeName()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2127 | fCodeGen->write("* "); |
| 2128 | fCodeGen->writeName(blockName); |
| 2129 | fCodeGen->write(";\n"); |
| 2130 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2131 | void visitTexture(const Type& type, const String& name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2132 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2133 | fCodeGen->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2134 | fCodeGen->writeType(type); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2135 | fCodeGen->write(" "); |
| 2136 | fCodeGen->writeName(name); |
| 2137 | fCodeGen->write(";\n"); |
| 2138 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2139 | void visitSampler(const Type&, const String& name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2140 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2141 | fCodeGen->write(" sampler "); |
| 2142 | fCodeGen->writeName(name); |
| 2143 | fCodeGen->write(";\n"); |
| 2144 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2145 | void visitVariable(const Variable& var, const Expression* value) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2146 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2147 | fCodeGen->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2148 | fCodeGen->writeType(var.type()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2149 | fCodeGen->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2150 | fCodeGen->writeName(var.name()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2151 | fCodeGen->write(";\n"); |
| 2152 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2153 | void addElement() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2154 | if (fFirst) { |
| 2155 | fCodeGen->write("struct Globals {\n"); |
| 2156 | fFirst = false; |
| 2157 | } |
| 2158 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2159 | void finish() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2160 | if (!fFirst) { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2161 | fCodeGen->writeLine("};"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2162 | fFirst = true; |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | MetalCodeGenerator* fCodeGen = nullptr; |
| 2167 | bool fFirst = true; |
| 2168 | } visitor; |
| 2169 | |
| 2170 | visitor.fCodeGen = this; |
| 2171 | this->visitGlobalStruct(&visitor); |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2172 | visitor.finish(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | void MetalCodeGenerator::writeGlobalInit() { |
| 2176 | class : public GlobalStructVisitor { |
| 2177 | public: |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2178 | void visitInterfaceBlock(const InterfaceBlock& blockType, |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2179 | const String& blockName) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2180 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2181 | fCodeGen->write("&"); |
| 2182 | fCodeGen->writeName(blockName); |
| 2183 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2184 | void visitTexture(const Type&, const String& name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2185 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2186 | fCodeGen->writeName(name); |
| 2187 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2188 | void visitSampler(const Type&, const String& name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2189 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2190 | fCodeGen->writeName(name); |
| 2191 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2192 | void visitVariable(const Variable& var, const Expression* value) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2193 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2194 | if (value) { |
| 2195 | fCodeGen->writeVarInitializer(var, *value); |
| 2196 | } else { |
| 2197 | fCodeGen->write("{}"); |
| 2198 | } |
| 2199 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2200 | void addElement() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2201 | if (fFirst) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 2202 | fCodeGen->write(" Globals _globals{"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2203 | fFirst = false; |
| 2204 | } else { |
| 2205 | fCodeGen->write(", "); |
| 2206 | } |
| 2207 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2208 | void finish() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2209 | if (!fFirst) { |
| 2210 | fCodeGen->writeLine("};"); |
John Stiles | 3727917 | 2021-01-21 22:24:28 -0500 | [diff] [blame] | 2211 | fCodeGen->writeLine(" (void)_globals;"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | MetalCodeGenerator* fCodeGen = nullptr; |
| 2215 | bool fFirst = true; |
| 2216 | } visitor; |
| 2217 | |
| 2218 | visitor.fCodeGen = this; |
| 2219 | this->visitGlobalStruct(&visitor); |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2220 | visitor.finish(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2221 | } |
| 2222 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2223 | void MetalCodeGenerator::writeProgramElement(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2224 | switch (e.kind()) { |
| 2225 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2226 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2227 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2228 | const GlobalVarDeclaration& global = e.as<GlobalVarDeclaration>(); |
| 2229 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 2230 | int builtin = decl.var().modifiers().fLayout.fBuiltin; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2231 | if (-1 == builtin) { |
| 2232 | // normal var |
| 2233 | this->writeVarDeclaration(decl, true); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 2234 | this->finishLine(); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2235 | } else if (SK_FRAGCOLOR_BUILTIN == builtin) { |
| 2236 | // ignore |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2237 | } |
| 2238 | break; |
| 2239 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2240 | case ProgramElement::Kind::kInterfaceBlock: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2241 | // handled in writeInterfaceBlocks, do nothing |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2242 | break; |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2243 | case ProgramElement::Kind::kStructDefinition: |
| 2244 | // Handled in writeStructDefinitions. Do nothing. |
| 2245 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2246 | case ProgramElement::Kind::kFunction: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2247 | this->writeFunction(e.as<FunctionDefinition>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2248 | break; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 2249 | case ProgramElement::Kind::kFunctionPrototype: |
| 2250 | this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
| 2251 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2252 | case ProgramElement::Kind::kModifiers: |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 2253 | this->writeModifiers(e.as<ModifiersDeclaration>().modifiers(), |
| 2254 | /*globalContext=*/true); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2255 | this->writeLine(";"); |
| 2256 | break; |
John Stiles | 712fd6b | 2020-11-25 22:25:43 -0500 | [diff] [blame] | 2257 | case ProgramElement::Kind::kEnum: |
| 2258 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2259 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 2260 | SkDEBUGFAILF("unsupported program element: %s\n", e.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2261 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2262 | } |
| 2263 | } |
| 2264 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2265 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Expression* e) { |
| 2266 | if (!e) { |
| 2267 | return kNo_Requirements; |
| 2268 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2269 | switch (e->kind()) { |
| 2270 | case Expression::Kind::kFunctionCall: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2271 | const FunctionCall& f = e->as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 2272 | Requirements result = this->requirements(f.function()); |
| 2273 | for (const auto& arg : f.arguments()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2274 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2275 | } |
| 2276 | return result; |
| 2277 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 2278 | case Expression::Kind::kConstructor: |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame^] | 2279 | case Expression::Kind::kConstructorComposite: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 2280 | case Expression::Kind::kConstructorArray: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 2281 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 2282 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 2283 | case Expression::Kind::kConstructorSplat: |
| 2284 | case Expression::Kind::kConstructorVectorCast: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 2285 | const AnyConstructor& c = e->asAnyConstructor(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2286 | Requirements result = kNo_Requirements; |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 2287 | for (const auto& arg : c.argumentSpan()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2288 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2289 | } |
| 2290 | return result; |
| 2291 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2292 | case Expression::Kind::kFieldAccess: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2293 | const FieldAccess& f = e->as<FieldAccess>(); |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 2294 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2295 | return kGlobals_Requirement; |
| 2296 | } |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 2297 | return this->requirements(f.base().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2298 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2299 | case Expression::Kind::kSwizzle: |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2300 | return this->requirements(e->as<Swizzle>().base().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2301 | case Expression::Kind::kBinary: { |
| 2302 | const BinaryExpression& bin = e->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2303 | return this->requirements(bin.left().get()) | |
| 2304 | this->requirements(bin.right().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2305 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2306 | case Expression::Kind::kIndex: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2307 | const IndexExpression& idx = e->as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 2308 | return this->requirements(idx.base().get()) | this->requirements(idx.index().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2309 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2310 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2311 | return this->requirements(e->as<PrefixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2312 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2313 | return this->requirements(e->as<PostfixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2314 | case Expression::Kind::kTernary: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2315 | const TernaryExpression& t = e->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2316 | return this->requirements(t.test().get()) | this->requirements(t.ifTrue().get()) | |
| 2317 | this->requirements(t.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2318 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2319 | case Expression::Kind::kVariableReference: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2320 | const VariableReference& v = e->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2321 | const Modifiers& modifiers = v.variable()->modifiers(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2322 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2323 | if (modifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 2324 | result = kGlobals_Requirement | kFragCoord_Requirement; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2325 | } else if (Variable::Storage::kGlobal == v.variable()->storage()) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2326 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2327 | result = kInputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2328 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2329 | result = kOutputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2330 | } else if (modifiers.fFlags & Modifiers::kUniform_Flag && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2331 | v.variable()->type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2332 | result = kUniforms_Requirement; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2333 | } else { |
| 2334 | result = kGlobals_Requirement; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | return result; |
| 2338 | } |
| 2339 | default: |
| 2340 | return kNo_Requirements; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2344 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Statement* s) { |
| 2345 | if (!s) { |
| 2346 | return kNo_Requirements; |
| 2347 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2348 | switch (s->kind()) { |
| 2349 | case Statement::Kind::kBlock: { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2350 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2351 | for (const std::unique_ptr<Statement>& child : s->as<Block>().children()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2352 | result |= this->requirements(child.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2353 | } |
| 2354 | return result; |
| 2355 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2356 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2357 | const VarDeclaration& var = s->as<VarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2358 | return this->requirements(var.value().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2359 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2360 | case Statement::Kind::kExpression: |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 2361 | return this->requirements(s->as<ExpressionStatement>().expression().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2362 | case Statement::Kind::kReturn: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2363 | const ReturnStatement& r = s->as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 2364 | return this->requirements(r.expression().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2365 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2366 | case Statement::Kind::kIf: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2367 | const IfStatement& i = s->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2368 | return this->requirements(i.test().get()) | |
| 2369 | this->requirements(i.ifTrue().get()) | |
| 2370 | this->requirements(i.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2371 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2372 | case Statement::Kind::kFor: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2373 | const ForStatement& f = s->as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2374 | return this->requirements(f.initializer().get()) | |
| 2375 | this->requirements(f.test().get()) | |
| 2376 | this->requirements(f.next().get()) | |
| 2377 | this->requirements(f.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2378 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2379 | case Statement::Kind::kDo: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2380 | const DoStatement& d = s->as<DoStatement>(); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 2381 | return this->requirements(d.test().get()) | |
| 2382 | this->requirements(d.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2383 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2384 | case Statement::Kind::kSwitch: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2385 | const SwitchStatement& sw = s->as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 2386 | Requirements result = this->requirements(sw.value().get()); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2387 | for (const std::unique_ptr<Statement>& sc : sw.cases()) { |
| 2388 | result |= this->requirements(sc->as<SwitchCase>().statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2389 | } |
| 2390 | return result; |
| 2391 | } |
| 2392 | default: |
| 2393 | return kNo_Requirements; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const FunctionDeclaration& f) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2398 | if (f.isBuiltin()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2399 | return kNo_Requirements; |
| 2400 | } |
| 2401 | auto found = fRequirements.find(&f); |
| 2402 | if (found == fRequirements.end()) { |
Ethan Nicholas | 65a8f56 | 2019-04-19 14:00:26 -0400 | [diff] [blame] | 2403 | fRequirements[&f] = kNo_Requirements; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2404 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2405 | if (e->is<FunctionDefinition>()) { |
| 2406 | const FunctionDefinition& def = e->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2407 | if (&def.declaration() == &f) { |
| 2408 | Requirements reqs = this->requirements(def.body().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2409 | fRequirements[&f] = reqs; |
| 2410 | return reqs; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 2414 | // We never found a definition for this declared function, but it's legal to prototype a |
| 2415 | // function without ever giving a definition, as long as you don't call it. |
| 2416 | return kNo_Requirements; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2417 | } |
| 2418 | return found->second; |
| 2419 | } |
| 2420 | |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 2421 | bool MetalCodeGenerator::generateCode() { |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 2422 | StringStream header; |
| 2423 | { |
| 2424 | AutoOutputStream outputToHeader(this, &header, &fIndentation); |
Michael Ludwig | bf58add | 2021-03-16 10:40:11 -0400 | [diff] [blame] | 2425 | this->writeHeader(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 2426 | this->writeStructDefinitions(); |
| 2427 | this->writeUniformStruct(); |
| 2428 | this->writeInputStruct(); |
| 2429 | this->writeOutputStruct(); |
| 2430 | this->writeInterfaceBlocks(); |
| 2431 | this->writeGlobalStruct(); |
| 2432 | } |
| 2433 | StringStream body; |
| 2434 | { |
| 2435 | AutoOutputStream outputToBody(this, &body, &fIndentation); |
| 2436 | for (const ProgramElement* e : fProgram.elements()) { |
| 2437 | this->writeProgramElement(*e); |
| 2438 | } |
| 2439 | } |
| 2440 | write_stringstream(header, *fOut); |
| 2441 | write_stringstream(fExtraFunctions, *fOut); |
| 2442 | write_stringstream(body, *fOut); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 2443 | return 0 == fErrors.errorCount(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2444 | } |
| 2445 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 2446 | } // namespace SkSL |