|
2 | 2 | #include "utils.h" |
3 | 3 | #include "osdep/timer.h" |
4 | 4 |
|
| 5 | +#if HAVE_WIN32_DESKTOP |
| 6 | +#include <aclapi.h> |
| 7 | +#include <dxgi1_2.h> |
| 8 | +#endif |
| 9 | + |
5 | 10 | // Controls the multiplication factor for new slab allocations. The new slab |
6 | 11 | // will always be allocated such that the size of the slab is this factor times |
7 | 12 | // 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, |
127 | 132 | .end = slab->size, |
128 | 133 | }); |
129 | 134 |
|
| 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 | + |
130 | 180 | VkExportMemoryAllocateInfoKHR eminfo = { |
131 | 181 | .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 |
132 | 186 | .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, |
| 187 | +#endif |
133 | 188 | }; |
134 | 189 |
|
135 | 190 | VkMemoryAllocateInfo minfo = { |
@@ -189,10 +244,31 @@ static struct vk_slab *slab_alloc(struct mpvk_ctx *vk, struct vk_heap *heap, |
189 | 244 | if (slab->buffer) |
190 | 245 | VK(vkBindBufferMemory(vk->dev, slab->buffer, slab->mem, 0)); |
191 | 246 |
|
| 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 | + |
192 | 257 | return slab; |
193 | 258 |
|
194 | 259 | error: |
195 | 260 | 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 | + |
196 | 272 | return NULL; |
197 | 273 | } |
198 | 274 |
|
@@ -442,8 +518,11 @@ bool vk_malloc_buffer(struct mpvk_ctx *vk, VkBufferUsageFlags bufFlags, |
442 | 518 | { |
443 | 519 | if (exportable) { |
444 | 520 | #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 | + } |
447 | 526 | #else |
448 | 527 | if (!vk->has_ext_external_memory_fd) { |
449 | 528 | MP_ERR(vk, "Exportable memory requires the %s extension\n", |
|
0 commit comments