Made allocation Name and UserData separate. COMPATIBILITY BREAKING!

Added function vmaSetAllocationName. Added member VmaAllocationInfo::pName.
VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is now deprecated but still works.

Code mostly by @medranSolus
This commit is contained in:
Adam Sawicki 2022-03-03 11:23:14 +01:00
parent bd39bed5e6
commit caf27e0428
26 changed files with 301 additions and 214 deletions

View file

@ -1,4 +1,4 @@
//
nn//
// Copyright (c) 2017-2022 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@ -552,9 +552,11 @@ typedef enum VmaAllocationCreateFlagBits
VMA_ALLOCATION_CREATE_RESERVED_1_BIT = 0x00000008,
/// \deprecated Removed. Do not use.
VMA_ALLOCATION_CREATE_RESERVED_2_BIT = 0x00000010,
/** Set this flag to treat VmaAllocationCreateInfo::pUserData as pointer to a
/** \deprecated Preserved for backward compatibility. Consider using vmaSetAllocationName() instead.
Set this flag to treat VmaAllocationCreateInfo::pUserData as pointer to a
null-terminated string. Instead of copying pointer value, a local copy of the
string is made and stored in allocation's `pUserData`. The string is automatically
string is made and stored in allocation's `pName`. The string is automatically
freed together with the allocation. It is also used in vmaBuildStatsString().
*/
VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT = 0x00000020,
@ -1381,6 +1383,14 @@ typedef struct VmaAllocationInfo
It can change after call to vmaSetAllocationUserData() for this allocation.
*/
void* VMA_NULLABLE pUserData;
/** \brief Custom allocation name that was set with vmaSetAllocationName().
It can change after call to vmaSetAllocationName() for this allocation.
Another way to set custom name is to pass it in VmaAllocationCreateInfo::pUserData with
additional flag VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT set [DEPRECATED].
*/
const char* VMA_NULLABLE pName;
} VmaAllocationInfo;
/** \brief Parameters for defragmentation.
@ -1913,15 +1923,8 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo(
/** \brief Sets pUserData in given allocation to new value.
If the allocation was created with VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT,
pUserData must be either null, or pointer to a null-terminated string. The function
makes local copy of the string and sets it as allocation's `pUserData`. String
passed as pUserData doesn't need to be valid for whole lifetime of the allocation -
you can free it after this call. String previously pointed by allocation's
pUserData is freed from memory.
If the flag was not used, the value of pointer `pUserData` is just copied to
allocation's `pUserData`. It is opaque, so you can use it however you want - e.g.
The value of pointer `pUserData` is copied to allocation's `pUserData`.
It is opaque, so you can use it however you want - e.g.
as a pointer, ordinal number or some handle to you own data.
*/
VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(
@ -1929,6 +1932,19 @@ VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(
VmaAllocation VMA_NOT_NULL allocation,
void* VMA_NULLABLE pUserData);
/** \brief Sets pName in given allocation to new value.
`pName` must be either null, or pointer to a null-terminated string. The function
makes local copy of the string and sets it as allocation's `pName`. String
passed as pName doesn't need to be valid for whole lifetime of the allocation -
you can free it after this call. String previously pointed by allocation's
`pName` is freed from memory.
*/
VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationName(
VmaAllocator VMA_NOT_NULL allocator,
VmaAllocation VMA_NOT_NULL allocation,
const char* VMA_NULLABLE pName);
/**
\brief Given an allocation, returns Property Flags of its memory type.
@ -5943,9 +5959,8 @@ struct VmaAllocation_T
enum FLAGS
{
FLAG_USER_DATA_STRING = 0x01,
FLAG_PERSISTENT_MAP = 0x02,
FLAG_MAPPING_ALLOWED = 0x04,
FLAG_PERSISTENT_MAP = 0x01,
FLAG_MAPPING_ALLOWED = 0x02,
};
public:
@ -5957,7 +5972,7 @@ public:
};
// This struct is allocated using VmaPoolAllocator.
VmaAllocation_T(bool userDataString, bool mappingAllowed);
VmaAllocation_T(bool mappingAllowed);
~VmaAllocation_T();
void InitBlockAllocation(
@ -5980,8 +5995,8 @@ public:
ALLOCATION_TYPE GetType() const { return (ALLOCATION_TYPE)m_Type; }
VkDeviceSize GetAlignment() const { return m_Alignment; }
VkDeviceSize GetSize() const { return m_Size; }
bool IsUserDataString() const { return (m_Flags & FLAG_USER_DATA_STRING) != 0; }
void* GetUserData() const { return m_pUserData; }
const char* GetName() const { return m_pName; }
VmaSuballocationType GetSuballocationType() const { return (VmaSuballocationType)m_SuballocationType; }
VmaDeviceMemoryBlock* GetBlock() const { VMA_ASSERT(m_Type == ALLOCATION_TYPE_BLOCK); return m_BlockAllocation.m_Block; }
@ -5989,7 +6004,9 @@ public:
bool IsPersistentMap() const { return (m_Flags & FLAG_PERSISTENT_MAP) != 0; }
bool IsMappingAllowed() const { return (m_Flags & FLAG_MAPPING_ALLOWED) != 0; }
void SetUserData(VmaAllocator hAllocator, void* pUserData);
void SetUserData(VmaAllocator hAllocator, void* pUserData) { m_pUserData = pUserData; }
void SetName(VmaAllocator hAllocator, const char* pName);
void FreeName(VmaAllocator hAllocator);
uint8_t SwapBlockAllocation(VmaAllocator hAllocator, VmaAllocation allocation);
VmaAllocHandle GetAllocHandle() const;
VkDeviceSize GetOffset() const;
@ -6036,6 +6053,7 @@ private:
VkDeviceSize m_Alignment;
VkDeviceSize m_Size;
void* m_pUserData;
char* m_pName;
uint32_t m_MemoryTypeIndex;
uint8_t m_Type; // ALLOCATION_TYPE
uint8_t m_SuballocationType; // VmaSuballocationType
@ -6045,8 +6063,6 @@ private:
#if VMA_STATS_STRING_ENABLED
uint32_t m_BufferImageUsage; // 0 if unknown.
#endif
void FreeUserDataString(VmaAllocator hAllocator);
};
#endif // _VMA_ALLOCATION_T
@ -6382,35 +6398,17 @@ void VmaBlockMetadata::DebugLogAllocation(VkDeviceSize offset, VkDeviceSize size
VmaAllocation allocation = reinterpret_cast<VmaAllocation>(userData);
userData = allocation->GetUserData();
const char* name = allocation->GetName();
#if VMA_STATS_STRING_ENABLED
if (userData != VMA_NULL && allocation->IsUserDataString())
{
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %s; Type: %s; Usage: %u",
offset, size, reinterpret_cast<const char*>(userData),
VMA_SUBALLOCATION_TYPE_NAMES[allocation->GetSuballocationType()],
allocation->GetBufferImageUsage());
}
else
{
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %p; Type: %s; Usage: %u",
offset, size, userData,
VMA_SUBALLOCATION_TYPE_NAMES[allocation->GetSuballocationType()],
allocation->GetBufferImageUsage());
}
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %p; Name: %s; Type: %s; Usage: %u",
offset, size, userData, name ? name : "vma_empty",
VMA_SUBALLOCATION_TYPE_NAMES[allocation->GetSuballocationType()],
allocation->GetBufferImageUsage());
#else
if (userData != VMA_NULL && allocation->IsUserDataString())
{
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %s; Type: %u",
offset, size, reinterpret_cast<const char*>(userData),
(uint32_t)allocation->GetSuballocationType());
}
else
{
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %p; Type: %u",
offset, size, userData,
(uint32_t)allocation->GetSuballocationType());
}
VMA_DEBUG_LOG("UNFREED ALLOCATION; Offset: %llu; Size: %llu; UserData: %p; Name: %s; Type: %u",
offset, size, userData, name ? name : "vma_empty",
(uint32_t)allocation->GetSuballocationType());
#endif // VMA_STATS_STRING_ENABLED
}
@ -11948,18 +11946,17 @@ VkResult VmaDeviceMemoryBlock::BindImageMemory(
#endif // _VMA_DEVICE_MEMORY_BLOCK_FUNCTIONS
#ifndef _VMA_ALLOCATION_T_FUNCTIONS
VmaAllocation_T::VmaAllocation_T(bool userDataString, bool mappingAllowed)
VmaAllocation_T::VmaAllocation_T(bool mappingAllowed)
: m_Alignment{ 1 },
m_Size{ 0 },
m_pUserData{ VMA_NULL },
m_pName{ VMA_NULL },
m_MemoryTypeIndex{ 0 },
m_Type{ (uint8_t)ALLOCATION_TYPE_NONE },
m_SuballocationType{ (uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN },
m_MapCount{ 0 },
m_Flags{ 0 }
{
if(userDataString)
m_Flags |= (uint8_t)FLAG_USER_DATA_STRING;
if(mappingAllowed)
m_Flags |= (uint8_t)FLAG_MAPPING_ALLOWED;
@ -11973,7 +11970,7 @@ VmaAllocation_T::~VmaAllocation_T()
VMA_ASSERT(m_MapCount == 0 && "Allocation was not unmapped before destruction.");
// Check if owned string was freed.
VMA_ASSERT((IsUserDataString() && m_pUserData == VMA_NULL) || !IsUserDataString());
VMA_ASSERT(m_pName == VMA_NULL);
}
void VmaAllocation_T::InitBlockAllocation(
@ -12028,23 +12025,14 @@ void VmaAllocation_T::InitDedicatedAllocation(
m_DedicatedAllocation.m_Next = VMA_NULL;
}
void VmaAllocation_T::SetUserData(VmaAllocator hAllocator, void* pUserData)
void VmaAllocation_T::SetName(VmaAllocator hAllocator, const char* pName)
{
if (IsUserDataString())
{
VMA_ASSERT(pUserData == VMA_NULL || pUserData != m_pUserData);
VMA_ASSERT(pName == VMA_NULL || pName != m_pName);
FreeUserDataString(hAllocator);
FreeName(hAllocator);
if (pUserData != VMA_NULL)
{
m_pUserData = VmaCreateStringCopy(hAllocator->GetAllocationCallbacks(), (const char*)pUserData);
}
}
else
{
m_pUserData = pUserData;
}
if (pName != VMA_NULL)
m_pName = VmaCreateStringCopy(hAllocator->GetAllocationCallbacks(), pName);
}
uint8_t VmaAllocation_T::SwapBlockAllocation(VmaAllocator hAllocator, VmaAllocation allocation)
@ -12255,16 +12243,14 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
if (m_pUserData != VMA_NULL)
{
json.WriteString("UserData");
if (IsUserDataString())
{
json.WriteString((const char*)m_pUserData);
}
else
{
json.BeginString();
json.ContinueString_Pointer(m_pUserData);
json.EndString();
}
json.BeginString();
json.ContinueString_Pointer(m_pUserData);
json.EndString();
}
if (m_pName != VMA_NULL)
{
json.WriteString("Name");
json.WriteString(m_pName);
}
if (m_BufferImageUsage != 0)
@ -12275,11 +12261,13 @@ void VmaAllocation_T::PrintParameters(class VmaJsonWriter& json) const
}
#endif // VMA_STATS_STRING_ENABLED
void VmaAllocation_T::FreeUserDataString(VmaAllocator hAllocator)
void VmaAllocation_T::FreeName(VmaAllocator hAllocator)
{
VMA_ASSERT(IsUserDataString());
VmaFreeString(hAllocator->GetAllocationCallbacks(), (char*)m_pUserData);
m_pUserData = VMA_NULL;
if(m_pName)
{
VmaFreeString(hAllocator->GetAllocationCallbacks(), m_pName);
m_pName = VMA_NULL;
}
}
#endif // _VMA_ALLOCATION_T_FUNCTIONS
@ -12810,7 +12798,7 @@ VkResult VmaBlockVector::CommitAllocationRequest(
}
}
*pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(isUserDataString, isMappingAllowed);
*pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(isMappingAllowed);
pBlock->m_pMetadata->Alloc(allocRequest, suballocType, *pAllocation);
(*pAllocation)->InitBlockAllocation(
pBlock,
@ -12821,7 +12809,10 @@ VkResult VmaBlockVector::CommitAllocationRequest(
suballocType,
mapped);
VMA_HEAVY_ASSERT(pBlock->Validate());
(*pAllocation)->SetUserData(m_hAllocator, pUserData);
if (isUserDataString)
(*pAllocation)->SetName(m_hAllocator, (const char*)pUserData);
else
(*pAllocation)->SetUserData(m_hAllocator, pUserData);
m_hAllocator->m_Budget.AddAllocation(m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex), allocRequest.size);
if (VMA_DEBUG_INITIALIZE_ALLOCATIONS)
{
@ -14687,9 +14678,12 @@ VkResult VmaAllocator_T::AllocateDedicatedMemoryPage(
}
}
*pAllocation = m_AllocationObjectAllocator.Allocate(isUserDataString, isMappingAllowed);
*pAllocation = m_AllocationObjectAllocator.Allocate(isMappingAllowed);
(*pAllocation)->InitDedicatedAllocation(pool, memTypeIndex, hMemory, suballocType, pMappedData, size);
(*pAllocation)->SetUserData(this, pUserData);
if (isUserDataString)
(*pAllocation)->SetName(this, (const char*)pUserData);
else
(*pAllocation)->SetUserData(this, pUserData);
m_Budget.AddAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), size);
if(VMA_DEBUG_INITIALIZE_ALLOCATIONS)
{
@ -15021,6 +15015,8 @@ void VmaAllocator_T::FreeMemory(
FillAllocation(allocation, VMA_ALLOCATION_FILL_PATTERN_DESTROYED);
}
allocation->FreeName(this);
switch(allocation->GetType())
{
case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:
@ -15166,6 +15162,7 @@ void VmaAllocator_T::GetAllocationInfo(VmaAllocation hAllocation, VmaAllocationI
pAllocationInfo->size = hAllocation->GetSize();
pAllocationInfo->pMappedData = hAllocation->GetMappedData();
pAllocationInfo->pUserData = hAllocation->GetUserData();
pAllocationInfo->pName = hAllocation->GetName();
}
VkResult VmaAllocator_T::CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPool* pPool)
@ -16600,6 +16597,14 @@ VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(
allocation->SetUserData(allocator, pUserData);
}
VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationName(
VmaAllocator VMA_NOT_NULL allocator,
VmaAllocation VMA_NOT_NULL allocation,
const char* VMA_NULLABLE pName)
{
allocation->SetName(allocator, pName);
}
VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationMemoryProperties(
VmaAllocator VMA_NOT_NULL allocator,
VmaAllocation VMA_NOT_NULL allocation,