From cabd60d991487b64ddc99dc283b9fa2090d668e9 Mon Sep 17 00:00:00 2001 From: Sunny Sachanandani Date: Tue, 8 Jun 2021 21:03:17 +0000 Subject: [PATCH] Add releaseMutexKey to ExternalImageAccessDescriptorDXGIKeyedMutex This allows the client to specify a different release key for the keyed mutex than the default of acquire key + 1. If the release key is the sentinel value of UINT64_MAX, it's set to acquire key + 1. Once Chromium transitions to always specifying the release key, it will be made a required parameter and the default behavior will be removed. Bug: chromium:1213977 Change-Id: I327f7157bb7ff23cf216e43043568ce7c6f38a60 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/53880 Reviewed-by: Austin Eng Reviewed-by: Corentin Wallez Commit-Queue: Sunny Sachanandani --- src/dawn_native/d3d12/D3D12Backend.cpp | 9 +++++++- src/dawn_native/d3d12/DeviceD3D12.cpp | 15 ++++++------ src/dawn_native/d3d12/DeviceD3D12.h | 1 + src/dawn_native/d3d12/TextureD3D12.cpp | 32 ++++++++++++++------------ src/dawn_native/d3d12/TextureD3D12.h | 3 +++ src/include/dawn_native/D3D12Backend.h | 2 ++ 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/dawn_native/d3d12/D3D12Backend.cpp b/src/dawn_native/d3d12/D3D12Backend.cpp index 5fc2e24ec4..a363757f02 100644 --- a/src/dawn_native/d3d12/D3D12Backend.cpp +++ b/src/dawn_native/d3d12/D3D12Backend.cpp @@ -84,9 +84,16 @@ namespace dawn_native { namespace d3d12 { textureDescriptor.mipLevelCount = mMipLevelCount; textureDescriptor.sampleCount = mSampleCount; + // Set the release key to acquire key + 1 if not set. This allows supporting the old keyed + // mutex protocol during the transition to making this a required parameter. + ExternalMutexSerial releaseMutexKey = + (descriptor->releaseMutexKey != UINT64_MAX) + ? ExternalMutexSerial(descriptor->releaseMutexKey) + : ExternalMutexSerial(descriptor->acquireMutexKey + 1); + Ref texture = backendDevice->CreateExternalTexture( &textureDescriptor, mD3D12Resource, ExternalMutexSerial(descriptor->acquireMutexKey), - descriptor->isSwapChainTexture, descriptor->isInitialized); + releaseMutexKey, descriptor->isSwapChainTexture, descriptor->isInitialized); return reinterpret_cast(texture.Detach()); } diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 01c638d034..18149ae2ea 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -453,13 +453,14 @@ namespace dawn_native { namespace d3d12 { Ref Device::CreateExternalTexture(const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture, bool isInitialized) { Ref dawnTexture; - if (ConsumedError( - Texture::CreateExternalImage(this, descriptor, std::move(d3d12Texture), - acquireMutexKey, isSwapChainTexture, isInitialized), - &dawnTexture)) { + if (ConsumedError(Texture::CreateExternalImage(this, descriptor, std::move(d3d12Texture), + acquireMutexKey, releaseMutexKey, + isSwapChainTexture, isInitialized), + &dawnTexture)) { return nullptr; } return {dawnTexture}; @@ -562,9 +563,9 @@ namespace dawn_native { namespace d3d12 { if (gpu_info::IsIntel(pciInfo.vendorId) && (gpu_info::IsSkylake(pciInfo.deviceId) || gpu_info::IsKabylake(pciInfo.deviceId) || gpu_info::IsCoffeelake(pciInfo.deviceId))) { - SetToggle( - Toggle::UseTempBufferInSmallFormatTextureToTextureCopyFromGreaterToLessMipLevel, - true); + SetToggle( + Toggle::UseTempBufferInSmallFormatTextureToTextureCopyFromGreaterToLessMipLevel, + true); } } diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 39af0d4fc2..152fc58022 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -125,6 +125,7 @@ namespace dawn_native { namespace d3d12 { Ref CreateExternalTexture(const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture, bool isInitialized); ResultOrError> CreateKeyedMutexForTexture( diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index b39e88d2e5..1248a6568a 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -422,12 +422,14 @@ namespace dawn_native { namespace d3d12 { const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture, bool isInitialized) { Ref dawnTexture = AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal)); DAWN_TRY(dawnTexture->InitializeAsExternalTexture(descriptor, std::move(d3d12Texture), - acquireMutexKey, isSwapChainTexture)); + acquireMutexKey, releaseMutexKey, + isSwapChainTexture)); // Importing a multi-planar format must be initialized. This is required because // a shared multi-planar format cannot be initialized by Dawn. @@ -454,6 +456,7 @@ namespace dawn_native { namespace d3d12 { MaybeError Texture::InitializeAsExternalTexture(const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture) { Device* dawnDevice = ToBackend(GetDevice()); @@ -464,6 +467,7 @@ namespace dawn_native { namespace d3d12 { "D3D12 acquiring shared mutex")); mAcquireMutexKey = acquireMutexKey; + mReleaseMutexKey = releaseMutexKey; mDxgiKeyedMutex = std::move(dxgiKeyedMutex); mSwapChainTexture = isSwapChainTexture; @@ -528,7 +532,7 @@ namespace dawn_native { namespace d3d12 { // 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(d3d12Texture), nullptr }; + mResourceAllocation = {info, 0, std::move(d3d12Texture), nullptr}; return {}; } @@ -569,7 +573,7 @@ namespace dawn_native { namespace d3d12 { mSwapChainTexture = false; if (mDxgiKeyedMutex != nullptr) { - mDxgiKeyedMutex->ReleaseSync(uint64_t(mAcquireMutexKey) + 1); + mDxgiKeyedMutex->ReleaseSync(uint64_t(mReleaseMutexKey)); device->ReleaseKeyedMutexForTexture(std::move(mDxgiKeyedMutex)); } } @@ -788,18 +792,17 @@ namespace dawn_native { namespace d3d12 { // This transitions assume it is a 2D texture ASSERT(GetDimension() == wgpu::TextureDimension::e2D); - mSubresourceStateAndDecay.Merge( - textureUsages, [&](const SubresourceRange& mergeRange, StateAndDecay* state, - wgpu::TextureUsage usage) { - // Skip if this subresource is not used during the current pass - if (usage == wgpu::TextureUsage::None) { - return; - } + mSubresourceStateAndDecay.Merge(textureUsages, [&](const SubresourceRange& mergeRange, + StateAndDecay* state, + wgpu::TextureUsage usage) { + // Skip if this subresource is not used during the current pass + if (usage == wgpu::TextureUsage::None) { + return; + } - D3D12_RESOURCE_STATES newState = D3D12TextureUsage(usage, GetFormat()); - TransitionSubresourceRange(barriers, mergeRange, state, newState, - pendingCommandSerial); - }); + D3D12_RESOURCE_STATES newState = D3D12TextureUsage(usage, GetFormat()); + TransitionSubresourceRange(barriers, mergeRange, state, newState, pendingCommandSerial); + }); } D3D12_RENDER_TARGET_VIEW_DESC Texture::GetRTVDescriptor(uint32_t mipLevel, @@ -869,7 +872,6 @@ namespace dawn_native { namespace d3d12 { MaybeError Texture::ClearTexture(CommandRecordingContext* commandContext, const SubresourceRange& range, TextureBase::ClearValue clearValue) { - ID3D12GraphicsCommandList* commandList = commandContext->GetCommandList(); Device* device = ToBackend(GetDevice()); diff --git a/src/dawn_native/d3d12/TextureD3D12.h b/src/dawn_native/d3d12/TextureD3D12.h index c7a652df07..d760c1e2ea 100644 --- a/src/dawn_native/d3d12/TextureD3D12.h +++ b/src/dawn_native/d3d12/TextureD3D12.h @@ -43,6 +43,7 @@ namespace dawn_native { namespace d3d12 { const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture, bool isInitialized); static ResultOrError> Create(Device* device, @@ -89,6 +90,7 @@ namespace dawn_native { namespace d3d12 { MaybeError InitializeAsExternalTexture(const TextureDescriptor* descriptor, ComPtr d3d12Texture, ExternalMutexSerial acquireMutexKey, + ExternalMutexSerial releaseMutexKey, bool isSwapChainTexture); MaybeError InitializeAsSwapChainTexture(ComPtr d3d12Texture); @@ -123,6 +125,7 @@ namespace dawn_native { namespace d3d12 { bool mSwapChainTexture = false; ExternalMutexSerial mAcquireMutexKey = ExternalMutexSerial(0); + ExternalMutexSerial mReleaseMutexKey = ExternalMutexSerial(0); ComPtr mDxgiKeyedMutex; }; diff --git a/src/include/dawn_native/D3D12Backend.h b/src/include/dawn_native/D3D12Backend.h index b85a988946..e545ee1949 100644 --- a/src/include/dawn_native/D3D12Backend.h +++ b/src/include/dawn_native/D3D12Backend.h @@ -56,6 +56,8 @@ namespace dawn_native { namespace d3d12 { : ExternalImageAccessDescriptor { public: uint64_t acquireMutexKey; + // Release key will be set to acquireMutexKey + 1 if set to sentinel value UINT64_MAX. + uint64_t releaseMutexKey = UINT64_MAX; bool isSwapChainTexture = false; };