Skip to content

Commit f858fec

Browse files
committed
vo_gpu: vulkan: add support for win32 external memory extension
1 parent 3e7d18b commit f858fec

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

video/out/opengl/hwdec_cuda.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ static int cuda_init(struct ra_hwdec *hw)
117117
#if HAVE_VULKAN
118118
if (is_vk) {
119119
#if HAVE_WIN32_DESKTOP
120-
MP_ERR(hw, "CUDA hwdec with Vulkan is currently not supported on Windows\n");
121-
return -1;
120+
if (!ra_vk_get(hw->ra)->has_ext_external_memory_win32) {
121+
MP_ERR(hw, "CUDA hwdec with Vulkan requires the %s extension\n",
122+
VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
123+
return -1;
124+
}
122125
#else
123126
if (!ra_vk_get(hw->ra)->has_ext_external_memory_fd) {
124127
MP_ERR(hw, "CUDA hwdec with Vulkan requires the %s extension\n",

video/out/vulkan/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
#include <vulkan/vulkan.h>
2727

28+
#if HAVE_WIN32_DESKTOP
29+
#include <vulkan/vulkan_win32.h>
30+
#endif
31+
2832
// Vulkan allows the optional use of a custom allocator. We don't need one but
2933
// mark this parameter with a better name in case we ever decide to change this
3034
// in the future. (And to make the code more readable)
@@ -77,4 +81,5 @@ struct mpvk_ctx {
7781
// Extension availability
7882
bool has_ext_external_memory;
7983
bool has_ext_external_memory_fd;
84+
bool has_ext_external_memory_win32;
8085
};

video/out/vulkan/malloc.c

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#include "utils.h"
33
#include "osdep/timer.h"
44

5+
#if HAVE_WIN32_DESKTOP
6+
#include <aclapi.h>
7+
#include <dxgi1_2.h>
8+
#endif
9+
510
// Controls the multiplication factor for new slab allocations. The new slab
611
// will always be allocated such that the size of the slab is this factor times
712
// the previous slab. Higher values make it grow faster.
@@ -127,9 +132,59 @@ static struct vk_slab *slab_alloc(struct mpvk_ctx *vk, struct vk_heap *heap,
127132
.end = slab->size,
128133
});
129134

135+
#if HAVE_WIN32_DESKTOP
136+
PSECURITY_DESCRIPTOR secdesc = (PSECURITY_DESCRIPTOR)calloc(1, SECURITY_DESCRIPTOR_MIN_LENGTH + 2 * sizeof(void*));
137+
PSID *sid = (PSID*)((PBYTE)secdesc + SECURITY_DESCRIPTOR_MIN_LENGTH);
138+
PACL *acl = (PACL*)((PBYTE)sid + sizeof(PSID*));
139+
SID_IDENTIFIER_AUTHORITY sididauth = SECURITY_WORLD_SID_AUTHORITY;
140+
141+
if (!secdesc)
142+
goto error;
143+
144+
EXPLICIT_ACCESS expaccess = {
145+
.grfAccessPermissions = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL,
146+
.grfAccessMode = SET_ACCESS,
147+
.grfInheritance = INHERIT_ONLY,
148+
.Trustee.TrusteeForm = TRUSTEE_IS_SID,
149+
.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP,
150+
.Trustee.ptstrName = (LPTSTR)*sid,
151+
};
152+
153+
if (!InitializeSecurityDescriptor(secdesc, SECURITY_DESCRIPTOR_REVISION))
154+
goto error;
155+
156+
if (!AllocateAndInitializeSid(&sididauth, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, sid))
157+
goto error;
158+
159+
if (SetEntriesInAcl(1, &expaccess, NULL, acl) != ERROR_SUCCESS)
160+
goto error;
161+
162+
if (!SetSecurityDescriptorDacl(secdesc, TRUE, *acl, FALSE))
163+
goto error;
164+
165+
SECURITY_ATTRIBUTES secattr = {
166+
.nLength = sizeof(SECURITY_ATTRIBUTES),
167+
.lpSecurityDescriptor = secdesc,
168+
.bInheritHandle = TRUE,
169+
};
170+
171+
VkExportMemoryWin32HandleInfoKHR emw32info = {
172+
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_WIN32_HANDLE_INFO_KHR,
173+
.pNext = NULL,
174+
.pAttributes = &secattr,
175+
.dwAccess = 0x80000001, // DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE
176+
.name = NULL,
177+
};
178+
#endif
179+
130180
VkExportMemoryAllocateInfoKHR eminfo = {
131181
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
182+
#if HAVE_WIN32_DESKTOP
183+
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR,
184+
.pNext = &emw32info,
185+
#else
132186
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
187+
#endif
133188
};
134189

135190
VkMemoryAllocateInfo minfo = {
@@ -189,10 +244,31 @@ static struct vk_slab *slab_alloc(struct mpvk_ctx *vk, struct vk_heap *heap,
189244
if (slab->buffer)
190245
VK(vkBindBufferMemory(vk->dev, slab->buffer, slab->mem, 0));
191246

247+
#if HAVE_WIN32_DESKTOP
248+
if (secdesc && *sid)
249+
FreeSid(*sid);
250+
251+
if (secdesc && *acl)
252+
LocalFree(*acl);
253+
254+
free(secdesc);
255+
#endif
256+
192257
return slab;
193258

194259
error:
195260
slab_free(vk, slab);
261+
262+
#if HAVE_WIN32_DESKTOP
263+
if (secdesc && *sid)
264+
FreeSid(*sid);
265+
266+
if (secdesc && *acl)
267+
LocalFree(*acl);
268+
269+
free(secdesc);
270+
#endif
271+
196272
return NULL;
197273
}
198274

@@ -442,8 +518,11 @@ bool vk_malloc_buffer(struct mpvk_ctx *vk, VkBufferUsageFlags bufFlags,
442518
{
443519
if (exportable) {
444520
#if HAVE_WIN32_DESKTOP
445-
MP_ERR(vk, "Exportable memory is not currently supported on Windows\n");
446-
return false;
521+
if (!vk->has_ext_external_memory_win32) {
522+
MP_ERR(vk, "Exportable memory requires the %s extension\n",
523+
VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
524+
return false;
525+
}
447526
#else
448527
if (!vk->has_ext_external_memory_fd) {
449528
MP_ERR(vk, "Exportable memory requires the %s extension\n",

video/out/vulkan/utils.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ static bool detect_device_extensions(struct mpvk_ctx *vk)
484484
vk->has_ext_external_memory_fd = true;
485485
continue;
486486
}
487+
#if HAVE_WIN32_DESKTOP
488+
if (!strcmp(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
489+
props[i].extensionName)) {
490+
vk->has_ext_external_memory_win32 = true;
491+
continue;
492+
}
493+
#endif
487494
}
488495

489496
ret = true;
@@ -559,6 +566,10 @@ bool mpvk_device_init(struct mpvk_ctx *vk, struct mpvk_device_opts opts)
559566
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME);
560567
if (vk->has_ext_external_memory_fd)
561568
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME);
569+
#if HAVE_WIN32_DESKTOP
570+
if (vk->has_ext_external_memory_win32)
571+
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
572+
#endif
562573
if (vk->spirv->required_ext)
563574
MP_TARRAY_APPEND(tmp, exts, num_exts, vk->spirv->required_ext);
564575

0 commit comments

Comments
 (0)