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