From 428ca9416d60cf7025ae97da88f44eabc2ec0aa2 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Wed, 23 Aug 2023 14:50:42 +0100 Subject: [PATCH] [d3d11] Implement synchronization on keyed mutexes Co-authored-by: Paul Gofman --- src/d3d11/d3d11_context_imm.h | 1 + src/d3d11/d3d11_initializer.cpp | 12 +++++++++++ src/d3d11/d3d11_initializer.h | 2 ++ src/d3d11/d3d11_resource.cpp | 36 ++++++++++++++++++++++++++++----- src/d3d11/d3d11_resource.h | 2 ++ 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/d3d11/d3d11_context_imm.h b/src/d3d11/d3d11_context_imm.h index b1fa34ebffb..eb3ed88f085 100644 --- a/src/d3d11/d3d11_context_imm.h +++ b/src/d3d11/d3d11_context_imm.h @@ -17,6 +17,7 @@ namespace dxvk { friend class D3D11CommonContext; friend class D3D11SwapChain; friend class D3D11VideoContext; + friend class D3D11DXGIKeyedMutex; public: D3D11ImmediateContext( diff --git a/src/d3d11/d3d11_initializer.cpp b/src/d3d11/d3d11_initializer.cpp index d965921c67d..105485fabb9 100644 --- a/src/d3d11/d3d11_initializer.cpp +++ b/src/d3d11/d3d11_initializer.cpp @@ -49,6 +49,8 @@ namespace dxvk { InitHostVisibleTexture(pTexture, pInitialData); else InitDeviceLocalTexture(pTexture, pInitialData); + + SyncKeyedMutex(pTexture->GetInterface()); } @@ -284,4 +286,14 @@ namespace dxvk { m_transferMemory = 0; } + + void D3D11Initializer::SyncKeyedMutex(ID3D11Resource *pResource) { + Com keyedMutex; + if (pResource->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast(&keyedMutex)) != S_OK) + return; + + keyedMutex->AcquireSync(0, 0); + keyedMutex->ReleaseSync(0); + } + } \ No newline at end of file diff --git a/src/d3d11/d3d11_initializer.h b/src/d3d11/d3d11_initializer.h index 8cfe84c637e..1cd94d8008f 100644 --- a/src/d3d11/d3d11_initializer.h +++ b/src/d3d11/d3d11_initializer.h @@ -71,6 +71,8 @@ namespace dxvk { void FlushImplicit(); void FlushInternal(); + void SyncKeyedMutex(ID3D11Resource *pResource); + }; } \ No newline at end of file diff --git a/src/d3d11/d3d11_resource.cpp b/src/d3d11/d3d11_resource.cpp index ba252692706..92b2d658259 100644 --- a/src/d3d11/d3d11_resource.cpp +++ b/src/d3d11/d3d11_resource.cpp @@ -1,6 +1,8 @@ #include "d3d11_buffer.h" #include "d3d11_texture.h" #include "d3d11_resource.h" +#include "d3d11_context_imm.h" +#include "d3d11_device.h" #include "../util/util_shared_res.h" @@ -9,7 +11,13 @@ namespace dxvk { D3D11DXGIKeyedMutex::D3D11DXGIKeyedMutex( ID3D11Resource* pResource) : m_resource(pResource) { + Com device; + m_resource->GetDevice(&device); + m_device = static_cast(device.ptr()); + m_supported = m_device->GetDXVKDevice()->features().khrWin32KeyedMutex + && m_device->GetDXVKDevice()->vkd()->wine_vkAcquireKeyedMutex != nullptr + && m_device->GetDXVKDevice()->vkd()->wine_vkReleaseKeyedMutex != nullptr; } @@ -77,20 +85,38 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D11DXGIKeyedMutex::AcquireSync( UINT64 Key, DWORD dwMilliseconds) { + if (!m_supported) { if (!m_warned) { m_warned = true; - Logger::err("D3D11DXGIKeyedMutex::AcquireSync: Stub"); + Logger::err("D3D11DXGIKeyedMutex::AcquireSync: Not supported"); } return S_OK; + } + + D3D11CommonTexture* texture = GetCommonTexture(m_resource); + Rc dxvkDevice = m_device->GetDXVKDevice(); + + VkResult vr = dxvkDevice->vkd()->wine_vkAcquireKeyedMutex(dxvkDevice->handle(), texture->GetImage()->memory().memory(), Key, dwMilliseconds); + switch (vr) { + case VK_SUCCESS: return S_OK; + case VK_TIMEOUT: return WAIT_TIMEOUT; + default: return DXGI_ERROR_INVALID_CALL; + } } HRESULT STDMETHODCALLTYPE D3D11DXGIKeyedMutex::ReleaseSync( UINT64 Key) { - if (!m_warned) { - m_warned = true; - Logger::err("D3D11DXGIKeyedMutex::AcquireSync: Stub"); - } + if (!m_supported) return S_OK; + + D3D11CommonTexture* texture = GetCommonTexture(m_resource); + Rc dxvkDevice = m_device->GetDXVKDevice(); + + m_device->GetContext()->WaitForResource(texture->GetImage(), DxvkCsThread::SynchronizeAll, D3D11_MAP_READ_WRITE, 0); + + return dxvkDevice->vkd()->wine_vkReleaseKeyedMutex(dxvkDevice->handle(), texture->GetImage()->memory().memory(), Key) == VK_SUCCESS + ? S_OK + : DXGI_ERROR_INVALID_CALL; } D3D11DXGIResource::D3D11DXGIResource( diff --git a/src/d3d11/d3d11_resource.h b/src/d3d11/d3d11_resource.h index 0c459ca3104..8dc73220917 100644 --- a/src/d3d11/d3d11_resource.h +++ b/src/d3d11/d3d11_resource.h @@ -74,7 +74,9 @@ namespace dxvk { private: ID3D11Resource* m_resource; + D3D11Device* m_device; bool m_warned = false; + bool m_supported = false; };