@@ -6273,11 +6273,17 @@ class VmaDeviceMemoryBlock
6273
6273
uint32_t m_MapCount;
6274
6274
void* m_pMappedData;
6275
6275
6276
- VmaWin32Handle m_Handle; // Win32 handle
6276
+ VmaWin32Handle m_Handle;
6277
6277
};
6278
6278
#endif // _VMA_DEVICE_MEMORY_BLOCK
6279
6279
6280
6280
#ifndef _VMA_ALLOCATION_T
6281
+ struct VmaAllocationExtraData
6282
+ {
6283
+ void* m_pMappedData = VMA_NULL; // Not null means memory is mapped.
6284
+ VmaWin32Handle m_Handle;
6285
+ };
6286
+
6281
6287
struct VmaAllocation_T
6282
6288
{
6283
6289
friend struct VmaDedicatedAllocationListItemTraits;
@@ -6310,12 +6316,14 @@ struct VmaAllocation_T
6310
6316
bool mapped);
6311
6317
// pMappedData not null means allocation is created with MAPPED flag.
6312
6318
void InitDedicatedAllocation(
6319
+ VmaAllocator allocator,
6313
6320
VmaPool hParentPool,
6314
6321
uint32_t memoryTypeIndex,
6315
6322
VkDeviceMemory hMemory,
6316
6323
VmaSuballocationType suballocationType,
6317
6324
void* pMappedData,
6318
6325
VkDeviceSize size);
6326
+ void Destroy(VmaAllocator allocator);
6319
6327
6320
6328
ALLOCATION_TYPE GetType() const { return (ALLOCATION_TYPE)m_Type; }
6321
6329
VkDeviceSize GetAlignment() const { return m_Alignment; }
@@ -6375,10 +6383,9 @@ struct VmaAllocation_T
6375
6383
{
6376
6384
VmaPool m_hParentPool; // VK_NULL_HANDLE if not belongs to custom pool.
6377
6385
VkDeviceMemory m_hMemory;
6378
- void* m_pMappedData; // Not null means memory is mapped.
6386
+ VmaAllocationExtraData* m_ExtraData;
6379
6387
VmaAllocation_T* m_Prev;
6380
6388
VmaAllocation_T* m_Next;
6381
- VmaWin32Handle m_Handle; // Win32 handle
6382
6389
};
6383
6390
union
6384
6391
{
@@ -6401,6 +6408,8 @@ struct VmaAllocation_T
6401
6408
#if VMA_STATS_STRING_ENABLED
6402
6409
VmaBufferImageUsage m_BufferImageUsage; // 0 if unknown.
6403
6410
#endif
6411
+
6412
+ void EnsureExtraData(VmaAllocator hAllocator);
6404
6413
};
6405
6414
#endif // _VMA_ALLOCATION_T
6406
6415
@@ -10867,6 +10876,7 @@ void VmaAllocation_T::InitBlockAllocation(
10867
10876
}
10868
10877
10869
10878
void VmaAllocation_T::InitDedicatedAllocation(
10879
+ VmaAllocator allocator,
10870
10880
VmaPool hParentPool,
10871
10881
uint32_t memoryTypeIndex,
10872
10882
VkDeviceMemory hMemory,
@@ -10881,16 +10891,29 @@ void VmaAllocation_T::InitDedicatedAllocation(
10881
10891
m_Size = size;
10882
10892
m_MemoryTypeIndex = memoryTypeIndex;
10883
10893
m_SuballocationType = (uint8_t)suballocationType;
10884
- if(pMappedData != VMA_NULL)
10885
- {
10886
- VMA_ASSERT(IsMappingAllowed() && "Mapping is not allowed on this allocation! Please use one of the new VMA_ALLOCATION_CREATE_HOST_ACCESS_* flags when creating it.");
10887
- m_Flags |= (uint8_t)FLAG_PERSISTENT_MAP;
10888
- }
10894
+ m_DedicatedAllocation.m_ExtraData = VMA_NULL;
10889
10895
m_DedicatedAllocation.m_hParentPool = hParentPool;
10890
10896
m_DedicatedAllocation.m_hMemory = hMemory;
10891
- m_DedicatedAllocation.m_pMappedData = pMappedData;
10892
10897
m_DedicatedAllocation.m_Prev = VMA_NULL;
10893
10898
m_DedicatedAllocation.m_Next = VMA_NULL;
10899
+
10900
+ if (pMappedData != VMA_NULL)
10901
+ {
10902
+ VMA_ASSERT(IsMappingAllowed() && "Mapping is not allowed on this allocation! Please use one of the new VMA_ALLOCATION_CREATE_HOST_ACCESS_* flags when creating it.");
10903
+ m_Flags |= (uint8_t)FLAG_PERSISTENT_MAP;
10904
+ EnsureExtraData(allocator);
10905
+ m_DedicatedAllocation.m_ExtraData->m_pMappedData = pMappedData;
10906
+ }
10907
+ }
10908
+
10909
+ void VmaAllocation_T::Destroy(VmaAllocator allocator)
10910
+ {
10911
+ FreeName(allocator);
10912
+
10913
+ if (GetType() == ALLOCATION_TYPE_DEDICATED)
10914
+ {
10915
+ vma_delete(allocator, m_DedicatedAllocation.m_ExtraData);
10916
+ }
10894
10917
}
10895
10918
10896
10919
void VmaAllocation_T::SetName(VmaAllocator hAllocator, const char* pName)
@@ -10995,8 +11018,9 @@ void* VmaAllocation_T::GetMappedData() const
10995
11018
}
10996
11019
break;
10997
11020
case ALLOCATION_TYPE_DEDICATED:
10998
- VMA_ASSERT((m_DedicatedAllocation.m_pMappedData != VMA_NULL) == (m_MapCount != 0 || IsPersistentMap()));
10999
- return m_DedicatedAllocation.m_pMappedData;
11021
+ VMA_ASSERT((m_DedicatedAllocation.m_ExtraData != VMA_NULL && m_DedicatedAllocation.m_ExtraData->m_pMappedData != VMA_NULL) ==
11022
+ (m_MapCount != 0 || IsPersistentMap()));
11023
+ return m_DedicatedAllocation.m_ExtraData != VMA_NULL ? m_DedicatedAllocation.m_ExtraData->m_pMappedData : VMA_NULL;
11000
11024
default:
11001
11025
VMA_ASSERT(0);
11002
11026
return VMA_NULL;
@@ -11037,12 +11061,14 @@ VkResult VmaAllocation_T::DedicatedAllocMap(VmaAllocator hAllocator, void** ppDa
11037
11061
VMA_ASSERT(GetType() == ALLOCATION_TYPE_DEDICATED);
11038
11062
VMA_ASSERT(IsMappingAllowed() && "Mapping is not allowed on this allocation! Please use one of the new VMA_ALLOCATION_CREATE_HOST_ACCESS_* flags when creating it.");
11039
11063
11064
+ EnsureExtraData(hAllocator);
11065
+
11040
11066
if (m_MapCount != 0 || IsPersistentMap())
11041
11067
{
11042
11068
if (m_MapCount < 0xFF)
11043
11069
{
11044
- VMA_ASSERT(m_DedicatedAllocation.m_pMappedData != VMA_NULL);
11045
- *ppData = m_DedicatedAllocation.m_pMappedData;
11070
+ VMA_ASSERT(m_DedicatedAllocation.m_ExtraData-> m_pMappedData != VMA_NULL);
11071
+ *ppData = m_DedicatedAllocation.m_ExtraData-> m_pMappedData;
11046
11072
++m_MapCount;
11047
11073
return VK_SUCCESS;
11048
11074
}
@@ -11063,7 +11089,7 @@ VkResult VmaAllocation_T::DedicatedAllocMap(VmaAllocator hAllocator, void** ppDa
11063
11089
ppData);
11064
11090
if (result == VK_SUCCESS)
11065
11091
{
11066
- m_DedicatedAllocation.m_pMappedData = *ppData;
11092
+ m_DedicatedAllocation.m_ExtraData-> m_pMappedData = *ppData;
11067
11093
m_MapCount = 1;
11068
11094
}
11069
11095
return result;
@@ -11079,7 +11105,8 @@ void VmaAllocation_T::DedicatedAllocUnmap(VmaAllocator hAllocator)
11079
11105
--m_MapCount;
11080
11106
if (m_MapCount == 0 && !IsPersistentMap())
11081
11107
{
11082
- m_DedicatedAllocation.m_pMappedData = VMA_NULL;
11108
+ VMA_ASSERT(m_DedicatedAllocation.m_ExtraData != VMA_NULL);
11109
+ m_DedicatedAllocation.m_ExtraData->m_pMappedData = VMA_NULL;
11083
11110
(*hAllocator->GetVulkanFunctions().vkUnmapMemory)(
11084
11111
hAllocator->m_hDevice,
11085
11112
m_DedicatedAllocation.m_hMemory);
@@ -11118,14 +11145,14 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
11118
11145
#if VMA_EXTERNAL_MEMORY_WIN32
11119
11146
VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTargetProcess, HANDLE* pHandle) noexcept
11120
11147
{
11121
- // Where do we get this function from?
11122
11148
auto pvkGetMemoryWin32HandleKHR = hAllocator->GetVulkanFunctions().vkGetMemoryWin32HandleKHR;
11123
11149
switch (m_Type)
11124
11150
{
11125
11151
case ALLOCATION_TYPE_BLOCK:
11126
11152
return m_BlockAllocation.m_Block->CreateWin32Handle(hAllocator, pvkGetMemoryWin32HandleKHR, hTargetProcess, pHandle);
11127
11153
case ALLOCATION_TYPE_DEDICATED:
11128
- return m_DedicatedAllocation.m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
11154
+ EnsureExtraData(hAllocator);
11155
+ return m_DedicatedAllocation.m_ExtraData->m_Handle.GetHandle(hAllocator->m_hDevice, m_DedicatedAllocation.m_hMemory, pvkGetMemoryWin32HandleKHR, hTargetProcess, hAllocator->m_UseMutex, pHandle);
11129
11156
default:
11130
11157
VMA_ASSERT(0);
11131
11158
return VK_ERROR_FEATURE_NOT_PRESENT;
@@ -11134,6 +11161,14 @@ VkResult VmaAllocation_T::GetWin32Handle(VmaAllocator hAllocator, HANDLE hTarget
11134
11161
#endif // VMA_EXTERNAL_MEMORY_WIN32
11135
11162
#endif // VMA_STATS_STRING_ENABLED
11136
11163
11164
+ void VmaAllocation_T::EnsureExtraData(VmaAllocator hAllocator)
11165
+ {
11166
+ if (m_DedicatedAllocation.m_ExtraData == VMA_NULL)
11167
+ {
11168
+ m_DedicatedAllocation.m_ExtraData = vma_new(hAllocator, VmaAllocationExtraData)();
11169
+ }
11170
+ }
11171
+
11137
11172
void VmaAllocation_T::FreeName(VmaAllocator hAllocator)
11138
11173
{
11139
11174
if(m_pName)
@@ -11562,6 +11597,7 @@ void VmaBlockVector::Free(const VmaAllocation hAllocation)
11562
11597
}
11563
11598
11564
11599
m_hAllocator->m_Budget.RemoveAllocation(m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex), hAllocation->GetSize());
11600
+ hAllocation->Destroy(m_hAllocator);
11565
11601
m_hAllocator->m_AllocationObjectAllocator.Free(hAllocation);
11566
11602
}
11567
11603
@@ -13705,7 +13741,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemoryPage(
13705
13741
}
13706
13742
13707
13743
*pAllocation = m_AllocationObjectAllocator.Allocate(isMappingAllowed);
13708
- (*pAllocation)->InitDedicatedAllocation(pool, memTypeIndex, hMemory, suballocType, pMappedData, size);
13744
+ (*pAllocation)->InitDedicatedAllocation(this, pool, memTypeIndex, hMemory, suballocType, pMappedData, size);
13709
13745
if (isUserDataString)
13710
13746
(*pAllocation)->SetName(this, (const char*)pUserData);
13711
13747
else
@@ -14041,8 +14077,6 @@ void VmaAllocator_T::FreeMemory(
14041
14077
FillAllocation(allocation, VMA_ALLOCATION_FILL_PATTERN_DESTROYED);
14042
14078
}
14043
14079
14044
- allocation->FreeName(this);
14045
-
14046
14080
switch(allocation->GetType())
14047
14081
{
14048
14082
case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:
@@ -14726,6 +14760,7 @@ void VmaAllocator_T::FreeDedicatedMemory(const VmaAllocation allocation)
14726
14760
FreeVulkanMemory(memTypeIndex, allocation->GetSize(), hMemory);
14727
14761
14728
14762
m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(allocation->GetMemoryTypeIndex()), allocation->GetSize());
14763
+ allocation->Destroy(this);
14729
14764
m_AllocationObjectAllocator.Free(allocation);
14730
14765
14731
14766
VMA_DEBUG_LOG_FORMAT(" Freed DedicatedMemory MemoryTypeIndex=%" PRIu32, memTypeIndex);
@@ -16615,7 +16650,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeVirtualBlockStatsString(VmaVirtualBlock V
16615
16650
VMA_CALL_PRE VkResult VMA_CALL_POST vmaGetMemoryWin32HandleKHR(VmaAllocator VMA_NOT_NULL allocator,
16616
16651
VmaAllocation VMA_NOT_NULL allocation, HANDLE hTargetProcess, HANDLE* VMA_NOT_NULL pHandle)
16617
16652
{
16618
- VMA_ASSERT(allocator && allocation);
16653
+ VMA_ASSERT(allocator && allocation && pHandle );
16619
16654
VMA_DEBUG_GLOBAL_MUTEX_LOCK;
16620
16655
return allocation->GetWin32Handle(allocator, hTargetProcess, pHandle);
16621
16656
}
@@ -16753,6 +16788,7 @@ VK_EXT_memory_budget | #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT
16753
16788
VK_KHR_buffer_device_address | #VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT
16754
16789
VK_EXT_memory_priority | #VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT
16755
16790
VK_AMD_device_coherent_memory | #VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT
16791
+ VK_KHR_external_memory_win32 | #VMA_ALLOCATOR_CREATE_KHR_EXTERNAL_MEMORY_WIN32_BIT
16756
16792
16757
16793
Example with fetching pointers to Vulkan functions dynamically:
16758
16794
0 commit comments