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