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