metal: Use default storage mode for IOSurface textures

The best practice is to use the default storage mode which is "shared"
on Apple GPUs and "managed" on non-Apple GPUs. Using a non-default
storage mode could have implications on how IOAccelerator allocations
are made for the IOSurface e.g. managed implies lack of coherence
between CPU and GPU and needs to be explicitly synchronized - it's not
clear if this can cause an extra allocation on Apple GPUs.

Bug: 388872587
Change-Id: Ief7905593d08251b8f4626e5f14b06e6e98a3c6a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/223275
Auto-Submit: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Sunny Sachanandani <sunnyps@chromium.org>
diff --git a/src/dawn/native/metal/SharedTextureMemoryMTL.h b/src/dawn/native/metal/SharedTextureMemoryMTL.h
index 20d92a6..7cf5206 100644
--- a/src/dawn/native/metal/SharedTextureMemoryMTL.h
+++ b/src/dawn/native/metal/SharedTextureMemoryMTL.h
@@ -79,7 +79,7 @@
 
     absl::InlinedVector<NSPRef<id<MTLTexture>>, kMaxPlanesPerFormat> mMtlPlaneTextures;
     MTLPixelFormat mMtlFormat = MTLPixelFormatInvalid;
-    MTLTextureUsage mMtlUsage;
+    MTLTextureUsage mMtlUsage = MTLTextureUsageUnknown;
     CFRef<IOSurfaceRef> mIOSurface;
 };
 
