d3d11: enable NonzeroBufferCreationTests

Bug: dawn:1705
Change-Id: I5c6f77a9728fd47593716a1ce2ea69a8960178ef
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/133964
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Peng Huang <penghuang@chromium.org>
Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/src/dawn/native/d3d11/BufferD3D11.cpp b/src/dawn/native/d3d11/BufferD3D11.cpp
index 233c195..4b135c0 100644
--- a/src/dawn/native/d3d11/BufferD3D11.cpp
+++ b/src/dawn/native/d3d11/BufferD3D11.cpp
@@ -196,6 +196,24 @@
     ASSERT(mD3d11NonConstantBuffer || mD3d11ConstantBuffer);
 
     SetLabelImpl();
+
+    if (!mappedAtCreation) {
+        if (GetDevice()->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting)) {
+            DAWN_TRY(ClearInternal(ToBackend(GetDevice())->GetPendingCommandContext(), 1u));
+        }
+
+        // Initialize the padding bytes to zero.
+        if (GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) {
+            uint32_t paddingBytes = GetAllocatedSize() - GetSize();
+            if (paddingBytes > 0) {
+                uint32_t clearSize = paddingBytes;
+                uint64_t clearOffset = GetSize();
+                DAWN_TRY(ClearInternal(ToBackend(GetDevice())->GetPendingCommandContext(), 0,
+                                       clearOffset, clearSize));
+            }
+        }
+    }
+
     return {};
 }
 
