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