Skip to content

Commit 15365f2

Browse files
committed
[d3d11] Synchronize shared texture initialization
1 parent 97091aa commit 15365f2

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/d3d11/d3d11_initializer.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace dxvk {
5050
else
5151
InitDeviceLocalTexture(pTexture, pInitialData);
5252

53-
SyncKeyedMutex(pTexture->GetInterface());
53+
SyncSharedTexture(pTexture);
5454
}
5555

5656

@@ -287,13 +287,30 @@ namespace dxvk {
287287
}
288288

289289

290-
void D3D11Initializer::SyncKeyedMutex(ID3D11Resource *pResource) {
291-
Com<IDXGIKeyedMutex> keyedMutex;
292-
if (pResource->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast<void**>(&keyedMutex)) != S_OK)
290+
void D3D11Initializer::SyncSharedTexture(D3D11CommonTexture* pResource) {
291+
if (!(pResource->Desc()->MiscFlags & (D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE)))
293292
return;
294293

295-
keyedMutex->AcquireSync(0, 0);
296-
keyedMutex->ReleaseSync(0);
294+
// Ensure that initialization commands are submitted and waited on before
295+
// returning control to the application in order to avoid race conditions
296+
// in case the texture is used immediately on a secondary device.
297+
auto mapMode = pResource->GetMapMode();
298+
299+
if (mapMode == D3D11_COMMON_TEXTURE_MAP_MODE_NONE
300+
|| mapMode == D3D11_COMMON_TEXTURE_MAP_MODE_BUFFER) {
301+
FlushInternal();
302+
303+
m_device->waitForResource(pResource->GetImage(), DxvkAccess::Write);
304+
}
305+
306+
// If a keyed mutex is used, initialize that to the correct state as well.
307+
Com<IDXGIKeyedMutex> keyedMutex;
308+
309+
if (SUCCEEDED(pResource->GetInterface()->QueryInterface(
310+
__uuidof(IDXGIKeyedMutex), reinterpret_cast<void**>(&keyedMutex)))) {
311+
keyedMutex->AcquireSync(0, 0);
312+
keyedMutex->ReleaseSync(0);
313+
}
297314
}
298315

299316
}

src/d3d11/d3d11_initializer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ namespace dxvk {
7171
void FlushImplicit();
7272
void FlushInternal();
7373

74-
void SyncKeyedMutex(ID3D11Resource *pResource);
74+
void SyncSharedTexture(
75+
D3D11CommonTexture* pResource);
7576

7677
};
7778

0 commit comments

Comments
 (0)