Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/SkSLCompiler.h" |
| 11 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 12 | #include "src/sksl/ir/SkSLExtension.h" |
| 13 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 14 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 15 | #include "src/sksl/ir/SkSLNop.h" |
| 16 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 17 | |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 18 | #ifdef SK_MOLTENVK |
| 19 | static const uint32_t MVKMagicNum = 0x19960412; |
| 20 | #endif |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 21 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 24 | void MetalCodeGenerator::setupIntrinsics() { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 25 | #define METAL(x) std::make_pair(kMetal_IntrinsicKind, k ## x ## _MetalIntrinsic) |
| 26 | #define SPECIAL(x) std::make_pair(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic) |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 27 | fIntrinsicMap[String("texture")] = SPECIAL(Texture); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 28 | fIntrinsicMap[String("mod")] = SPECIAL(Mod); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 29 | fIntrinsicMap[String("equal")] = METAL(Equal); |
| 30 | fIntrinsicMap[String("notEqual")] = METAL(NotEqual); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 31 | fIntrinsicMap[String("lessThan")] = METAL(LessThan); |
| 32 | fIntrinsicMap[String("lessThanEqual")] = METAL(LessThanEqual); |
| 33 | fIntrinsicMap[String("greaterThan")] = METAL(GreaterThan); |
| 34 | fIntrinsicMap[String("greaterThanEqual")] = METAL(GreaterThanEqual); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 37 | void MetalCodeGenerator::write(const char* s) { |
| 38 | if (!s[0]) { |
| 39 | return; |
| 40 | } |
| 41 | if (fAtLineStart) { |
| 42 | for (int i = 0; i < fIndentation; i++) { |
| 43 | fOut->writeText(" "); |
| 44 | } |
| 45 | } |
| 46 | fOut->writeText(s); |
| 47 | fAtLineStart = false; |
| 48 | } |
| 49 | |
| 50 | void MetalCodeGenerator::writeLine(const char* s) { |
| 51 | this->write(s); |
| 52 | fOut->writeText(fLineEnding); |
| 53 | fAtLineStart = true; |
| 54 | } |
| 55 | |
| 56 | void MetalCodeGenerator::write(const String& s) { |
| 57 | this->write(s.c_str()); |
| 58 | } |
| 59 | |
| 60 | void MetalCodeGenerator::writeLine(const String& s) { |
| 61 | this->writeLine(s.c_str()); |
| 62 | } |
| 63 | |
| 64 | void MetalCodeGenerator::writeLine() { |
| 65 | this->writeLine(""); |
| 66 | } |
| 67 | |
| 68 | void MetalCodeGenerator::writeExtension(const Extension& ext) { |
| 69 | this->writeLine("#extension " + ext.fName + " : enable"); |
| 70 | } |
| 71 | |
| 72 | void MetalCodeGenerator::writeType(const Type& type) { |
| 73 | switch (type.kind()) { |
| 74 | case Type::kStruct_Kind: |
| 75 | for (const Type* search : fWrittenStructs) { |
| 76 | if (*search == type) { |
| 77 | // already written |
| 78 | this->write(type.name()); |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | fWrittenStructs.push_back(&type); |
| 83 | this->writeLine("struct " + type.name() + " {"); |
| 84 | fIndentation++; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 85 | this->writeFields(type.fields(), type.fOffset); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 86 | fIndentation--; |
| 87 | this->write("}"); |
| 88 | break; |
| 89 | case Type::kVector_Kind: |
| 90 | this->writeType(type.componentType()); |
| 91 | this->write(to_string(type.columns())); |
| 92 | break; |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 93 | case Type::kMatrix_Kind: |
| 94 | this->writeType(type.componentType()); |
| 95 | this->write(to_string(type.columns())); |
| 96 | this->write("x"); |
| 97 | this->write(to_string(type.rows())); |
| 98 | break; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 99 | case Type::kSampler_Kind: |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 100 | this->write("texture2d<float> "); // FIXME - support other texture types; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 101 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 102 | default: |
Timothy Liang | 43d225f | 2018-07-19 15:27:13 -0400 | [diff] [blame] | 103 | if (type == *fContext.fHalf_Type) { |
| 104 | // FIXME - Currently only supporting floats in MSL to avoid type coercion issues. |
| 105 | this->write(fContext.fFloat_Type->name()); |
| 106 | } else if (type == *fContext.fByte_Type) { |
| 107 | this->write("char"); |
| 108 | } else if (type == *fContext.fUByte_Type) { |
| 109 | this->write("uchar"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 110 | } else { |
| 111 | this->write(type.name()); |
| 112 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void MetalCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
| 117 | switch (expr.fKind) { |
| 118 | case Expression::kBinary_Kind: |
| 119 | this->writeBinaryExpression((BinaryExpression&) expr, parentPrecedence); |
| 120 | break; |
| 121 | case Expression::kBoolLiteral_Kind: |
| 122 | this->writeBoolLiteral((BoolLiteral&) expr); |
| 123 | break; |
| 124 | case Expression::kConstructor_Kind: |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 125 | this->writeConstructor((Constructor&) expr, parentPrecedence); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 126 | break; |
| 127 | case Expression::kIntLiteral_Kind: |
| 128 | this->writeIntLiteral((IntLiteral&) expr); |
| 129 | break; |
| 130 | case Expression::kFieldAccess_Kind: |
| 131 | this->writeFieldAccess(((FieldAccess&) expr)); |
| 132 | break; |
| 133 | case Expression::kFloatLiteral_Kind: |
| 134 | this->writeFloatLiteral(((FloatLiteral&) expr)); |
| 135 | break; |
| 136 | case Expression::kFunctionCall_Kind: |
| 137 | this->writeFunctionCall((FunctionCall&) expr); |
| 138 | break; |
| 139 | case Expression::kPrefix_Kind: |
| 140 | this->writePrefixExpression((PrefixExpression&) expr, parentPrecedence); |
| 141 | break; |
| 142 | case Expression::kPostfix_Kind: |
| 143 | this->writePostfixExpression((PostfixExpression&) expr, parentPrecedence); |
| 144 | break; |
| 145 | case Expression::kSetting_Kind: |
| 146 | this->writeSetting((Setting&) expr); |
| 147 | break; |
| 148 | case Expression::kSwizzle_Kind: |
| 149 | this->writeSwizzle((Swizzle&) expr); |
| 150 | break; |
| 151 | case Expression::kVariableReference_Kind: |
| 152 | this->writeVariableReference((VariableReference&) expr); |
| 153 | break; |
| 154 | case Expression::kTernary_Kind: |
| 155 | this->writeTernaryExpression((TernaryExpression&) expr, parentPrecedence); |
| 156 | break; |
| 157 | case Expression::kIndex_Kind: |
| 158 | this->writeIndexExpression((IndexExpression&) expr); |
| 159 | break; |
| 160 | default: |
| 161 | ABORT("unsupported expression: %s", expr.description().c_str()); |
| 162 | } |
| 163 | } |
| 164 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 165 | void MetalCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 166 | auto i = fIntrinsicMap.find(c.fFunction.fName); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 167 | SkASSERT(i != fIntrinsicMap.end()); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 168 | Intrinsic intrinsic = i->second; |
| 169 | int32_t intrinsicId = intrinsic.second; |
| 170 | switch (intrinsic.first) { |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 171 | case kSpecial_IntrinsicKind: |
| 172 | return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 173 | break; |
| 174 | case kMetal_IntrinsicKind: |
| 175 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 176 | switch ((MetalIntrinsic) intrinsicId) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 177 | case kEqual_MetalIntrinsic: |
| 178 | this->write(" == "); |
| 179 | break; |
| 180 | case kNotEqual_MetalIntrinsic: |
| 181 | this->write(" != "); |
| 182 | break; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 183 | case kLessThan_MetalIntrinsic: |
| 184 | this->write(" < "); |
| 185 | break; |
| 186 | case kLessThanEqual_MetalIntrinsic: |
| 187 | this->write(" <= "); |
| 188 | break; |
| 189 | case kGreaterThan_MetalIntrinsic: |
| 190 | this->write(" > "); |
| 191 | break; |
| 192 | case kGreaterThanEqual_MetalIntrinsic: |
| 193 | this->write(" >= "); |
| 194 | break; |
| 195 | default: |
| 196 | ABORT("unsupported metal intrinsic kind"); |
| 197 | } |
| 198 | this->writeExpression(*c.fArguments[1], kSequence_Precedence); |
| 199 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 200 | default: |
| 201 | ABORT("unsupported intrinsic kind"); |
| 202 | } |
| 203 | } |
| 204 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 205 | void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 206 | const auto& entry = fIntrinsicMap.find(c.fFunction.fName); |
| 207 | if (entry != fIntrinsicMap.end()) { |
| 208 | this->writeIntrinsicCall(c); |
| 209 | return; |
| 210 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 211 | if (c.fFunction.fBuiltin && "atan" == c.fFunction.fName && 2 == c.fArguments.size()) { |
| 212 | this->write("atan2"); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 213 | } else if (c.fFunction.fBuiltin && "inversesqrt" == c.fFunction.fName) { |
| 214 | this->write("rsqrt"); |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 215 | } else if (c.fFunction.fBuiltin && "inverse" == c.fFunction.fName) { |
| 216 | SkASSERT(c.fArguments.size() == 1); |
| 217 | this->writeInverseHack(*c.fArguments[0]); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 218 | } else if (c.fFunction.fBuiltin && "dFdx" == c.fFunction.fName) { |
| 219 | this->write("dfdx"); |
| 220 | } else if (c.fFunction.fBuiltin && "dFdy" == c.fFunction.fName) { |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 221 | // Flipping Y also negates the Y derivatives. |
| 222 | this->write((fProgram.fSettings.fFlipY) ? "-dfdy" : "dfdy"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 223 | } else { |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 224 | this->writeName(c.fFunction.fName); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 225 | } |
| 226 | this->write("("); |
| 227 | const char* separator = ""; |
| 228 | if (this->requirements(c.fFunction) & kInputs_Requirement) { |
| 229 | this->write("_in"); |
| 230 | separator = ", "; |
| 231 | } |
| 232 | if (this->requirements(c.fFunction) & kOutputs_Requirement) { |
| 233 | this->write(separator); |
| 234 | this->write("_out"); |
| 235 | separator = ", "; |
| 236 | } |
| 237 | if (this->requirements(c.fFunction) & kUniforms_Requirement) { |
| 238 | this->write(separator); |
| 239 | this->write("_uniforms"); |
| 240 | separator = ", "; |
| 241 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 242 | if (this->requirements(c.fFunction) & kGlobals_Requirement) { |
| 243 | this->write(separator); |
| 244 | this->write("_globals"); |
| 245 | separator = ", "; |
| 246 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 247 | for (size_t i = 0; i < c.fArguments.size(); ++i) { |
| 248 | const Expression& arg = *c.fArguments[i]; |
| 249 | this->write(separator); |
| 250 | separator = ", "; |
| 251 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 252 | this->write("&"); |
| 253 | } |
| 254 | this->writeExpression(arg, kSequence_Precedence); |
| 255 | } |
| 256 | this->write(")"); |
| 257 | } |
| 258 | |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 259 | void MetalCodeGenerator::writeInverseHack(const Expression& mat) { |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 260 | String typeName = mat.fType.name(); |
| 261 | String name = typeName + "_inverse"; |
| 262 | if (mat.fType == *fContext.fFloat2x2_Type || mat.fType == *fContext.fHalf2x2_Type) { |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 263 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 264 | fWrittenIntrinsics.insert(name); |
| 265 | fExtraFunctions.writeText(( |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 266 | typeName + " " + name + "(" + typeName + " m) {" |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 267 | " return float2x2(m[1][1], -m[0][1], -m[1][0], m[0][0]) * (1/determinant(m));" |
| 268 | "}" |
| 269 | ).c_str()); |
| 270 | } |
| 271 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 272 | else if (mat.fType == *fContext.fFloat3x3_Type || mat.fType == *fContext.fHalf3x3_Type) { |
| 273 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 274 | fWrittenIntrinsics.insert(name); |
| 275 | fExtraFunctions.writeText(( |
| 276 | typeName + " " + name + "(" + typeName + " m) {" |
| 277 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];" |
| 278 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];" |
| 279 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];" |
| 280 | " float b01 = a22 * a11 - a12 * a21;" |
| 281 | " float b11 = -a22 * a10 + a12 * a20;" |
| 282 | " float b21 = a21 * a10 - a11 * a20;" |
| 283 | " float det = a00 * b01 + a01 * b11 + a02 * b21;" |
| 284 | " return " + typeName + |
| 285 | " (b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11)," |
| 286 | " b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10)," |
| 287 | " b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) * " |
| 288 | " (1/det);" |
| 289 | "}" |
| 290 | ).c_str()); |
| 291 | } |
| 292 | } |
| 293 | else if (mat.fType == *fContext.fFloat4x4_Type || mat.fType == *fContext.fHalf4x4_Type) { |
| 294 | if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) { |
| 295 | fWrittenIntrinsics.insert(name); |
| 296 | fExtraFunctions.writeText(( |
| 297 | typeName + " " + name + "(" + typeName + " m) {" |
| 298 | " float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3];" |
| 299 | " float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3];" |
| 300 | " float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3];" |
| 301 | " float a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3];" |
| 302 | " float b00 = a00 * a11 - a01 * a10;" |
| 303 | " float b01 = a00 * a12 - a02 * a10;" |
| 304 | " float b02 = a00 * a13 - a03 * a10;" |
| 305 | " float b03 = a01 * a12 - a02 * a11;" |
| 306 | " float b04 = a01 * a13 - a03 * a11;" |
| 307 | " float b05 = a02 * a13 - a03 * a12;" |
| 308 | " float b06 = a20 * a31 - a21 * a30;" |
| 309 | " float b07 = a20 * a32 - a22 * a30;" |
| 310 | " float b08 = a20 * a33 - a23 * a30;" |
| 311 | " float b09 = a21 * a32 - a22 * a31;" |
| 312 | " float b10 = a21 * a33 - a23 * a31;" |
| 313 | " float b11 = a22 * a33 - a23 * a32;" |
| 314 | " float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - " |
| 315 | " b04 * b07 + b05 * b06;" |
| 316 | " return " + typeName + "(a11 * b11 - a12 * b10 + a13 * b09," |
| 317 | " a02 * b10 - a01 * b11 - a03 * b09," |
| 318 | " a31 * b05 - a32 * b04 + a33 * b03," |
| 319 | " a22 * b04 - a21 * b05 - a23 * b03," |
| 320 | " a12 * b08 - a10 * b11 - a13 * b07," |
| 321 | " a00 * b11 - a02 * b08 + a03 * b07," |
| 322 | " a32 * b02 - a30 * b05 - a33 * b01," |
| 323 | " a20 * b05 - a22 * b02 + a23 * b01," |
| 324 | " a10 * b10 - a11 * b08 + a13 * b06," |
| 325 | " a01 * b08 - a00 * b10 - a03 * b06," |
| 326 | " a30 * b04 - a31 * b02 + a33 * b00," |
| 327 | " a21 * b02 - a20 * b04 - a23 * b00," |
| 328 | " a11 * b07 - a10 * b09 - a12 * b06," |
| 329 | " a00 * b09 - a01 * b07 + a02 * b06," |
| 330 | " a31 * b01 - a30 * b03 - a32 * b00," |
| 331 | " a20 * b03 - a21 * b01 + a22 * b00) / det;" |
| 332 | "}" |
| 333 | ).c_str()); |
| 334 | } |
| 335 | } |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 336 | this->write(name); |
| 337 | } |
| 338 | |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 339 | void MetalCodeGenerator::writeSpecialIntrinsic(const FunctionCall & c, SpecialIntrinsic kind) { |
| 340 | switch (kind) { |
| 341 | case kTexture_SpecialIntrinsic: |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 342 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 343 | this->write(".sample("); |
| 344 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 345 | this->write(SAMPLER_SUFFIX); |
| 346 | this->write(", "); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 347 | this->writeExpression(*c.fArguments[1], kSequence_Precedence); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 348 | if (c.fArguments[1]->fType == *fContext.fFloat3_Type) { |
| 349 | this->write(".xy)"); // FIXME - add projection functionality |
| 350 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 351 | SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 352 | this->write(")"); |
| 353 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 354 | break; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 355 | case kMod_SpecialIntrinsic: |
| 356 | // fmod(x, y) in metal calculates x - y * trunc(x / y) instead of x - y * floor(x / y) |
| 357 | this->write("(("); |
| 358 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 359 | this->write(") - ("); |
| 360 | this->writeExpression(*c.fArguments[1], kSequence_Precedence); |
| 361 | this->write(") * floor(("); |
| 362 | this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
| 363 | this->write(") / ("); |
| 364 | this->writeExpression(*c.fArguments[1], kSequence_Precedence); |
| 365 | this->write(")))"); |
| 366 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 367 | default: |
| 368 | ABORT("unsupported special intrinsic kind"); |
| 369 | } |
| 370 | } |
| 371 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 372 | // If it hasn't already been written, writes a constructor for 'matrix' which takes a single value |
| 373 | // of type 'arg'. |
| 374 | String MetalCodeGenerator::getMatrixConstructHelper(const Type& matrix, const Type& arg) { |
| 375 | String key = matrix.name() + arg.name(); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 376 | auto found = fHelpers.find(key); |
| 377 | if (found != fHelpers.end()) { |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 378 | return found->second; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 379 | } |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 380 | String name; |
| 381 | int columns = matrix.columns(); |
| 382 | int rows = matrix.rows(); |
| 383 | if (arg.isNumber()) { |
| 384 | // creating a matrix from a single scalar value |
| 385 | name = "float" + to_string(columns) + "x" + to_string(rows) + "_from_float"; |
| 386 | fExtraFunctions.printf("float%dx%d %s(float x) {\n", |
| 387 | columns, rows, name.c_str()); |
| 388 | fExtraFunctions.printf(" return float%dx%d(", columns, rows); |
| 389 | for (int i = 0; i < columns; ++i) { |
| 390 | if (i > 0) { |
| 391 | fExtraFunctions.writeText(", "); |
| 392 | } |
| 393 | fExtraFunctions.printf("float%d(", rows); |
| 394 | for (int j = 0; j < rows; ++j) { |
| 395 | if (j > 0) { |
| 396 | fExtraFunctions.writeText(", "); |
| 397 | } |
| 398 | if (i == j) { |
| 399 | fExtraFunctions.writeText("x"); |
| 400 | } else { |
| 401 | fExtraFunctions.writeText("0"); |
| 402 | } |
| 403 | } |
| 404 | fExtraFunctions.writeText(")"); |
| 405 | } |
| 406 | fExtraFunctions.writeText(");\n}\n"); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 407 | } else if (arg.kind() == Type::kMatrix_Kind) { |
| 408 | // creating a matrix from another matrix |
| 409 | int argColumns = arg.columns(); |
| 410 | int argRows = arg.rows(); |
| 411 | name = "float" + to_string(columns) + "x" + to_string(rows) + "_from_float" + |
| 412 | to_string(argColumns) + "x" + to_string(argRows); |
| 413 | fExtraFunctions.printf("float%dx%d %s(float%dx%d m) {\n", |
| 414 | columns, rows, name.c_str(), argColumns, argRows); |
| 415 | fExtraFunctions.printf(" return float%dx%d(", columns, rows); |
| 416 | for (int i = 0; i < columns; ++i) { |
| 417 | if (i > 0) { |
| 418 | fExtraFunctions.writeText(", "); |
| 419 | } |
| 420 | fExtraFunctions.printf("float%d(", rows); |
| 421 | for (int j = 0; j < rows; ++j) { |
| 422 | if (j > 0) { |
| 423 | fExtraFunctions.writeText(", "); |
| 424 | } |
| 425 | if (i < argColumns && j < argRows) { |
| 426 | fExtraFunctions.printf("m[%d][%d]", i, j); |
| 427 | } else { |
| 428 | fExtraFunctions.writeText("0"); |
| 429 | } |
| 430 | } |
| 431 | fExtraFunctions.writeText(")"); |
| 432 | } |
| 433 | fExtraFunctions.writeText(");\n}\n"); |
| 434 | } else if (matrix.rows() == 2 && matrix.columns() == 2 && arg == *fContext.fFloat4_Type) { |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 435 | // float2x2(float4) doesn't work, need to split it into float2x2(float2, float2) |
| 436 | name = "float2x2_from_float4"; |
| 437 | fExtraFunctions.printf( |
| 438 | "float2x2 %s(float4 v) {\n" |
| 439 | " return float2x2(float2(v[0], v[1]), float2(v[2], v[3]));\n" |
| 440 | "}\n", |
| 441 | name.c_str() |
| 442 | ); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 443 | } else { |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 444 | SkASSERT(false); |
| 445 | name = "<error>"; |
| 446 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 447 | fHelpers[key] = name; |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 448 | return name; |
| 449 | } |
| 450 | |
| 451 | bool MetalCodeGenerator::canCoerce(const Type& t1, const Type& t2) { |
| 452 | if (t1.columns() != t2.columns() || t1.rows() != t2.rows()) { |
| 453 | return false; |
| 454 | } |
| 455 | if (t1.columns() > 1) { |
| 456 | return this->canCoerce(t1.componentType(), t2.componentType()); |
| 457 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 458 | return t1.isFloat() && t2.isFloat(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void MetalCodeGenerator::writeConstructor(const Constructor& c, Precedence parentPrecedence) { |
| 462 | if (c.fArguments.size() == 1 && this->canCoerce(c.fType, c.fArguments[0]->fType)) { |
| 463 | this->writeExpression(*c.fArguments[0], parentPrecedence); |
| 464 | return; |
| 465 | } |
| 466 | if (c.fType.kind() == Type::kMatrix_Kind && c.fArguments.size() == 1) { |
| 467 | const Expression& arg = *c.fArguments[0]; |
| 468 | String name = this->getMatrixConstructHelper(c.fType, arg.fType); |
| 469 | this->write(name); |
| 470 | this->write("("); |
| 471 | this->writeExpression(arg, kSequence_Precedence); |
| 472 | this->write(")"); |
| 473 | } else { |
| 474 | this->writeType(c.fType); |
| 475 | this->write("("); |
| 476 | const char* separator = ""; |
| 477 | int scalarCount = 0; |
| 478 | for (const auto& arg : c.fArguments) { |
| 479 | this->write(separator); |
| 480 | separator = ", "; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 481 | if (Type::kMatrix_Kind == c.fType.kind() && arg->fType.columns() != c.fType.rows()) { |
| 482 | // merge scalars and smaller vectors together |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 483 | if (!scalarCount) { |
| 484 | this->writeType(c.fType.componentType()); |
| 485 | this->write(to_string(c.fType.rows())); |
| 486 | this->write("("); |
| 487 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 488 | scalarCount += arg->fType.columns(); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 489 | } |
| 490 | this->writeExpression(*arg, kSequence_Precedence); |
| 491 | if (scalarCount && scalarCount == c.fType.rows()) { |
| 492 | this->write(")"); |
| 493 | scalarCount = 0; |
| 494 | } |
| 495 | } |
| 496 | this->write(")"); |
| 497 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void MetalCodeGenerator::writeFragCoord() { |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 501 | if (fProgram.fInputs.fRTHeight) { |
| 502 | this->write("float4(_fragCoord.x, _anonInterface0.u_skRTHeight - _fragCoord.y, 0.0, " |
| 503 | "_fragCoord.w)"); |
| 504 | } else { |
| 505 | this->write("float4(_fragCoord.x, _fragCoord.y, 0.0, _fragCoord.w)"); |
| 506 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void MetalCodeGenerator::writeVariableReference(const VariableReference& ref) { |
| 510 | switch (ref.fVariable.fModifiers.fLayout.fBuiltin) { |
| 511 | case SK_FRAGCOLOR_BUILTIN: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 512 | this->write("_out->sk_FragColor"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 513 | break; |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 514 | case SK_FRAGCOORD_BUILTIN: |
| 515 | this->writeFragCoord(); |
| 516 | break; |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 517 | case SK_VERTEXID_BUILTIN: |
| 518 | this->write("sk_VertexID"); |
| 519 | break; |
| 520 | case SK_INSTANCEID_BUILTIN: |
| 521 | this->write("sk_InstanceID"); |
| 522 | break; |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 523 | case SK_CLOCKWISE_BUILTIN: |
| 524 | // We'd set the front facing winding in the MTLRenderCommandEncoder to be counter |
| 525 | // clockwise to match Skia convention. This is also the default in MoltenVK. |
| 526 | this->write(fProgram.fSettings.fFlipY ? "_frontFacing" : "(!_frontFacing)"); |
| 527 | break; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 528 | default: |
| 529 | if (Variable::kGlobal_Storage == ref.fVariable.fStorage) { |
| 530 | if (ref.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) { |
| 531 | this->write("_in."); |
| 532 | } else if (ref.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 533 | this->write("_out->"); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 534 | } else if (ref.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && |
| 535 | ref.fVariable.fType.kind() != Type::kSampler_Kind) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 536 | this->write("_uniforms."); |
| 537 | } else { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 538 | this->write("_globals->"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 539 | } |
| 540 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 541 | this->writeName(ref.fVariable.fName); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | void MetalCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
| 546 | this->writeExpression(*expr.fBase, kPostfix_Precedence); |
| 547 | this->write("["); |
| 548 | this->writeExpression(*expr.fIndex, kTopLevel_Precedence); |
| 549 | this->write("]"); |
| 550 | } |
| 551 | |
| 552 | void MetalCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 553 | const Type::Field* field = &f.fBase->fType.fields()[f.fFieldIndex]; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 554 | if (FieldAccess::kDefault_OwnerKind == f.fOwnerKind) { |
| 555 | this->writeExpression(*f.fBase, kPostfix_Precedence); |
| 556 | this->write("."); |
| 557 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 558 | switch (field->fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 559 | case SK_CLIPDISTANCE_BUILTIN: |
| 560 | this->write("gl_ClipDistance"); |
| 561 | break; |
| 562 | case SK_POSITION_BUILTIN: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 563 | this->write("_out->sk_Position"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 564 | break; |
| 565 | default: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 566 | if (field->fName == "sk_PointSize") { |
| 567 | this->write("_out->sk_PointSize"); |
| 568 | } else { |
| 569 | if (FieldAccess::kAnonymousInterfaceBlock_OwnerKind == f.fOwnerKind) { |
| 570 | this->write("_globals->"); |
| 571 | this->write(fInterfaceBlockNameMap[fInterfaceBlockMap[field]]); |
| 572 | this->write("->"); |
| 573 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 574 | this->writeName(field->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 575 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | void MetalCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 580 | int last = swizzle.fComponents.back(); |
| 581 | if (last == SKSL_SWIZZLE_0 || last == SKSL_SWIZZLE_1) { |
| 582 | this->writeType(swizzle.fType); |
| 583 | this->write("("); |
| 584 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 585 | this->writeExpression(*swizzle.fBase, kPostfix_Precedence); |
| 586 | this->write("."); |
| 587 | for (int c : swizzle.fComponents) { |
Ethan Nicholas | 5476f2e | 2019-03-07 15:11:31 -0500 | [diff] [blame] | 588 | if (c >= 0) { |
| 589 | this->write(&("x\0y\0z\0w\0"[c * 2])); |
| 590 | } |
| 591 | } |
| 592 | if (last == SKSL_SWIZZLE_0) { |
| 593 | this->write(", 0)"); |
| 594 | } |
| 595 | else if (last == SKSL_SWIZZLE_1) { |
| 596 | this->write(", 1)"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | MetalCodeGenerator::Precedence MetalCodeGenerator::GetBinaryPrecedence(Token::Kind op) { |
| 601 | switch (op) { |
| 602 | case Token::STAR: // fall through |
| 603 | case Token::SLASH: // fall through |
| 604 | case Token::PERCENT: return MetalCodeGenerator::kMultiplicative_Precedence; |
| 605 | case Token::PLUS: // fall through |
| 606 | case Token::MINUS: return MetalCodeGenerator::kAdditive_Precedence; |
| 607 | case Token::SHL: // fall through |
| 608 | case Token::SHR: return MetalCodeGenerator::kShift_Precedence; |
| 609 | case Token::LT: // fall through |
| 610 | case Token::GT: // fall through |
| 611 | case Token::LTEQ: // fall through |
| 612 | case Token::GTEQ: return MetalCodeGenerator::kRelational_Precedence; |
| 613 | case Token::EQEQ: // fall through |
| 614 | case Token::NEQ: return MetalCodeGenerator::kEquality_Precedence; |
| 615 | case Token::BITWISEAND: return MetalCodeGenerator::kBitwiseAnd_Precedence; |
| 616 | case Token::BITWISEXOR: return MetalCodeGenerator::kBitwiseXor_Precedence; |
| 617 | case Token::BITWISEOR: return MetalCodeGenerator::kBitwiseOr_Precedence; |
| 618 | case Token::LOGICALAND: return MetalCodeGenerator::kLogicalAnd_Precedence; |
| 619 | case Token::LOGICALXOR: return MetalCodeGenerator::kLogicalXor_Precedence; |
| 620 | case Token::LOGICALOR: return MetalCodeGenerator::kLogicalOr_Precedence; |
| 621 | case Token::EQ: // fall through |
| 622 | case Token::PLUSEQ: // fall through |
| 623 | case Token::MINUSEQ: // fall through |
| 624 | case Token::STAREQ: // fall through |
| 625 | case Token::SLASHEQ: // fall through |
| 626 | case Token::PERCENTEQ: // fall through |
| 627 | case Token::SHLEQ: // fall through |
| 628 | case Token::SHREQ: // fall through |
| 629 | case Token::LOGICALANDEQ: // fall through |
| 630 | case Token::LOGICALXOREQ: // fall through |
| 631 | case Token::LOGICALOREQ: // fall through |
| 632 | case Token::BITWISEANDEQ: // fall through |
| 633 | case Token::BITWISEXOREQ: // fall through |
| 634 | case Token::BITWISEOREQ: return MetalCodeGenerator::kAssignment_Precedence; |
| 635 | case Token::COMMA: return MetalCodeGenerator::kSequence_Precedence; |
| 636 | default: ABORT("unsupported binary operator"); |
| 637 | } |
| 638 | } |
| 639 | |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 640 | void MetalCodeGenerator::writeMatrixTimesEqualHelper(const Type& left, const Type& right, |
| 641 | const Type& result) { |
| 642 | String key = "TimesEqual" + left.name() + right.name(); |
| 643 | if (fHelpers.find(key) == fHelpers.end()) { |
| 644 | fExtraFunctions.printf("%s operator*=(thread %s& left, thread const %s& right) {\n" |
| 645 | " left = left * right;\n" |
| 646 | " return left;\n" |
| 647 | "}", result.name().c_str(), left.name().c_str(), |
| 648 | right.name().c_str()); |
| 649 | } |
| 650 | } |
| 651 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 652 | void MetalCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 653 | Precedence parentPrecedence) { |
| 654 | Precedence precedence = GetBinaryPrecedence(b.fOperator); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 655 | bool needParens = precedence >= parentPrecedence; |
| 656 | switch (b.fOperator) { |
| 657 | case Token::EQEQ: |
| 658 | if (b.fLeft->fType.kind() == Type::kVector_Kind) { |
| 659 | this->write("all"); |
| 660 | needParens = true; |
| 661 | } |
| 662 | break; |
| 663 | case Token::NEQ: |
| 664 | if (b.fLeft->fType.kind() == Type::kVector_Kind) { |
Jim Van Verth | 36477b4 | 2019-04-11 14:57:30 -0400 | [diff] [blame] | 665 | this->write("any"); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 666 | needParens = true; |
| 667 | } |
| 668 | break; |
| 669 | default: |
| 670 | break; |
| 671 | } |
| 672 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 673 | this->write("("); |
| 674 | } |
| 675 | if (Compiler::IsAssignment(b.fOperator) && |
| 676 | Expression::kVariableReference_Kind == b.fLeft->fKind && |
| 677 | Variable::kParameter_Storage == ((VariableReference&) *b.fLeft).fVariable.fStorage && |
| 678 | (((VariableReference&) *b.fLeft).fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) { |
| 679 | // writing to an out parameter. Since we have to turn those into pointers, we have to |
| 680 | // dereference it here. |
| 681 | this->write("*"); |
| 682 | } |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 683 | if (b.fOperator == Token::STAREQ && b.fLeft->fType.kind() == Type::kMatrix_Kind && |
| 684 | b.fRight->fType.kind() == Type::kMatrix_Kind) { |
| 685 | this->writeMatrixTimesEqualHelper(b.fLeft->fType, b.fRight->fType, b.fType); |
| 686 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 687 | this->writeExpression(*b.fLeft, precedence); |
| 688 | if (b.fOperator != Token::EQ && Compiler::IsAssignment(b.fOperator) && |
| 689 | Expression::kSwizzle_Kind == b.fLeft->fKind && !b.fLeft->hasSideEffects()) { |
| 690 | // This doesn't compile in Metal: |
| 691 | // float4 x = float4(1); |
| 692 | // x.xy *= float2x2(...); |
| 693 | // with the error message "non-const reference cannot bind to vector element", |
| 694 | // but switching it to x.xy = x.xy * float2x2(...) fixes it. We perform this tranformation |
| 695 | // as long as the LHS has no side effects, and hope for the best otherwise. |
| 696 | this->write(" = "); |
| 697 | this->writeExpression(*b.fLeft, kAssignment_Precedence); |
| 698 | this->write(" "); |
| 699 | String op = Compiler::OperatorName(b.fOperator); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 700 | SkASSERT(op.endsWith("=")); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 701 | this->write(op.substr(0, op.size() - 1).c_str()); |
| 702 | this->write(" "); |
| 703 | } else { |
| 704 | this->write(String(" ") + Compiler::OperatorName(b.fOperator) + " "); |
| 705 | } |
| 706 | this->writeExpression(*b.fRight, precedence); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 707 | if (needParens) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 708 | this->write(")"); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | void MetalCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 713 | Precedence parentPrecedence) { |
| 714 | if (kTernary_Precedence >= parentPrecedence) { |
| 715 | this->write("("); |
| 716 | } |
| 717 | this->writeExpression(*t.fTest, kTernary_Precedence); |
| 718 | this->write(" ? "); |
| 719 | this->writeExpression(*t.fIfTrue, kTernary_Precedence); |
| 720 | this->write(" : "); |
| 721 | this->writeExpression(*t.fIfFalse, kTernary_Precedence); |
| 722 | if (kTernary_Precedence >= parentPrecedence) { |
| 723 | this->write(")"); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void MetalCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 728 | Precedence parentPrecedence) { |
| 729 | if (kPrefix_Precedence >= parentPrecedence) { |
| 730 | this->write("("); |
| 731 | } |
| 732 | this->write(Compiler::OperatorName(p.fOperator)); |
| 733 | this->writeExpression(*p.fOperand, kPrefix_Precedence); |
| 734 | if (kPrefix_Precedence >= parentPrecedence) { |
| 735 | this->write(")"); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void MetalCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 740 | Precedence parentPrecedence) { |
| 741 | if (kPostfix_Precedence >= parentPrecedence) { |
| 742 | this->write("("); |
| 743 | } |
| 744 | this->writeExpression(*p.fOperand, kPostfix_Precedence); |
| 745 | this->write(Compiler::OperatorName(p.fOperator)); |
| 746 | if (kPostfix_Precedence >= parentPrecedence) { |
| 747 | this->write(")"); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | void MetalCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 752 | this->write(b.fValue ? "true" : "false"); |
| 753 | } |
| 754 | |
| 755 | void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 756 | if (i.fType == *fContext.fUInt_Type) { |
| 757 | this->write(to_string(i.fValue & 0xffffffff) + "u"); |
| 758 | } else { |
| 759 | this->write(to_string((int32_t) i.fValue)); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | void MetalCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 764 | this->write(to_string(f.fValue)); |
| 765 | } |
| 766 | |
| 767 | void MetalCodeGenerator::writeSetting(const Setting& s) { |
| 768 | ABORT("internal error; setting was not folded to a constant during compilation\n"); |
| 769 | } |
| 770 | |
| 771 | void MetalCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 772 | const char* separator = ""; |
| 773 | if ("main" == f.fDeclaration.fName) { |
| 774 | switch (fProgram.fKind) { |
| 775 | case Program::kFragment_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 776 | #ifdef SK_MOLTENVK |
| 777 | this->write("fragment Outputs main0"); |
| 778 | #else |
| 779 | this->write("fragment Outputs fragmentMain"); |
| 780 | #endif |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 781 | break; |
| 782 | case Program::kVertex_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 783 | #ifdef SK_MOLTENVK |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 784 | this->write("vertex Outputs main0"); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 785 | #else |
| 786 | this->write("vertex Outputs vertexMain"); |
| 787 | #endif |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 788 | break; |
| 789 | default: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 790 | SkASSERT(false); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 791 | } |
| 792 | this->write("(Inputs _in [[stage_in]]"); |
| 793 | if (-1 != fUniformBuffer) { |
| 794 | this->write(", constant Uniforms& _uniforms [[buffer(" + |
| 795 | to_string(fUniformBuffer) + ")]]"); |
| 796 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 797 | for (const auto& e : fProgram) { |
| 798 | if (ProgramElement::kVar_Kind == e.fKind) { |
| 799 | VarDeclarations& decls = (VarDeclarations&) e; |
| 800 | if (!decls.fVars.size()) { |
| 801 | continue; |
| 802 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 803 | for (const auto& stmt: decls.fVars) { |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 804 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 805 | if (var.fVar->fType.kind() == Type::kSampler_Kind) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 806 | this->write(", texture2d<float> "); // FIXME - support other texture types |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 807 | this->writeName(var.fVar->fName); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 808 | this->write("[[texture("); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 809 | this->write(to_string(var.fVar->fModifiers.fLayout.fBinding)); |
| 810 | this->write(")]]"); |
| 811 | this->write(", sampler "); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 812 | this->writeName(var.fVar->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 813 | this->write(SAMPLER_SUFFIX); |
| 814 | this->write("[[sampler("); |
| 815 | this->write(to_string(var.fVar->fModifiers.fLayout.fBinding)); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 816 | this->write(")]]"); |
| 817 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 818 | } |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 819 | } else if (ProgramElement::kInterfaceBlock_Kind == e.fKind) { |
| 820 | InterfaceBlock& intf = (InterfaceBlock&) e; |
| 821 | if ("sk_PerVertex" == intf.fTypeName) { |
| 822 | continue; |
| 823 | } |
| 824 | this->write(", constant "); |
| 825 | this->writeType(intf.fVariable.fType); |
| 826 | this->write("& " ); |
| 827 | this->write(fInterfaceBlockNameMap[&intf]); |
| 828 | this->write(" [[buffer("); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 829 | #ifdef SK_MOLTENVK |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 830 | this->write(to_string(intf.fVariable.fModifiers.fLayout.fSet)); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 831 | #else |
| 832 | this->write(to_string(intf.fVariable.fModifiers.fLayout.fBinding)); |
| 833 | #endif |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 834 | this->write(")]]"); |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 835 | } |
| 836 | } |
Jim Van Verth | 6bc650e | 2019-02-07 14:53:23 -0500 | [diff] [blame] | 837 | if (fProgram.fKind == Program::kFragment_Kind) { |
| 838 | if (fProgram.fInputs.fRTHeight && fInterfaceBlockNameMap.empty()) { |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 839 | #ifdef SK_MOLTENVK |
| 840 | this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(0)]]"); |
| 841 | #else |
| 842 | this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(1)]]"); |
| 843 | #endif |
| 844 | } |
Timothy Liang | 7b8875d | 2018-08-10 09:42:31 -0400 | [diff] [blame] | 845 | this->write(", bool _frontFacing [[front_facing]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 846 | this->write(", float4 _fragCoord [[position]]"); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 847 | } else if (fProgram.fKind == Program::kVertex_Kind) { |
| 848 | this->write(", uint sk_VertexID [[vertex_id]], uint sk_InstanceID [[instance_id]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 849 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 850 | separator = ", "; |
| 851 | } else { |
| 852 | this->writeType(f.fDeclaration.fReturnType); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 853 | this->write(" "); |
| 854 | this->writeName(f.fDeclaration.fName); |
| 855 | this->write("("); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 856 | if (this->requirements(f.fDeclaration) & kInputs_Requirement) { |
| 857 | this->write("Inputs _in"); |
| 858 | separator = ", "; |
| 859 | } |
| 860 | if (this->requirements(f.fDeclaration) & kOutputs_Requirement) { |
| 861 | this->write(separator); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 862 | this->write("thread Outputs* _out"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 863 | separator = ", "; |
| 864 | } |
| 865 | if (this->requirements(f.fDeclaration) & kUniforms_Requirement) { |
| 866 | this->write(separator); |
| 867 | this->write("Uniforms _uniforms"); |
| 868 | separator = ", "; |
| 869 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 870 | if (this->requirements(f.fDeclaration) & kGlobals_Requirement) { |
| 871 | this->write(separator); |
| 872 | this->write("thread Globals* _globals"); |
| 873 | separator = ", "; |
| 874 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 875 | } |
| 876 | for (const auto& param : f.fDeclaration.fParameters) { |
| 877 | this->write(separator); |
| 878 | separator = ", "; |
| 879 | this->writeModifiers(param->fModifiers, false); |
| 880 | std::vector<int> sizes; |
| 881 | const Type* type = ¶m->fType; |
| 882 | while (Type::kArray_Kind == type->kind()) { |
| 883 | sizes.push_back(type->columns()); |
| 884 | type = &type->componentType(); |
| 885 | } |
| 886 | this->writeType(*type); |
| 887 | if (param->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 888 | this->write("*"); |
| 889 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 890 | this->write(" "); |
| 891 | this->writeName(param->fName); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 892 | for (int s : sizes) { |
| 893 | if (s <= 0) { |
| 894 | this->write("[]"); |
| 895 | } else { |
| 896 | this->write("[" + to_string(s) + "]"); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | this->writeLine(") {"); |
| 901 | |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 902 | SkASSERT(!fProgram.fSettings.fFragColorIsInOut); |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 903 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 904 | if ("main" == f.fDeclaration.fName) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 905 | if (fNeedsGlobalStructInit) { |
| 906 | this->writeLine(" Globals globalStruct;"); |
| 907 | this->writeLine(" thread Globals* _globals = &globalStruct;"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 908 | for (const auto& intf: fInterfaceBlockNameMap) { |
| 909 | const auto& intfName = intf.second; |
| 910 | this->write(" _globals->"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 911 | this->writeName(intfName); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 912 | this->write(" = &"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 913 | this->writeName(intfName); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 914 | this->write(";\n"); |
| 915 | } |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 916 | for (const auto& var: fInitNonConstGlobalVars) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 917 | this->write(" _globals->"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 918 | this->writeName(var->fVar->fName); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 919 | this->write(" = "); |
| 920 | this->writeVarInitializer(*var->fVar, *var->fValue); |
| 921 | this->writeLine(";"); |
| 922 | } |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 923 | for (const auto& texture: fTextures) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 924 | this->write(" _globals->"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 925 | this->writeName(texture->fName); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 926 | this->write(" = "); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 927 | this->writeName(texture->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 928 | this->write(";\n"); |
| 929 | this->write(" _globals->"); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 930 | this->writeName(texture->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 931 | this->write(SAMPLER_SUFFIX); |
| 932 | this->write(" = "); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 933 | this->writeName(texture->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 934 | this->write(SAMPLER_SUFFIX); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 935 | this->write(";\n"); |
| 936 | } |
Timothy Liang | 6403b0e | 2018-05-17 10:40:04 -0400 | [diff] [blame] | 937 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 938 | this->writeLine(" Outputs _outputStruct;"); |
| 939 | this->writeLine(" thread Outputs* _out = &_outputStruct;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 940 | } |
| 941 | fFunctionHeader = ""; |
| 942 | OutputStream* oldOut = fOut; |
| 943 | StringStream buffer; |
| 944 | fOut = &buffer; |
| 945 | fIndentation++; |
| 946 | this->writeStatements(((Block&) *f.fBody).fStatements); |
| 947 | if ("main" == f.fDeclaration.fName) { |
| 948 | switch (fProgram.fKind) { |
| 949 | case Program::kFragment_Kind: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 950 | this->writeLine("return *_out;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 951 | break; |
| 952 | case Program::kVertex_Kind: |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 953 | this->writeLine("_out->sk_Position.y = -_out->sk_Position.y;"); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 954 | this->writeLine("return *_out;"); // FIXME - detect if function already has return |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 955 | break; |
| 956 | default: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 957 | SkASSERT(false); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | fIndentation--; |
| 961 | this->writeLine("}"); |
| 962 | |
| 963 | fOut = oldOut; |
| 964 | this->write(fFunctionHeader); |
| 965 | this->write(buffer.str()); |
| 966 | } |
| 967 | |
| 968 | void MetalCodeGenerator::writeModifiers(const Modifiers& modifiers, |
| 969 | bool globalContext) { |
| 970 | if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 971 | this->write("thread "); |
| 972 | } |
| 973 | if (modifiers.fFlags & Modifiers::kConst_Flag) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 974 | this->write("constant "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
| 978 | void MetalCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
| 979 | if ("sk_PerVertex" == intf.fTypeName) { |
| 980 | return; |
| 981 | } |
| 982 | this->writeModifiers(intf.fVariable.fModifiers, true); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 983 | this->write("struct "); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 984 | this->writeLine(intf.fTypeName + " {"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 985 | const Type* structType = &intf.fVariable.fType; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 986 | fWrittenStructs.push_back(structType); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 987 | while (Type::kArray_Kind == structType->kind()) { |
| 988 | structType = &structType->componentType(); |
| 989 | } |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 990 | fIndentation++; |
| 991 | writeFields(structType->fields(), structType->fOffset, &intf); |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 992 | if (fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 993 | this->writeLine("float u_skRTHeight;"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 994 | } |
| 995 | fIndentation--; |
| 996 | this->write("}"); |
| 997 | if (intf.fInstanceName.size()) { |
| 998 | this->write(" "); |
| 999 | this->write(intf.fInstanceName); |
| 1000 | for (const auto& size : intf.fSizes) { |
| 1001 | this->write("["); |
| 1002 | if (size) { |
| 1003 | this->writeExpression(*size, kTopLevel_Precedence); |
| 1004 | } |
| 1005 | this->write("]"); |
| 1006 | } |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1007 | fInterfaceBlockNameMap[&intf] = intf.fInstanceName; |
| 1008 | } else { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1009 | fInterfaceBlockNameMap[&intf] = "_anonInterface" + to_string(fAnonInterfaceCount++); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1010 | } |
| 1011 | this->writeLine(";"); |
| 1012 | } |
| 1013 | |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1014 | void MetalCodeGenerator::writeFields(const std::vector<Type::Field>& fields, int parentOffset, |
| 1015 | const InterfaceBlock* parentIntf) { |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1016 | #ifdef SK_MOLTENVK |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1017 | MemoryLayout memoryLayout(MemoryLayout::k140_Standard); |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1018 | #else |
| 1019 | MemoryLayout memoryLayout(MemoryLayout::kMetal_Standard); |
| 1020 | #endif |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1021 | int currentOffset = 0; |
| 1022 | for (const auto& field: fields) { |
| 1023 | int fieldOffset = field.fModifiers.fLayout.fOffset; |
| 1024 | const Type* fieldType = field.fType; |
| 1025 | if (fieldOffset != -1) { |
| 1026 | if (currentOffset > fieldOffset) { |
| 1027 | fErrors.error(parentOffset, |
| 1028 | "offset of field '" + field.fName + "' must be at least " + |
| 1029 | to_string((int) currentOffset)); |
| 1030 | } else if (currentOffset < fieldOffset) { |
| 1031 | this->write("char pad"); |
| 1032 | this->write(to_string(fPaddingCount++)); |
| 1033 | this->write("["); |
| 1034 | this->write(to_string(fieldOffset - currentOffset)); |
| 1035 | this->writeLine("];"); |
| 1036 | currentOffset = fieldOffset; |
| 1037 | } |
| 1038 | int alignment = memoryLayout.alignment(*fieldType); |
| 1039 | if (fieldOffset % alignment) { |
| 1040 | fErrors.error(parentOffset, |
| 1041 | "offset of field '" + field.fName + "' must be a multiple of " + |
| 1042 | to_string((int) alignment)); |
| 1043 | } |
| 1044 | } |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1045 | #ifdef SK_MOLTENVK |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1046 | if (fieldType->kind() == Type::kVector_Kind && |
| 1047 | fieldType->columns() == 3) { |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1048 | SkASSERT(memoryLayout.size(*fieldType) == 3); |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1049 | // Pack all vec3 types so that their size in bytes will match what was expected in the |
| 1050 | // original SkSL code since MSL has vec3 sizes equal to 4 * component type, while SkSL |
| 1051 | // has vec3 equal to 3 * component type. |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1052 | |
| 1053 | // FIXME - Packed vectors can't be accessed by swizzles, but can be indexed into. A |
| 1054 | // combination of this being a problem which only occurs when using MoltenVK and the |
| 1055 | // fact that we haven't swizzled a vec3 yet means that this problem hasn't been |
| 1056 | // addressed. |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1057 | this->write(PACKED_PREFIX); |
| 1058 | } |
Timothy Liang | 609fbe3 | 2018-08-10 16:40:49 -0400 | [diff] [blame] | 1059 | #endif |
Timothy Liang | dc89f19 | 2018-06-13 09:20:31 -0400 | [diff] [blame] | 1060 | currentOffset += memoryLayout.size(*fieldType); |
| 1061 | std::vector<int> sizes; |
| 1062 | while (fieldType->kind() == Type::kArray_Kind) { |
| 1063 | sizes.push_back(fieldType->columns()); |
| 1064 | fieldType = &fieldType->componentType(); |
| 1065 | } |
| 1066 | this->writeModifiers(field.fModifiers, false); |
| 1067 | this->writeType(*fieldType); |
| 1068 | this->write(" "); |
| 1069 | this->writeName(field.fName); |
| 1070 | for (int s : sizes) { |
| 1071 | if (s <= 0) { |
| 1072 | this->write("[]"); |
| 1073 | } else { |
| 1074 | this->write("[" + to_string(s) + "]"); |
| 1075 | } |
| 1076 | } |
| 1077 | this->writeLine(";"); |
| 1078 | if (parentIntf) { |
| 1079 | fInterfaceBlockMap[&field] = parentIntf; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1084 | void MetalCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 1085 | this->writeExpression(value, kTopLevel_Precedence); |
| 1086 | } |
| 1087 | |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1088 | void MetalCodeGenerator::writeName(const String& name) { |
| 1089 | if (fReservedWords.find(name) != fReservedWords.end()) { |
| 1090 | this->write("_"); // adding underscore before name to avoid conflict with reserved words |
| 1091 | } |
| 1092 | this->write(name); |
| 1093 | } |
| 1094 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1095 | void MetalCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1096 | SkASSERT(decl.fVars.size() > 0); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1097 | bool wroteType = false; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1098 | for (const auto& stmt : decl.fVars) { |
| 1099 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1100 | if (global && !(var.fVar->fModifiers.fFlags & Modifiers::kConst_Flag)) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1101 | continue; |
| 1102 | } |
| 1103 | if (wroteType) { |
| 1104 | this->write(", "); |
| 1105 | } else { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1106 | this->writeModifiers(var.fVar->fModifiers, global); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1107 | this->writeType(decl.fBaseType); |
| 1108 | this->write(" "); |
| 1109 | wroteType = true; |
| 1110 | } |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1111 | this->writeName(var.fVar->fName); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1112 | for (const auto& size : var.fSizes) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1113 | this->write("["); |
| 1114 | if (size) { |
| 1115 | this->writeExpression(*size, kTopLevel_Precedence); |
| 1116 | } |
| 1117 | this->write("]"); |
| 1118 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1119 | if (var.fValue) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1120 | this->write(" = "); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1121 | this->writeVarInitializer(*var.fVar, *var.fValue); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1122 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1123 | } |
| 1124 | if (wroteType) { |
| 1125 | this->write(";"); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | void MetalCodeGenerator::writeStatement(const Statement& s) { |
| 1130 | switch (s.fKind) { |
| 1131 | case Statement::kBlock_Kind: |
| 1132 | this->writeBlock((Block&) s); |
| 1133 | break; |
| 1134 | case Statement::kExpression_Kind: |
| 1135 | this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopLevel_Precedence); |
| 1136 | this->write(";"); |
| 1137 | break; |
| 1138 | case Statement::kReturn_Kind: |
| 1139 | this->writeReturnStatement((ReturnStatement&) s); |
| 1140 | break; |
| 1141 | case Statement::kVarDeclarations_Kind: |
| 1142 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, false); |
| 1143 | break; |
| 1144 | case Statement::kIf_Kind: |
| 1145 | this->writeIfStatement((IfStatement&) s); |
| 1146 | break; |
| 1147 | case Statement::kFor_Kind: |
| 1148 | this->writeForStatement((ForStatement&) s); |
| 1149 | break; |
| 1150 | case Statement::kWhile_Kind: |
| 1151 | this->writeWhileStatement((WhileStatement&) s); |
| 1152 | break; |
| 1153 | case Statement::kDo_Kind: |
| 1154 | this->writeDoStatement((DoStatement&) s); |
| 1155 | break; |
| 1156 | case Statement::kSwitch_Kind: |
| 1157 | this->writeSwitchStatement((SwitchStatement&) s); |
| 1158 | break; |
| 1159 | case Statement::kBreak_Kind: |
| 1160 | this->write("break;"); |
| 1161 | break; |
| 1162 | case Statement::kContinue_Kind: |
| 1163 | this->write("continue;"); |
| 1164 | break; |
| 1165 | case Statement::kDiscard_Kind: |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1166 | this->write("discard_fragment();"); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1167 | break; |
| 1168 | case Statement::kNop_Kind: |
| 1169 | this->write(";"); |
| 1170 | break; |
| 1171 | default: |
| 1172 | ABORT("unsupported statement: %s", s.description().c_str()); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | void MetalCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) { |
| 1177 | for (const auto& s : statements) { |
| 1178 | if (!s->isEmpty()) { |
| 1179 | this->writeStatement(*s); |
| 1180 | this->writeLine(); |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | void MetalCodeGenerator::writeBlock(const Block& b) { |
| 1186 | this->writeLine("{"); |
| 1187 | fIndentation++; |
| 1188 | this->writeStatements(b.fStatements); |
| 1189 | fIndentation--; |
| 1190 | this->write("}"); |
| 1191 | } |
| 1192 | |
| 1193 | void MetalCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1194 | this->write("if ("); |
| 1195 | this->writeExpression(*stmt.fTest, kTopLevel_Precedence); |
| 1196 | this->write(") "); |
| 1197 | this->writeStatement(*stmt.fIfTrue); |
| 1198 | if (stmt.fIfFalse) { |
| 1199 | this->write(" else "); |
| 1200 | this->writeStatement(*stmt.fIfFalse); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void MetalCodeGenerator::writeForStatement(const ForStatement& f) { |
| 1205 | this->write("for ("); |
| 1206 | if (f.fInitializer && !f.fInitializer->isEmpty()) { |
| 1207 | this->writeStatement(*f.fInitializer); |
| 1208 | } else { |
| 1209 | this->write("; "); |
| 1210 | } |
| 1211 | if (f.fTest) { |
| 1212 | this->writeExpression(*f.fTest, kTopLevel_Precedence); |
| 1213 | } |
| 1214 | this->write("; "); |
| 1215 | if (f.fNext) { |
| 1216 | this->writeExpression(*f.fNext, kTopLevel_Precedence); |
| 1217 | } |
| 1218 | this->write(") "); |
| 1219 | this->writeStatement(*f.fStatement); |
| 1220 | } |
| 1221 | |
| 1222 | void MetalCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 1223 | this->write("while ("); |
| 1224 | this->writeExpression(*w.fTest, kTopLevel_Precedence); |
| 1225 | this->write(") "); |
| 1226 | this->writeStatement(*w.fStatement); |
| 1227 | } |
| 1228 | |
| 1229 | void MetalCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 1230 | this->write("do "); |
| 1231 | this->writeStatement(*d.fStatement); |
| 1232 | this->write(" while ("); |
| 1233 | this->writeExpression(*d.fTest, kTopLevel_Precedence); |
| 1234 | this->write(");"); |
| 1235 | } |
| 1236 | |
| 1237 | void MetalCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1238 | this->write("switch ("); |
| 1239 | this->writeExpression(*s.fValue, kTopLevel_Precedence); |
| 1240 | this->writeLine(") {"); |
| 1241 | fIndentation++; |
| 1242 | for (const auto& c : s.fCases) { |
| 1243 | if (c->fValue) { |
| 1244 | this->write("case "); |
| 1245 | this->writeExpression(*c->fValue, kTopLevel_Precedence); |
| 1246 | this->writeLine(":"); |
| 1247 | } else { |
| 1248 | this->writeLine("default:"); |
| 1249 | } |
| 1250 | fIndentation++; |
| 1251 | for (const auto& stmt : c->fStatements) { |
| 1252 | this->writeStatement(*stmt); |
| 1253 | this->writeLine(); |
| 1254 | } |
| 1255 | fIndentation--; |
| 1256 | } |
| 1257 | fIndentation--; |
| 1258 | this->write("}"); |
| 1259 | } |
| 1260 | |
| 1261 | void MetalCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 1262 | this->write("return"); |
| 1263 | if (r.fExpression) { |
| 1264 | this->write(" "); |
| 1265 | this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
| 1266 | } |
| 1267 | this->write(";"); |
| 1268 | } |
| 1269 | |
| 1270 | void MetalCodeGenerator::writeHeader() { |
| 1271 | this->write("#include <metal_stdlib>\n"); |
| 1272 | this->write("#include <simd/simd.h>\n"); |
| 1273 | this->write("using namespace metal;\n"); |
| 1274 | } |
| 1275 | |
| 1276 | void MetalCodeGenerator::writeUniformStruct() { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1277 | for (const auto& e : fProgram) { |
| 1278 | if (ProgramElement::kVar_Kind == e.fKind) { |
| 1279 | VarDeclarations& decls = (VarDeclarations&) e; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1280 | if (!decls.fVars.size()) { |
| 1281 | continue; |
| 1282 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1283 | const Variable& first = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1284 | if (first.fModifiers.fFlags & Modifiers::kUniform_Flag && |
| 1285 | first.fType.kind() != Type::kSampler_Kind) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1286 | if (-1 == fUniformBuffer) { |
| 1287 | this->write("struct Uniforms {\n"); |
| 1288 | fUniformBuffer = first.fModifiers.fLayout.fSet; |
| 1289 | if (-1 == fUniformBuffer) { |
| 1290 | fErrors.error(decls.fOffset, "Metal uniforms must have 'layout(set=...)'"); |
| 1291 | } |
| 1292 | } else if (first.fModifiers.fLayout.fSet != fUniformBuffer) { |
| 1293 | if (-1 == fUniformBuffer) { |
| 1294 | fErrors.error(decls.fOffset, "Metal backend requires all uniforms to have " |
| 1295 | "the same 'layout(set=...)'"); |
| 1296 | } |
| 1297 | } |
| 1298 | this->write(" "); |
| 1299 | this->writeType(first.fType); |
| 1300 | this->write(" "); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1301 | for (const auto& stmt : decls.fVars) { |
| 1302 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1303 | this->writeName(var.fVar->fName); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1304 | } |
| 1305 | this->write(";\n"); |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | if (-1 != fUniformBuffer) { |
| 1310 | this->write("};\n"); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | void MetalCodeGenerator::writeInputStruct() { |
| 1315 | this->write("struct Inputs {\n"); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1316 | for (const auto& e : fProgram) { |
| 1317 | if (ProgramElement::kVar_Kind == e.fKind) { |
| 1318 | VarDeclarations& decls = (VarDeclarations&) e; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1319 | if (!decls.fVars.size()) { |
| 1320 | continue; |
| 1321 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1322 | const Variable& first = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1323 | if (first.fModifiers.fFlags & Modifiers::kIn_Flag && |
| 1324 | -1 == first.fModifiers.fLayout.fBuiltin) { |
| 1325 | this->write(" "); |
| 1326 | this->writeType(first.fType); |
| 1327 | this->write(" "); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1328 | for (const auto& stmt : decls.fVars) { |
| 1329 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1330 | this->writeName(var.fVar->fName); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1331 | if (-1 != var.fVar->fModifiers.fLayout.fLocation) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1332 | if (fProgram.fKind == Program::kVertex_Kind) { |
| 1333 | this->write(" [[attribute(" + |
| 1334 | to_string(var.fVar->fModifiers.fLayout.fLocation) + ")]]"); |
| 1335 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
| 1336 | this->write(" [[user(locn" + |
| 1337 | to_string(var.fVar->fModifiers.fLayout.fLocation) + ")]]"); |
| 1338 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | this->write(";\n"); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | this->write("};\n"); |
| 1346 | } |
| 1347 | |
| 1348 | void MetalCodeGenerator::writeOutputStruct() { |
| 1349 | this->write("struct Outputs {\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1350 | if (fProgram.fKind == Program::kVertex_Kind) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1351 | this->write(" float4 sk_Position [[position]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1352 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 1353 | this->write(" float4 sk_FragColor [[color(0)]];\n"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1354 | } |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1355 | for (const auto& e : fProgram) { |
| 1356 | if (ProgramElement::kVar_Kind == e.fKind) { |
| 1357 | VarDeclarations& decls = (VarDeclarations&) e; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1358 | if (!decls.fVars.size()) { |
| 1359 | continue; |
| 1360 | } |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1361 | const Variable& first = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1362 | if (first.fModifiers.fFlags & Modifiers::kOut_Flag && |
| 1363 | -1 == first.fModifiers.fLayout.fBuiltin) { |
| 1364 | this->write(" "); |
| 1365 | this->writeType(first.fType); |
| 1366 | this->write(" "); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1367 | for (const auto& stmt : decls.fVars) { |
| 1368 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1369 | this->writeName(var.fVar->fName); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1370 | if (fProgram.fKind == Program::kVertex_Kind) { |
| 1371 | this->write(" [[user(locn" + |
| 1372 | to_string(var.fVar->fModifiers.fLayout.fLocation) + ")]]"); |
| 1373 | } else if (fProgram.fKind == Program::kFragment_Kind) { |
| 1374 | this->write(" [[color(" + |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 1375 | to_string(var.fVar->fModifiers.fLayout.fLocation) +")"); |
| 1376 | int colorIndex = var.fVar->fModifiers.fLayout.fIndex; |
| 1377 | if (colorIndex) { |
| 1378 | this->write(", index(" + to_string(colorIndex) + ")"); |
| 1379 | } |
| 1380 | this->write("]]"); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1381 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1382 | } |
| 1383 | this->write(";\n"); |
| 1384 | } |
| 1385 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1386 | } |
| 1387 | if (fProgram.fKind == Program::kVertex_Kind) { |
| 1388 | this->write(" float sk_PointSize;\n"); |
| 1389 | } |
| 1390 | this->write("};\n"); |
| 1391 | } |
| 1392 | |
| 1393 | void MetalCodeGenerator::writeInterfaceBlocks() { |
| 1394 | bool wroteInterfaceBlock = false; |
| 1395 | for (const auto& e : fProgram) { |
| 1396 | if (ProgramElement::kInterfaceBlock_Kind == e.fKind) { |
| 1397 | this->writeInterfaceBlock((InterfaceBlock&) e); |
| 1398 | wroteInterfaceBlock = true; |
| 1399 | } |
| 1400 | } |
Jim Van Verth | 3d48299 | 2019-02-07 10:48:05 -0500 | [diff] [blame] | 1401 | if (!wroteInterfaceBlock && fProgram.fInputs.fRTHeight) { |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1402 | this->writeLine("struct sksl_synthetic_uniforms {"); |
| 1403 | this->writeLine(" float u_skRTHeight;"); |
| 1404 | this->writeLine("};"); |
| 1405 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1408 | void MetalCodeGenerator::writeGlobalStruct() { |
| 1409 | bool wroteStructDecl = false; |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1410 | for (const auto& intf : fInterfaceBlockNameMap) { |
| 1411 | if (!wroteStructDecl) { |
| 1412 | this->write("struct Globals {\n"); |
| 1413 | wroteStructDecl = true; |
| 1414 | } |
| 1415 | fNeedsGlobalStructInit = true; |
| 1416 | const auto& intfType = intf.first; |
| 1417 | const auto& intfName = intf.second; |
| 1418 | this->write(" constant "); |
| 1419 | this->write(intfType->fTypeName); |
| 1420 | this->write("* "); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1421 | this->writeName(intfName); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1422 | this->write(";\n"); |
| 1423 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1424 | for (const auto& e : fProgram) { |
| 1425 | if (ProgramElement::kVar_Kind == e.fKind) { |
| 1426 | VarDeclarations& decls = (VarDeclarations&) e; |
| 1427 | if (!decls.fVars.size()) { |
| 1428 | continue; |
| 1429 | } |
| 1430 | const Variable& first = *((VarDeclaration&) *decls.fVars[0]).fVar; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1431 | if ((!first.fModifiers.fFlags && -1 == first.fModifiers.fLayout.fBuiltin) || |
| 1432 | first.fType.kind() == Type::kSampler_Kind) { |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1433 | if (!wroteStructDecl) { |
| 1434 | this->write("struct Globals {\n"); |
| 1435 | wroteStructDecl = true; |
| 1436 | } |
| 1437 | fNeedsGlobalStructInit = true; |
| 1438 | this->write(" "); |
| 1439 | this->writeType(first.fType); |
| 1440 | this->write(" "); |
| 1441 | for (const auto& stmt : decls.fVars) { |
| 1442 | VarDeclaration& var = (VarDeclaration&) *stmt; |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1443 | this->writeName(var.fVar->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1444 | if (var.fVar->fType.kind() == Type::kSampler_Kind) { |
| 1445 | fTextures.push_back(var.fVar); |
| 1446 | this->write(";\n"); |
| 1447 | this->write(" sampler "); |
Timothy Liang | 651286f | 2018-06-07 09:55:33 -0400 | [diff] [blame] | 1448 | this->writeName(var.fVar->fName); |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1449 | this->write(SAMPLER_SUFFIX); |
| 1450 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1451 | if (var.fValue) { |
| 1452 | fInitNonConstGlobalVars.push_back(&var); |
| 1453 | } |
| 1454 | } |
| 1455 | this->write(";\n"); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1459 | if (wroteStructDecl) { |
| 1460 | this->write("};\n"); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1464 | void MetalCodeGenerator::writeProgramElement(const ProgramElement& e) { |
| 1465 | switch (e.fKind) { |
| 1466 | case ProgramElement::kExtension_Kind: |
| 1467 | break; |
| 1468 | case ProgramElement::kVar_Kind: { |
| 1469 | VarDeclarations& decl = (VarDeclarations&) e; |
| 1470 | if (decl.fVars.size() > 0) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1471 | int builtin = ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1472 | if (-1 == builtin) { |
| 1473 | // normal var |
| 1474 | this->writeVarDeclarations(decl, true); |
| 1475 | this->writeLine(); |
| 1476 | } else if (SK_FRAGCOLOR_BUILTIN == builtin) { |
| 1477 | // ignore |
| 1478 | } |
| 1479 | } |
| 1480 | break; |
| 1481 | } |
| 1482 | case ProgramElement::kInterfaceBlock_Kind: |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1483 | // handled in writeInterfaceBlocks, do nothing |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1484 | break; |
| 1485 | case ProgramElement::kFunction_Kind: |
| 1486 | this->writeFunction((FunctionDefinition&) e); |
| 1487 | break; |
| 1488 | case ProgramElement::kModifiers_Kind: |
| 1489 | this->writeModifiers(((ModifiersDeclaration&) e).fModifiers, true); |
| 1490 | this->writeLine(";"); |
| 1491 | break; |
| 1492 | default: |
| 1493 | printf("%s\n", e.description().c_str()); |
| 1494 | ABORT("unsupported program element"); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Expression& e) { |
| 1499 | switch (e.fKind) { |
| 1500 | case Expression::kFunctionCall_Kind: { |
| 1501 | const FunctionCall& f = (const FunctionCall&) e; |
| 1502 | Requirements result = this->requirements(f.fFunction); |
| 1503 | for (const auto& e : f.fArguments) { |
| 1504 | result |= this->requirements(*e); |
| 1505 | } |
| 1506 | return result; |
| 1507 | } |
| 1508 | case Expression::kConstructor_Kind: { |
| 1509 | const Constructor& c = (const Constructor&) e; |
| 1510 | Requirements result = kNo_Requirements; |
| 1511 | for (const auto& e : c.fArguments) { |
| 1512 | result |= this->requirements(*e); |
| 1513 | } |
| 1514 | return result; |
| 1515 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1516 | case Expression::kFieldAccess_Kind: { |
| 1517 | const FieldAccess& f = (const FieldAccess&) e; |
| 1518 | if (FieldAccess::kAnonymousInterfaceBlock_OwnerKind == f.fOwnerKind) { |
| 1519 | return kGlobals_Requirement; |
| 1520 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1521 | return this->requirements(*((const FieldAccess&) e).fBase); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1522 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1523 | case Expression::kSwizzle_Kind: |
| 1524 | return this->requirements(*((const Swizzle&) e).fBase); |
| 1525 | case Expression::kBinary_Kind: { |
| 1526 | const BinaryExpression& b = (const BinaryExpression&) e; |
| 1527 | return this->requirements(*b.fLeft) | this->requirements(*b.fRight); |
| 1528 | } |
| 1529 | case Expression::kIndex_Kind: { |
| 1530 | const IndexExpression& idx = (const IndexExpression&) e; |
| 1531 | return this->requirements(*idx.fBase) | this->requirements(*idx.fIndex); |
| 1532 | } |
| 1533 | case Expression::kPrefix_Kind: |
| 1534 | return this->requirements(*((const PrefixExpression&) e).fOperand); |
| 1535 | case Expression::kPostfix_Kind: |
| 1536 | return this->requirements(*((const PostfixExpression&) e).fOperand); |
| 1537 | case Expression::kTernary_Kind: { |
| 1538 | const TernaryExpression& t = (const TernaryExpression&) e; |
| 1539 | return this->requirements(*t.fTest) | this->requirements(*t.fIfTrue) | |
| 1540 | this->requirements(*t.fIfFalse); |
| 1541 | } |
| 1542 | case Expression::kVariableReference_Kind: { |
| 1543 | const VariableReference& v = (const VariableReference&) e; |
| 1544 | Requirements result = kNo_Requirements; |
| 1545 | if (v.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) { |
| 1546 | result = kInputs_Requirement; |
| 1547 | } else if (Variable::kGlobal_Storage == v.fVariable.fStorage) { |
| 1548 | if (v.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) { |
| 1549 | result = kInputs_Requirement; |
| 1550 | } else if (v.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 1551 | result = kOutputs_Requirement; |
Timothy Liang | a06f215 | 2018-05-24 15:33:31 -0400 | [diff] [blame] | 1552 | } else if (v.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && |
| 1553 | v.fVariable.fType.kind() != Type::kSampler_Kind) { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1554 | result = kUniforms_Requirement; |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1555 | } else { |
| 1556 | result = kGlobals_Requirement; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | return result; |
| 1560 | } |
| 1561 | default: |
| 1562 | return kNo_Requirements; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Statement& s) { |
| 1567 | switch (s.fKind) { |
| 1568 | case Statement::kBlock_Kind: { |
| 1569 | Requirements result = kNo_Requirements; |
| 1570 | for (const auto& child : ((const Block&) s).fStatements) { |
| 1571 | result |= this->requirements(*child); |
| 1572 | } |
| 1573 | return result; |
| 1574 | } |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1575 | case Statement::kVarDeclaration_Kind: { |
| 1576 | Requirements result = kNo_Requirements; |
| 1577 | const VarDeclaration& var = (const VarDeclaration&) s; |
| 1578 | if (var.fValue) { |
| 1579 | result = this->requirements(*var.fValue); |
| 1580 | } |
| 1581 | return result; |
| 1582 | } |
| 1583 | case Statement::kVarDeclarations_Kind: { |
| 1584 | Requirements result = kNo_Requirements; |
| 1585 | const VarDeclarations& decls = *((const VarDeclarationsStatement&) s).fDeclaration; |
| 1586 | for (const auto& stmt : decls.fVars) { |
| 1587 | result |= this->requirements(*stmt); |
| 1588 | } |
| 1589 | return result; |
| 1590 | } |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1591 | case Statement::kExpression_Kind: |
| 1592 | return this->requirements(*((const ExpressionStatement&) s).fExpression); |
| 1593 | case Statement::kReturn_Kind: { |
| 1594 | const ReturnStatement& r = (const ReturnStatement&) s; |
| 1595 | if (r.fExpression) { |
| 1596 | return this->requirements(*r.fExpression); |
| 1597 | } |
| 1598 | return kNo_Requirements; |
| 1599 | } |
| 1600 | case Statement::kIf_Kind: { |
| 1601 | const IfStatement& i = (const IfStatement&) s; |
| 1602 | return this->requirements(*i.fTest) | |
| 1603 | this->requirements(*i.fIfTrue) | |
| 1604 | (i.fIfFalse && this->requirements(*i.fIfFalse)); |
| 1605 | } |
| 1606 | case Statement::kFor_Kind: { |
| 1607 | const ForStatement& f = (const ForStatement&) s; |
| 1608 | return this->requirements(*f.fInitializer) | |
| 1609 | this->requirements(*f.fTest) | |
| 1610 | this->requirements(*f.fNext) | |
| 1611 | this->requirements(*f.fStatement); |
| 1612 | } |
| 1613 | case Statement::kWhile_Kind: { |
| 1614 | const WhileStatement& w = (const WhileStatement&) s; |
| 1615 | return this->requirements(*w.fTest) | |
| 1616 | this->requirements(*w.fStatement); |
| 1617 | } |
| 1618 | case Statement::kDo_Kind: { |
| 1619 | const DoStatement& d = (const DoStatement&) s; |
| 1620 | return this->requirements(*d.fTest) | |
| 1621 | this->requirements(*d.fStatement); |
| 1622 | } |
| 1623 | case Statement::kSwitch_Kind: { |
| 1624 | const SwitchStatement& sw = (const SwitchStatement&) s; |
| 1625 | Requirements result = this->requirements(*sw.fValue); |
| 1626 | for (const auto& c : sw.fCases) { |
| 1627 | for (const auto& st : c->fStatements) { |
| 1628 | result |= this->requirements(*st); |
| 1629 | } |
| 1630 | } |
| 1631 | return result; |
| 1632 | } |
| 1633 | default: |
| 1634 | return kNo_Requirements; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const FunctionDeclaration& f) { |
| 1639 | if (f.fBuiltin) { |
| 1640 | return kNo_Requirements; |
| 1641 | } |
| 1642 | auto found = fRequirements.find(&f); |
| 1643 | if (found == fRequirements.end()) { |
Ethan Nicholas | 65a8f56 | 2019-04-19 14:00:26 -0400 | [diff] [blame] | 1644 | fRequirements[&f] = kNo_Requirements; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1645 | for (const auto& e : fProgram) { |
| 1646 | if (ProgramElement::kFunction_Kind == e.fKind) { |
| 1647 | const FunctionDefinition& def = (const FunctionDefinition&) e; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1648 | if (&def.fDeclaration == &f) { |
| 1649 | Requirements reqs = this->requirements(*def.fBody); |
| 1650 | fRequirements[&f] = reqs; |
| 1651 | return reqs; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | return found->second; |
| 1657 | } |
| 1658 | |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1659 | bool MetalCodeGenerator::generateCode() { |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1660 | OutputStream* rawOut = fOut; |
| 1661 | fOut = &fHeader; |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1662 | #ifdef SK_MOLTENVK |
| 1663 | fOut->write((const char*) &MVKMagicNum, sizeof(MVKMagicNum)); |
| 1664 | #endif |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1665 | fProgramKind = fProgram.fKind; |
| 1666 | this->writeHeader(); |
| 1667 | this->writeUniformStruct(); |
| 1668 | this->writeInputStruct(); |
Timothy Liang | 7d63778 | 2018-06-05 09:58:07 -0400 | [diff] [blame] | 1669 | this->writeOutputStruct(); |
| 1670 | this->writeInterfaceBlocks(); |
Timothy Liang | ee84fe1 | 2018-05-18 14:38:19 -0400 | [diff] [blame] | 1671 | this->writeGlobalStruct(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1672 | StringStream body; |
| 1673 | fOut = &body; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1674 | for (const auto& e : fProgram) { |
| 1675 | this->writeProgramElement(e); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1676 | } |
| 1677 | fOut = rawOut; |
| 1678 | |
| 1679 | write_stringstream(fHeader, *rawOut); |
Chris Dalton | dba7aab | 2018-11-15 10:57:49 -0500 | [diff] [blame] | 1680 | write_stringstream(fExtraFunctions, *rawOut); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1681 | write_stringstream(body, *rawOut); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1682 | #ifdef SK_MOLTENVK |
| 1683 | this->write("\0"); |
| 1684 | #endif |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1685 | return true; |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | } |