Skip to content

Commit

Permalink
extra safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Agrael1 committed Aug 26, 2024
1 parent 8c665c4 commit a069b40
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions include/vk_mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6093,7 +6093,7 @@ class VmaWin32Handle

public:
// Strengthened
VkResult GetHandle(VkDevice device, VkDeviceMemory memory, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
VkResult GetHandle(VkDevice device, VkDeviceMemory memory, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, bool useMutex, HANDLE* pHandle) noexcept
{
*pHandle = VMA_NULL;
// We only care about atomicity of handle retrieval, not about memory order.
Expand All @@ -6108,8 +6108,15 @@ class VmaWin32Handle

HANDLE hCreatedHandle = VMA_NULL;

VkResult res = VK_SUCCESS;
// If failed, try to create it.
VkResult res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle);
{
VmaMutexLockWrite lock(m_Mutex, useMutex);
if (m_hHandle.load(std::memory_order_relaxed) == VMA_NULL)
{
VkResult res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle);
}
}
if (res == VK_SUCCESS)
{
// Successfully created handle, try to set it.
Expand Down Expand Up @@ -6164,6 +6171,7 @@ class VmaWin32Handle
}
private:
std::atomic<HANDLE> m_hHandle;
VMA_RW_MUTEX m_Mutex; // Protects access m_Handle
};
#else
class VmaWin32Handle
Expand Down Expand Up @@ -10802,7 +10810,7 @@ VkResult VmaDeviceMemoryBlock::BindImageMemory(
VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
{
VMA_ASSERT(pHandle);
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
}
#endif // VK_USE_PLATFORM_WIN32_KHR
#endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS
Expand Down Expand Up @@ -11115,7 +11123,7 @@ VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTarget
case ALLOCATION_TYPE_BLOCK:
return m_BlockAllocation.m_Block->CreateWin32Handle(hAllocator, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
case ALLOCATION_TYPE_DEDICATED:
return m_DedicatedAllocation.m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
return m_DedicatedAllocation.m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
default:
VMA_ASSERT(0);
return VK_ERROR_FEATURE_NOT_PRESENT;
Expand Down

0 comments on commit a069b40

Please sign in to comment.