Skip to content

Commit 33d7d27

Browse files
committed
Merge remote-tracking branch 'Agrael1/master'
Pulled from #503 - thanks @Agrael1
2 parents a7e9bf4 + e83e606 commit 33d7d27

2 files changed

Lines changed: 62 additions & 26 deletions

File tree

include/vk_mem_alloc.h

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,18 @@ or by manually passing it through VmaAllocatorCreateInfo::pVulkanFunctions.
21672167

21682168
For more information, see chapter \ref vk_khr_external_memory_win32.
21692169
*/
2170-
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32Handle(VmaAllocator VMA_NOT_NULL allocator,
2171-
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* VMA_NOT_NULL pHandle);
2170+
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32Handle(
2171+
VmaAllocator VMA_NOT_NULL allocator,
2172+
VmaAllocation VMA_NOT_NULL allocation,
2173+
HANDLE hTargetProcess,
2174+
HANDLE* VMA_NOT_NULL pHandle);
2175+
2176+
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32Handle2(
2177+
VmaAllocator VMA_NOT_NULL allocator,
2178+
VmaAllocation VMA_NOT_NULL allocation,
2179+
VkExternalMemoryHandleTypeFlagBits handleType,
2180+
HANDLE hTargetProcess,
2181+
HANDLE* VMA_NOT_NULL pHandle);
21722182
#endif // VMA_EXTERNAL_MEMORY_WIN32
21732183

