Skip to content

Commit c9b2a6a

Browse files
committed
Tests, documentation and fix
1 parent 0c8feb2 commit c9b2a6a

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

include/vk_mem_alloc.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,21 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationMemoryProperties(
20742074
VmaAllocation VMA_NOT_NULL allocation,
20752075
VkMemoryPropertyFlags* VMA_NOT_NULL pFlags);
20762076

2077+
2078+
#if VMA_EXTERNAL_MEMORY_WIN32
2079+
/**
2080+
\brief Given an allocation, returns Win32 Handle, that may be imported by other processes or APIs.
2081+
2082+
`hTargetProcess` must be a valid handle to target process or NULL. If it's `NULL`, the function returns
2083+
handle for the current process.
2084+
2085+
If the allocation was created with `VMA_ALLOCATION_CREATE_EXPORT_WIN32_HANDLE_BIT` flag,
2086+
the function fills `pHandle` with handle that can be used in target process.
2087+
*/
2088+
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator,
2089+
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* VMA_NOT_NULL pHandle);
2090+
#endif // VMA_EXTERNAL_MEMORY_WIN32
2091+
20772092
/** \brief Maps memory represented by given allocation and returns pointer to it.
20782093

20792094
Maps memory represented by given allocation to make it accessible to CPU code.
@@ -6345,7 +6360,7 @@ struct VmaAllocation_T
63456360
#endif
63466361

63476362
#if VMA_EXTERNAL_MEMORY_WIN32
6348-
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) const noexcept;
6363+
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) noexcept;
63496364
#endif // VMA_EXTERNAL_MEMORY_WIN32
63506365

63516366
private:
@@ -6363,7 +6378,7 @@ struct VmaAllocation_T
63636378
void* m_pMappedData; // Not null means memory is mapped.
63646379
VmaAllocation_T* m_Prev;
63656380
VmaAllocation_T* m_Next;
6366-
mutable VmaWin32Handle m_Handle; // Win32 handle
6381+
VmaWin32Handle m_Handle; // Win32 handle
63676382
};
63686383
union
63696384
{
@@ -11101,7 +11116,7 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
1110111116
}
1110211117
}
1110311118
#if VMA_EXTERNAL_MEMORY_WIN32
11104-
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) const noexcept
11119+
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
1110511120
{
1110611121
// Where do we get this function from?
1110711122
auto pvkGetMemoryWin32HandleKHR = hAllocator->GetVulkanFunctions().vkGetMemoryWin32HandleKHR;
@@ -13127,12 +13142,6 @@ void VmaAllocator_T::ImportVulkanFunctions_Static()
1312713142
m_VulkanFunctions.vkGetDeviceImageMemoryRequirements = (PFN_vkGetDeviceImageMemoryRequirements)vkGetDeviceImageMemoryRequirements;
1312813143
}
1312913144
#endif
13130-
#if VMA_EXTERNAL_MEMORY_WIN32
13131-
// Can only be fetched dynamically
13132-
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = (PFN_vkGetMemoryWin32HandleKHR)m_VulkanFunctions.vkGetDeviceProcAddr(m_hDevice, "vkGetMemoryWin32HandleKHR");
13133-
#else
13134-
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = VMA_NULL;
13135-
#endif
1313613145
}
1313713146

1313813147
#endif // VMA_STATIC_VULKAN_FUNCTIONS == 1
@@ -16604,7 +16613,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeVirtualBlockStatsString(VmaVirtualBlock V
1660416613
}
1660516614
#if VMA_EXTERNAL_MEMORY_WIN32
1660616615
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator,
16607-
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* pHandle)
16616+
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* VMA_NOT_NULL pHandle)
1660816617
{
1660916618
VMA_ASSERT(allocator && allocation);
1661016619
VMA_DEBUG_GLOBAL_MUTEX_LOCK;

src/Tests.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8253,6 +8253,34 @@ static void TestMappingHysteresis()
82538253
}
82548254
}
82558255

8256+
8257+
static void TestWin32Handles()
8258+
{
8259+
#if VMA_EXTERNAL_MEMORY_WIN32
8260+
wprintf(L"Test Win32 handles\n");
8261+
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
8262+
bufCreateInfo.size = 1024;
8263+
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
8264+
VmaAllocationCreateInfo allocCreateInfo = {};
8265+
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
8266+
VkBuffer buf;
8267+
VmaAllocation alloc;
8268+
VmaAllocationInfo allocInfo;
8269+
TEST(vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo) == VK_SUCCESS);
8270+
HANDLE handle;
8271+
HANDLE handle2;
8272+
TEST(vmaGetMemoryWin32HandleKHR(g_hAllocator, alloc, nullptr, &handle) == VK_SUCCESS);
8273+
TEST(handle != nullptr);
8274+
TEST(vmaGetMemoryWin32HandleKHR(g_hAllocator, alloc, nullptr, &handle2) == VK_SUCCESS);
8275+
TEST(handle2 != nullptr);
8276+
TEST(handle2 != handle);
8277+
8278+
vmaDestroyBuffer(g_hAllocator, buf, alloc);
8279+
TEST(CloseHandle(handle));
8280+
TEST(CloseHandle(handle2));
8281+
#endif
8282+
}
8283+
82568284
void Test()
82578285
{
82588286
wprintf(L"TESTING:\n");
@@ -8295,6 +8323,7 @@ void Test()
82958323
TestMappingHysteresis();
82968324
TestDeviceLocalMapped();
82978325
TestMaintenance5();
8326+
TestWin32Handles();
82988327
TestMappingMultithreaded();
82998328
TestLinearAllocator();
83008329
ManuallyTestLinearAllocator();

0 commit comments

Comments
 (0)