Skip to content

Commit a069b40

Browse files
committed
extra safety
1 parent 8c665c4 commit a069b40

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

include/vk_mem_alloc.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6093,7 +6093,7 @@ class VmaWin32Handle
60936093

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

61096109
HANDLE hCreatedHandle = VMA_NULL;
61106110

6111+
VkResult res = VK_SUCCESS;
61116112
// If failed, try to create it.
6112-
VkResult res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle);
6113+
{
6114+
VmaMutexLockWrite lock(m_Mutex, useMutex);
6115+
if (m_hHandle.load(std::memory_order_relaxed) == VMA_NULL)
6116+
{
6117+
VkResult res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &hCreatedHandle);
6118+
}
6119+
}
61136120
if (res == VK_SUCCESS)
61146121
{
61156122
// Successfully created handle, try to set it.
@@ -6164,6 +6171,7 @@ class VmaWin32Handle
61646171
}
61656172
private:
61666173
std::atomic<HANDLE> m_hHandle;
6174+
VMA_RW_MUTEX m_Mutex; // Protects access m_Handle
61676175
};
61686176
#else
61696177
class VmaWin32Handle
@@ -10802,7 +10810,7 @@ VkResult VmaDeviceMemoryBlock::BindImageMemory(
1080210810
VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, decltype(&vkGetMemoryWin32HandleKHR) pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
1080310811
{
1080410812
VMA_ASSERT(pHandle);
10805-
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
10813+
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, &vkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
1080610814
}
1080710815
#endif // VK_USE_PLATFORM_WIN32_KHR
1080810816
#endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS
@@ -11115,7 +11123,7 @@ VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTarget
1111511123
case ALLOCATION_TYPE_BLOCK:
1111611124
return m_BlockAllocation.m_Block->CreateWin32Handle(hAllocator, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
1111711125
case ALLOCATION_TYPE_DEDICATED:
11118-
return m_DedicatedAllocation.m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
11126+
return m_DedicatedAllocation.m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
1111911127
default:
1112011128
VMA_ASSERT(0);
1112111129
return VK_ERROR_FEATURE_NOT_PRESENT;

0 commit comments

Comments
 (0)