Skip to content

Commit f9fc69b

Browse files
committed
[d3d11] Use the D3DKMT local handles to allocate shared handles
1 parent 15c47a5 commit f9fc69b

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/d3d11/d3d11_fence.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,35 @@ namespace dxvk {
6969
DWORD dwAccess,
7070
LPCWSTR lpName,
7171
HANDLE* pHandle) {
72+
InitReturnPtr(pHandle);
7273
if (!(m_flags & D3D11_FENCE_FLAG_SHARED))
7374
return E_INVALIDARG;
7475

76+
OBJECT_ATTRIBUTES attr = { };
77+
attr.Length = sizeof(attr);
78+
attr.SecurityDescriptor = (void *)pAttributes;
79+
80+
WCHAR buffer[MAX_PATH];
81+
UNICODE_STRING name;
82+
if (lpName) {
83+
DWORD session, len;
84+
85+
ProcessIdToSessionId(GetCurrentProcessId(), &session);
86+
len = swprintf(buffer, ARRAYSIZE(buffer), L"\\Sessions\\%u\\BaseNamedObjects\\%s", session, name);
87+
name.MaximumLength = name.Length = len * sizeof(WCHAR);
88+
name.MaximumLength += sizeof(WCHAR);
89+
name.Buffer = buffer;
90+
91+
attr.ObjectName = &name;
92+
attr.Attributes = OBJ_CASE_INSENSITIVE;
93+
}
94+
95+
D3DKMT_HANDLE local = m_fence->kmtLocal();
96+
if (!D3DKMTShareObjects(1, &local, &attr, dwAccess, pHandle))
97+
return S_OK;
98+
99+
/* try legacy Proton shared resource implementation */
100+
75101
if (pAttributes)
76102
Logger::warn(str::format("CreateSharedHandle: attributes ", pAttributes, " not handled"));
77103
if (dwAccess)

src/d3d11/d3d11_resource.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "d3d11_context_imm.h"
55
#include "d3d11_device.h"
66

7+
#include "../util/util_win32_compat.h"
78
#include "../util/util_shared_res.h"
89

910
namespace dxvk {
@@ -220,6 +221,14 @@ namespace dxvk {
220221
return S_OK;
221222
}
222223

224+
D3DKMT_HANDLE global = texture->GetImage()->storage()->kmtGlobal();
225+
if (global) {
226+
*pSharedHandle = (HANDLE)(uintptr_t)global;
227+
return S_OK;
228+
}
229+
230+
/* try legacy Proton shared resource implementation */
231+
223232
HANDLE kmtHandle = texture->GetImage()->sharedHandle();
224233

225234
if (kmtHandle == INVALID_HANDLE_VALUE)
@@ -281,6 +290,31 @@ namespace dxvk {
281290
!(texture->Desc()->MiscFlags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE))
282291
return E_INVALIDARG;
283292

293+
OBJECT_ATTRIBUTES attr = { };
294+
attr.Length = sizeof(attr);
295+
attr.SecurityDescriptor = (void *)pAttributes;
296+
297+
WCHAR buffer[MAX_PATH];
298+
UNICODE_STRING name;
299+
if (lpName) {
300+
DWORD session, len;
301+
302+
ProcessIdToSessionId(GetCurrentProcessId(), &session);
303+
len = swprintf(buffer, ARRAYSIZE(buffer), L"\\Sessions\\%u\\BaseNamedObjects\\%s", session, name);
304+
name.MaximumLength = name.Length = len * sizeof(WCHAR);
305+
name.MaximumLength += sizeof(WCHAR);
306+
name.Buffer = buffer;
307+
308+
attr.ObjectName = &name;
309+
attr.Attributes = OBJ_CASE_INSENSITIVE;
310+
}
311+
312+
D3DKMT_HANDLE local = texture->GetImage()->storage()->kmtLocal();
313+
if (!D3DKMTShareObjects(1, &local, &attr, dwAccess, pHandle))
314+
return S_OK;
315+
316+
/* try legacy Proton shared resource implementation */
317+
284318
if (lpName)
285319
Logger::warn("Naming shared resources not supported");
286320

src/util/util_gdi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ namespace dxvk {
6363
Logger::warn("D3DKMTOpenSyncObjectFromNtHandle: Not available on this platform.");
6464
return -1;
6565
}
66+
67+
NTSTATUS WINAPI D3DKMTShareObjects(UINT count, const D3DKMT_HANDLE *handles, OBJECT_ATTRIBUTES *attr, UINT access, HANDLE *handle) {
68+
Logger::warn("D3DKMTShareObjects: Not available on this platform.");
69+
return -1;
70+
}
6671
#endif
6772

6873
}

src/util/util_gdi.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ namespace dxvk {
187187
D3DKMT_HANDLE hSyncObject;
188188
} D3DKMT_OPENSYNCOBJECTFROMNTHANDLE;
189189

190+
typedef struct _UNICODE_STRING {
191+
USHORT Length; /* bytes */
192+
USHORT MaximumLength; /* bytes */
193+
WCHAR *Buffer;
194+
} UNICODE_STRING;
195+
196+
typedef struct _OBJECT_ATTRIBUTES {
197+
ULONG Length;
198+
HANDLE RootDirectory;
199+
UNICODE_STRING *ObjectName;
200+
ULONG Attributes;
201+
void *SecurityDescriptor;
202+
void *SecurityQualityOfService;
203+
} OBJECT_ATTRIBUTES;
204+
205+
#define OBJ_CASE_INSENSITIVE 0x00000040
206+
190207
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTCloseAdapter(const D3DKMT_CLOSEADAPTER *desc);
191208
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTCreateDCFromMemory(D3DKMT_CREATEDCFROMMEMORY *desc);
192209
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTCreateDevice(D3DKMT_CREATEDEVICE *desc);
@@ -199,4 +216,5 @@ namespace dxvk {
199216
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTOpenResourceFromNtHandle(D3DKMT_OPENRESOURCEFROMNTHANDLE *desc);
200217
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTOpenSynchronizationObject(D3DKMT_OPENSYNCHRONIZATIONOBJECT *desc);
201218
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTOpenSyncObjectFromNtHandle(D3DKMT_OPENSYNCOBJECTFROMNTHANDLE *desc);
219+
EXTERN_C WINBASEAPI NTSTATUS WINAPI D3DKMTShareObjects(UINT count, const D3DKMT_HANDLE *handles, OBJECT_ATTRIBUTES *attr, UINT access, HANDLE *handle);
202220
}

0 commit comments

Comments
 (0)