Add workaround for depth stencil textures created on non-zero heap on Intel GPUs

This patch always adds RenderAttachment usage for depth stencil
textures which are created on a non-zero heap and initialized with
non-zero value because for such textures, their first usage will
also be as copy destination, so it will also trigger the Intel driver
issue about copying data into a placed depth stencil texture with a
dirty memory.

This patch also combines two pathes that will clear a texture into one
to skip one extra clear.

Bug: dawn:484, dawn:1487
Test: dawn_end2end_tests/Nonzero*TextureCreationTests
Change-Id: I915954a739e2b939946595dc722c3296bc4b5ffa
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/147883
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/dawn/native/d3d12/TextureD3D12.cpp b/src/dawn/native/d3d12/TextureD3D12.cpp
index 9a39a07..91a3547 100644
--- a/src/dawn/native/d3d12/TextureD3D12.cpp
+++ b/src/dawn/native/d3d12/TextureD3D12.cpp
@@ -239,9 +239,16 @@
     resourceDescriptor.DepthOrArraySize = size.depthOrArrayLayers;
 
     Device* device = ToBackend(GetDevice());
+    // When the depth stencil texture is created on a not-zeroed heap, its first usage will also be
+    // copy destination when it is initialized with a non-zero value, which also triggered the issue
+    // about copying data into a placed depth stencil texture with a dirty memory, so in this
+    // situation the workaround should also be enabled.
     bool applyForceClearCopyableDepthStencilTextureOnCreationToggle =
         device->IsToggleEnabled(Toggle::D3D12ForceClearCopyableDepthStencilTextureOnCreation) &&
-        GetFormat().HasDepthOrStencil() && (GetInternalUsage() & wgpu::TextureUsage::CopyDst);
+        GetFormat().HasDepthOrStencil() &&
+        ((GetInternalUsage() & wgpu::TextureUsage::CopyDst) ||
+         (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting) &&
+          device->IsToggleEnabled(Toggle::D3D12CreateNotZeroedHeap)));
     if (applyForceClearCopyableDepthStencilTextureOnCreationToggle) {
         AddInternalUsage(wgpu::TextureUsage::RenderAttachment);
     }
@@ -282,18 +289,15 @@
 
     SetLabelImpl();
 
-    if (applyForceClearCopyableDepthStencilTextureOnCreationToggle) {
+    if (applyForceClearCopyableDepthStencilTextureOnCreationToggle ||
+        device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
         CommandRecordingContext* commandContext;
         DAWN_TRY_ASSIGN(commandContext, device->GetPendingCommandContext());
-        DAWN_TRY(ClearTexture(commandContext, GetAllSubresources(), TextureBase::ClearValue::Zero));
-    }
-
-    if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
-        CommandRecordingContext* commandContext;
-        DAWN_TRY_ASSIGN(commandContext, device->GetPendingCommandContext());
-
-        DAWN_TRY(
-            ClearTexture(commandContext, GetAllSubresources(), TextureBase::ClearValue::NonZero));
+        ClearValue clearValue =
+            device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)
+                ? ClearValue::NonZero
+                : ClearValue::Zero;
+        DAWN_TRY(ClearTexture(commandContext, GetAllSubresources(), clearValue));
     }
 
     return {};