21742184
/** \brief Maps memory represented by given allocation and returns pointer to it.
@@ -6204,48 +6214,53 @@ class VmaWin32Handle
62046214
{
62056215
public:
62066216
VmaWin32Handle() noexcept : m_hHandle(VMA_NULL) { }
6207-
explicit VmaWin32Handle(HANDLE hHandle) noexcept : m_hHandle(hHandle) { }
6208-
~VmaWin32Handle() noexcept { if (m_hHandle != VMA_NULL) { ::CloseHandle(m_hHandle); } }
6217+
explicit VmaWin32Handle(HANDLE hHandle) noexcept
6218+
: m_hHandle(hHandle)
6219+
, m_IsNTHandle(IsNTHandle(hHandle))
6220+
{
6221+
}
6222+
~VmaWin32Handle() noexcept { if (m_hHandle != VMA_NULL && m_IsNTHandle) { ::CloseHandle(m_hHandle); } }
62096223
VMA_CLASS_NO_COPY_NO_MOVE(VmaWin32Handle)
62106224

62116225
public:
62126226
// Strengthened
6213-
VkResult GetHandle(VkDevice device, VkDeviceMemory memory, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, bool useMutex, HANDLE* pHandle) noexcept
6227+
VkResult GetHandle(VkDevice device, VkDeviceMemory memory, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE hTargetProcess, bool useMutex, HANDLE* pHandle) noexcept
62146228
{
62156229
*pHandle = VMA_NULL;
62166230
// Try to get handle first.
6217-
if (m_hHandle != VMA_NULL)
6218-
{
6219-
*pHandle = Duplicate(hTargetProcess);
6220-
return VK_SUCCESS;
6221-
}
6222-
62236231
VkResult res = VK_SUCCESS;
6224-
// If failed, try to create it.
6232+
if (m_hHandle == VMA_NULL)
62256233
{
62266234
VmaMutexLockWrite lock(m_Mutex, useMutex);
62276235
if (m_hHandle == VMA_NULL)
62286236
{
6229-
res = Create(device, memory, pvkGetMemoryWin32HandleKHR, &m_hHandle);
6237+
res = Create(device, memory, pvkGetMemoryWin32HandleKHR, handleType, &m_hHandle);
6238+
if (res != VK_SUCCESS) {
6239+
m_hHandle = VMA_NULL;
6240+
return res;
6241+
}
6242+
m_IsNTHandle = IsNTHandle(m_hHandle);
62306243
}
62316244
}
6232-
6233-
*pHandle = Duplicate(hTargetProcess);
6245+
if (res == VK_SUCCESS) {
6246+
// KMT handle is returned as is.
6247+
*pHandle = m_IsNTHandle ? Duplicate(hTargetProcess) : m_hHandle;
6248+
}
62346249
return res;
62356250
}
62366251

62376252
operator bool() const noexcept { return m_hHandle != VMA_NULL; }
62386253
private:
62396254
// Not atomic
6240-
static VkResult Create(VkDevice device, VkDeviceMemory memory, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, HANDLE* pHandle) noexcept
6255+
static VkResult Create(VkDevice device, VkDeviceMemory memory, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE* pHandle) noexcept
62416256
{
62426257
VkResult res = VK_ERROR_FEATURE_NOT_PRESENT;
62436258
if (pvkGetMemoryWin32HandleKHR != VMA_NULL)
62446259
{
62456260
VkMemoryGetWin32HandleInfoKHR handleInfo{ };
62466261
handleInfo.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR;
62476262
handleInfo.memory = memory;
6248-
handleInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR;
6263+
handleInfo.handleType = handleType;
62496264
res = pvkGetMemoryWin32HandleKHR(device, &handleInfo, pHandle);
62506265
}
62516266
return res;
@@ -6263,16 +6278,23 @@ class VmaWin32Handle
62636278
}
62646279
return hDupHandle;
62656280
}
6281+
static bool IsNTHandle(HANDLE hHandle) noexcept
6282+
{
6283+
DWORD flags = 0;
6284+
return (hHandle != VMA_NULL) ? (::GetHandleInformation(hHandle, &flags) != 0) : false;
6285+
}
62666286
private:
62676287
HANDLE m_hHandle;
62686288
VMA_RW_MUTEX m_Mutex; // Protects access m_Handle
6289+
bool m_IsNTHandle = false; // True if m_Handle is NT handle, false if it's a KMT handle.
62696290
};
62706291
#else
62716292
class VmaWin32Handle
62726293
{
62736294
// ABI compatibility
62746295
void* placeholder = VMA_NULL;
62756296
VMA_RW_MUTEX placeholder2;
6297+
bool placeholder3 = false;
62766298
};
62776299
#endif // VMA_EXTERNAL_MEMORY_WIN32
62786300

@@ -6347,6 +6369,7 @@ class VmaDeviceMemoryBlock
63476369
VkResult CreateWin32Handle(
63486370
const VmaAllocator hAllocator,
63496371
PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR,
6372+
VkExternalMemoryHandleTypeFlagBits handleType,
63506373
HANDLE hTargetProcess,
63516374
HANDLE* pHandle)noexcept;
63526375
#endif // VMA_EXTERNAL_MEMORY_WIN32
@@ -6461,7 +6484,7 @@ struct VmaAllocation_T
64616484
#endif
64626485

64636486
#if VMA_EXTERNAL_MEMORY_WIN32
6464-
VkResult GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* hHandle) noexcept;
6487+
VkResult GetWin32Handle(VmaAllocator hAllocator, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE hTargetProcess, HANDLE* hHandle) noexcept;
64656488
#endif // VMA_EXTERNAL_MEMORY_WIN32
64666489

64676490
private:
@@ -10905,10 +10928,10 @@ VkResult VmaDeviceMemoryBlock::BindImageMemory(
1090510928
}
1090610929

1090710930
#if VMA_EXTERNAL_MEMORY_WIN32
10908-
VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
10931+
VkResult VmaDeviceMemoryBlock::CreateWin32Handle(const VmaAllocator hAllocator, PFN_vkGetMemoryWin32HandleKHR pvkGetMemoryWin32HandleKHR, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
1090910932
{
1091010933
VMA_ASSERT(pHandle);
10911-
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
10934+
return m_Handle.GetHandle(hAllocator->m_hDevice, m_hMemory, pvkGetMemoryWin32HandleKHR, handleType, hTargetProcess, hAllocator->m_UseMutex, pHandle);
1091210935
}
1091310936
#endif // VMA_EXTERNAL_MEMORY_WIN32
1091410937
#endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS
@@ -11226,16 +11249,16 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
1122611249
}
1122711250
}
1122811251
#if VMA_EXTERNAL_MEMORY_WIN32
11229-
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
11252+
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
1123011253
{
1123111254
auto pvkGetMemoryWin32HandleKHR = hAllocator->GetVulkanFunctions().vkGetMemoryWin32HandleKHR;
1123211255
switch (m_Type)
1123311256
{
1123411257
case ALLOCATION_TYPE_BLOCK:
11235-
return m_BlockAllocation.m_Block->CreateWin32Handle(hAllocator, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
11258+
return m_BlockAllocation.m_Block->CreateWin32Handle(hAllocator, pvkGetMemoryWin32HandleKHR, handleType, hTargetProcess, pHandle);
1123611259
case ALLOCATION_TYPE_DEDICATED:
1123711260
EnsureExtraData(hAllocator);
11238-
return m_DedicatedAllocation.m_ExtraData->m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
11261+
return m_DedicatedAllocation.m_ExtraData->m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, handleType, hTargetProcess, hAllocator->m_UseMutex, pHandle);
1123911262
default:
1124011263
VMA_ASSERT(0);
1124111264
return VK_ERROR_FEATURE_NOT_PRESENT;
@@ -16842,7 +16865,20 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32Handle(VmaAllocator VMA_NOT
1684216865
{
1684316866
VMA_ASSERT(allocator && allocation && pHandle);
1684416867
VMA_DEBUG_GLOBAL_MUTEX_LOCK;
16845-
return allocation->GetWin32Handle(allocator, hTargetProcess, pHandle);
16868+
return allocation->GetWin32Handle(allocator, VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT, hTargetProcess, pHandle);
16869+
}
16870+
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32Handle2(VmaAllocator VMA_NOT_NULL allocator,
16871+
VmaAllocation VMA_NOT_NULL allocation, VkExternalMemoryHandleTypeFlagBits handleType, HANDLE hTargetProcess, HANDLE* VMA_NOT_NULL pHandle)
16872+
{
16873+
VMA_ASSERT(allocator && allocation && pHandle);
16874+
VMA_ASSERT(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR ||
16875+
handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT_KHR ||
16876+
handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT_KHR ||
16877+
handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT_KHR ||
16878+
handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_HEAP_BIT_KHR ||
16879+
handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT_KHR);
16880+
VMA_DEBUG_GLOBAL_MUTEX_LOCK;
16881+
return allocation->GetWin32Handle(allocator, handleType, hTargetProcess, pHandle);
1684616882
}
1684716883
#endif // VMA_EXTERNAL_MEMORY_WIN32
1684816884
#endif // VMA_STATS_STRING_ENABLED

src/Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ VkResult MainTest(Result& outResult, const Config& config)
380380

381381
time_point timeBeg = std::chrono::high_resolution_clock::now();
382382

383-
std::atomic<size_t> allocationCount = 0;
383+
std::atomic<size_t> allocationCount{ 0 };
384384
VkResult res = VK_SUCCESS;
385385

386386
uint32_t memUsageProbabilitySum =
@@ -545,7 +545,7 @@ VkResult MainTest(Result& outResult, const Config& config)
545545
}
546546
};
547547

548-
std::atomic<uint32_t> numThreadsReachedMaxAllocations = 0;
548+
std::atomic<uint32_t> numThreadsReachedMaxAllocations{ 0 };
549549
HANDLE threadsFinishEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
550550

551551
auto ThreadProc = [&](uint32_t randSeed) -> void

0 commit comments

Comments
 (0)