diff --git a/src/dawn/native/d3d11/BufferD3D11.cpp b/src/dawn/native/d3d11/BufferD3D11.cpp index 233c195e0d..4b135c06ce 100644 --- a/src/dawn/native/d3d11/BufferD3D11.cpp +++ b/src/dawn/native/d3d11/BufferD3D11.cpp @@ -196,6 +196,24 @@ MaybeError Buffer::Initialize(bool mappedAtCreation) { 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 055b2ba76a..e45d35a645 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 d3d11DeviceContext; - device->GetD3D11Device()->GetImmediateContext(&d3d11DeviceContext); - - ComPtr 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 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 d3d11DeviceContext; + device->GetD3D11Device()->GetImmediateContext(&d3d11DeviceContext); + + ComPtr 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 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 c3d2623e0a..9c9e1e0ca9 100644 --- a/src/dawn/native/d3d11/CommandRecordingContextD3D11.h +++ b/src/dawn/native/d3d11/CommandRecordingContextD3D11.h @@ -26,7 +26,7 @@ class Device; 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 d724920043..0d7d9d1358 100644 --- a/src/dawn/native/d3d11/DeviceD3D11.cpp +++ b/src/dawn/native/d3d11/DeviceD3D11.cpp @@ -121,7 +121,7 @@ MaybeError Device::Initialize(const DeviceDescriptor* descriptor) { // Create the fence event. mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); - DAWN_TRY(PreparePendingCommandContext()); + DAWN_TRY(mPendingCommands.Intialize(this)); SetLabelImpl(); @@ -149,13 +149,6 @@ CommandRecordingContext* Device::GetPendingCommandContext(Device::SubmitMode sub 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 4753d56ebb..a56cf54020 100644 --- a/src/dawn/native/d3d11/DeviceD3D11.h +++ b/src/dawn/native/d3d11/DeviceD3D11.h @@ -42,7 +42,6 @@ class Device final : public d3d::Device { 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 2f33fae530..f70b9f66b4 100644 --- a/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp +++ b/src/dawn/tests/end2end/NonzeroBufferCreationTests.cpp @@ -129,6 +129,8 @@ TEST_P(NonzeroBufferCreationTests, BufferCreationWithMappedAtCreation) { } 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"},