Revert "Revert "Revert "Don't build GL on Metal, Vulkan, Dawn, Direct3D bots"""

This reverts commit fb27c9a25fb585c2f397e79267872d82b5339227.

Revert "Remove MoltenVK support"

Reason: TSAN Vulkan bots hanging.

This reverts commit 6cafe73da93195b458eddce5b01c6db99e8be448.

Change-Id: I8ec9db35c112f3c8da8636dab2065e6f18de7d0b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/277936
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/sksl/SkSLMetalCodeGenerator.cpp b/src/sksl/SkSLMetalCodeGenerator.cpp
index b8a6d9c..c42061f 100644
--- a/src/sksl/SkSLMetalCodeGenerator.cpp
+++ b/src/sksl/SkSLMetalCodeGenerator.cpp
@@ -15,6 +15,10 @@
 #include "src/sksl/ir/SkSLNop.h"
 #include "src/sksl/ir/SkSLVariableReference.h"
 
+#ifdef SK_MOLTENVK
+    static const uint32_t MVKMagicNum = 0x19960412;
+#endif
+
 namespace SkSL {
 
 void MetalCodeGenerator::setupIntrinsics() {
@@ -533,7 +537,7 @@
             break;
         case SK_CLOCKWISE_BUILTIN:
             // We'd set the front facing winding in the MTLRenderCommandEncoder to be counter
-            // clockwise to match Skia convention.
+            // clockwise to match Skia convention. This is also the default in MoltenVK.
             this->write(fProgram.fSettings.fFlipY ? "_frontFacing" : "(!_frontFacing)");
             break;
         default:
@@ -785,10 +789,18 @@
     if ("main" == f.fDeclaration.fName) {
         switch (fProgram.fKind) {
             case Program::kFragment_Kind:
+#ifdef SK_MOLTENVK
+                this->write("fragment Outputs main0");
+#else
                 this->write("fragment Outputs fragmentMain");
+#endif
                 break;
             case Program::kVertex_Kind:
+#ifdef SK_MOLTENVK
+                this->write("vertex Outputs main0");
+#else
                 this->write("vertex Outputs vertexMain");
+#endif
                 break;
             default:
                 SkASSERT(false);
@@ -830,13 +842,21 @@
                 this->write("& " );
                 this->write(fInterfaceBlockNameMap[&intf]);
                 this->write(" [[buffer(");
+#ifdef SK_MOLTENVK
+                this->write(to_string(intf.fVariable.fModifiers.fLayout.fSet));
+#else
                 this->write(to_string(intf.fVariable.fModifiers.fLayout.fBinding));
+#endif
                 this->write(")]]");
             }
         }
         if (fProgram.fKind == Program::kFragment_Kind) {
             if (fProgram.fInputs.fRTHeight && fInterfaceBlockNameMap.empty()) {
+#ifdef SK_MOLTENVK
+                this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(0)]]");
+#else
                 this->write(", constant sksl_synthetic_uniforms& _anonInterface0 [[buffer(1)]]");
+#endif
                 fRTHeightName = "_anonInterface0.u_skRTHeight";
             }
             this->write(", bool _frontFacing [[front_facing]]");
@@ -1010,7 +1030,11 @@
 
 void MetalCodeGenerator::writeFields(const std::vector<Type::Field>& fields, int parentOffset,
                                      const InterfaceBlock* parentIntf) {
+#ifdef SK_MOLTENVK
+    MemoryLayout memoryLayout(MemoryLayout::k140_Standard);
+#else
     MemoryLayout memoryLayout(MemoryLayout::kMetal_Standard);
+#endif
     int currentOffset = 0;
     for (const auto& field: fields) {
         int fieldOffset = field.fModifiers.fLayout.fOffset;
@@ -1035,6 +1059,21 @@
                               to_string((int) alignment));
             }
         }
+#ifdef SK_MOLTENVK
+        if (fieldType->kind() == Type::kVector_Kind &&
+            fieldType->columns() == 3) {
+            SkASSERT(memoryLayout.size(*fieldType) == 3);
+            // Pack all vec3 types so that their size in bytes will match what was expected in the
+            // original SkSL code since MSL has vec3 sizes equal to 4 * component type, while SkSL
+            // has vec3 equal to 3 * component type.
+
+            // FIXME - Packed vectors can't be accessed by swizzles, but can be indexed into. A
+            // combination of this being a problem which only occurs when using MoltenVK and the
+            // fact that we haven't swizzled a vec3 yet means that this problem hasn't been
+            // addressed.
+            this->write(PACKED_PREFIX);
+        }
+#endif
         currentOffset += memoryLayout.size(*fieldType);
         std::vector<int> sizes;
         while (fieldType->kind() == Type::kArray_Kind) {
@@ -1642,6 +1681,9 @@
 bool MetalCodeGenerator::generateCode() {
     OutputStream* rawOut = fOut;
     fOut = &fHeader;
+#ifdef SK_MOLTENVK
+    fOut->write((const char*) &MVKMagicNum, sizeof(MVKMagicNum));
+#endif
     fProgramKind = fProgram.fKind;
     this->writeHeader();
     this->writeUniformStruct();
@@ -1659,6 +1701,9 @@
     write_stringstream(fHeader, *rawOut);
     write_stringstream(fExtraFunctions, *rawOut);
     write_stringstream(body, *rawOut);
+#ifdef SK_MOLTENVK
+    this->write("\0");
+#endif
     return true;
 }