Skip to content

Commit

Permalink
Tests, documentation and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Agrael1 committed Aug 27, 2024
1 parent 0c8feb2 commit 0fb2980
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 18 additions & 9 deletions include/vk_mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,21 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationMemoryProperties(
VmaAllocation VMA_NOT_NULL allocation,
VkMemoryPropertyFlags* VMA_NOT_NULL pFlags);


#if VMA_EXTERNAL_MEMORY_WIN32
/**
\brief Given an allocation, returns Win32 Handle, that may be imported by other processes or APIs.

`hTargetProcess` must be a valid handle to target process or NULL. If it's `NULL`, the function returns
handle for the current process.

If the allocation was created with `VMA_ALLOCATION_CREATE_EXPORT_WIN32_HANDLE_BIT` flag,
the function fills `pHandle` with handle that can be used in target process.
*/
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator,
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* pHandle);

This comment has been minimized.

Copy link
@adam-sawicki-a

adam-sawicki-a Aug 27, 2024

Contributor

Doesn't pHandle parameter also need VMA_NOT_NULL?

#endif // VMA_EXTERNAL_MEMORY_WIN32

/** \brief Maps memory represented by given allocation and returns pointer to it.

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

#if VMA_EXTERNAL_MEMORY_WIN32
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) const noexcept;
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) noexcept;
#endif // VMA_EXTERNAL_MEMORY_WIN32

private:
Expand All @@ -6363,7 +6378,7 @@ struct VmaAllocation_T
void* m_pMappedData; // Not null means memory is mapped.
VmaAllocation_T* m_Prev;
VmaAllocation_T* m_Next;
mutable VmaWin32Handle m_Handle; // Win32 handle
VmaWin32Handle m_Handle; // Win32 handle
};
union
{
Expand Down Expand Up @@ -11101,7 +11116,7 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
}
}
#if VMA_EXTERNAL_MEMORY_WIN32
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) const noexcept
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
{
// Where do we get this function from?
auto pvkGetMemoryWin32HandleKHR = hAllocator->GetVulkanFunctions().vkGetMemoryWin32HandleKHR;
Expand Down Expand Up @@ -13127,12 +13142,6 @@ void VmaAllocator_T::ImportVulkanFunctions_Static()
m_VulkanFunctions.vkGetDeviceImageMemoryRequirements = (PFN_vkGetDeviceImageMemoryRequirements)vkGetDeviceImageMemoryRequirements;
}
#endif
#if VMA_EXTERNAL_MEMORY_WIN32
// Can only be fetched dynamically
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = (PFN_vkGetMemoryWin32HandleKHR)m_VulkanFunctions.vkGetDeviceProcAddr(m_hDevice, "vkGetMemoryWin32HandleKHR");
#else
m_VulkanFunctions.vkGetMemoryWin32HandleKHR = VMA_NULL;
#endif
}

#endif // VMA_STATIC_VULKAN_FUNCTIONS == 1
Expand Down
29 changes: 29 additions & 0 deletions src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8253,6 +8253,34 @@ static void TestMappingHysteresis()
}
}


static void TestWin32Handles()
{
#if VMA_EXTERNAL_MEMORY_WIN32
wprintf(L"Test Win32 handles\n");
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufCreateInfo.size = 1024;
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;

This comment has been minimized.

Copy link
@adam-sawicki-a

adam-sawicki-a Aug 27, 2024

Contributor

We probably want to test a buffer that is created in the system memory not video memory, so maybe you should add HOST_ACCESS flag?

How about the pNext chain where you specify you are going to export a handle? Don't we need to create a custom pool with this to make it work?

This comment has been minimized.

Copy link
@Agrael1

Agrael1 Aug 27, 2024

Author Contributor

forgot that it is not my lib

VmaAllocationCreateInfo allocCreateInfo = {};
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
VkBuffer buf;
VmaAllocation alloc;
VmaAllocationInfo allocInfo;
TEST(vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo) == VK_SUCCESS);
HANDLE handle;
HANDLE handle2;
TEST(vmaGetMemoryWin32HandleKHR(g_hAllocator, alloc, nullptr, &handle) == VK_SUCCESS);
TEST(handle != nullptr);
TEST(vmaGetMemoryWin32HandleKHR(g_hAllocator, alloc, nullptr, &handle2) == VK_SUCCESS);
TEST(handle2 != nullptr);
TEST(handle2 != handle);

vmaDestroyBuffer(g_hAllocator, buf, alloc);
TEST(CloseHandle(handle));
TEST(CloseHandle(handle2));
#endif
}

void Test()
{
wprintf(L"TESTING:\n");
Expand Down Expand Up @@ -8295,6 +8323,7 @@ void Test()
TestMappingHysteresis();
TestDeviceLocalMapped();
TestMaintenance5();
TestWin32Handles();
TestMappingMultithreaded();
TestLinearAllocator();
ManuallyTestLinearAllocator();
Expand Down

0 comments on commit 0fb2980

Please sign in to comment.