diff --git a/src/dawn/native/metal/SharedTextureMemoryMTL.mm b/src/dawn/native/metal/SharedTextureMemoryMTL.mm
index ac2d013..c8f1eed 100644
--- a/src/dawn/native/metal/SharedTextureMemoryMTL.mm
+++ b/src/dawn/native/metal/SharedTextureMemoryMTL.mm
@@ -220,24 +220,19 @@
     // is asked to create must be 2D/single-sampled/array length of 1/single
     // mipmap level.
     if (!format->IsMultiPlanar()) {
-        // Create the descriptor for the Metal texture.
-        NSRef<MTLTextureDescriptor> mtlDescRef = AcquireNSRef([MTLTextureDescriptor new]);
-        MTLTextureDescriptor* mtlDesc = mtlDescRef.Get();
+        mMtlUsage = kMetalTextureUsage;
+        mMtlFormat = MetalPixelFormat(device, format->format);
 
-        mtlDesc.storageMode = IOSurfaceStorageMode();
-        mtlDesc.width = properties.size.width;
-        mtlDesc.height = properties.size.height;
+        // Create the descriptor for the Metal texture.
+        auto mtlDesc =
+            [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:mMtlFormat
+                                                               width:properties.size.width
+                                                              height:properties.size.height
+                                                           mipmapped:NO];
         // NOTE: MetalTextureDescriptor defaults to the values mentioned above
         // for the given parameters, so none of these need to be set explicitly.
+        mtlDesc.usage = mMtlUsage;
 
-        // Metal only allows format reinterpretation to happen on swizzle pattern or conversion
-        // between linear space and sRGB. For example, creating bgra8Unorm texture view on
-        // rgba8Unorm texture or creating rgba8Unorm_srgb texture view on rgab8Unorm texture.
-        mtlDesc.usage = kMetalTextureUsage;
-        mtlDesc.pixelFormat = MetalPixelFormat(device, format->format);
-
-        mMtlUsage = mtlDesc.usage;
-        mMtlFormat = mtlDesc.pixelFormat;
         mMtlPlaneTextures.resize(1);
         mMtlPlaneTextures[0] =
             AcquireNSPRef([device->GetMTLDevice() newTextureWithDescriptor:mtlDesc
@@ -247,11 +242,11 @@
         mMtlUsage = kMetalTextureUsage;
         // Multiplanar format doesn't have equivalent MTLPixelFormat so just set it to invalid.
         mMtlFormat = MTLPixelFormatInvalid;
-        const size_t numPlanes = IOSurfaceGetPlaneCount(mIOSurface.Get());
-        mMtlPlaneTextures.resize(numPlanes);
-        for (size_t plane = 0; plane < numPlanes; ++plane) {
-            mMtlPlaneTextures[plane] = AcquireNSPRef(CreateTextureMtlForPlane(
-                mMtlUsage, *format, plane, device, /*sampleCount=*/1, mIOSurface.Get()));
+
+        mMtlPlaneTextures.resize(IOSurfaceGetPlaneCount(mIOSurface.Get()));
+        for (size_t plane = 0; plane < mMtlPlaneTextures.size(); ++plane) {
+            mMtlPlaneTextures[plane] = AcquireNSPRef(
+                CreateTextureMtlForPlane(mMtlUsage, *format, plane, device, mIOSurface.Get()));
             if (mMtlPlaneTextures[plane] == nil) {
                 return DAWN_INTERNAL_ERROR("Failed to create MTLTexture plane view for IOSurface.");
             }
diff --git a/src/dawn/native/metal/TextureMTL.mm b/src/dawn/native/metal/TextureMTL.mm
index 8fe5973..ee51c1a 100644
--- a/src/dawn/native/metal/TextureMTL.mm
+++ b/src/dawn/native/metal/TextureMTL.mm
@@ -368,8 +368,8 @@
         const size_t numPlanes = IOSurfaceGetPlaneCount(GetIOSurface());
         mMtlPlaneTextures.resize(numPlanes);
         for (size_t plane = 0; plane < numPlanes; ++plane) {
-            mMtlPlaneTextures[plane] = AcquireNSPRef(CreateTextureMtlForPlane(
-                mMtlUsage, GetFormat(), plane, device, GetSampleCount(), GetIOSurface()));
+            mMtlPlaneTextures[plane] = AcquireNSPRef(
+                CreateTextureMtlForPlane(mMtlUsage, GetFormat(), plane, device, GetIOSurface()));
             if (mMtlPlaneTextures[plane] == nil) {
                 return DAWN_INTERNAL_ERROR("Failed to create MTLTexture plane view for IOSurface.");
             }
diff --git a/src/dawn/native/metal/UtilsMetal.h b/src/dawn/native/metal/UtilsMetal.h
index f52866b..61e8a6c 100644
--- a/src/dawn/native/metal/UtilsMetal.h
+++ b/src/dawn/native/metal/UtilsMetal.h
@@ -130,13 +130,10 @@
                                  EncodeInsideRenderPass encodeInside,
                                  BeginRenderPassCmd* renderPassCmd = nullptr);
 
-MTLStorageMode IOSurfaceStorageMode();
-
 id<MTLTexture> CreateTextureMtlForPlane(MTLTextureUsage mtlUsage,
                                         const Format& format,
                                         size_t plane,
                                         Device* device,
-                                        uint32_t sampleCount,
                                         IOSurfaceRef ioSurface);
 
 MaybeError EncodeEmptyMetalRenderPass(Device* device,
diff --git a/src/dawn/native/metal/UtilsMetal.mm b/src/dawn/native/metal/UtilsMetal.mm
index 4b6607f..0dd49c4 100644
--- a/src/dawn/native/metal/UtilsMetal.mm
+++ b/src/dawn/native/metal/UtilsMetal.mm
@@ -813,41 +813,23 @@
     return [device supportsCounterSampling:MTLCounterSamplingPointAtStageBoundary];
 }
 
-MTLStorageMode IOSurfaceStorageMode() {
-#if DAWN_PLATFORM_IS(MACOS)
-    return MTLStorageModeManaged;
-#elif DAWN_PLATFORM_IS(IOS)
-    return MTLStorageModePrivate;
-#else
-#error "Unsupported Apple platform."
-#endif
-}
-
 id<MTLTexture> CreateTextureMtlForPlane(MTLTextureUsage mtlUsage,
                                         const Format& format,
                                         size_t plane,
                                         Device* device,
-                                        uint32_t sampleCount,
                                         IOSurfaceRef ioSurface) {
     Aspect aspect = GetPlaneAspect(format, plane);
     const auto& aspectInfo = format.GetAspectInfo(aspect);
 
-    NSRef<MTLTextureDescriptor> mtlDescRef = AcquireNSRef([MTLTextureDescriptor new]);
-    MTLTextureDescriptor* mtlDesc = mtlDescRef.Get();
-
-    mtlDesc.sampleCount = sampleCount;
-    mtlDesc.usage = mtlUsage;
-    mtlDesc.pixelFormat = MetalPixelFormat(device, aspectInfo.format);
-    mtlDesc.storageMode = IOSurfaceStorageMode();
-
-    mtlDesc.width = IOSurfaceGetWidthOfPlane(ioSurface, plane);
-    mtlDesc.height = IOSurfaceGetHeightOfPlane(ioSurface, plane);
-
     // Multiplanar texture is validated to only have single layer, single mipLevel
     // and 2d textures (depth == 1)
-    mtlDesc.mipmapLevelCount = 1;
-    mtlDesc.arrayLength = 1;
-    mtlDesc.depth = 1;
+    auto mtlDesc = [MTLTextureDescriptor
+        texture2DDescriptorWithPixelFormat:MetalPixelFormat(device, aspectInfo.format)
+                                     width:IOSurfaceGetWidthOfPlane(ioSurface, plane)
+                                    height:IOSurfaceGetHeightOfPlane(ioSurface, plane)
+                                 mipmapped:NO];
+    mtlDesc.usage = mtlUsage;
+
     return [device->GetMTLDevice() newTextureWithDescriptor:mtlDesc
                                                   iosurface:ioSurface
                                                       plane:plane];