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