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