diff --git a/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp b/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
index 055b2ba..e45d35a 100644
--- a/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
+++ b/src/dawn/native/d3d11/CommandRecordingContextD3D11.cpp
@@ -27,53 +27,51 @@
 
 namespace dawn::native::d3d11 {
 
-MaybeError CommandRecordingContext::Open(Device* device) {
+MaybeError CommandRecordingContext::Intialize(Device* device) {
     ASSERT(!IsOpen());
     ASSERT(device);
     mDevice = device;
-
-    if (!mD3D11DeviceContext4) {
-        ID3D11Device* d3d11Device = device->GetD3D11Device();
-
-        ComPtr<ID3D11DeviceContext> d3d11DeviceContext;
-        device->GetD3D11Device()->GetImmediateContext(&d3d11DeviceContext);
-
-        ComPtr<ID3D11DeviceContext4> d3d11DeviceContext4;
-        DAWN_TRY(
-            CheckHRESULT(d3d11DeviceContext.As(&d3d11DeviceContext4),
-                         "D3D11 querying immediate context for ID3D11DeviceContext4 interface"));
-
-        DAWN_TRY(CheckHRESULT(
-            d3d11DeviceContext4.As(&mD3D11UserDefinedAnnotation),
-            "D3D11 querying immediate context for ID3DUserDefinedAnnotation interface"));
-
-        // Create a uniform buffer for built in variables.
-        BufferDescriptor descriptor;
-        // The maximum number of builtin elements is 4 (vec4). It must be multiple of 4.
-        constexpr size_t kMaxNumBuiltinElements = 4;
-        descriptor.size = sizeof(uint32_t) * kMaxNumBuiltinElements;
-        descriptor.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
-        descriptor.mappedAtCreation = false;
-        descriptor.label = "builtin uniform buffer";
-        Ref<BufferBase> uniformBuffer;
-        DAWN_TRY_ASSIGN(uniformBuffer, device->CreateBuffer(&descriptor));
-
-        mD3D11Device = d3d11Device;
-        mD3D11DeviceContext4 = std::move(d3d11DeviceContext4);
-        mUniformBuffer = ToBackend(std::move(uniformBuffer));
-
-        // Always bind the uniform buffer to the reserved slot for all pipelines.
-        // This buffer will be updated with the correct values before each draw or dispatch call.
-        ID3D11Buffer* bufferPtr = mUniformBuffer->GetD3D11ConstantBuffer();
-        mD3D11DeviceContext4->VSSetConstantBuffers(PipelineLayout::kReservedConstantBufferSlot, 1,
-                                                   &bufferPtr);
-        mD3D11DeviceContext4->CSSetConstantBuffers(PipelineLayout::kReservedConstantBufferSlot, 1,
-                                                   &bufferPtr);
-    }
-
-    mIsOpen = true;
     mNeedsSubmit = false;
 
+    ID3D11Device* d3d11Device = device->GetD3D11Device();
+
+    ComPtr<ID3D11DeviceContext> d3d11DeviceContext;
+    device->GetD3D11Device()->GetImmediateContext(&d3d11DeviceContext);
+
+    ComPtr<ID3D11DeviceContext4> d3d11DeviceContext4;
+    DAWN_TRY(CheckHRESULT(d3d11DeviceContext.As(&d3d11DeviceContext4),
+                          "D3D11 querying immediate context for ID3D11DeviceContext4 interface"));
+
+    DAWN_TRY(
+        CheckHRESULT(d3d11DeviceContext4.As(&mD3D11UserDefinedAnnotation),
+                     "D3D11 querying immediate context for ID3DUserDefinedAnnotation interface"));
+
+    mD3D11Device = d3d11Device;
+    mD3D11DeviceContext4 = std::move(d3d11DeviceContext4);
+    mIsOpen = true;
+
+    // Create a uniform buffer for built in variables.
+    BufferDescriptor descriptor;
+    // The maximum number of builtin elements is 4 (vec4). It must be multiple of 4.
+    constexpr size_t kMaxNumBuiltinElements = 4;
+    descriptor.size = sizeof(uint32_t) * kMaxNumBuiltinElements;
+    descriptor.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
+    descriptor.mappedAtCreation = false;
+    descriptor.label = "builtin uniform buffer";
+
+    Ref<BufferBase> uniformBuffer;
+    DAWN_TRY_ASSIGN(uniformBuffer, device->CreateBuffer(&descriptor));
+
+    mUniformBuffer = ToBackend(std::move(uniformBuffer));
+
+    // Always bind the uniform buffer to the reserved slot for all pipelines.
+    // This buffer will be updated with the correct values before each draw or dispatch call.
+    ID3D11Buffer* bufferPtr = mUniformBuffer->GetD3D11ConstantBuffer();
+    mD3D11DeviceContext4->VSSetConstantBuffers(PipelineLayout::kReservedConstantBufferSlot, 1,
+                                               &bufferPtr);
+    mD3D11DeviceContext4->CSSetConstantBuffers(PipelineLayout::kReservedConstantBufferSlot, 1,
+                                               &bufferPtr);
+
     return {};
 }
 
diff --git a/src/dawn/native/d3d11/CommandRecordingContextD3D11.h b/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
index c3d2623..9c9e1e0 100644
--- a/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
+++ b/src/dawn/native/d3d11/CommandRecordingContextD3D11.h
@@ -26,7 +26,7 @@
 
 class CommandRecordingContext {
   public:
-    MaybeError Open(Device* device);
+    MaybeError Intialize(Device* device);
 
     void Release();
     bool IsOpen() const;
diff --git a/src/dawn/native/d3d11/DeviceD3D11.cpp b/src/dawn/native/d3d11/DeviceD3D11.cpp
index d724920..0d7d9d1 100644
--- a/src/dawn/native/d3d11/DeviceD3D11.cpp
+++ b/src/dawn/native/d3d11/DeviceD3D11.cpp
@@ -121,7 +121,7 @@
     // Create the fence event.
     mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
 
-    DAWN_TRY(PreparePendingCommandContext());
+    DAWN_TRY(mPendingCommands.Intialize(this));
 
     SetLabelImpl();
 
@@ -149,13 +149,6 @@
     return &mPendingCommands;
 }
 
-MaybeError Device::PreparePendingCommandContext() {
-    if (!mPendingCommands.IsOpen()) {
-        DAWN_TRY(mPendingCommands.Open(this));
-    }
-    return {};
-}
-
 MaybeError Device::TickImpl() {
     // Perform cleanup operations to free unused objects
     [[maybe_unused]] ExecutionSerial completedSerial = GetCompletedCommandSerial();
diff --git a/src/dawn/native/d3d11/DeviceD3D11.h b/src/dawn/native/d3d11/DeviceD3D11.h
index 4753d56..a56cf54 100644
--- a/src/dawn/native/d3d11/DeviceD3D11.h
+++ b/src/dawn/native/d3d11/DeviceD3D11.h
@@ -42,7 +42,6 @@
     ID3D11Device5* GetD3D11Device5() const;
 
     CommandRecordingContext* GetPendingCommandContext(SubmitMode submitMode = SubmitMode::Normal);
-    MaybeError PreparePendingCommandContext();
 
     const DeviceInfo& GetDeviceInfo() const;
 
diff --git a/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp b/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp
index 2f33fae..f70b9f6 100644
--- a/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp
+++ b/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp
@@ -129,6 +129,8 @@
 }
 
 DAWN_INSTANTIATE_TEST(NonzeroBufferCreationTests,
+                      D3D11Backend({"nonzero_clear_resources_on_creation_for_testing"},
+                                   {"lazy_clear_resource_on_first_use"}),
                       D3D12Backend({"nonzero_clear_resources_on_creation_for_testing"},
                                    {"lazy_clear_resource_on_first_use"}),
                       MetalBackend({"nonzero_clear_resources_on_creation_for_testing"},