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