Skip to content

Commit

Permalink
[d3d11] Implement synchronization on keyed mutexes
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Gofman <pgofman@codeweavers.com>
  • Loading branch information
2 people authored and doitsujin committed Aug 23, 2023
1 parent c26f402 commit 428ca94
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/d3d11/d3d11_context_imm.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace dxvk {
friend class D3D11CommonContext<D3D11ImmediateContext>;
friend class D3D11SwapChain;
friend class D3D11VideoContext;
friend class D3D11DXGIKeyedMutex;
public:

D3D11ImmediateContext(
Expand Down
12 changes: 12 additions & 0 deletions src/d3d11/d3d11_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace dxvk {
InitHostVisibleTexture(pTexture, pInitialData);
else
InitDeviceLocalTexture(pTexture, pInitialData);

SyncKeyedMutex(pTexture->GetInterface());
}


Expand Down Expand Up @@ -284,4 +286,14 @@ namespace dxvk {
m_transferMemory = 0;
}


void D3D11Initializer::SyncKeyedMutex(ID3D11Resource *pResource) {
Com<IDXGIKeyedMutex> keyedMutex;
if (pResource->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast<void**>(&keyedMutex)) != S_OK)
return;

keyedMutex->AcquireSync(0, 0);
keyedMutex->ReleaseSync(0);
}

}
2 changes: 2 additions & 0 deletions src/d3d11/d3d11_initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ namespace dxvk {
void FlushImplicit();
void FlushInternal();

void SyncKeyedMutex(ID3D11Resource *pResource);

};

}
36 changes: 31 additions & 5 deletions src/d3d11/d3d11_resource.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -9,7 +11,13 @@ namespace dxvk {
D3D11DXGIKeyedMutex::D3D11DXGIKeyedMutex(
ID3D11Resource* pResource)
: m_resource(pResource) {
Com<ID3D11Device> device;
m_resource->GetDevice(&device);
m_device = static_cast<D3D11Device*>(device.ptr());

m_supported = m_device->GetDXVKDevice()->features().khrWin32KeyedMutex
&& m_device->GetDXVKDevice()->vkd()->wine_vkAcquireKeyedMutex != nullptr
&& m_device->GetDXVKDevice()->vkd()->wine_vkReleaseKeyedMutex != nullptr;
}


Expand Down Expand Up @@ -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> 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> 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(
Expand Down
2 changes: 2 additions & 0 deletions src/d3d11/d3d11_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ namespace dxvk {
private:

ID3D11Resource* m_resource;
D3D11Device* m_device;
bool m_warned = false;
bool m_supported = false;
};


Expand Down

0 comments on commit 428ca94

Please sign in to comment.