From 111ba65a5e3dbd23a58dd63991e92e9300f44d13 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 13 Jan 2021 08:53:34 +0000 Subject: [PATCH] D3D12: Use Texture::Create pattern for swapchain textures Bug: dawn:269 Change-Id: Ia4b48126c153ddff2feefc1bf04dcc9adc783bd1 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/37421 Auto-Submit: Corentin Wallez Reviewed-by: Austin Eng Reviewed-by: Rafael Cintron Reviewed-by: Corentin Wallez Commit-Queue: Corentin Wallez --- src/dawn_native/d3d12/DeviceD3D12.cpp | 8 ++-- src/dawn_native/d3d12/SwapChainD3D12.cpp | 12 +++++- src/dawn_native/d3d12/TextureD3D12.cpp | 49 +++++++++++++++--------- src/dawn_native/d3d12/TextureD3D12.h | 21 +++++----- 4 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index a03b04874c..c41c87ffac 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -446,13 +446,13 @@ namespace dawn_native { namespace d3d12 { HANDLE sharedHandle, ExternalMutexSerial acquireMutexKey, bool isSwapChainTexture) { - Ref dawnTexture; + Ref dawnTexture; if (ConsumedError(Texture::Create(this, descriptor, sharedHandle, acquireMutexKey, isSwapChainTexture), - &dawnTexture)) + &dawnTexture)) { return nullptr; - - return dawnTexture; + } + return {dawnTexture}; } // We use IDXGIKeyedMutexes to synchronize access between D3D11 and D3D12. D3D11/12 fences diff --git a/src/dawn_native/d3d12/SwapChainD3D12.cpp b/src/dawn_native/d3d12/SwapChainD3D12.cpp index 74798059df..4d890bfa10 100644 --- a/src/dawn_native/d3d12/SwapChainD3D12.cpp +++ b/src/dawn_native/d3d12/SwapChainD3D12.cpp @@ -36,16 +36,24 @@ namespace dawn_native { namespace d3d12 { } TextureBase* SwapChain::GetNextTextureImpl(const TextureDescriptor* descriptor) { + DeviceBase* device = GetDevice(); const auto& im = GetImplementation(); DawnSwapChainNextTexture next = {}; DawnSwapChainError error = im.GetNextTexture(im.userData, &next); if (error) { - GetDevice()->HandleError(InternalErrorType::Internal, error); + device->HandleError(InternalErrorType::Internal, error); return nullptr; } ComPtr d3d12Texture = static_cast(next.texture.ptr); - return new Texture(ToBackend(GetDevice()), descriptor, std::move(d3d12Texture)); + Ref dawnTexture; + if (device->ConsumedError( + Texture::Create(ToBackend(GetDevice()), descriptor, std::move(d3d12Texture)), + &dawnTexture)) { + return nullptr; + } + + return dawnTexture.Detach(); } MaybeError SwapChain::OnBeforePresent(TextureViewBase* view) { diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index 0cac08bd20..675577dbea 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -378,19 +378,21 @@ namespace dawn_native { namespace d3d12 { return {}; } - ResultOrError> Texture::Create(Device* device, - const TextureDescriptor* descriptor) { + // static + ResultOrError> Texture::Create(Device* device, + const TextureDescriptor* descriptor) { Ref dawnTexture = AcquireRef(new Texture(device, descriptor, TextureState::OwnedInternal)); DAWN_TRY(dawnTexture->InitializeAsInternalTexture()); return std::move(dawnTexture); } - ResultOrError> Texture::Create(Device* device, - const ExternalImageDescriptor* descriptor, - HANDLE sharedHandle, - ExternalMutexSerial acquireMutexKey, - bool isSwapChainTexture) { + // static + ResultOrError> Texture::Create(Device* device, + const ExternalImageDescriptor* descriptor, + HANDLE sharedHandle, + ExternalMutexSerial acquireMutexKey, + bool isSwapChainTexture) { const TextureDescriptor* textureDescriptor = reinterpret_cast(descriptor->cTextureDescriptor); @@ -403,6 +405,16 @@ namespace dawn_native { namespace d3d12 { return std::move(dawnTexture); } + // static + ResultOrError> Texture::Create(Device* device, + const TextureDescriptor* descriptor, + ComPtr d3d12Texture) { + Ref dawnTexture = + AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal)); + DAWN_TRY(dawnTexture->InitializeAsSwapChainTexture(std::move(d3d12Texture))); + return std::move(dawnTexture); + } + MaybeError Texture::InitializeAsExternalTexture(const TextureDescriptor* descriptor, HANDLE sharedHandle, ExternalMutexSerial acquireMutexKey, @@ -485,25 +497,24 @@ namespace dawn_native { namespace d3d12 { return {}; } - Texture::Texture(Device* device, const TextureDescriptor* descriptor, TextureState state) - : TextureBase(device, descriptor, state), - mSubresourceStateAndDecay( - GetSubresourceCount(), - {D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON, kMaxExecutionSerial, false}) { - } - - Texture::Texture(Device* device, - const TextureDescriptor* descriptor, - ComPtr nativeTexture) - : Texture(device, descriptor, TextureState::OwnedExternal) { + MaybeError Texture::InitializeAsSwapChainTexture(ComPtr d3d12Texture) { AllocationInfo info; info.mMethod = AllocationMethod::kExternal; // When creating the ResourceHeapAllocation, the resource heap is set to nullptr because the // texture is owned externally. The texture's owning entity must remain responsible for // memory management. - mResourceAllocation = {info, 0, std::move(nativeTexture), nullptr}; + mResourceAllocation = { info, 0, std::move(d3d12Texture), nullptr }; SetIsSubresourceContentInitialized(true, GetAllSubresources()); + + return {}; + } + + Texture::Texture(Device* device, const TextureDescriptor* descriptor, TextureState state) + : TextureBase(device, descriptor, state), + mSubresourceStateAndDecay( + GetSubresourceCount(), + {D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON, kMaxExecutionSerial, false}) { } Texture::~Texture() { diff --git a/src/dawn_native/d3d12/TextureD3D12.h b/src/dawn_native/d3d12/TextureD3D12.h index 5d7aab7d43..79539eb0a8 100644 --- a/src/dawn_native/d3d12/TextureD3D12.h +++ b/src/dawn_native/d3d12/TextureD3D12.h @@ -36,16 +36,16 @@ namespace dawn_native { namespace d3d12 { class Texture final : public TextureBase { public: - static ResultOrError> Create(Device* device, - const TextureDescriptor* descriptor); - static ResultOrError> Create(Device* device, - const ExternalImageDescriptor* descriptor, - HANDLE sharedHandle, - ExternalMutexSerial acquireMutexKey, - bool isSwapChainTexture); - Texture(Device* device, - const TextureDescriptor* descriptor, - ComPtr d3d12Texture); + static ResultOrError> Create(Device* device, + const TextureDescriptor* descriptor); + static ResultOrError> Create(Device* device, + const ExternalImageDescriptor* descriptor, + HANDLE sharedHandle, + ExternalMutexSerial acquireMutexKey, + bool isSwapChainTexture); + static ResultOrError> Create(Device* device, + const TextureDescriptor* descriptor, + ComPtr d3d12Texture); DXGI_FORMAT GetD3D12Format() const; ID3D12Resource* GetD3D12Resource() const; @@ -88,6 +88,7 @@ namespace dawn_native { namespace d3d12 { HANDLE sharedHandle, ExternalMutexSerial acquireMutexKey, bool isSwapChainTexture); + MaybeError InitializeAsSwapChainTexture(ComPtr d3d12Texture); // Dawn API void DestroyImpl() override;