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