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 | |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 8 | #include "src/sksl/codegen/SkSLMetalCodeGenerator.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 9 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 10 | #include "src/core/SkScopeExit.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLMemoryLayout.h" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 14 | #include "src/sksl/ir/SkSLConstructorArrayCast.h" |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLConstructorCompoundCast.h" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLConstructorStruct.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 21 | #include "src/sksl/ir/SkSLExtension.h" |
| 22 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 23 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 24 | #include "src/sksl/ir/SkSLNop.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 27 | |
Brian Osman | c262a12 | 2020-08-06 16:34:34 -0400 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 30 | namespace SkSL { |
| 31 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 32 | const char* MetalCodeGenerator::OperatorName(Operator op) { |
| 33 | switch (op.kind()) { |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 34 | case Token::Kind::TK_LOGICALXOR: return "!="; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 35 | default: return op.operatorName(); |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 39 | class MetalCodeGenerator::GlobalStructVisitor { |
| 40 | public: |
| 41 | virtual ~GlobalStructVisitor() = default; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 42 | virtual void visitInterfaceBlock(const InterfaceBlock& block, skstd::string_view blockName) = 0; |
| 43 | virtual void visitTexture(const Type& type, skstd::string_view name) = 0; |
| 44 | virtual void visitSampler(const Type& type, skstd::string_view name) = 0; |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 45 | virtual void visitVariable(const Variable& var, const Expression* value) = 0; |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 48 | void MetalCodeGenerator::write(skstd::string_view s) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 49 | if (s.empty()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | if (fAtLineStart) { |
| 53 | for (int i = 0; i < fIndentation; i++) { |
| 54 | fOut->writeText(" "); |
| 55 | } |
| 56 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 57 | fOut->writeText(String(s).c_str()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 58 | fAtLineStart = false; |
| 59 | } |
| 60 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 61 | void MetalCodeGenerator::writeLine(skstd::string_view s) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 62 | this->write(s); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 63 | fOut->writeText(fLineEnding); |
| 64 | fAtLineStart = true; |
| 65 | } |
| 66 | |
| 67 | void MetalCodeGenerator::finishLine() { |
| 68 | if (!fAtLineStart) { |
| 69 | this->writeLine(); |
| 70 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void MetalCodeGenerator::writeExtension(const Extension& ext) { |
Ethan Nicholas | efb09e2 | 2020-09-30 10:17:00 -0400 | [diff] [blame] | 74 | this->writeLine("#extension " + ext.name() + " : enable"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 77 | String MetalCodeGenerator::typeName(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 78 | switch (type.typeKind()) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 79 | case Type::TypeKind::kArray: |
| 80 | SkASSERTF(type.columns() > 0, "invalid array size: %s", type.description().c_str()); |
| 81 | return String::printf("array<%s, %d>", |
| 82 | this->typeName(type.componentType()).c_str(), type.columns()); |
| 83 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 84 | case Type::TypeKind::kVector: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 85 | return this->typeName(type.componentType()) + to_string(type.columns()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 86 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 87 | case Type::TypeKind::kMatrix: |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 88 | return this->typeName(type.componentType()) + to_string(type.columns()) + "x" + |
| 89 | to_string(type.rows()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 90 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 91 | case Type::TypeKind::kSampler: |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 92 | return "texture2d<float>"; // FIXME - support other texture types |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 93 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 94 | default: |
John Stiles | 7b2b858 | 2021-08-09 14:20:54 -0400 | [diff] [blame] | 95 | // We currently only support full-precision types in MSL to avoid type coercion issues. |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 96 | if (type == *fContext.fTypes.fHalf) { |
John Stiles | 7b2b858 | 2021-08-09 14:20:54 -0400 | [diff] [blame] | 97 | return "float"; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 98 | } |
John Stiles | 7b2b858 | 2021-08-09 14:20:54 -0400 | [diff] [blame] | 99 | if (type == *fContext.fTypes.fShort) { |
| 100 | return "int"; |
| 101 | } |
| 102 | if (type == *fContext.fTypes.fUShort) { |
| 103 | return "uint"; |
| 104 | } |
| 105 | return String(type.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 109 | void MetalCodeGenerator::writeStructDefinition(const StructDefinition& s) { |
| 110 | const Type& type = s.type(); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 111 | this->writeLine("struct " + type.name() + " {"); |
| 112 | fIndentation++; |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 113 | this->writeFields(type.fields(), type.fOffset); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 114 | fIndentation--; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 115 | this->writeLine("};"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 116 | } |
| 117 | |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 118 | void MetalCodeGenerator::writeType(const Type& type) { |
| 119 | this->write(this->typeName(type)); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 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; |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 127 | case Expression::Kind::kConstructorArray: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 128 | case Expression::Kind::kConstructorStruct: |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 129 | this->writeAnyConstructor(expr.asAnyConstructor(), "{", "}", parentPrecedence); |
| 130 | break; |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 131 | case Expression::Kind::kConstructorArrayCast: |
| 132 | this->writeExpression(*expr.as<ConstructorArrayCast>().argument(), parentPrecedence); |
| 133 | break; |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 134 | case Expression::Kind::kConstructorCompound: |
| 135 | this->writeConstructorCompound(expr.as<ConstructorCompound>(), parentPrecedence); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 136 | break; |
| 137 | case Expression::Kind::kConstructorDiagonalMatrix: |
| 138 | case Expression::Kind::kConstructorSplat: |
| 139 | this->writeAnyConstructor(expr.asAnyConstructor(), "(", ")", parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 140 | break; |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 141 | case Expression::Kind::kConstructorMatrixResize: |
| 142 | this->writeConstructorMatrixResize(expr.as<ConstructorMatrixResize>(), |
| 143 | parentPrecedence); |
| 144 | break; |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 145 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 146 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 147 | this->writeCastConstructor(expr.asAnyConstructor(), "(", ")", parentPrecedence); |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 148 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 149 | case Expression::Kind::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 150 | this->writeFieldAccess(expr.as<FieldAccess>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 151 | break; |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 152 | case Expression::Kind::kLiteral: |
| 153 | this->writeLiteral(expr.as<Literal>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 154 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 155 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 156 | this->writeFunctionCall(expr.as<FunctionCall>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 157 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 158 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 159 | this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 160 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 161 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 162 | this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 163 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 164 | case Expression::Kind::kSetting: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 165 | this->writeSetting(expr.as<Setting>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 166 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 167 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 168 | this->writeSwizzle(expr.as<Swizzle>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 169 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 170 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 171 | this->writeVariableReference(expr.as<VariableReference>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 172 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 173 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 174 | this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 175 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 176 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 177 | this->writeIndexExpression(expr.as<IndexExpression>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 178 | break; |
| 179 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 180 | SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 181 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 185 | String MetalCodeGenerator::getOutParamHelper(const FunctionCall& call, |
| 186 | const ExpressionArray& arguments, |
| 187 | const SkTArray<VariableReference*>& outVars) { |
| 188 | AutoOutputStream outputToExtraFunctions(this, &fExtraFunctions, &fIndentation); |
| 189 | const FunctionDeclaration& function = call.function(); |
| 190 | |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 191 | String name = "_skOutParamHelper" + to_string(fSwizzleHelperCount++) + |
| 192 | "_" + function.mangledName(); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 193 | const char* separator = ""; |
| 194 | |
| 195 | // Emit a prototype for the function we'll be calling through to in our helper. |
| 196 | if (!function.isBuiltin()) { |
| 197 | this->writeFunctionDeclaration(function); |
| 198 | this->writeLine(";"); |
| 199 | } |
| 200 | |
| 201 | // Synthesize a helper function that takes the same inputs as `function`, except in places where |
| 202 | // `outVars` is non-null; in those places, we take the type of the VariableReference. |
| 203 | // |
| 204 | // float _skOutParamHelper0_originalFuncName(float _var0, float _var1, float& outParam) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 205 | this->writeType(call.type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 206 | this->write(" "); |
| 207 | this->write(name); |
| 208 | this->write("("); |
| 209 | this->writeFunctionRequirementParams(function, separator); |
| 210 | |
| 211 | SkASSERT(outVars.size() == arguments.size()); |
| 212 | SkASSERT(outVars.size() == function.parameters().size()); |
| 213 | |
John Stiles | 73e2c89 | 2021-02-13 13:58:01 -0500 | [diff] [blame] | 214 | // We need to detect cases where the caller passes the same variable as an out-param more than |
| 215 | // once, and avoid reusing the variable name. (In those cases we can actually just ignore the |
| 216 | // redundant input parameter entirely, and not give it any name.) |
| 217 | std::unordered_set<const Variable*> writtenVars; |
| 218 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 219 | for (int index = 0; index < arguments.count(); ++index) { |
| 220 | this->write(separator); |
| 221 | separator = ", "; |
| 222 | |
| 223 | const Variable* param = function.parameters()[index]; |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 224 | this->writeModifiers(param->modifiers()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 225 | |
| 226 | const Type* type = outVars[index] ? &outVars[index]->type() : &arguments[index]->type(); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 227 | this->writeType(*type); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 228 | |
| 229 | if (param->modifiers().fFlags & Modifiers::kOut_Flag) { |
| 230 | this->write("&"); |
| 231 | } |
| 232 | if (outVars[index]) { |
John Stiles | 73e2c89 | 2021-02-13 13:58:01 -0500 | [diff] [blame] | 233 | auto [iter, didInsert] = writtenVars.insert(outVars[index]->variable()); |
| 234 | if (didInsert) { |
| 235 | this->write(" "); |
| 236 | fIgnoreVariableReferenceModifiers = true; |
| 237 | this->writeVariableReference(*outVars[index]); |
| 238 | fIgnoreVariableReferenceModifiers = false; |
| 239 | } |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 240 | } else { |
| 241 | this->write(" _var"); |
| 242 | this->write(to_string(index)); |
| 243 | } |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 244 | } |
| 245 | this->writeLine(") {"); |
| 246 | |
| 247 | ++fIndentation; |
| 248 | for (int index = 0; index < outVars.count(); ++index) { |
| 249 | if (!outVars[index]) { |
| 250 | continue; |
| 251 | } |
| 252 | // float3 _var2[ = outParam.zyx]; |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 253 | this->writeType(arguments[index]->type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 254 | this->write(" _var"); |
| 255 | this->write(to_string(index)); |
| 256 | |
| 257 | const Variable* param = function.parameters()[index]; |
| 258 | if (param->modifiers().fFlags & Modifiers::kIn_Flag) { |
| 259 | this->write(" = "); |
| 260 | fIgnoreVariableReferenceModifiers = true; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 261 | this->writeExpression(*arguments[index], Precedence::kAssignment); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 262 | fIgnoreVariableReferenceModifiers = false; |
| 263 | } |
| 264 | |
| 265 | this->writeLine(";"); |
| 266 | } |
| 267 | |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 268 | // [int _skResult = ] myFunction(inputs, outputs, _globals, _var0, _var1, _var2, _var3); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 269 | bool hasResult = (call.type().name() != "void"); |
| 270 | if (hasResult) { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 271 | this->writeType(call.type()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 272 | this->write(" _skResult = "); |
| 273 | } |
| 274 | |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 275 | this->writeName(function.mangledName()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 276 | this->write("("); |
| 277 | separator = ""; |
| 278 | this->writeFunctionRequirementArgs(function, separator); |
| 279 | |
| 280 | for (int index = 0; index < arguments.count(); ++index) { |
| 281 | this->write(separator); |
| 282 | separator = ", "; |
| 283 | |
| 284 | this->write("_var"); |
| 285 | this->write(to_string(index)); |
| 286 | } |
| 287 | this->writeLine(");"); |
| 288 | |
| 289 | for (int index = 0; index < outVars.count(); ++index) { |
| 290 | if (!outVars[index]) { |
| 291 | continue; |
| 292 | } |
| 293 | // outParam.zyx = _var2; |
| 294 | fIgnoreVariableReferenceModifiers = true; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 295 | this->writeExpression(*arguments[index], Precedence::kAssignment); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 296 | fIgnoreVariableReferenceModifiers = false; |
| 297 | this->write(" = _var"); |
| 298 | this->write(to_string(index)); |
| 299 | this->writeLine(";"); |
| 300 | } |
| 301 | |
| 302 | if (hasResult) { |
| 303 | this->writeLine("return _skResult;"); |
| 304 | } |
| 305 | |
| 306 | --fIndentation; |
| 307 | this->writeLine("}"); |
| 308 | |
| 309 | return name; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 310 | } |
| 311 | |
John Stiles | f64e407 | 2020-12-10 10:34:27 -0500 | [diff] [blame] | 312 | String MetalCodeGenerator::getBitcastIntrinsic(const Type& outType) { |
| 313 | return "as_type<" + outType.displayName() + ">"; |
| 314 | } |
| 315 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 316 | void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 317 | const FunctionDeclaration& function = c.function(); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 318 | |
| 319 | // Many intrinsics need to be rewritten in Metal. |
| 320 | if (function.isIntrinsic()) { |
| 321 | if (this->writeIntrinsicCall(c, function.intrinsicKind())) { |
John Stiles | 89ac7c2 | 2020-12-30 17:47:31 -0500 | [diff] [blame] | 322 | return; |
| 323 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 324 | } |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 325 | |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 326 | // Determine whether or not we need to emulate GLSL's out-param semantics for Metal using a |
| 327 | // helper function. (Specifically, out-parameters in GLSL are only written back to the original |
| 328 | // variable at the end of the function call; also, swizzles are supported, whereas Metal doesn't |
| 329 | // allow a swizzle to be passed to a `floatN&`.) |
| 330 | const ExpressionArray& arguments = c.arguments(); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 331 | const std::vector<const Variable*>& parameters = function.parameters(); |
| 332 | SkASSERT(arguments.size() == parameters.size()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 333 | |
| 334 | bool foundOutParam = false; |
| 335 | SkSTArray<16, VariableReference*> outVars; |
John Stiles | f64e407 | 2020-12-10 10:34:27 -0500 | [diff] [blame] | 336 | outVars.push_back_n(arguments.count(), (VariableReference*)nullptr); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 337 | |
| 338 | for (int index = 0; index < arguments.count(); ++index) { |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 339 | // If this is an out parameter... |
| 340 | if (parameters[index]->modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 341 | // Find the expression's inner variable being written to. |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 342 | Analysis::AssignmentInfo info; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 343 | // Assignability was verified at IRGeneration time, so this should always succeed. |
| 344 | SkAssertResult(Analysis::IsAssignable(*arguments[index], &info)); |
| 345 | outVars[index] = info.fAssignedVar; |
| 346 | foundOutParam = true; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 350 | if (foundOutParam) { |
| 351 | // Out parameters need to be written back to at the end of the function. To do this, we |
| 352 | // synthesize a helper function which evaluates the out-param expression into a temporary |
| 353 | // variable, calls the original function, then writes the temp var back into the out param |
| 354 | // using the original out-param expression. (This lets us support things like swizzles and |
| 355 | // array indices.) |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 356 | this->write(getOutParamHelper(c, arguments, outVars)); |
| 357 | } else { |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 358 | this->write(function.mangledName()); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 361 | this->write("("); |
| 362 | const char* separator = ""; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 363 | this->writeFunctionRequirementArgs(function, separator); |
| 364 | for (int i = 0; i < arguments.count(); ++i) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 365 | this->write(separator); |
| 366 | separator = ", "; |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 367 | |
| 368 | if (outVars[i]) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 369 | this->writeExpression(*outVars[i], Precedence::kSequence); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 370 | } else { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 371 | this->writeExpression(*arguments[i], Precedence::kSequence); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 372 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 373 | } |
| 374 | this->write(")"); |
| 375 | } |
| 376 | |
Brian Osman | 964f0a0 | 2020-12-29 10:15:22 -0500 | [diff] [blame] | 377 | static constexpr char kInverse2x2[] = R"( |
| 378 | float2x2 float2x2_inverse(float2x2 m) { |
| 379 | return float2x2(m[1][1], -m[0][1], -m[1][0], m[0][0]) * (1/determinant(m)); |
| 380 | } |
| 381 | )"; |
| 382 | |
| 383 | static constexpr char kInverse3x3[] = R"( |
| 384 | float3x3 float3x3_inverse(float3x3 m) { |
| 385 | float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; |
| 386 | float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; |
| 387 | float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; |
| 388 | float b01 = a22*a11 - a12*a21; |
| 389 | float b11 = -a22*a10 + a12*a20; |
| 390 | float b21 = a21*a10 - a11*a20; |
| 391 | float det = a00*b01 + a01*b11 + a02*b21; |
| 392 | return float3x3(b01, (-a22*a01 + a02*a21), ( a12*a01 - a02*a11), |
| 393 | b11, ( a22*a00 - a02*a20), (-a12*a00 + a02*a10), |
| 394 | b21, (-a21*a00 + a01*a20), ( a11*a00 - a01*a10)) * (1/det); |
| 395 | } |
| 396 | )"; |
| 397 | |
| 398 | static constexpr char kInverse4x4[] = R"( |
| 399 | float4x4 float4x4_inverse(float4x4 m) { |
| 400 | float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3]; |
| 401 | float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3]; |
| 402 | float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3]; |
| 403 | float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3]; |
| 404 | float b00 = a00*a11 - a01*a10; |
| 405 | float b01 = a00*a12 - a02*a10; |
| 406 | float b02 = a00*a13 - a03*a10; |
| 407 | float b03 = a01*a12 - a02*a11; |
| 408 | float b04 = a01*a13 - a03*a11; |
| 409 | float b05 = a02*a13 - a03*a12; |
| 410 | float b06 = a20*a31 - a21*a30; |
| 411 | float b07 = a20*a32 - a22*a30; |
| 412 | float b08 = a20*a33 - a23*a30; |
| 413 | float b09 = a21*a32 - a22*a31; |
| 414 | float b10 = a21*a33 - a23*a31; |
| 415 | float b11 = a22*a33 - a23*a32; |
| 416 | float det = b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06; |
| 417 | return float4x4(a11*b11 - a12*b10 + a13*b09, |
| 418 | a02*b10 - a01*b11 - a03*b09, |
| 419 | a31*b05 - a32*b04 + a33*b03, |
| 420 | a22*b04 - a21*b05 - a23*b03, |
| 421 | a12*b08 - a10*b11 - a13*b07, |
| 422 | a00*b11 - a02*b08 + a03*b07, |
| 423 | a32*b02 - a30*b05 - a33*b01, |
| 424 | a20*b05 - a22*b02 + a23*b01, |
| 425 | a10*b10 - a11*b08 + a13*b06, |
| 426 | a01*b08 - a00*b10 - a03*b06, |
| 427 | a30*b04 - a31*b02 + a33*b00, |
| 428 | a21*b02 - a20*b04 - a23*b00, |
| 429 | a11*b07 - a10*b09 - a12*b06, |
| 430 | a00*b09 - a01*b07 + a02*b06, |
| 431 | a31*b01 - a30*b03 - a32*b00, |
| 432 | a20*b03 - a21*b01 + a22*b00) * (1/det); |
| 433 | } |
| 434 | )"; |
| 435 | |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 436 | String MetalCodeGenerator::getInversePolyfill(const ExpressionArray& arguments) { |
| 437 | // Only use polyfills for a function taking a single-argument square matrix. |
| 438 | if (arguments.size() == 1) { |
| 439 | const Type& type = arguments.front()->type(); |
| 440 | if (type.isMatrix() && type.rows() == type.columns()) { |
| 441 | // Inject the correct polyfill based on the matrix size. |
| 442 | String name = this->typeName(type) + "_inverse"; |
| 443 | auto [iter, didInsert] = fWrittenIntrinsics.insert(name); |
| 444 | if (didInsert) { |
| 445 | switch (type.rows()) { |
| 446 | case 2: |
| 447 | fExtraFunctions.writeText(kInverse2x2); |
| 448 | break; |
| 449 | case 3: |
| 450 | fExtraFunctions.writeText(kInverse3x3); |
| 451 | break; |
| 452 | case 4: |
| 453 | fExtraFunctions.writeText(kInverse4x4); |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | return name; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 458 | } |
| 459 | } |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 460 | // This isn't the built-in `inverse`. We don't want to polyfill it at all. |
| 461 | return "inverse"; |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 462 | } |
| 463 | |
John Stiles | feb1e12 | 2021-09-09 14:27:28 -0400 | [diff] [blame] | 464 | void MetalCodeGenerator::writeMatrixCompMult() { |
| 465 | static constexpr char kMatrixCompMult[] = R"( |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 466 | template <int C, int R> |
John Stiles | b701fa0 | 2021-09-14 13:09:36 -0400 | [diff] [blame] | 467 | matrix<float, C, R> matrixCompMult(matrix<float, C, R> a, const matrix<float, C, R> b) { |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 468 | for (int c = 0; c < C; ++c) { |
John Stiles | b701fa0 | 2021-09-14 13:09:36 -0400 | [diff] [blame] | 469 | a[c] *= b[c]; |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 470 | } |
John Stiles | b701fa0 | 2021-09-14 13:09:36 -0400 | [diff] [blame] | 471 | return a; |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 472 | } |
| 473 | )"; |
| 474 | |
Brian Osman | 93aed9a | 2020-12-28 15:18:46 -0500 | [diff] [blame] | 475 | String name = "matrixCompMult"; |
| 476 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 477 | fWrittenIntrinsics.insert(name); |
| 478 | fExtraFunctions.writeText(kMatrixCompMult); |
| 479 | } |
| 480 | } |
| 481 | |
John Stiles | feb1e12 | 2021-09-09 14:27:28 -0400 | [diff] [blame] | 482 | void MetalCodeGenerator::writeOuterProduct() { |
| 483 | static constexpr char kOuterProduct[] = R"( |
| 484 | template <int C, int R> |
| 485 | matrix<float, C, R> outerProduct(const vec<float, R> a, const vec<float, C> b) { |
| 486 | matrix<float, C, R> result; |
| 487 | for (int c = 0; c < C; ++c) { |
| 488 | result[c] = a * b[c]; |
| 489 | } |
| 490 | return result; |
| 491 | } |
| 492 | )"; |
| 493 | |
| 494 | String name = "outerProduct"; |
| 495 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 496 | fWrittenIntrinsics.insert(name); |
| 497 | fExtraFunctions.writeText(kOuterProduct); |
| 498 | } |
| 499 | } |
| 500 | |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 501 | String MetalCodeGenerator::getTempVariable(const Type& type) { |
| 502 | String tempVar = "_skTemp" + to_string(fVarCount++); |
| 503 | this->fFunctionHeader += " " + this->typeName(type) + " " + tempVar + ";\n"; |
| 504 | return tempVar; |
| 505 | } |
| 506 | |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 507 | void MetalCodeGenerator::writeSimpleIntrinsic(const FunctionCall& c) { |
| 508 | // Write out an intrinsic function call exactly as-is. No muss no fuss. |
| 509 | this->write(c.function().name()); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 510 | this->writeArgumentList(c.arguments()); |
| 511 | } |
| 512 | |
| 513 | void MetalCodeGenerator::writeArgumentList(const ExpressionArray& arguments) { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 514 | this->write("("); |
| 515 | const char* separator = ""; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 516 | for (const std::unique_ptr<Expression>& arg : arguments) { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 517 | this->write(separator); |
| 518 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 519 | this->writeExpression(*arg, Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 520 | } |
| 521 | this->write(")"); |
| 522 | } |
| 523 | |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 524 | bool MetalCodeGenerator::writeIntrinsicCall(const FunctionCall& c, IntrinsicKind kind) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 525 | const ExpressionArray& arguments = c.arguments(); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 526 | switch (kind) { |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 527 | case k_sample_IntrinsicKind: { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 528 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 529 | this->write(".sample("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 530 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 531 | this->write(SAMPLER_SUFFIX); |
| 532 | this->write(", "); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 533 | const Type& arg1Type = arguments[1]->type(); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 534 | if (arg1Type.columns() == 3) { |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 535 | // have to store the vector in a temp variable to avoid double evaluating it |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 536 | String tmpVar = this->getTempVariable(arg1Type); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 537 | this->write("(" + tmpVar + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 538 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 539 | this->write(", " + tmpVar + ".xy / " + tmpVar + ".z))"); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 540 | } else { |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 541 | SkASSERT(arg1Type.columns() == 2); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 542 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 543 | this->write(")"); |
| 544 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 545 | return true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 546 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 547 | case k_mod_IntrinsicKind: { |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 548 | // fmod(x, y) in metal calculates x - y * trunc(x / y) instead of x - y * floor(x / y) |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 549 | String tmpX = this->getTempVariable(arguments[0]->type()); |
John Stiles | 61b2e81 | 2020-12-30 18:27:31 -0500 | [diff] [blame] | 550 | String tmpY = this->getTempVariable(arguments[1]->type()); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 551 | this->write("(" + tmpX + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 552 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 553 | this->write(", " + tmpY + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 554 | this->writeExpression(*arguments[1], Precedence::kSequence); |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 555 | this->write(", " + tmpX + " - " + tmpY + " * floor(" + tmpX + " / " + tmpY + "))"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 556 | return true; |
Ethan Nicholas | 45fa810 | 2020-01-13 10:58:49 -0500 | [diff] [blame] | 557 | } |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 558 | // GLSL declares scalar versions of most geometric intrinsics, but these don't exist in MSL |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 559 | case k_distance_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 560 | if (arguments[0]->type().columns() == 1) { |
| 561 | this->write("abs("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 562 | this->writeExpression(*arguments[0], Precedence::kAdditive); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 563 | this->write(" - "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 564 | this->writeExpression(*arguments[1], Precedence::kAdditive); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 565 | this->write(")"); |
| 566 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 567 | this->writeSimpleIntrinsic(c); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 568 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 569 | return true; |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 570 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 571 | case k_dot_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 572 | if (arguments[0]->type().columns() == 1) { |
| 573 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 574 | this->writeExpression(*arguments[0], Precedence::kMultiplicative); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 575 | this->write(" * "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 576 | this->writeExpression(*arguments[1], Precedence::kMultiplicative); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 577 | this->write(")"); |
| 578 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 579 | this->writeSimpleIntrinsic(c); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 580 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 581 | return true; |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 582 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 583 | case k_faceforward_IntrinsicKind: { |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 584 | if (arguments[0]->type().columns() == 1) { |
| 585 | // ((((Nref) * (I) < 0) ? 1 : -1) * (N)) |
| 586 | this->write("(((("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 587 | this->writeExpression(*arguments[2], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 588 | this->write(") * ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 589 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 590 | this->write(") < 0) ? 1 : -1) * ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 591 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 592 | this->write("))"); |
| 593 | } else { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 594 | this->writeSimpleIntrinsic(c); |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 595 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 596 | return true; |
John Stiles | ad0571f | 2020-12-10 18:03:10 -0500 | [diff] [blame] | 597 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 598 | case k_length_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 599 | this->write(arguments[0]->type().columns() == 1 ? "abs(" : "length("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 600 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 601 | this->write(")"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 602 | return true; |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 603 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 604 | case k_normalize_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 605 | this->write(arguments[0]->type().columns() == 1 ? "sign(" : "normalize("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 606 | this->writeExpression(*arguments[0], Precedence::kSequence); |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 607 | this->write(")"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 608 | return true; |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 609 | } |
John Stiles | b6981fb | 2021-09-08 15:11:27 -0400 | [diff] [blame] | 610 | case k_packUnorm2x16_IntrinsicKind: { |
| 611 | this->write("pack_float_to_unorm2x16("); |
| 612 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 613 | this->write(")"); |
| 614 | return true; |
| 615 | } |
| 616 | case k_unpackUnorm2x16_IntrinsicKind: { |
| 617 | this->write("unpack_unorm2x16_to_float("); |
| 618 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 619 | this->write(")"); |
| 620 | return true; |
| 621 | } |
| 622 | case k_packSnorm2x16_IntrinsicKind: { |
| 623 | this->write("pack_float_to_snorm2x16("); |
| 624 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 625 | this->write(")"); |
| 626 | return true; |
| 627 | } |
| 628 | case k_unpackSnorm2x16_IntrinsicKind: { |
| 629 | this->write("unpack_snorm2x16_to_float("); |
| 630 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 631 | this->write(")"); |
| 632 | return true; |
| 633 | } |
| 634 | case k_packUnorm4x8_IntrinsicKind: { |
| 635 | this->write("pack_float_to_unorm4x8("); |
| 636 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 637 | this->write(")"); |
| 638 | return true; |
| 639 | } |
| 640 | case k_unpackUnorm4x8_IntrinsicKind: { |
| 641 | this->write("unpack_unorm4x8_to_float("); |
| 642 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 643 | this->write(")"); |
| 644 | return true; |
| 645 | } |
| 646 | case k_packSnorm4x8_IntrinsicKind: { |
| 647 | this->write("pack_float_to_snorm4x8("); |
| 648 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 649 | this->write(")"); |
| 650 | return true; |
| 651 | } |
| 652 | case k_unpackSnorm4x8_IntrinsicKind: { |
| 653 | this->write("unpack_snorm4x8_to_float("); |
| 654 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 655 | this->write(")"); |
| 656 | return true; |
| 657 | } |
| 658 | case k_packHalf2x16_IntrinsicKind: { |
| 659 | this->write("as_type<uint>(half2("); |
| 660 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 661 | this->write("))"); |
| 662 | return true; |
| 663 | } |
| 664 | case k_unpackHalf2x16_IntrinsicKind: { |
| 665 | this->write("float2(as_type<half2>("); |
| 666 | this->writeExpression(*arguments[0], Precedence::kSequence); |
| 667 | this->write("))"); |
| 668 | return true; |
| 669 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 670 | case k_floatBitsToInt_IntrinsicKind: |
| 671 | case k_floatBitsToUint_IntrinsicKind: |
| 672 | case k_intBitsToFloat_IntrinsicKind: |
| 673 | case k_uintBitsToFloat_IntrinsicKind: { |
John Stiles | 0063a9f | 2020-12-10 18:01:45 -0500 | [diff] [blame] | 674 | this->write(this->getBitcastIntrinsic(c.type())); |
| 675 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 676 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 0063a9f | 2020-12-10 18:01:45 -0500 | [diff] [blame] | 677 | this->write(")"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 678 | return true; |
John Stiles | 0063a9f | 2020-12-10 18:01:45 -0500 | [diff] [blame] | 679 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 680 | case k_degrees_IntrinsicKind: { |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 681 | this->write("(("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 682 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 683 | this->write(") * 57.2957795)"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 684 | return true; |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 685 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 686 | case k_radians_IntrinsicKind: { |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 687 | this->write("(("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 688 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 689 | this->write(") * 0.0174532925)"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 690 | return true; |
John Stiles | e2d34f8 | 2020-12-10 18:02:02 -0500 | [diff] [blame] | 691 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 692 | case k_dFdx_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 693 | this->write("dfdx"); |
| 694 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 695 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 696 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 697 | case k_dFdy_IntrinsicKind: { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 698 | this->write(fRTFlipName + ".y*dfdy"); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 699 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 700 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 701 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 702 | case k_inverse_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 703 | this->write(this->getInversePolyfill(arguments)); |
| 704 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 705 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 706 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 707 | case k_inversesqrt_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 708 | this->write("rsqrt"); |
| 709 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 710 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 711 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 712 | case k_atan_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 713 | this->write(c.arguments().size() == 2 ? "atan2" : "atan"); |
| 714 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 715 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 716 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 717 | case k_reflect_IntrinsicKind: { |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 718 | if (arguments[0]->type().columns() == 1) { |
| 719 | // We need to synthesize `I - 2 * N * I * N`. |
| 720 | String tmpI = this->getTempVariable(arguments[0]->type()); |
| 721 | String tmpN = this->getTempVariable(arguments[1]->type()); |
| 722 | |
| 723 | // (_skTempI = ... |
| 724 | this->write("(" + tmpI + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 725 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 726 | |
| 727 | // , _skTempN = ... |
| 728 | this->write(", " + tmpN + " = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 729 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 730 | |
| 731 | // , _skTempI - 2 * _skTempN * _skTempI * _skTempN) |
| 732 | this->write(", " + tmpI + " - 2 * " + tmpN + " * " + tmpI + " * " + tmpN + ")"); |
| 733 | } else { |
| 734 | this->writeSimpleIntrinsic(c); |
| 735 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 736 | return true; |
John Stiles | 791c27d | 2020-12-30 14:56:57 -0500 | [diff] [blame] | 737 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 738 | case k_refract_IntrinsicKind: { |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 739 | if (arguments[0]->type().columns() == 1) { |
| 740 | // Metal does implement refract for vectors; rather than reimplementing refract from |
| 741 | // scratch, we can replace the call with `refract(float2(I,0), float2(N,0), eta).x`. |
| 742 | this->write("(refract(float2("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 743 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 744 | this->write(", 0), float2("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 745 | this->writeExpression(*arguments[1], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 746 | this->write(", 0), "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 747 | this->writeExpression(*arguments[2], Precedence::kSequence); |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 748 | this->write(").x)"); |
| 749 | } else { |
| 750 | this->writeSimpleIntrinsic(c); |
| 751 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 752 | return true; |
John Stiles | 4b783d6 | 2020-12-30 14:58:07 -0500 | [diff] [blame] | 753 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 754 | case k_roundEven_IntrinsicKind: { |
John Stiles | 2345653 | 2020-12-30 18:12:48 -0500 | [diff] [blame] | 755 | this->write("rint"); |
| 756 | this->writeArgumentList(c.arguments()); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 757 | return true; |
John Stiles | 2345653 | 2020-12-30 18:12:48 -0500 | [diff] [blame] | 758 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 759 | case k_bitCount_IntrinsicKind: { |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame] | 760 | this->write("popcount("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 761 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame] | 762 | this->write(")"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 763 | return true; |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame] | 764 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 765 | case k_findLSB_IntrinsicKind: { |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 766 | // Create a temp variable to store the expression, to avoid double-evaluating it. |
| 767 | String skTemp = this->getTempVariable(arguments[0]->type()); |
| 768 | String exprType = this->typeName(arguments[0]->type()); |
| 769 | |
| 770 | // ctz returns numbits(type) on zero inputs; GLSL documents it as generating -1 instead. |
| 771 | // Use select to detect zero inputs and force a -1 result. |
| 772 | |
| 773 | // (_skTemp1 = (.....), select(ctz(_skTemp1), int4(-1), _skTemp1 == int4(0))) |
| 774 | this->write("("); |
| 775 | this->write(skTemp); |
| 776 | this->write(" = ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 777 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 778 | this->write("), select(ctz("); |
| 779 | this->write(skTemp); |
| 780 | this->write("), "); |
| 781 | this->write(exprType); |
| 782 | this->write("(-1), "); |
| 783 | this->write(skTemp); |
| 784 | this->write(" == "); |
| 785 | this->write(exprType); |
| 786 | this->write("(0)))"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 787 | return true; |
John Stiles | 86424eb | 2020-12-11 11:17:14 -0500 | [diff] [blame] | 788 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 789 | case k_findMSB_IntrinsicKind: { |
John Stiles | 9685e52 | 2020-12-14 11:52:14 -0500 | [diff] [blame] | 790 | // Create a temp variable to store the expression, to avoid double-evaluating it. |
| 791 | String skTemp1 = this->getTempVariable(arguments[0]->type()); |
| 792 | String exprType = this->typeName(arguments[0]->type()); |
| 793 | |
| 794 | // GLSL findMSB is actually quite different from Metal's clz: |
| 795 | // - For signed negative numbers, it returns the first zero bit, not the first one bit! |
| 796 | // - For an empty input (0/~0 depending on sign), findMSB gives -1; clz is numbits(type) |
| 797 | |
| 798 | // (_skTemp1 = (.....), |
| 799 | this->write("("); |
| 800 | this->write(skTemp1); |
| 801 | this->write(" = ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 802 | this->writeExpression(*arguments[0], Precedence::kSequence); |
John Stiles | 9685e52 | 2020-12-14 11:52:14 -0500 | [diff] [blame] | 803 | this->write("), "); |
| 804 | |
| 805 | // Signed input types might be negative; we need another helper variable to negate the |
| 806 | // input (since we can only find one bits, not zero bits). |
| 807 | String skTemp2; |
| 808 | if (arguments[0]->type().isSigned()) { |
| 809 | // ... _skTemp2 = (select(_skTemp1, ~_skTemp1, _skTemp1 < 0)), |
| 810 | skTemp2 = this->getTempVariable(arguments[0]->type()); |
| 811 | this->write(skTemp2); |
| 812 | this->write(" = (select("); |
| 813 | this->write(skTemp1); |
| 814 | this->write(", ~"); |
| 815 | this->write(skTemp1); |
| 816 | this->write(", "); |
| 817 | this->write(skTemp1); |
| 818 | this->write(" < 0)), "); |
| 819 | } else { |
| 820 | skTemp2 = skTemp1; |
| 821 | } |
| 822 | |
| 823 | // ... select(int4(clz(_skTemp2)), int4(-1), _skTemp2 == int4(0))) |
| 824 | this->write("select("); |
| 825 | this->write(this->typeName(c.type())); |
| 826 | this->write("(clz("); |
| 827 | this->write(skTemp2); |
| 828 | this->write(")), "); |
| 829 | this->write(this->typeName(c.type())); |
| 830 | this->write("(-1), "); |
| 831 | this->write(skTemp2); |
| 832 | this->write(" == "); |
| 833 | this->write(exprType); |
| 834 | this->write("(0)))"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 835 | return true; |
John Stiles | 9685e52 | 2020-12-14 11:52:14 -0500 | [diff] [blame] | 836 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 837 | case k_matrixCompMult_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 838 | this->writeMatrixCompMult(); |
| 839 | this->writeSimpleIntrinsic(c); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 840 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 841 | } |
John Stiles | feb1e12 | 2021-09-09 14:27:28 -0400 | [diff] [blame] | 842 | case k_outerProduct_IntrinsicKind: { |
| 843 | this->writeOuterProduct(); |
| 844 | this->writeSimpleIntrinsic(c); |
| 845 | return true; |
| 846 | } |
John Stiles | cc2d9cc | 2021-07-09 17:38:41 -0400 | [diff] [blame] | 847 | case k_mix_IntrinsicKind: { |
| 848 | SkASSERT(c.arguments().size() == 3); |
| 849 | if (arguments[2]->type().componentType().isBoolean()) { |
| 850 | // The Boolean forms of GLSL mix() use the select() intrinsic in Metal. |
| 851 | this->write("select"); |
| 852 | this->writeArgumentList(c.arguments()); |
| 853 | return true; |
| 854 | } |
| 855 | // The basic form of mix() is supported by Metal as-is. |
| 856 | this->writeSimpleIntrinsic(c); |
| 857 | return true; |
| 858 | } |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 859 | case k_equal_IntrinsicKind: |
| 860 | case k_greaterThan_IntrinsicKind: |
| 861 | case k_greaterThanEqual_IntrinsicKind: |
| 862 | case k_lessThan_IntrinsicKind: |
| 863 | case k_lessThanEqual_IntrinsicKind: |
| 864 | case k_notEqual_IntrinsicKind: { |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 865 | this->write("("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 866 | this->writeExpression(*c.arguments()[0], Precedence::kRelational); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 867 | switch (kind) { |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 868 | case k_equal_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 869 | this->write(" == "); |
| 870 | break; |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 871 | case k_notEqual_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 872 | this->write(" != "); |
| 873 | break; |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 874 | case k_lessThan_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 875 | this->write(" < "); |
| 876 | break; |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 877 | case k_lessThanEqual_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 878 | this->write(" <= "); |
| 879 | break; |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 880 | case k_greaterThan_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 881 | this->write(" > "); |
| 882 | break; |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 883 | case k_greaterThanEqual_IntrinsicKind: |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 884 | this->write(" >= "); |
| 885 | break; |
| 886 | default: |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 887 | SK_ABORT("unsupported comparison intrinsic kind"); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 888 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 889 | this->writeExpression(*c.arguments()[1], Precedence::kRelational); |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 890 | this->write(")"); |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 891 | return true; |
John Stiles | d7199b2 | 2020-12-30 16:22:58 -0500 | [diff] [blame] | 892 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 893 | default: |
John Stiles | 496b7d1 | 2021-05-06 10:26:45 -0400 | [diff] [blame] | 894 | return false; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 898 | // Assembles a matrix of type floatRxC by resizing another matrix named `x0`. |
| 899 | // Cells that don't exist in the source matrix will be populated with identity-matrix values. |
| 900 | void MetalCodeGenerator::assembleMatrixFromMatrix(const Type& sourceMatrix, int rows, int columns) { |
| 901 | SkASSERT(rows <= 4); |
| 902 | SkASSERT(columns <= 4); |
| 903 | |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 904 | std::string matrixType = this->typeName(sourceMatrix.componentType()); |
| 905 | |
| 906 | const char* separator = ""; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 907 | for (int c = 0; c < columns; ++c) { |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 908 | fExtraFunctions.printf("%s%s%d(", separator, matrixType.c_str(), rows); |
| 909 | separator = "), "; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 910 | |
| 911 | // Determine how many values to take from the source matrix for this row. |
| 912 | int swizzleLength = 0; |
| 913 | if (c < sourceMatrix.columns()) { |
| 914 | swizzleLength = std::min<>(rows, sourceMatrix.rows()); |
| 915 | } |
| 916 | |
| 917 | // Emit all the values from the source matrix row. |
| 918 | bool firstItem; |
| 919 | switch (swizzleLength) { |
| 920 | case 0: firstItem = true; break; |
| 921 | case 1: firstItem = false; fExtraFunctions.printf("x0[%d].x", c); break; |
| 922 | case 2: firstItem = false; fExtraFunctions.printf("x0[%d].xy", c); break; |
| 923 | case 3: firstItem = false; fExtraFunctions.printf("x0[%d].xyz", c); break; |
| 924 | case 4: firstItem = false; fExtraFunctions.printf("x0[%d].xyzw", c); break; |
| 925 | default: SkUNREACHABLE; |
| 926 | } |
| 927 | |
| 928 | // Emit the placeholder identity-matrix cells. |
| 929 | for (int r = swizzleLength; r < rows; ++r) { |
| 930 | fExtraFunctions.printf("%s%s", firstItem ? "" : ", ", (r == c) ? "1.0" : "0.0"); |
| 931 | firstItem = false; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | fExtraFunctions.writeText(")"); |
| 936 | } |
| 937 | |
John Stiles | 1f7300b | 2021-07-08 15:40:38 -0400 | [diff] [blame] | 938 | // Assembles a matrix of type floatCxR by concatenating an arbitrary mix of values, named `x0`, |
| 939 | // `x1`, etc. An error is written if the expression list don't contain exactly C*R scalars. |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 940 | void MetalCodeGenerator::assembleMatrixFromExpressions(const AnyConstructor& ctor, |
John Stiles | 1f7300b | 2021-07-08 15:40:38 -0400 | [diff] [blame] | 941 | int columns, int rows) { |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 942 | SkASSERT(rows <= 4); |
| 943 | SkASSERT(columns <= 4); |
| 944 | |
| 945 | std::string matrixType = this->typeName(ctor.type().componentType()); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 946 | size_t argIndex = 0; |
| 947 | int argPosition = 0; |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 948 | auto args = ctor.argumentSpan(); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 949 | |
John Stiles | 4f1593e | 2021-09-15 12:35:25 -0400 | [diff] [blame] | 950 | static constexpr char kSwizzle[] = "xyzw"; |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 951 | const char* separator = ""; |
John Stiles | 143e850 | 2021-09-14 12:04:11 -0400 | [diff] [blame] | 952 | for (int c = 0; c < columns; ++c) { |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 953 | fExtraFunctions.printf("%s%s%d(", separator, matrixType.c_str(), rows); |
| 954 | separator = "), "; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 955 | |
John Stiles | 1f7300b | 2021-07-08 15:40:38 -0400 | [diff] [blame] | 956 | const char* columnSeparator = ""; |
John Stiles | 4f1593e | 2021-09-15 12:35:25 -0400 | [diff] [blame] | 957 | for (int r = 0; r < rows;) { |
John Stiles | 1f7300b | 2021-07-08 15:40:38 -0400 | [diff] [blame] | 958 | fExtraFunctions.writeText(columnSeparator); |
| 959 | columnSeparator = ", "; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 960 | |
| 961 | if (argIndex < args.size()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 962 | const Type& argType = args[argIndex]->type(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 963 | switch (argType.typeKind()) { |
| 964 | case Type::TypeKind::kScalar: { |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 965 | fExtraFunctions.printf("x%zu", argIndex); |
John Stiles | 4f1593e | 2021-09-15 12:35:25 -0400 | [diff] [blame] | 966 | ++r; |
| 967 | ++argPosition; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 968 | break; |
| 969 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 970 | case Type::TypeKind::kVector: { |
John Stiles | 4f1593e | 2021-09-15 12:35:25 -0400 | [diff] [blame] | 971 | fExtraFunctions.printf("x%zu.", argIndex); |
| 972 | do { |
| 973 | fExtraFunctions.write8(kSwizzle[argPosition]); |
| 974 | ++r; |
| 975 | ++argPosition; |
| 976 | } while (r < rows && argPosition < argType.columns()); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 977 | break; |
| 978 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 979 | case Type::TypeKind::kMatrix: { |
John Stiles | 4f1593e | 2021-09-15 12:35:25 -0400 | [diff] [blame] | 980 | fExtraFunctions.printf("x%zu[%d].", argIndex, argPosition / argType.rows()); |
| 981 | do { |
| 982 | fExtraFunctions.write8(kSwizzle[argPosition]); |
| 983 | ++r; |
| 984 | ++argPosition; |
| 985 | } while (r < rows && (argPosition % argType.rows()) != 0); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 986 | break; |
| 987 | } |
| 988 | default: { |
| 989 | SkDEBUGFAIL("incorrect type of argument for matrix constructor"); |
| 990 | fExtraFunctions.writeText("<error>"); |
| 991 | break; |
| 992 | } |
| 993 | } |
| 994 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 995 | if (argPosition >= argType.columns() * argType.rows()) { |
| 996 | ++argIndex; |
| 997 | argPosition = 0; |
| 998 | } |
| 999 | } else { |
| 1000 | SkDEBUGFAIL("not enough arguments for matrix constructor"); |
| 1001 | fExtraFunctions.writeText("<error>"); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (argPosition != 0 || argIndex != args.size()) { |
| 1007 | SkDEBUGFAIL("incorrect number of arguments for matrix constructor"); |
| 1008 | fExtraFunctions.writeText(", <error>"); |
| 1009 | } |
| 1010 | |
| 1011 | fExtraFunctions.writeText(")"); |
| 1012 | } |
| 1013 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1014 | // Generates a constructor for 'matrix' which reorganizes the input arguments into the proper shape. |
| 1015 | // Keeps track of previously generated constructors so that we won't generate more than one |
| 1016 | // constructor for any given permutation of input argument types. Returns the name of the |
| 1017 | // generated constructor method. |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1018 | String MetalCodeGenerator::getMatrixConstructHelper(const AnyConstructor& c) { |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1019 | const Type& type = c.type(); |
| 1020 | int columns = type.columns(); |
| 1021 | int rows = type.rows(); |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1022 | auto args = c.argumentSpan(); |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1023 | String typeName = this->typeName(type); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1024 | |
| 1025 | // Create the helper-method name and use it as our lookup key. |
| 1026 | String name; |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1027 | name.appendf("%s_from", typeName.c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1028 | for (const std::unique_ptr<Expression>& expr : args) { |
John Stiles | 3612913 | 2020-12-29 12:28:04 -0500 | [diff] [blame] | 1029 | name.appendf("_%s", this->typeName(expr->type()).c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | // If a helper-method has already been synthesized, we don't need to synthesize it again. |
| 1033 | auto [iter, newlyCreated] = fHelpers.insert(name); |
| 1034 | if (!newlyCreated) { |
| 1035 | return name; |
| 1036 | } |
| 1037 | |
| 1038 | // Unlike GLSL, Metal requires that matrices are initialized with exactly R vectors of C |
| 1039 | // components apiece. (In Metal 2.0, you can also supply R*C scalars, but you still cannot |
| 1040 | // supply a mixture of scalars and vectors.) |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1041 | fExtraFunctions.printf("%s %s(", typeName.c_str(), name.c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1042 | |
| 1043 | size_t argIndex = 0; |
| 1044 | const char* argSeparator = ""; |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 1045 | for (const std::unique_ptr<Expression>& expr : args) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1046 | fExtraFunctions.printf("%s%s x%zu", argSeparator, |
John Stiles | 3612913 | 2020-12-29 12:28:04 -0500 | [diff] [blame] | 1047 | this->typeName(expr->type()).c_str(), argIndex++); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1048 | argSeparator = ", "; |
| 1049 | } |
| 1050 | |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1051 | fExtraFunctions.printf(") {\n return %s(", typeName.c_str()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1052 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1053 | if (args.size() == 1 && args.front()->type().isMatrix()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1054 | this->assembleMatrixFromMatrix(args.front()->type(), rows, columns); |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 1055 | } else { |
John Stiles | 1f7300b | 2021-07-08 15:40:38 -0400 | [diff] [blame] | 1056 | this->assembleMatrixFromExpressions(c, columns, rows); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
John Stiles | fcf8cb2 | 2020-08-06 14:29:22 -0400 | [diff] [blame] | 1059 | fExtraFunctions.writeText(");\n}\n"); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1060 | return name; |
| 1061 | } |
| 1062 | |
| 1063 | bool MetalCodeGenerator::canCoerce(const Type& t1, const Type& t2) { |
| 1064 | if (t1.columns() != t2.columns() || t1.rows() != t2.rows()) { |
| 1065 | return false; |
| 1066 | } |
| 1067 | if (t1.columns() > 1) { |
| 1068 | return this->canCoerce(t1.componentType(), t2.componentType()); |
| 1069 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 1070 | return t1.isFloat() && t2.isFloat(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1071 | } |
| 1072 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1073 | bool MetalCodeGenerator::matrixConstructHelperIsNeeded(const ConstructorCompound& c) { |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1074 | SkASSERT(c.type().isMatrix()); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1075 | |
| 1076 | // GLSL is fairly free-form about inputs to its matrix constructors, but Metal is not; it |
| 1077 | // expects exactly R vectors of C components apiece. (Metal 2.0 also allows a list of R*C |
| 1078 | // scalars.) Some cases are simple to translate and so we handle those inline--e.g. a list of |
| 1079 | // scalars can be constructed trivially. In more complex cases, we generate a helper function |
| 1080 | // that converts our inputs into a properly-shaped matrix. |
| 1081 | // A matrix construct helper method is always used if any input argument is a matrix. |
| 1082 | // Helper methods are also necessary when any argument would span multiple rows. For instance: |
| 1083 | // |
| 1084 | // float2 x = (1, 2); |
| 1085 | // float3x2(x, 3, 4, 5, 6) = | 1 3 5 | = no helper needed; conversion can be done inline |
| 1086 | // | 2 4 6 | |
| 1087 | // |
| 1088 | // float2 x = (2, 3); |
| 1089 | // float3x2(1, x, 4, 5, 6) = | 1 3 5 | = x spans multiple rows; a helper method will be used |
| 1090 | // | 2 4 6 | |
| 1091 | // |
| 1092 | // float4 x = (1, 2, 3, 4); |
| 1093 | // float2x2(x) = | 1 3 | = x spans multiple rows; a helper method will be used |
| 1094 | // | 2 4 | |
| 1095 | // |
| 1096 | |
| 1097 | int position = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1098 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1099 | // If an input argument is a matrix, we need a helper function. |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1100 | if (expr->type().isMatrix()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1101 | return true; |
| 1102 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1103 | position += expr->type().columns(); |
| 1104 | if (position > c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1105 | // An input argument would span multiple rows; a helper function is required. |
| 1106 | return true; |
| 1107 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1108 | if (position == c.type().rows()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1109 | // We've advanced to the end of a row. Wrap to the start of the next row. |
| 1110 | position = 0; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | return false; |
| 1115 | } |
| 1116 | |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1117 | void MetalCodeGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& c, |
| 1118 | Precedence parentPrecedence) { |
| 1119 | // Matrix-resize via casting doesn't natively exist in Metal at all, so we always need to use a |
| 1120 | // matrix-construct helper here. |
| 1121 | this->write(this->getMatrixConstructHelper(c)); |
| 1122 | this->write("("); |
| 1123 | this->writeExpression(*c.argument(), Precedence::kSequence); |
| 1124 | this->write(")"); |
| 1125 | } |
| 1126 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1127 | void MetalCodeGenerator::writeConstructorCompound(const ConstructorCompound& c, |
| 1128 | Precedence parentPrecedence) { |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1129 | if (c.type().isVector()) { |
| 1130 | this->writeConstructorCompoundVector(c, parentPrecedence); |
| 1131 | } else if (c.type().isMatrix()) { |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1132 | this->writeConstructorCompoundMatrix(c, parentPrecedence); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1133 | } else { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1134 | fContext.fErrors->error(c.fOffset, "unsupported compound constructor"); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1135 | } |
| 1136 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1137 | |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1138 | String MetalCodeGenerator::getVectorFromMat2x2ConstructorHelper(const Type& matrixType) { |
| 1139 | SkASSERT(matrixType.isMatrix()); |
| 1140 | SkASSERT(matrixType.rows() == 2); |
| 1141 | SkASSERT(matrixType.columns() == 2); |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1142 | |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1143 | String baseType = this->typeName(matrixType.componentType()); |
| 1144 | String name = String::printf("%s4_from_%s2x2", baseType.c_str(), baseType.c_str()); |
| 1145 | if (fHelpers.find(name) == fHelpers.end()) { |
| 1146 | fHelpers.insert(name); |
| 1147 | |
| 1148 | fExtraFunctions.printf(R"( |
| 1149 | %s4 %s(%s2x2 x) { |
| 1150 | return %s4(x[0].xy, x[1].xy); |
| 1151 | } |
| 1152 | )", baseType.c_str(), name.c_str(), baseType.c_str(), baseType.c_str()); |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1153 | } |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1154 | |
| 1155 | return name; |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | void MetalCodeGenerator::writeConstructorCompoundVector(const ConstructorCompound& c, |
| 1159 | Precedence parentPrecedence) { |
| 1160 | SkASSERT(c.type().isVector()); |
| 1161 | |
| 1162 | // Metal supports constructing vectors from a mix of scalars and vectors, but not matrices. |
| 1163 | // GLSL supports vec4(mat2x2), so we detect that case here and emit a helper function. |
| 1164 | if (c.type().columns() == 4 && c.argumentSpan().size() == 1) { |
| 1165 | const Expression& expr = *c.argumentSpan().front(); |
| 1166 | if (expr.type().isMatrix()) { |
John Stiles | c18ee4e | 2021-08-13 17:53:49 -0400 | [diff] [blame] | 1167 | this->write(this->getVectorFromMat2x2ConstructorHelper(expr.type())); |
| 1168 | this->write("("); |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1169 | this->writeExpression(expr, Precedence::kSequence); |
| 1170 | this->write(")"); |
| 1171 | return; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | this->writeAnyConstructor(c, "(", ")", parentPrecedence); |
| 1176 | } |
| 1177 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1178 | void MetalCodeGenerator::writeConstructorCompoundMatrix(const ConstructorCompound& c, |
| 1179 | Precedence parentPrecedence) { |
John Stiles | 6de2e1d | 2021-07-09 12:41:55 -0400 | [diff] [blame] | 1180 | SkASSERT(c.type().isMatrix()); |
| 1181 | |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1182 | // Emit and invoke a matrix-constructor helper method if one is necessary. |
| 1183 | if (this->matrixConstructHelperIsNeeded(c)) { |
| 1184 | this->write(this->getMatrixConstructHelper(c)); |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1185 | this->write("("); |
| 1186 | const char* separator = ""; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1187 | for (const std::unique_ptr<Expression>& expr : c.arguments()) { |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1188 | this->write(separator); |
| 1189 | separator = ", "; |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1190 | this->writeExpression(*expr, Precedence::kSequence); |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1191 | } |
John Stiles | 1fa15b1 | 2020-05-28 17:36:54 +0000 | [diff] [blame] | 1192 | this->write(")"); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1193 | return; |
John Stiles | daa573e | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1194 | } |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1195 | |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1196 | // Metal doesn't allow creating matrices by passing in scalars and vectors in a jumble; it |
| 1197 | // requires your scalars to be grouped up into columns. Because `matrixConstructHelperIsNeeded` |
| 1198 | // returned false, we know that none of our scalars/vectors "wrap" across across a column, so we |
| 1199 | // can group our inputs up and synthesize a constructor for each column. |
| 1200 | const Type& matrixType = c.type(); |
| 1201 | const Type& columnType = matrixType.componentType().toCompound( |
| 1202 | fContext, /*columns=*/matrixType.rows(), /*rows=*/1); |
| 1203 | |
| 1204 | this->writeType(matrixType); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1205 | this->write("("); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1206 | const char* separator = ""; |
| 1207 | int scalarCount = 0; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1208 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1209 | this->write(separator); |
| 1210 | separator = ", "; |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1211 | if (arg->type().columns() < matrixType.rows()) { |
| 1212 | // Write a `floatN(` constructor to group scalars and smaller vectors together. |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1213 | if (!scalarCount) { |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1214 | this->writeType(columnType); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1215 | this->write("("); |
| 1216 | } |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1217 | scalarCount += arg->type().columns(); |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1218 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1219 | this->writeExpression(*arg, Precedence::kSequence); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 1220 | if (scalarCount && scalarCount == matrixType.rows()) { |
| 1221 | // Close our `floatN(...` constructor block from above. |
John Stiles | 1bdafbf | 2020-05-28 12:17:20 -0400 | [diff] [blame] | 1222 | this->write(")"); |
| 1223 | scalarCount = 0; |
| 1224 | } |
| 1225 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1226 | this->write(")"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1227 | } |
| 1228 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1229 | void MetalCodeGenerator::writeAnyConstructor(const AnyConstructor& c, |
| 1230 | const char* leftBracket, |
| 1231 | const char* rightBracket, |
| 1232 | Precedence parentPrecedence) { |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 1233 | this->writeType(c.type()); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1234 | this->write(leftBracket); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1235 | const char* separator = ""; |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1236 | for (const std::unique_ptr<Expression>& arg : c.argumentSpan()) { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1237 | this->write(separator); |
| 1238 | separator = ", "; |
| 1239 | this->writeExpression(*arg, Precedence::kSequence); |
| 1240 | } |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1241 | this->write(rightBracket); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1244 | void MetalCodeGenerator::writeCastConstructor(const AnyConstructor& c, |
| 1245 | const char* leftBracket, |
| 1246 | const char* rightBracket, |
| 1247 | Precedence parentPrecedence) { |
| 1248 | // If the type is coercible, emit it directly without the cast. |
| 1249 | auto args = c.argumentSpan(); |
| 1250 | if (args.size() == 1) { |
| 1251 | if (this->canCoerce(c.type(), args.front()->type())) { |
| 1252 | this->writeExpression(*args.front(), parentPrecedence); |
| 1253 | return; |
| 1254 | } |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1257 | return this->writeAnyConstructor(c, leftBracket, rightBracket, parentPrecedence); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1260 | void MetalCodeGenerator::writeFragCoord() { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1261 | SkASSERT(fRTFlipName.length()); |
| 1262 | this->write("float4(_fragCoord.x, "); |
| 1263 | this->write(fRTFlipName.c_str()); |
| 1264 | this->write(".x + "); |
| 1265 | this->write(fRTFlipName.c_str()); |
| 1266 | this->write(".y * _fragCoord.y, 0.0, _fragCoord.w)"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | void MetalCodeGenerator::writeVariableReference(const VariableReference& ref) { |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1270 | // When assembling out-param helper functions, we copy variables into local clones with matching |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1271 | // names. We never want to prepend "_in." or "_globals." when writing these variables since |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1272 | // we're actually targeting the clones. |
| 1273 | if (fIgnoreVariableReferenceModifiers) { |
| 1274 | this->writeName(ref.variable()->name()); |
| 1275 | return; |
| 1276 | } |
| 1277 | |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1278 | switch (ref.variable()->modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1279 | case SK_FRAGCOLOR_BUILTIN: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1280 | this->write("_out.sk_FragColor"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1281 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1282 | case SK_FRAGCOORD_BUILTIN: |
| 1283 | this->writeFragCoord(); |
| 1284 | break; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1285 | case SK_VERTEXID_BUILTIN: |
| 1286 | this->write("sk_VertexID"); |
| 1287 | break; |
| 1288 | case SK_INSTANCEID_BUILTIN: |
| 1289 | this->write("sk_InstanceID"); |
| 1290 | break; |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1291 | case SK_CLOCKWISE_BUILTIN: |
| 1292 | // 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] | 1293 | // clockwise to match Skia convention. |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1294 | this->write("(" + fRTFlipName + ".y < 0 ? _frontFacing : !_frontFacing)"); |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1295 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1296 | default: |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1297 | const Variable& var = *ref.variable(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1298 | if (var.storage() == Variable::Storage::kGlobal) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1299 | if (var.modifiers().fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1300 | this->write("_in."); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1301 | } else if (var.modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1302 | this->write("_out."); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1303 | } else if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
| 1304 | var.type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1305 | this->write("_uniforms."); |
| 1306 | } else { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1307 | this->write("_globals."); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1308 | } |
| 1309 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1310 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | void MetalCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1315 | this->writeExpression(*expr.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1316 | this->write("["); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1317 | this->writeExpression(*expr.index(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1318 | this->write("]"); |
| 1319 | } |
| 1320 | |
| 1321 | void MetalCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1322 | const Type::Field* field = &f.base()->type().fields()[f.fieldIndex()]; |
| 1323 | if (FieldAccess::OwnerKind::kDefault == f.ownerKind()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1324 | this->writeExpression(*f.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1325 | this->write("."); |
| 1326 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1327 | switch (field->fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1328 | case SK_POSITION_BUILTIN: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1329 | this->write("_out.sk_Position"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1330 | break; |
| 1331 | default: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1332 | if (field->fName == "sk_PointSize") { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1333 | this->write("_out.sk_PointSize"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1334 | } else { |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1335 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1336 | this->write("_globals."); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1337 | this->write(fInterfaceBlockNameMap[fInterfaceBlockMap[field]]); |
| 1338 | this->write("->"); |
| 1339 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1340 | this->writeName(field->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1341 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | void MetalCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1346 | this->writeExpression(*swizzle.base(), Precedence::kPostfix); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1347 | this->write("."); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1348 | for (int c : swizzle.components()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 1349 | SkASSERT(c >= 0 && c <= 3); |
| 1350 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1354 | void MetalCodeGenerator::writeMatrixTimesEqualHelper(const Type& left, const Type& right, |
| 1355 | const Type& result) { |
John Stiles | c985e14 | 2021-05-13 14:01:48 -0400 | [diff] [blame] | 1356 | SkASSERT(left.isMatrix()); |
| 1357 | SkASSERT(right.isMatrix()); |
| 1358 | SkASSERT(result.isMatrix()); |
| 1359 | SkASSERT(left.rows() == right.rows()); |
| 1360 | SkASSERT(left.columns() == right.columns()); |
| 1361 | SkASSERT(left.rows() == result.rows()); |
| 1362 | SkASSERT(left.columns() == result.columns()); |
| 1363 | |
| 1364 | String key = "Matrix *= " + this->typeName(left) + ":" + this->typeName(right); |
John Stiles | 56233d1 | 2021-02-03 13:03:29 -0500 | [diff] [blame] | 1365 | |
| 1366 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1367 | if (wasInserted) { |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 1368 | fExtraFunctions.printf("thread %s& operator*=(thread %s& left, thread const %s& right) {\n" |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1369 | " left = left * right;\n" |
| 1370 | " return left;\n" |
John Stiles | 368db7c | 2020-12-29 11:20:08 -0500 | [diff] [blame] | 1371 | "}\n", |
| 1372 | this->typeName(result).c_str(), this->typeName(left).c_str(), |
| 1373 | this->typeName(right).c_str()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1377 | void MetalCodeGenerator::writeMatrixEqualityHelpers(const Type& left, const Type& right) { |
| 1378 | SkASSERT(left.isMatrix()); |
| 1379 | SkASSERT(right.isMatrix()); |
| 1380 | SkASSERT(left.rows() == right.rows()); |
| 1381 | SkASSERT(left.columns() == right.columns()); |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1382 | |
John Stiles | c985e14 | 2021-05-13 14:01:48 -0400 | [diff] [blame] | 1383 | String key = "Matrix == " + this->typeName(left) + ":" + this->typeName(right); |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1384 | |
| 1385 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1386 | if (wasInserted) { |
John Stiles | 82d4c12 | 2021-08-10 16:03:23 -0400 | [diff] [blame] | 1387 | fExtraFunctionPrototypes.printf(R"( |
| 1388 | thread bool operator==(const %s left, const %s right); |
| 1389 | thread bool operator!=(const %s left, const %s right); |
| 1390 | )", |
| 1391 | this->typeName(left).c_str(), |
| 1392 | this->typeName(right).c_str(), |
| 1393 | this->typeName(left).c_str(), |
| 1394 | this->typeName(right).c_str()); |
| 1395 | |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1396 | fExtraFunctions.printf( |
| 1397 | "thread bool operator==(const %s left, const %s right) {\n" |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1398 | " return ", |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1399 | this->typeName(left).c_str(), this->typeName(right).c_str()); |
| 1400 | |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1401 | const char* separator = ""; |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1402 | for (int index=0; index<left.columns(); ++index) { |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1403 | fExtraFunctions.printf("%sall(left[%d] == right[%d])", separator, index, index); |
| 1404 | separator = " &&\n "; |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1405 | } |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1406 | |
| 1407 | fExtraFunctions.printf( |
| 1408 | ";\n" |
| 1409 | "}\n" |
| 1410 | "thread bool operator!=(const %s left, const %s right) {\n" |
| 1411 | " return !(left == right);\n" |
| 1412 | "}\n", |
| 1413 | this->typeName(left).c_str(), this->typeName(right).c_str()); |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
John Stiles | c985e14 | 2021-05-13 14:01:48 -0400 | [diff] [blame] | 1417 | void MetalCodeGenerator::writeMatrixDivisionHelpers(const Type& type) { |
| 1418 | SkASSERT(type.isMatrix()); |
| 1419 | |
| 1420 | String key = "Matrix / " + this->typeName(type); |
| 1421 | |
| 1422 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1423 | if (wasInserted) { |
| 1424 | String typeName = this->typeName(type); |
| 1425 | |
| 1426 | fExtraFunctions.printf( |
| 1427 | "thread %s operator/(const %s left, const %s right) {\n" |
| 1428 | " return %s(", |
| 1429 | typeName.c_str(), typeName.c_str(), typeName.c_str(), typeName.c_str()); |
| 1430 | |
| 1431 | const char* separator = ""; |
| 1432 | for (int index=0; index<type.columns(); ++index) { |
| 1433 | fExtraFunctions.printf("%sleft[%d] / right[%d]", separator, index, index); |
| 1434 | separator = ", "; |
| 1435 | } |
| 1436 | |
| 1437 | fExtraFunctions.printf(");\n" |
| 1438 | "}\n" |
| 1439 | "thread %s& operator/=(thread %s& left, thread const %s& right) {\n" |
| 1440 | " left = left / right;\n" |
| 1441 | " return left;\n" |
| 1442 | "}\n", |
| 1443 | typeName.c_str(), typeName.c_str(), typeName.c_str()); |
| 1444 | } |
| 1445 | } |
| 1446 | |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1447 | void MetalCodeGenerator::writeArrayEqualityHelpers(const Type& type) { |
| 1448 | SkASSERT(type.isArray()); |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1449 | |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1450 | // If the array's component type needs a helper as well, we need to emit that one first. |
| 1451 | this->writeEqualityHelpers(type.componentType(), type.componentType()); |
| 1452 | |
| 1453 | auto [iter, wasInserted] = fHelpers.insert("ArrayEquality []"); |
| 1454 | if (wasInserted) { |
John Stiles | 82d4c12 | 2021-08-10 16:03:23 -0400 | [diff] [blame] | 1455 | fExtraFunctionPrototypes.writeText(R"( |
| 1456 | template <typename T1, typename T2, size_t N> |
| 1457 | bool operator==(thread const array<T1, N>& left, thread const array<T2, N>& right); |
| 1458 | template <typename T1, typename T2, size_t N> |
| 1459 | bool operator!=(thread const array<T1, N>& left, thread const array<T2, N>& right); |
| 1460 | )"); |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1461 | fExtraFunctions.writeText(R"( |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 1462 | template <typename T1, typename T2, size_t N> |
| 1463 | bool operator==(thread const array<T1, N>& left, thread const array<T2, N>& right) { |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1464 | for (size_t index = 0; index < N; ++index) { |
John Stiles | e076c38 | 2021-08-10 16:56:23 +0000 | [diff] [blame] | 1465 | if (!all(left[index] == right[index])) { |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
| 1468 | } |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 1472 | template <typename T1, typename T2, size_t N> |
| 1473 | bool operator!=(thread const array<T1, N>& left, thread const array<T2, N>& right) { |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1474 | return !(left == right); |
| 1475 | } |
| 1476 | )"); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | void MetalCodeGenerator::writeStructEqualityHelpers(const Type& type) { |
| 1481 | SkASSERT(type.isStruct()); |
| 1482 | String key = "StructEquality " + this->typeName(type); |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1483 | |
| 1484 | auto [iter, wasInserted] = fHelpers.insert(key); |
| 1485 | if (wasInserted) { |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1486 | // If one of the struct's fields needs a helper as well, we need to emit that one first. |
| 1487 | for (const Type::Field& field : type.fields()) { |
| 1488 | this->writeEqualityHelpers(*field.fType, *field.fType); |
John Stiles | 830c69c | 2021-04-28 17:16:16 -0400 | [diff] [blame] | 1489 | } |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1490 | |
| 1491 | // Write operator== and operator!= for this struct, since those are assumed to exist in SkSL |
| 1492 | // and GLSL but do not exist by default in Metal. |
John Stiles | 82d4c12 | 2021-08-10 16:03:23 -0400 | [diff] [blame] | 1493 | fExtraFunctionPrototypes.printf(R"( |
| 1494 | thread bool operator==(thread const %s& left, thread const %s& right); |
| 1495 | thread bool operator!=(thread const %s& left, thread const %s& right); |
| 1496 | )", |
| 1497 | this->typeName(type).c_str(), |
| 1498 | this->typeName(type).c_str(), |
| 1499 | this->typeName(type).c_str(), |
| 1500 | this->typeName(type).c_str()); |
| 1501 | |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1502 | fExtraFunctions.printf( |
| 1503 | "thread bool operator==(thread const %s& left, thread const %s& right) {\n" |
| 1504 | " return ", |
| 1505 | this->typeName(type).c_str(), |
| 1506 | this->typeName(type).c_str()); |
| 1507 | |
| 1508 | const char* separator = ""; |
| 1509 | for (const Type::Field& field : type.fields()) { |
| 1510 | fExtraFunctions.printf("%s(left.%.*s == right.%.*s)", |
| 1511 | separator, |
| 1512 | (int)field.fName.size(), field.fName.data(), |
| 1513 | (int)field.fName.size(), field.fName.data()); |
| 1514 | separator = " &&\n "; |
| 1515 | } |
| 1516 | fExtraFunctions.printf( |
| 1517 | ";\n" |
| 1518 | "}\n" |
| 1519 | "thread bool operator!=(thread const %s& left, thread const %s& right) {\n" |
| 1520 | " return !(left == right);\n" |
| 1521 | "}\n", |
| 1522 | this->typeName(type).c_str(), |
| 1523 | this->typeName(type).c_str()); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | void MetalCodeGenerator::writeEqualityHelpers(const Type& leftType, const Type& rightType) { |
| 1528 | if (leftType.isArray() && rightType.isArray()) { |
| 1529 | this->writeArrayEqualityHelpers(leftType); |
| 1530 | return; |
| 1531 | } |
| 1532 | if (leftType.isStruct() && rightType.isStruct()) { |
| 1533 | this->writeStructEqualityHelpers(leftType); |
| 1534 | return; |
| 1535 | } |
| 1536 | if (leftType.isMatrix() && rightType.isMatrix()) { |
| 1537 | this->writeMatrixEqualityHelpers(leftType, rightType); |
| 1538 | return; |
John Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
John Stiles | 6b13129 | 2021-05-12 17:12:21 -0400 | [diff] [blame] | 1542 | void MetalCodeGenerator::writeNumberAsMatrix(const Expression& expr, const Type& matrixType) { |
| 1543 | SkASSERT(expr.type().isNumber()); |
| 1544 | SkASSERT(matrixType.isMatrix()); |
| 1545 | |
| 1546 | // Componentwise multiply the scalar against a matrix of the desired size which contains all 1s. |
| 1547 | this->write("("); |
| 1548 | this->writeType(matrixType); |
| 1549 | this->write("("); |
| 1550 | |
| 1551 | const char* separator = ""; |
| 1552 | for (int index = matrixType.slotCount(); index--;) { |
| 1553 | this->write(separator); |
| 1554 | this->write("1.0"); |
| 1555 | separator = ", "; |
| 1556 | } |
| 1557 | |
| 1558 | this->write(") * "); |
| 1559 | this->writeExpression(expr, Precedence::kMultiplicative); |
| 1560 | this->write(")"); |
| 1561 | } |
| 1562 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1563 | void MetalCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 1564 | Precedence parentPrecedence) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1565 | const Expression& left = *b.left(); |
| 1566 | const Expression& right = *b.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1567 | const Type& leftType = left.type(); |
| 1568 | const Type& rightType = right.type(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1569 | Operator op = b.getOperator(); |
| 1570 | Precedence precedence = op.getBinaryPrecedence(); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1571 | bool needParens = precedence >= parentPrecedence; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1572 | switch (op.kind()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1573 | case Token::Kind::TK_EQEQ: |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1574 | this->writeEqualityHelpers(leftType, rightType); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1575 | if (leftType.isVector()) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1576 | this->write("all"); |
| 1577 | needParens = true; |
| 1578 | } |
| 1579 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1580 | case Token::Kind::TK_NEQ: |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1581 | this->writeEqualityHelpers(leftType, rightType); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1582 | if (leftType.isVector()) { |
Jim Van Verth | 36477b4 | 2019-04-11 14:57:30 -0400 | [diff] [blame] | 1583 | this->write("any"); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1584 | needParens = true; |
| 1585 | } |
| 1586 | break; |
| 1587 | default: |
| 1588 | break; |
| 1589 | } |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1590 | if (leftType.isMatrix() && rightType.isMatrix() && op.kind() == Token::Kind::TK_STAREQ) { |
| 1591 | this->writeMatrixTimesEqualHelper(leftType, rightType, b.type()); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1592 | } |
John Stiles | c985e14 | 2021-05-13 14:01:48 -0400 | [diff] [blame] | 1593 | if (op.removeAssignment().kind() == Token::Kind::TK_SLASH && |
| 1594 | ((leftType.isMatrix() && rightType.isMatrix()) || |
| 1595 | (leftType.isScalar() && rightType.isMatrix()) || |
| 1596 | (leftType.isMatrix() && rightType.isScalar()))) { |
| 1597 | this->writeMatrixDivisionHelpers(leftType.isMatrix() ? leftType : rightType); |
| 1598 | } |
| 1599 | if (needParens) { |
| 1600 | this->write("("); |
| 1601 | } |
John Stiles | 6011bbc | 2021-05-19 22:56:18 -0400 | [diff] [blame] | 1602 | bool needMatrixSplatOnScalar = rightType.isMatrix() && leftType.isNumber() && |
| 1603 | op.isValidForMatrixOrVector() && |
John Stiles | 6b13129 | 2021-05-12 17:12:21 -0400 | [diff] [blame] | 1604 | op.removeAssignment().kind() != Token::Kind::TK_STAR; |
| 1605 | if (needMatrixSplatOnScalar) { |
| 1606 | this->writeNumberAsMatrix(left, rightType); |
| 1607 | } else { |
| 1608 | this->writeExpression(left, precedence); |
| 1609 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1610 | if (op.kind() != Token::Kind::TK_EQ && op.isAssignment() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1611 | left.kind() == Expression::Kind::kSwizzle && !left.hasSideEffects()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1612 | // This doesn't compile in Metal: |
| 1613 | // float4 x = float4(1); |
| 1614 | // x.xy *= float2x2(...); |
| 1615 | // with the error message "non-const reference cannot bind to vector element", |
| 1616 | // but switching it to x.xy = x.xy * float2x2(...) fixes it. We perform this tranformation |
| 1617 | // as long as the LHS has no side effects, and hope for the best otherwise. |
| 1618 | this->write(" = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1619 | this->writeExpression(left, Precedence::kAssignment); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1620 | this->write(" "); |
John Stiles | 7cbe66b | 2021-05-12 17:01:23 -0400 | [diff] [blame] | 1621 | this->write(OperatorName(op.removeAssignment())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1622 | this->write(" "); |
| 1623 | } else { |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1624 | this->write(String(" ") + OperatorName(op) + " "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1625 | } |
John Stiles | 6b13129 | 2021-05-12 17:12:21 -0400 | [diff] [blame] | 1626 | |
John Stiles | 6011bbc | 2021-05-19 22:56:18 -0400 | [diff] [blame] | 1627 | needMatrixSplatOnScalar = leftType.isMatrix() && rightType.isNumber() && |
| 1628 | op.isValidForMatrixOrVector() && |
John Stiles | 6b13129 | 2021-05-12 17:12:21 -0400 | [diff] [blame] | 1629 | op.removeAssignment().kind() != Token::Kind::TK_STAR; |
| 1630 | if (needMatrixSplatOnScalar) { |
| 1631 | this->writeNumberAsMatrix(right, leftType); |
| 1632 | } else { |
| 1633 | this->writeExpression(right, precedence); |
| 1634 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1635 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1636 | this->write(")"); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | void MetalCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 1641 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1642 | if (Precedence::kTernary >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1643 | this->write("("); |
| 1644 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1645 | this->writeExpression(*t.test(), Precedence::kTernary); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1646 | this->write(" ? "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1647 | this->writeExpression(*t.ifTrue(), Precedence::kTernary); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1648 | this->write(" : "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1649 | this->writeExpression(*t.ifFalse(), Precedence::kTernary); |
| 1650 | if (Precedence::kTernary >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1651 | this->write(")"); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | void MetalCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 1656 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1657 | if (Precedence::kPrefix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1658 | this->write("("); |
| 1659 | } |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1660 | this->write(OperatorName(p.getOperator())); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1661 | this->writeExpression(*p.operand(), Precedence::kPrefix); |
| 1662 | if (Precedence::kPrefix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1663 | this->write(")"); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | void MetalCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 1668 | Precedence parentPrecedence) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1669 | if (Precedence::kPostfix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1670 | this->write("("); |
| 1671 | } |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1672 | this->writeExpression(*p.operand(), Precedence::kPostfix); |
John Stiles | d6449e9 | 2020-11-30 09:13:23 -0500 | [diff] [blame] | 1673 | this->write(OperatorName(p.getOperator())); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 1674 | if (Precedence::kPostfix >= parentPrecedence) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1675 | this->write(")"); |
| 1676 | } |
| 1677 | } |
| 1678 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1679 | void MetalCodeGenerator::writeLiteral(const Literal& l) { |
| 1680 | const Type& type = l.type(); |
| 1681 | if (type.isFloat()) { |
| 1682 | this->write(to_string(l.floatValue())); |
| 1683 | return; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1684 | } |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1685 | if (type.isInteger()) { |
| 1686 | if (type == *fContext.fTypes.fUInt) { |
| 1687 | this->write(to_string(l.intValue() & 0xffffffff) + "u"); |
| 1688 | } else if (type == *fContext.fTypes.fUShort) { |
| 1689 | this->write(to_string(l.intValue() & 0xffff) + "u"); |
| 1690 | } else { |
| 1691 | this->write(to_string(l.intValue())); |
| 1692 | } |
| 1693 | return; |
| 1694 | } |
| 1695 | SkASSERT(type.isBoolean()); |
| 1696 | this->write(l.boolValue() ? "true" : "false"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | void MetalCodeGenerator::writeSetting(const Setting& s) { |
John Stiles | f57207b | 2021-02-02 17:50:34 -0500 | [diff] [blame] | 1700 | SK_ABORT("internal error; setting was not folded to a constant during compilation\n"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1701 | } |
| 1702 | |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1703 | void MetalCodeGenerator::writeFunctionRequirementArgs(const FunctionDeclaration& f, |
| 1704 | const char*& separator) { |
| 1705 | Requirements requirements = this->requirements(f); |
| 1706 | if (requirements & kInputs_Requirement) { |
| 1707 | this->write(separator); |
| 1708 | this->write("_in"); |
| 1709 | separator = ", "; |
| 1710 | } |
| 1711 | if (requirements & kOutputs_Requirement) { |
| 1712 | this->write(separator); |
| 1713 | this->write("_out"); |
| 1714 | separator = ", "; |
| 1715 | } |
| 1716 | if (requirements & kUniforms_Requirement) { |
| 1717 | this->write(separator); |
| 1718 | this->write("_uniforms"); |
| 1719 | separator = ", "; |
| 1720 | } |
| 1721 | if (requirements & kGlobals_Requirement) { |
| 1722 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1723 | this->write("_globals"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1724 | separator = ", "; |
| 1725 | } |
| 1726 | if (requirements & kFragCoord_Requirement) { |
| 1727 | this->write(separator); |
| 1728 | this->write("_fragCoord"); |
| 1729 | separator = ", "; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | void MetalCodeGenerator::writeFunctionRequirementParams(const FunctionDeclaration& f, |
| 1734 | const char*& separator) { |
| 1735 | Requirements requirements = this->requirements(f); |
| 1736 | if (requirements & kInputs_Requirement) { |
| 1737 | this->write(separator); |
| 1738 | this->write("Inputs _in"); |
| 1739 | separator = ", "; |
| 1740 | } |
| 1741 | if (requirements & kOutputs_Requirement) { |
| 1742 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1743 | this->write("thread Outputs& _out"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1744 | separator = ", "; |
| 1745 | } |
| 1746 | if (requirements & kUniforms_Requirement) { |
| 1747 | this->write(separator); |
| 1748 | this->write("Uniforms _uniforms"); |
| 1749 | separator = ", "; |
| 1750 | } |
| 1751 | if (requirements & kGlobals_Requirement) { |
| 1752 | this->write(separator); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1753 | this->write("thread Globals& _globals"); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1754 | separator = ", "; |
| 1755 | } |
| 1756 | if (requirements & kFragCoord_Requirement) { |
| 1757 | this->write(separator); |
| 1758 | this->write("float4 _fragCoord"); |
| 1759 | separator = ", "; |
| 1760 | } |
| 1761 | } |
| 1762 | |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1763 | int MetalCodeGenerator::getUniformBinding(const Modifiers& m) { |
| 1764 | return (m.fLayout.fBinding >= 0) ? m.fLayout.fBinding |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1765 | : fProgram.fConfig->fSettings.fDefaultUniformBinding; |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | int MetalCodeGenerator::getUniformSet(const Modifiers& m) { |
| 1769 | return (m.fLayout.fSet >= 0) ? m.fLayout.fSet |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1770 | : fProgram.fConfig->fSettings.fDefaultUniformSet; |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1771 | } |
| 1772 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1773 | bool MetalCodeGenerator::writeFunctionDeclaration(const FunctionDeclaration& f) { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1774 | fRTFlipName = fProgram.fInputs.fUseFlipRTUniform |
| 1775 | ? "_globals._anonInterface0->" SKSL_RTFLIP_NAME |
| 1776 | : ""; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1777 | const char* separator = ""; |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1778 | if (f.isMain()) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1779 | switch (fProgram.fConfig->fKind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1780 | case ProgramKind::kFragment: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1781 | this->write("fragment Outputs fragmentMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1782 | break; |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1783 | case ProgramKind::kVertex: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1784 | this->write("vertex Outputs vertexMain"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1785 | break; |
| 1786 | default: |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame] | 1787 | fContext.fErrors->error(-1, "unsupported kind of program"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1788 | return false; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1789 | } |
| 1790 | this->write("(Inputs _in [[stage_in]]"); |
| 1791 | if (-1 != fUniformBuffer) { |
| 1792 | this->write(", constant Uniforms& _uniforms [[buffer(" + |
| 1793 | to_string(fUniformBuffer) + ")]]"); |
| 1794 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1795 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1796 | if (e->is<GlobalVarDeclaration>()) { |
| 1797 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1798 | const VarDeclaration& var = decls.declaration()->as<VarDeclaration>(); |
| 1799 | if (var.var().type().typeKind() == Type::TypeKind::kSampler) { |
| 1800 | if (var.var().modifiers().fLayout.fBinding < 0) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1801 | fContext.fErrors->error(decls.fOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 1802 | "Metal samplers must have 'layout(binding=...)'"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1803 | return false; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1804 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1805 | if (var.var().type().dimensions() != SpvDim2D) { |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1806 | // Not yet implemented--Skia currently only uses 2D textures. |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1807 | fContext.fErrors->error(decls.fOffset, "Unsupported texture dimensions"); |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1808 | return false; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1809 | } |
| 1810 | this->write(", texture2d<float> "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1811 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1812 | this->write("[[texture("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1813 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1814 | this->write(")]]"); |
| 1815 | this->write(", sampler "); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1816 | this->writeName(var.var().name()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1817 | this->write(SAMPLER_SUFFIX); |
| 1818 | this->write("[[sampler("); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1819 | this->write(to_string(var.var().modifiers().fLayout.fBinding)); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1820 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1821 | } |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1822 | } else if (e->is<InterfaceBlock>()) { |
| 1823 | const InterfaceBlock& intf = e->as<InterfaceBlock>(); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1824 | if (intf.typeName() == "sk_PerVertex") { |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1825 | continue; |
| 1826 | } |
| 1827 | this->write(", constant "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1828 | this->writeType(intf.variable().type()); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1829 | this->write("& " ); |
| 1830 | this->write(fInterfaceBlockNameMap[&intf]); |
| 1831 | this->write(" [[buffer("); |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 1832 | this->write(to_string(this->getUniformBinding(intf.variable().modifiers()))); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1833 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 1834 | } |
| 1835 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1836 | if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1837 | if (fProgram.fInputs.fUseFlipRTUniform && fInterfaceBlockNameMap.empty()) { |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1838 | this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(1)]]"); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1839 | fRTFlipName = "_anonInterface0." SKSL_RTFLIP_NAME; |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 1840 | } |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 1841 | this->write(", bool _frontFacing [[front_facing]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1842 | this->write(", float4 _fragCoord [[position]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1843 | } else if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1844 | this->write(", uint sk_VertexID [[vertex_id]], uint sk_InstanceID [[instance_id]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1845 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1846 | separator = ", "; |
| 1847 | } else { |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1848 | this->writeType(f.returnType()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1849 | this->write(" "); |
John Stiles | e106834 | 2021-03-22 11:57:41 -0400 | [diff] [blame] | 1850 | this->writeName(f.mangledName()); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1851 | this->write("("); |
John Stiles | 06b84ef | 2020-12-09 12:35:48 -0500 | [diff] [blame] | 1852 | this->writeFunctionRequirementParams(f, separator); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1853 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1854 | for (const auto& param : f.parameters()) { |
Brian Osman | 716aeb9 | 2021-04-21 13:20:00 -0400 | [diff] [blame] | 1855 | if (f.isMain() && param->modifiers().fLayout.fBuiltin != -1) { |
| 1856 | continue; |
| 1857 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1858 | this->write(separator); |
| 1859 | separator = ", "; |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 1860 | this->writeModifiers(param->modifiers()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1861 | const Type* type = ¶m->type(); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 1862 | this->writeType(*type); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1863 | if (param->modifiers().fFlags & Modifiers::kOut_Flag) { |
John Stiles | f2bd501 | 2020-12-04 11:58:26 -0500 | [diff] [blame] | 1864 | this->write("&"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1865 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1866 | this->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 1867 | this->writeName(param->name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1868 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1869 | this->write(")"); |
| 1870 | return true; |
| 1871 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1872 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1873 | void MetalCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 1874 | this->writeFunctionDeclaration(f.declaration()); |
| 1875 | this->writeLine(";"); |
| 1876 | } |
| 1877 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1878 | static bool is_block_ending_with_return(const Statement* stmt) { |
| 1879 | // This function detects (potentially nested) blocks that end in a return statement. |
| 1880 | if (!stmt->is<Block>()) { |
| 1881 | return false; |
| 1882 | } |
| 1883 | const StatementArray& block = stmt->as<Block>().children(); |
| 1884 | for (int index = block.count(); index--; ) { |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 1885 | stmt = block[index].get(); |
| 1886 | if (stmt->is<ReturnStatement>()) { |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1887 | return true; |
| 1888 | } |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 1889 | if (stmt->is<Block>()) { |
| 1890 | return is_block_ending_with_return(stmt); |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1891 | } |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 1892 | if (!stmt->is<Nop>()) { |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1893 | break; |
| 1894 | } |
| 1895 | } |
| 1896 | return false; |
| 1897 | } |
| 1898 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1899 | void MetalCodeGenerator::writeFunction(const FunctionDefinition& f) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1900 | SkASSERT(!fProgram.fConfig->fSettings.fFragColorIsInOut); |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 1901 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1902 | if (!this->writeFunctionDeclaration(f.declaration())) { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 1906 | fCurrentFunction = &f.declaration(); |
| 1907 | SkScopeExit clearCurrentFunction([&] { fCurrentFunction = nullptr; }); |
| 1908 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1909 | this->writeLine(" {"); |
| 1910 | |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1911 | if (f.declaration().isMain()) { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 1912 | this->writeGlobalInit(); |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 1913 | this->writeLine(" Outputs _out;"); |
John Stiles | 3727917 | 2021-01-21 22:24:28 -0500 | [diff] [blame] | 1914 | this->writeLine(" (void)_out;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1915 | } |
John Stiles | c67b362 | 2020-05-28 17:53:13 -0400 | [diff] [blame] | 1916 | |
John Stiles | 9568023 | 2021-04-22 15:05:20 -0400 | [diff] [blame] | 1917 | fFunctionHeader.clear(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1918 | StringStream buffer; |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1919 | { |
| 1920 | AutoOutputStream outputToBuffer(this, &buffer); |
| 1921 | fIndentation++; |
| 1922 | for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
| 1923 | if (!stmt->isEmpty()) { |
| 1924 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1925 | this->finishLine(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1926 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1927 | } |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 1928 | if (f.declaration().isMain()) { |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1929 | // If the main function doesn't end with a return, we need to synthesize one here. |
| 1930 | if (!is_block_ending_with_return(f.body().get())) { |
| 1931 | this->writeReturnStatementFromMain(); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 1932 | this->finishLine(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1933 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1934 | } |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 1935 | fIndentation--; |
| 1936 | this->writeLine("}"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1937 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1938 | this->write(fFunctionHeader); |
| 1939 | this->write(buffer.str()); |
| 1940 | } |
| 1941 | |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 1942 | void MetalCodeGenerator::writeModifiers(const Modifiers& modifiers) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1943 | if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 1944 | this->write("thread "); |
| 1945 | } |
| 1946 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
John Stiles | 0bd5578 | 2021-02-09 18:29:40 -0500 | [diff] [blame] | 1947 | this->write("const "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | void MetalCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1952 | if ("sk_PerVertex" == intf.typeName()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1953 | return; |
| 1954 | } |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 1955 | this->writeModifiers(intf.variable().modifiers()); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1956 | this->write("struct "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1957 | this->writeLine(intf.typeName() + " {"); |
| 1958 | const Type* structType = &intf.variable().type(); |
John Stiles | bb43b7e | 2020-12-03 17:36:32 -0500 | [diff] [blame] | 1959 | if (structType->isArray()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1960 | structType = &structType->componentType(); |
| 1961 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1962 | fIndentation++; |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1963 | this->writeFields(structType->fields(), structType->fOffset, &intf); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 1964 | if (fProgram.fInputs.fUseFlipRTUniform) { |
| 1965 | this->writeLine("float2 " SKSL_RTFLIP_NAME ";"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1966 | } |
| 1967 | fIndentation--; |
| 1968 | this->write("}"); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1969 | if (intf.instanceName().size()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1970 | this->write(" "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1971 | this->write(intf.instanceName()); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1972 | if (intf.arraySize() > 0) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1973 | this->write("["); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1974 | this->write(to_string(intf.arraySize())); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1975 | this->write("]"); |
| 1976 | } |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 1977 | fInterfaceBlockNameMap[&intf] = intf.instanceName(); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1978 | } else { |
Ethan Nicholas | 3533ff1 | 2021-08-02 12:53:29 -0400 | [diff] [blame] | 1979 | fInterfaceBlockNameMap[&intf] = *fProgram.fSymbols->takeOwnershipOfString("_anonInterface" + |
| 1980 | to_string(fAnonInterfaceCount++)); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1981 | } |
| 1982 | this->writeLine(";"); |
| 1983 | } |
| 1984 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1985 | void MetalCodeGenerator::writeFields(const std::vector<Type::Field>& fields, int parentOffset, |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1986 | const InterfaceBlock* parentIntf) { |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1987 | MemoryLayout memoryLayout(MemoryLayout::kMetal_Standard); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1988 | int currentOffset = 0; |
John Stiles | 3934647 | 2021-04-29 14:39:35 -0400 | [diff] [blame] | 1989 | for (const Type::Field& field : fields) { |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1990 | int fieldOffset = field.fModifiers.fLayout.fOffset; |
| 1991 | const Type* fieldType = field.fType; |
John Stiles | 21f5f45 | 2020-11-30 09:57:59 -0500 | [diff] [blame] | 1992 | if (!MemoryLayout::LayoutIsSupported(*fieldType)) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1993 | fContext.fErrors->error(parentOffset, "type '" + fieldType->name() + |
| 1994 | "' is not permitted here"); |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 1995 | return; |
| 1996 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1997 | if (fieldOffset != -1) { |
| 1998 | if (currentOffset > fieldOffset) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1999 | fContext.fErrors->error(parentOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 2000 | "offset of field '" + field.fName + "' must be at least " + |
| 2001 | to_string((int) currentOffset)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 2002 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 2003 | } else if (currentOffset < fieldOffset) { |
| 2004 | this->write("char pad"); |
| 2005 | this->write(to_string(fPaddingCount++)); |
| 2006 | this->write("["); |
| 2007 | this->write(to_string(fieldOffset - currentOffset)); |
| 2008 | this->writeLine("];"); |
| 2009 | currentOffset = fieldOffset; |
| 2010 | } |
| 2011 | int alignment = memoryLayout.alignment(*fieldType); |
| 2012 | if (fieldOffset % alignment) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 2013 | fContext.fErrors->error(parentOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 2014 | "offset of field '" + field.fName + "' must be a multiple of " + |
| 2015 | to_string((int) alignment)); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 2016 | return; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 2017 | } |
| 2018 | } |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 2019 | size_t fieldSize = memoryLayout.size(*fieldType); |
| 2020 | if (fieldSize > static_cast<size_t>(std::numeric_limits<int>::max() - currentOffset)) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 2021 | fContext.fErrors->error(parentOffset, "field offset overflow"); |
Brian Osman | 8609a24 | 2020-09-08 14:01:49 -0400 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | currentOffset += fieldSize; |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2025 | this->writeModifiers(field.fModifiers); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2026 | this->writeType(*fieldType); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 2027 | this->write(" "); |
| 2028 | this->writeName(field.fName); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 2029 | this->writeLine(";"); |
| 2030 | if (parentIntf) { |
| 2031 | fInterfaceBlockMap[&field] = parentIntf; |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2036 | void MetalCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2037 | this->writeExpression(value, Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2038 | } |
| 2039 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2040 | void MetalCodeGenerator::writeName(skstd::string_view name) { |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 2041 | if (fReservedWords.find(name) != fReservedWords.end()) { |
| 2042 | this->write("_"); // adding underscore before name to avoid conflict with reserved words |
| 2043 | } |
| 2044 | this->write(name); |
| 2045 | } |
| 2046 | |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2047 | void MetalCodeGenerator::writeVarDeclaration(const VarDeclaration& varDecl) { |
| 2048 | this->writeModifiers(varDecl.var().modifiers()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2049 | this->writeType(varDecl.var().type()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2050 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2051 | this->writeName(varDecl.var().name()); |
| 2052 | if (varDecl.value()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2053 | this->write(" = "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2054 | this->writeVarInitializer(varDecl.var(), *varDecl.value()); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2055 | } |
| 2056 | this->write(";"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | void MetalCodeGenerator::writeStatement(const Statement& s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2060 | switch (s.kind()) { |
| 2061 | case Statement::Kind::kBlock: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2062 | this->writeBlock(s.as<Block>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2063 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2064 | case Statement::Kind::kExpression: |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2065 | this->writeExpression(*s.as<ExpressionStatement>().expression(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2066 | this->write(";"); |
| 2067 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2068 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2069 | this->writeReturnStatement(s.as<ReturnStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2070 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2071 | case Statement::Kind::kVarDeclaration: |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2072 | this->writeVarDeclaration(s.as<VarDeclaration>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2073 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2074 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2075 | this->writeIfStatement(s.as<IfStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2076 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2077 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2078 | this->writeForStatement(s.as<ForStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2079 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2080 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2081 | this->writeDoStatement(s.as<DoStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2082 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2083 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2084 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2085 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2086 | case Statement::Kind::kBreak: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2087 | this->write("break;"); |
| 2088 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2089 | case Statement::Kind::kContinue: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2090 | this->write("continue;"); |
| 2091 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2092 | case Statement::Kind::kDiscard: |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 2093 | this->write("discard_fragment();"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2094 | break; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 2095 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2096 | case Statement::Kind::kNop: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2097 | this->write(";"); |
| 2098 | break; |
| 2099 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 2100 | SkDEBUGFAILF("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2101 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2105 | void MetalCodeGenerator::writeBlock(const Block& b) { |
John Stiles | 3744bd6 | 2021-01-26 13:49:43 -0500 | [diff] [blame] | 2106 | // Write scope markers if this block is a scope, or if the block is empty (since we need to emit |
| 2107 | // something here to make the code valid). |
| 2108 | bool isScope = b.isScope() || b.isEmpty(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2109 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 2110 | this->writeLine("{"); |
| 2111 | fIndentation++; |
| 2112 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2113 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 2114 | if (!stmt->isEmpty()) { |
| 2115 | this->writeStatement(*stmt); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 2116 | this->finishLine(); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | if (isScope) { |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 2120 | fIndentation--; |
| 2121 | this->write("}"); |
| 2122 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | void MetalCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 2126 | this->write("if ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2127 | this->writeExpression(*stmt.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2128 | this->write(") "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2129 | this->writeStatement(*stmt.ifTrue()); |
| 2130 | if (stmt.ifFalse()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2131 | this->write(" else "); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2132 | this->writeStatement(*stmt.ifFalse()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | void MetalCodeGenerator::writeForStatement(const ForStatement& f) { |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 2137 | // Emit loops of the form 'for(;test;)' as 'while(test)', which is probably how they started |
| 2138 | if (!f.initializer() && f.test() && !f.next()) { |
| 2139 | this->write("while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2140 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 2141 | this->write(") "); |
| 2142 | this->writeStatement(*f.statement()); |
| 2143 | return; |
| 2144 | } |
| 2145 | |
John Stiles | 1a15e57 | 2021-04-19 14:57:15 -0400 | [diff] [blame] | 2146 | this->write("for ("); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2147 | if (f.initializer() && !f.initializer()->isEmpty()) { |
John Stiles | 1a15e57 | 2021-04-19 14:57:15 -0400 | [diff] [blame] | 2148 | this->writeStatement(*f.initializer()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2149 | } else { |
John Stiles | 1a15e57 | 2021-04-19 14:57:15 -0400 | [diff] [blame] | 2150 | this->write("; "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2151 | } |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2152 | if (f.test()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2153 | this->writeExpression(*f.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2154 | } |
| 2155 | this->write("; "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2156 | if (f.next()) { |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2157 | this->writeExpression(*f.next(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2158 | } |
| 2159 | this->write(") "); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2160 | this->writeStatement(*f.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2163 | void MetalCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 2164 | this->write("do "); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 2165 | this->writeStatement(*d.statement()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2166 | this->write(" while ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2167 | this->writeExpression(*d.test(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2168 | this->write(");"); |
| 2169 | } |
| 2170 | |
| 2171 | void MetalCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 2172 | this->write("switch ("); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2173 | this->writeExpression(*s.value(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2174 | this->writeLine(") {"); |
| 2175 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2176 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 2177 | const SwitchCase& c = stmt->as<SwitchCase>(); |
| 2178 | if (c.value()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2179 | this->write("case "); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2180 | this->writeExpression(*c.value(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2181 | this->writeLine(":"); |
| 2182 | } else { |
| 2183 | this->writeLine("default:"); |
| 2184 | } |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2185 | if (!c.statement()->isEmpty()) { |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 2186 | fIndentation++; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2187 | this->writeStatement(*c.statement()); |
John Stiles | e8b5a73 | 2021-03-12 13:25:52 -0500 | [diff] [blame] | 2188 | this->finishLine(); |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 2189 | fIndentation--; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2190 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2191 | } |
| 2192 | fIndentation--; |
| 2193 | this->write("}"); |
| 2194 | } |
| 2195 | |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 2196 | void MetalCodeGenerator::writeReturnStatementFromMain() { |
| 2197 | // main functions in Metal return a magic _out parameter that doesn't exist in SkSL. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2198 | switch (fProgram.fConfig->fKind) { |
Brian Salomon | 3e2fe2b | 2021-06-02 09:16:30 -0400 | [diff] [blame] | 2199 | case ProgramKind::kVertex: |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 2200 | case ProgramKind::kFragment: |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 2201 | this->write("return _out;"); |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 2202 | break; |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 2203 | default: |
| 2204 | SkDEBUGFAIL("unsupported kind of program"); |
| 2205 | } |
| 2206 | } |
| 2207 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2208 | void MetalCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 2209 | if (fCurrentFunction && fCurrentFunction->isMain()) { |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 2210 | if (r.expression()) { |
John Stiles | 97d1817 | 2021-01-22 16:47:42 -0500 | [diff] [blame] | 2211 | if (r.expression()->type() == *fContext.fTypes.fHalf4) { |
| 2212 | this->write("_out.sk_FragColor = "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2213 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
John Stiles | 97d1817 | 2021-01-22 16:47:42 -0500 | [diff] [blame] | 2214 | this->writeLine(";"); |
| 2215 | } else { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 2216 | fContext.fErrors->error(r.fOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 2217 | "Metal does not support returning '" + |
| 2218 | r.expression()->type().description() + "' from main()"); |
John Stiles | 97d1817 | 2021-01-22 16:47:42 -0500 | [diff] [blame] | 2219 | } |
John Stiles | 986c7fb | 2020-12-01 14:44:56 -0500 | [diff] [blame] | 2220 | } |
| 2221 | this->writeReturnStatementFromMain(); |
| 2222 | return; |
| 2223 | } |
| 2224 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2225 | this->write("return"); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 2226 | if (r.expression()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2227 | this->write(" "); |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 2228 | this->writeExpression(*r.expression(), Precedence::kTopLevel); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2229 | } |
| 2230 | this->write(";"); |
| 2231 | } |
| 2232 | |
Michael Ludwig | bf58add | 2021-03-16 10:40:11 -0400 | [diff] [blame] | 2233 | void MetalCodeGenerator::writeHeader() { |
| 2234 | this->write("#include <metal_stdlib>\n"); |
| 2235 | this->write("#include <simd/simd.h>\n"); |
| 2236 | this->write("using namespace metal;\n"); |
| 2237 | } |
| 2238 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2239 | void MetalCodeGenerator::writeUniformStruct() { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2240 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2241 | if (e->is<GlobalVarDeclaration>()) { |
| 2242 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2243 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2244 | if (var.modifiers().fFlags & Modifiers::kUniform_Flag && |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2245 | var.type().typeKind() != Type::TypeKind::kSampler) { |
John Stiles | da5cdf6 | 2021-01-28 11:47:29 -0500 | [diff] [blame] | 2246 | int uniformSet = this->getUniformSet(var.modifiers()); |
John Stiles | d8fc95d | 2021-01-26 16:22:53 -0500 | [diff] [blame] | 2247 | // Make sure that the program's uniform-set value is consistent throughout. |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2248 | if (-1 == fUniformBuffer) { |
| 2249 | this->write("struct Uniforms {\n"); |
John Stiles | d8fc95d | 2021-01-26 16:22:53 -0500 | [diff] [blame] | 2250 | fUniformBuffer = uniformSet; |
| 2251 | } else if (uniformSet != fUniformBuffer) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 2252 | fContext.fErrors->error(decls.fOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 2253 | "Metal backend requires all uniforms to have the same " |
| 2254 | "'layout(set=...)'"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2255 | } |
| 2256 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2257 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2258 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2259 | this->writeName(var.name()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2260 | this->write(";\n"); |
| 2261 | } |
| 2262 | } |
| 2263 | } |
| 2264 | if (-1 != fUniformBuffer) { |
| 2265 | this->write("};\n"); |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | void MetalCodeGenerator::writeInputStruct() { |
| 2270 | this->write("struct Inputs {\n"); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2271 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2272 | if (e->is<GlobalVarDeclaration>()) { |
| 2273 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2274 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2275 | if (var.modifiers().fFlags & Modifiers::kIn_Flag && |
| 2276 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2277 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2278 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2279 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2280 | this->writeName(var.name()); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2281 | if (-1 != var.modifiers().fLayout.fLocation) { |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2282 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2283 | this->write(" [[attribute(" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2284 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2285 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2286 | this->write(" [[user(locn" + |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2287 | to_string(var.modifiers().fLayout.fLocation) + ")]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | this->write(";\n"); |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | this->write("};\n"); |
| 2295 | } |
| 2296 | |
| 2297 | void MetalCodeGenerator::writeOutputStruct() { |
| 2298 | this->write("struct Outputs {\n"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2299 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 2300 | this->write(" float4 sk_Position [[position]];\n"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2301 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 2302 | this->write(" float4 sk_FragColor [[color(0)]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2303 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2304 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2305 | if (e->is<GlobalVarDeclaration>()) { |
| 2306 | const GlobalVarDeclaration& decls = e->as<GlobalVarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2307 | const Variable& var = decls.declaration()->as<VarDeclaration>().var(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2308 | if (var.modifiers().fFlags & Modifiers::kOut_Flag && |
| 2309 | -1 == var.modifiers().fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2310 | this->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2311 | this->writeType(var.type()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2312 | this->write(" "); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2313 | this->writeName(var.name()); |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2314 | |
| 2315 | int location = var.modifiers().fLayout.fLocation; |
| 2316 | if (location < 0) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 2317 | fContext.fErrors->error(var.fOffset, |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 2318 | "Metal out variables must have 'layout(location=...)'"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2319 | } else if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2320 | this->write(" [[user(locn" + to_string(location) + ")]]"); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2321 | } else if (fProgram.fConfig->fKind == ProgramKind::kFragment) { |
John Stiles | 842b359 | 2020-12-01 10:36:37 -0500 | [diff] [blame] | 2322 | this->write(" [[color(" + to_string(location) + ")"); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2323 | int colorIndex = var.modifiers().fLayout.fIndex; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2324 | if (colorIndex) { |
| 2325 | this->write(", index(" + to_string(colorIndex) + ")"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2326 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2327 | this->write("]]"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2328 | } |
| 2329 | this->write(";\n"); |
| 2330 | } |
| 2331 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2332 | } |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 2333 | if (fProgram.fConfig->fKind == ProgramKind::kVertex) { |
Jim Van Verth | 3913d3e | 2020-08-31 15:16:57 -0400 | [diff] [blame] | 2334 | this->write(" float sk_PointSize [[point_size]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2335 | } |
| 2336 | this->write("};\n"); |
| 2337 | } |
| 2338 | |
| 2339 | void MetalCodeGenerator::writeInterfaceBlocks() { |
| 2340 | bool wroteInterfaceBlock = false; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2341 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2342 | if (e->is<InterfaceBlock>()) { |
| 2343 | this->writeInterfaceBlock(e->as<InterfaceBlock>()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2344 | wroteInterfaceBlock = true; |
| 2345 | } |
| 2346 | } |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 2347 | if (!wroteInterfaceBlock && fProgram.fInputs.fUseFlipRTUniform) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2348 | this->writeLine("struct sksl_synthetic_uniforms {"); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 2349 | this->writeLine(" float2 " SKSL_RTFLIP_NAME ";"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2350 | this->writeLine("};"); |
| 2351 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2352 | } |
| 2353 | |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2354 | void MetalCodeGenerator::writeStructDefinitions() { |
| 2355 | for (const ProgramElement* e : fProgram.elements()) { |
| 2356 | if (e->is<StructDefinition>()) { |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 2357 | this->writeStructDefinition(e->as<StructDefinition>()); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2362 | void MetalCodeGenerator::visitGlobalStruct(GlobalStructVisitor* visitor) { |
| 2363 | // Visit the interface blocks. |
| 2364 | for (const auto& [interfaceType, interfaceName] : fInterfaceBlockNameMap) { |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2365 | visitor->visitInterfaceBlock(*interfaceType, interfaceName); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2366 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2367 | for (const ProgramElement* element : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2368 | if (!element->is<GlobalVarDeclaration>()) { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2369 | continue; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2370 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2371 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 2372 | const VarDeclaration& decl = global.declaration()->as<VarDeclaration>(); |
| 2373 | const Variable& var = decl.var(); |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2374 | if (var.type().typeKind() == Type::TypeKind::kSampler) { |
| 2375 | // Samplers are represented as a "texture/sampler" duo in the global struct. |
| 2376 | visitor->visitTexture(var.type(), var.name()); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 2377 | visitor->visitSampler(var.type(), var.name() + SAMPLER_SUFFIX); |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2378 | continue; |
| 2379 | } |
| 2380 | |
| 2381 | if (!(var.modifiers().fFlags & ~Modifiers::kConst_Flag) && |
| 2382 | -1 == var.modifiers().fLayout.fBuiltin) { |
| 2383 | // Visit a regular variable. |
| 2384 | visitor->visitVariable(var, decl.value().get()); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2385 | } |
| 2386 | } |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | void MetalCodeGenerator::writeGlobalStruct() { |
| 2390 | class : public GlobalStructVisitor { |
| 2391 | public: |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2392 | void visitInterfaceBlock(const InterfaceBlock& block, |
| 2393 | skstd::string_view blockName) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2394 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2395 | fCodeGen->write(" constant "); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2396 | fCodeGen->write(block.typeName()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2397 | fCodeGen->write("* "); |
| 2398 | fCodeGen->writeName(blockName); |
| 2399 | fCodeGen->write(";\n"); |
| 2400 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2401 | void visitTexture(const Type& type, skstd::string_view name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2402 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2403 | fCodeGen->write(" "); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2404 | fCodeGen->writeType(type); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2405 | fCodeGen->write(" "); |
| 2406 | fCodeGen->writeName(name); |
| 2407 | fCodeGen->write(";\n"); |
| 2408 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2409 | void visitSampler(const Type&, skstd::string_view name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2410 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2411 | fCodeGen->write(" sampler "); |
| 2412 | fCodeGen->writeName(name); |
| 2413 | fCodeGen->write(";\n"); |
| 2414 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2415 | void visitVariable(const Variable& var, const Expression* value) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2416 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2417 | fCodeGen->write(" "); |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2418 | fCodeGen->writeModifiers(var.modifiers()); |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 2419 | fCodeGen->writeType(var.type()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2420 | fCodeGen->write(" "); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2421 | fCodeGen->writeName(var.name()); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2422 | fCodeGen->write(";\n"); |
| 2423 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2424 | void addElement() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2425 | if (fFirst) { |
| 2426 | fCodeGen->write("struct Globals {\n"); |
| 2427 | fFirst = false; |
| 2428 | } |
| 2429 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2430 | void finish() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2431 | if (!fFirst) { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2432 | fCodeGen->writeLine("};"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2433 | fFirst = true; |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | MetalCodeGenerator* fCodeGen = nullptr; |
| 2438 | bool fFirst = true; |
| 2439 | } visitor; |
| 2440 | |
| 2441 | visitor.fCodeGen = this; |
| 2442 | this->visitGlobalStruct(&visitor); |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2443 | visitor.finish(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | void MetalCodeGenerator::writeGlobalInit() { |
| 2447 | class : public GlobalStructVisitor { |
| 2448 | public: |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2449 | void visitInterfaceBlock(const InterfaceBlock& blockType, |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2450 | skstd::string_view blockName) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2451 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2452 | fCodeGen->write("&"); |
| 2453 | fCodeGen->writeName(blockName); |
| 2454 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2455 | void visitTexture(const Type&, skstd::string_view name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2456 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2457 | fCodeGen->writeName(name); |
| 2458 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 2459 | void visitSampler(const Type&, skstd::string_view name) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2460 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2461 | fCodeGen->writeName(name); |
| 2462 | } |
John Stiles | fdb8dbe | 2020-12-04 11:00:03 -0500 | [diff] [blame] | 2463 | void visitVariable(const Variable& var, const Expression* value) override { |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2464 | this->addElement(); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2465 | if (value) { |
| 2466 | fCodeGen->writeVarInitializer(var, *value); |
| 2467 | } else { |
| 2468 | fCodeGen->write("{}"); |
| 2469 | } |
| 2470 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2471 | void addElement() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2472 | if (fFirst) { |
John Stiles | f7410bd | 2021-01-19 13:07:55 -0500 | [diff] [blame] | 2473 | fCodeGen->write(" Globals _globals{"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2474 | fFirst = false; |
| 2475 | } else { |
| 2476 | fCodeGen->write(", "); |
| 2477 | } |
| 2478 | } |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2479 | void finish() { |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2480 | if (!fFirst) { |
| 2481 | fCodeGen->writeLine("};"); |
John Stiles | 3727917 | 2021-01-21 22:24:28 -0500 | [diff] [blame] | 2482 | fCodeGen->writeLine(" (void)_globals;"); |
John Stiles | cdcdb04 | 2020-07-06 09:03:51 -0400 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | MetalCodeGenerator* fCodeGen = nullptr; |
| 2486 | bool fFirst = true; |
| 2487 | } visitor; |
| 2488 | |
| 2489 | visitor.fCodeGen = this; |
| 2490 | this->visitGlobalStruct(&visitor); |
John Stiles | 83f3b8d | 2020-12-07 17:52:25 -0500 | [diff] [blame] | 2491 | visitor.finish(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2492 | } |
| 2493 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2494 | void MetalCodeGenerator::writeProgramElement(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2495 | switch (e.kind()) { |
| 2496 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2497 | break; |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2498 | case ProgramElement::Kind::kGlobalVar: |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2499 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2500 | case ProgramElement::Kind::kInterfaceBlock: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2501 | // handled in writeInterfaceBlocks, do nothing |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2502 | break; |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 2503 | case ProgramElement::Kind::kStructDefinition: |
| 2504 | // Handled in writeStructDefinitions. Do nothing. |
| 2505 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2506 | case ProgramElement::Kind::kFunction: |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2507 | this->writeFunction(e.as<FunctionDefinition>()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2508 | break; |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 2509 | case ProgramElement::Kind::kFunctionPrototype: |
| 2510 | this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
| 2511 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2512 | case ProgramElement::Kind::kModifiers: |
Brian Osman | 58134e1 | 2021-05-13 14:51:07 -0400 | [diff] [blame] | 2513 | this->writeModifiers(e.as<ModifiersDeclaration>().modifiers()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2514 | this->writeLine(";"); |
| 2515 | break; |
| 2516 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 2517 | SkDEBUGFAILF("unsupported program element: %s\n", e.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2518 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2522 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Expression* e) { |
| 2523 | if (!e) { |
| 2524 | return kNo_Requirements; |
| 2525 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2526 | switch (e->kind()) { |
| 2527 | case Expression::Kind::kFunctionCall: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2528 | const FunctionCall& f = e->as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 2529 | Requirements result = this->requirements(f.function()); |
| 2530 | for (const auto& arg : f.arguments()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2531 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2532 | } |
| 2533 | return result; |
| 2534 | } |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 2535 | case Expression::Kind::kConstructorCompound: |
| 2536 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 2537 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 2538 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 2539 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 2540 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 2541 | case Expression::Kind::kConstructorSplat: |
| 2542 | case Expression::Kind::kConstructorStruct: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 2543 | const AnyConstructor& c = e->asAnyConstructor(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2544 | Requirements result = kNo_Requirements; |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 2545 | for (const auto& arg : c.argumentSpan()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2546 | result |= this->requirements(arg.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2547 | } |
| 2548 | return result; |
| 2549 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2550 | case Expression::Kind::kFieldAccess: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2551 | const FieldAccess& f = e->as<FieldAccess>(); |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 2552 | if (FieldAccess::OwnerKind::kAnonymousInterfaceBlock == f.ownerKind()) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2553 | return kGlobals_Requirement; |
| 2554 | } |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 2555 | return this->requirements(f.base().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2556 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2557 | case Expression::Kind::kSwizzle: |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2558 | return this->requirements(e->as<Swizzle>().base().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2559 | case Expression::Kind::kBinary: { |
| 2560 | const BinaryExpression& bin = e->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2561 | return this->requirements(bin.left().get()) | |
| 2562 | this->requirements(bin.right().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2563 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2564 | case Expression::Kind::kIndex: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2565 | const IndexExpression& idx = e->as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 2566 | return this->requirements(idx.base().get()) | this->requirements(idx.index().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2567 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2568 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2569 | return this->requirements(e->as<PrefixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2570 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2571 | return this->requirements(e->as<PostfixExpression>().operand().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2572 | case Expression::Kind::kTernary: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2573 | const TernaryExpression& t = e->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2574 | return this->requirements(t.test().get()) | this->requirements(t.ifTrue().get()) | |
| 2575 | this->requirements(t.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2576 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2577 | case Expression::Kind::kVariableReference: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2578 | const VariableReference& v = e->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2579 | const Modifiers& modifiers = v.variable()->modifiers(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2580 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2581 | if (modifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) { |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 2582 | result = kGlobals_Requirement | kFragCoord_Requirement; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 2583 | } else if (Variable::Storage::kGlobal == v.variable()->storage()) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2584 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2585 | result = kInputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2586 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2587 | result = kOutputs_Requirement; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2588 | } else if (modifiers.fFlags & Modifiers::kUniform_Flag && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 2589 | v.variable()->type().typeKind() != Type::TypeKind::kSampler) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2590 | result = kUniforms_Requirement; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 2591 | } else { |
| 2592 | result = kGlobals_Requirement; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | return result; |
| 2596 | } |
| 2597 | default: |
| 2598 | return kNo_Requirements; |
| 2599 | } |
| 2600 | } |
| 2601 | |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2602 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Statement* s) { |
| 2603 | if (!s) { |
| 2604 | return kNo_Requirements; |
| 2605 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2606 | switch (s->kind()) { |
| 2607 | case Statement::Kind::kBlock: { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2608 | Requirements result = kNo_Requirements; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2609 | for (const std::unique_ptr<Statement>& child : s->as<Block>().children()) { |
Ethan Nicholas | ff350cb | 2020-05-14 14:05:13 -0400 | [diff] [blame] | 2610 | result |= this->requirements(child.get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2611 | } |
| 2612 | return result; |
| 2613 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2614 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2615 | const VarDeclaration& var = s->as<VarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2616 | return this->requirements(var.value().get()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 2617 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2618 | case Statement::Kind::kExpression: |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 2619 | return this->requirements(s->as<ExpressionStatement>().expression().get()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2620 | case Statement::Kind::kReturn: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2621 | const ReturnStatement& r = s->as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 2622 | return this->requirements(r.expression().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2623 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2624 | case Statement::Kind::kIf: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2625 | const IfStatement& i = s->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2626 | return this->requirements(i.test().get()) | |
| 2627 | this->requirements(i.ifTrue().get()) | |
| 2628 | this->requirements(i.ifFalse().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2629 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2630 | case Statement::Kind::kFor: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2631 | const ForStatement& f = s->as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2632 | return this->requirements(f.initializer().get()) | |
| 2633 | this->requirements(f.test().get()) | |
| 2634 | this->requirements(f.next().get()) | |
| 2635 | this->requirements(f.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2636 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2637 | case Statement::Kind::kDo: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2638 | const DoStatement& d = s->as<DoStatement>(); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 2639 | return this->requirements(d.test().get()) | |
| 2640 | this->requirements(d.statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2641 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2642 | case Statement::Kind::kSwitch: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 2643 | const SwitchStatement& sw = s->as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 2644 | Requirements result = this->requirements(sw.value().get()); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 2645 | for (const std::unique_ptr<Statement>& sc : sw.cases()) { |
| 2646 | result |= this->requirements(sc->as<SwitchCase>().statement().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2647 | } |
| 2648 | return result; |
| 2649 | } |
| 2650 | default: |
| 2651 | return kNo_Requirements; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const FunctionDeclaration& f) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2656 | if (f.isBuiltin()) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2657 | return kNo_Requirements; |
| 2658 | } |
| 2659 | auto found = fRequirements.find(&f); |
| 2660 | if (found == fRequirements.end()) { |
Ethan Nicholas | 65a8f56 | 2019-04-19 14:00:26 -0400 | [diff] [blame] | 2661 | fRequirements[&f] = kNo_Requirements; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2662 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2663 | if (e->is<FunctionDefinition>()) { |
| 2664 | const FunctionDefinition& def = e->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2665 | if (&def.declaration() == &f) { |
| 2666 | Requirements reqs = this->requirements(def.body().get()); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2667 | fRequirements[&f] = reqs; |
| 2668 | return reqs; |
| 2669 | } |
| 2670 | } |
| 2671 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 2672 | // We never found a definition for this declared function, but it's legal to prototype a |
| 2673 | // function without ever giving a definition, as long as you don't call it. |
| 2674 | return kNo_Requirements; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2675 | } |
| 2676 | return found->second; |
| 2677 | } |
| 2678 | |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 2679 | bool MetalCodeGenerator::generateCode() { |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 2680 | StringStream header; |
| 2681 | { |
| 2682 | AutoOutputStream outputToHeader(this, &header, &fIndentation); |
Michael Ludwig | bf58add | 2021-03-16 10:40:11 -0400 | [diff] [blame] | 2683 | this->writeHeader(); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 2684 | this->writeStructDefinitions(); |
| 2685 | this->writeUniformStruct(); |
| 2686 | this->writeInputStruct(); |
| 2687 | this->writeOutputStruct(); |
| 2688 | this->writeInterfaceBlocks(); |
| 2689 | this->writeGlobalStruct(); |
| 2690 | } |
| 2691 | StringStream body; |
| 2692 | { |
| 2693 | AutoOutputStream outputToBody(this, &body, &fIndentation); |
| 2694 | for (const ProgramElement* e : fProgram.elements()) { |
| 2695 | this->writeProgramElement(*e); |
| 2696 | } |
| 2697 | } |
| 2698 | write_stringstream(header, *fOut); |
John Stiles | 82d4c12 | 2021-08-10 16:03:23 -0400 | [diff] [blame] | 2699 | write_stringstream(fExtraFunctionPrototypes, *fOut); |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 2700 | write_stringstream(fExtraFunctions, *fOut); |
| 2701 | write_stringstream(body, *fOut); |
Ethan Nicholas | 0f36d11 | 2021-08-23 15:27:36 -0400 | [diff] [blame] | 2702 | fContext.fErrors->reportPendingErrors(PositionInfo()); |
Ethan Nicholas | 39f6da4 | 2021-08-23 13:10:07 -0400 | [diff] [blame] | 2703 | return fContext.fErrors->errorCount() == 0; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 2704 | } |
| 2705 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 2706 | } // namespace SkSL |