Metal: Add CommandRecordingContext argument to ClearTexture

This is more consistent with the other backends that have a
CommandRecordingContext.

Bug: dawn:780
Change-Id: Icb689d00cc4d873a489fc4d4c247feeb15f8797c
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/50120
Commit-Queue: Stephen White <senorblanco@chromium.org>
Auto-Submit: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
diff --git a/src/dawn_native/metal/CommandBufferMTL.mm b/src/dawn_native/metal/CommandBufferMTL.mm
index 5e40668..d7efd83 100644
--- a/src/dawn_native/metal/CommandBufferMTL.mm
+++ b/src/dawn_native/metal/CommandBufferMTL.mm
@@ -565,7 +565,7 @@
                 scope.textureUsages[i].Iterate(
                     [&](const SubresourceRange& range, wgpu::TextureUsage usage) {
                         if (usage & ~wgpu::TextureUsage::RenderAttachment) {
-                            texture->EnsureSubresourceContentInitialized(range);
+                            texture->EnsureSubresourceContentInitialized(commandContext, range);
                         }
                     });
             }
@@ -634,7 +634,8 @@
                     Texture* texture = ToBackend(dst.texture.Get());
 
                     buffer->EnsureDataInitialized(commandContext);
-                    EnsureDestinationTextureInitialized(texture, copy->destination, copy->copySize);
+                    EnsureDestinationTextureInitialized(commandContext, texture, copy->destination,
+                                                        copy->copySize);
 
                     TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit(
                         texture, dst.mipLevel, dst.origin, copySize, buffer->GetSize(), src.offset,
@@ -684,7 +685,7 @@
                     buffer->EnsureDataInitializedAsDestination(commandContext, copy);
 
                     texture->EnsureSubresourceContentInitialized(
-                        GetSubresourcesAffectedByCopy(src, copySize));
+                        commandContext, GetSubresourcesAffectedByCopy(src, copySize));
 
                     TextureBufferCopySplit splitCopies = ComputeTextureBufferCopySplit(
                         texture, src.mipLevel, src.origin, copySize, buffer->GetSize(), dst.offset,
@@ -730,9 +731,10 @@
                     Texture* dstTexture = ToBackend(copy->destination.texture.Get());
 
                     srcTexture->EnsureSubresourceContentInitialized(
+                        commandContext,
                         GetSubresourcesAffectedByCopy(copy->source, copy->copySize));
-                    EnsureDestinationTextureInitialized(dstTexture, copy->destination,
-                                                        copy->copySize);
+                    EnsureDestinationTextureInitialized(commandContext, dstTexture,
+                                                        copy->destination, copy->copySize);
 
                     // TODO(jiawei.shao@intel.com): support copies with 1D and 3D textures.
                     ASSERT(srcTexture->GetDimension() == wgpu::TextureDimension::e2D &&
diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm
index 5a8b3a1..514c804 100644
--- a/src/dawn_native/metal/DeviceMTL.mm
+++ b/src/dawn_native/metal/DeviceMTL.mm
@@ -306,7 +306,8 @@
         ASSERT(dataLayout.bytesPerRow ==
                copySizePixels.width / blockInfo.width * blockInfo.byteSize);
 
-        EnsureDestinationTextureInitialized(texture, *dst, copySizePixels);
+        EnsureDestinationTextureInitialized(GetPendingCommandContext(), texture, *dst,
+                                            copySizePixels);
 
         // Metal validation layer requires that if the texture's pixel format is a compressed
         // format, the sourceSize must be a multiple of the pixel format's block size or be
diff --git a/src/dawn_native/metal/TextureMTL.h b/src/dawn_native/metal/TextureMTL.h
index 1265a4e..9c0f4b3 100644
--- a/src/dawn_native/metal/TextureMTL.h
+++ b/src/dawn_native/metal/TextureMTL.h
@@ -25,6 +25,7 @@
 
 namespace dawn_native { namespace metal {
 
+    class CommandRecordingContext;
     class Device;
 
     MTLPixelFormat MetalPixelFormat(wgpu::TextureFormat format);
@@ -48,7 +49,8 @@
 
         id<MTLTexture> GetMTLTexture();
 
-        void EnsureSubresourceContentInitialized(const SubresourceRange& range);
+        void EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext,
+                                                 const SubresourceRange& range);
 
       private:
         Texture(Device* device, const TextureDescriptor* descriptor);
@@ -56,7 +58,9 @@
 
         void DestroyImpl() override;
 
-        MaybeError ClearTexture(const SubresourceRange& range, TextureBase::ClearValue clearValue);
+        MaybeError ClearTexture(CommandRecordingContext* commandContext,
+                                const SubresourceRange& range,
+                                TextureBase::ClearValue clearValue);
 
         NSPRef<id<MTLTexture>> mMtlTexture;
     };
diff --git a/src/dawn_native/metal/TextureMTL.mm b/src/dawn_native/metal/TextureMTL.mm
index 29485ba..2c0de28 100644
--- a/src/dawn_native/metal/TextureMTL.mm
+++ b/src/dawn_native/metal/TextureMTL.mm
@@ -359,8 +359,9 @@
             AcquireNSPRef([device->GetMTLDevice() newTextureWithDescriptor:mtlDesc.Get()]);
 
         if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
-            device->ConsumedError(
-                ClearTexture(GetAllSubresources(), TextureBase::ClearValue::NonZero));
+            device->ConsumedError(ClearTexture(device->GetPendingCommandContext(),
+                                               GetAllSubresources(),
+                                               TextureBase::ClearValue::NonZero));
         }
     }
 
