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