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