@@ -401,12 +402,11 @@
         return mMtlTexture.Get();
     }
 
-    MaybeError Texture::ClearTexture(const SubresourceRange& range,
+    MaybeError Texture::ClearTexture(CommandRecordingContext* commandContext,
+                                     const SubresourceRange& range,
                                      TextureBase::ClearValue clearValue) {
         Device* device = ToBackend(GetDevice());
 
-        CommandRecordingContext* commandContext = device->GetPendingCommandContext();
-
         const uint8_t clearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0 : 1;
         const double dClearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0.0 : 1.0;
 
@@ -595,14 +595,16 @@
         return {};
     }
 
-    void Texture::EnsureSubresourceContentInitialized(const SubresourceRange& range) {
+    void Texture::EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext,
+                                                      const SubresourceRange& range) {
         if (!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
             return;
         }
         if (!IsSubresourceContentInitialized(range)) {
             // If subresource has not been initialized, clear it to black as it could
             // contain dirty bits from recycled memory
-            GetDevice()->ConsumedError(ClearTexture(range, TextureBase::ClearValue::Zero));
+            GetDevice()->ConsumedError(
+                ClearTexture(commandContext, range, TextureBase::ClearValue::Zero));
         }
     }
 
diff --git a/src/dawn_native/metal/UtilsMetal.h b/src/dawn_native/metal/UtilsMetal.h
index a7a5dee..c5d2a58 100644
--- a/src/dawn_native/metal/UtilsMetal.h
+++ b/src/dawn_native/metal/UtilsMetal.h
@@ -50,7 +50,8 @@
                                                          uint32_t rowsPerImage,
                                                          Aspect aspect);
 
-    void EnsureDestinationTextureInitialized(Texture* texture,
+    void EnsureDestinationTextureInitialized(CommandRecordingContext* commandContext,
+                                             Texture* texture,
                                              const TextureCopy& dst,
                                              const Extent3D& size);
 
diff --git a/src/dawn_native/metal/UtilsMetal.mm b/src/dawn_native/metal/UtilsMetal.mm
index bc41ec5..40b23bc 100644
--- a/src/dawn_native/metal/UtilsMetal.mm
+++ b/src/dawn_native/metal/UtilsMetal.mm
@@ -154,7 +154,8 @@
         return copy;
     }
 
-    void EnsureDestinationTextureInitialized(Texture* texture,
+    void EnsureDestinationTextureInitialized(CommandRecordingContext* commandContext,
+                                             Texture* texture,
                                              const TextureCopy& dst,
                                              const Extent3D& size) {
         ASSERT(texture == dst.texture.Get());
@@ -162,7 +163,7 @@
         if (IsCompleteSubresourceCopiedTo(dst.texture.Get(), size, dst.mipLevel)) {
             texture->SetIsSubresourceContentInitialized(true, range);
         } else {
-            texture->EnsureSubresourceContentInitialized(range);
+            texture->EnsureSubresourceContentInitialized(commandContext, range);
         }
     }