Skip to content

Commit 90eb51c

Browse files
Changed behavior of custom pools: VmaPoolCreateInfo::blockSize 0 (default) now means that pool may use variable block sizes, just like default pools do.
1 parent f9b6868 commit 90eb51c

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/vk_mem_alloc.h

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,9 +1951,13 @@ typedef struct VmaPoolCreateInfo {
19511951
/** \brief Use combination of #VmaPoolCreateFlagBits.
19521952
*/
19531953
VmaPoolCreateFlags flags;
1954-
/** \brief Size of a single `VkDeviceMemory` block to be allocated as part of this pool, in bytes.
1954+
/** \brief Size of a single `VkDeviceMemory` block to be allocated as part of this pool, in bytes. Optional.
19551955
1956-
Optional. Leave 0 to use default.
1956+
Specify nonzero to set explicit, constant size of memory blocks used by this
1957+
pool.
1958+
1959+
Leave 0 to use default and let the library manage block sizes automatically.
1960+
Sizes of particular blocks may vary.
19571961
*/
19581962
VkDeviceSize blockSize;
19591963
/** \brief Minimum number of blocks to be always allocated in this pool, even if they stay empty.
@@ -4926,6 +4930,7 @@ struct VmaBlockVector
49264930
VkDeviceSize bufferImageGranularity,
49274931
uint32_t frameInUseCount,
49284932
bool isCustomPool,
4933+
bool explicitBlockSize,
49294934
bool linearAlgorithm);
49304935
~VmaBlockVector();
49314936

@@ -4988,6 +4993,7 @@ struct VmaBlockVector
49884993
const VkDeviceSize m_BufferImageGranularity;
49894994
const uint32_t m_FrameInUseCount;
49904995
const bool m_IsCustomPool;
4996+
const bool m_ExplicitBlockSize;
49914997
const bool m_LinearAlgorithm;
49924998
bool m_HasEmptyBlock;
49934999
VMA_MUTEX m_Mutex;
@@ -5019,7 +5025,8 @@ struct VmaPool_T
50195025

50205026
VmaPool_T(
50215027
VmaAllocator hAllocator,
5022-
const VmaPoolCreateInfo& createInfo);
5028+
const VmaPoolCreateInfo& createInfo,
5029+
VkDeviceSize preferredBlockSize);
50235030
~VmaPool_T();
50245031

50255032
uint32_t GetId() const { return m_Id; }
@@ -9288,16 +9295,18 @@ static void VmaPostprocessCalcStatInfo(VmaStatInfo& inoutInfo)
92889295

92899296
VmaPool_T::VmaPool_T(
92909297
VmaAllocator hAllocator,
9291-
const VmaPoolCreateInfo& createInfo) :
9298+
const VmaPoolCreateInfo& createInfo,
9299+
VkDeviceSize preferredBlockSize) :
92929300
m_BlockVector(
92939301
hAllocator,
92949302
createInfo.memoryTypeIndex,
9295-
createInfo.blockSize,
9303+
createInfo.blockSize != 0 ? createInfo.blockSize : preferredBlockSize,
92969304
createInfo.minBlockCount,
92979305
createInfo.maxBlockCount,
92989306
(createInfo.flags & VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT) != 0 ? 1 : hAllocator->GetBufferImageGranularity(),
92999307
createInfo.frameInUseCount,
93009308
true, // isCustomPool
9309+
createInfo.blockSize != 0, // explicitBlockSize
93019310
(createInfo.flags & VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT) != 0), // linearAlgorithm
93029311
m_Id(0)
93039312
{
@@ -9320,6 +9329,7 @@ VmaBlockVector::VmaBlockVector(
93209329
VkDeviceSize bufferImageGranularity,
93219330
uint32_t frameInUseCount,
93229331
bool isCustomPool,
9332+
bool explicitBlockSize,
93239333
bool linearAlgorithm) :
93249334
m_hAllocator(hAllocator),
93259335
m_MemoryTypeIndex(memoryTypeIndex),
@@ -9329,6 +9339,7 @@ VmaBlockVector::VmaBlockVector(
93299339
m_BufferImageGranularity(bufferImageGranularity),
93309340
m_FrameInUseCount(frameInUseCount),
93319341
m_IsCustomPool(isCustomPool),
9342+
m_ExplicitBlockSize(explicitBlockSize),
93329343
m_LinearAlgorithm(linearAlgorithm),
93339344
m_Blocks(VmaStlAllocator<VmaDeviceMemoryBlock*>(hAllocator->GetAllocationCallbacks())),
93349345
m_HasEmptyBlock(false),
@@ -9499,9 +9510,7 @@ VkResult VmaBlockVector::Allocate(
94999510
uint32_t newBlockSizeShift = 0;
95009511
const uint32_t NEW_BLOCK_SIZE_SHIFT_MAX = 3;
95019512

9502-
// Allocating blocks of other sizes is allowed only in default pools.
9503-
// In custom pools block size is fixed.
9504-
if(m_IsCustomPool == false)
9513+
if(!m_ExplicitBlockSize)
95059514
{
95069515
// Allocate 1/8, 1/4, 1/2 as first blocks.
95079516
const VkDeviceSize maxExistingBlockSize = CalcMaxBlockSize();
@@ -9523,7 +9532,7 @@ VkResult VmaBlockVector::Allocate(
95239532
size_t newBlockIndex = 0;
95249533
VkResult res = CreateBlock(newBlockSize, &newBlockIndex);
95259534
// Allocation of this size failed? Try 1/2, 1/4, 1/8 of m_PreferredBlockSize.
9526-
if(m_IsCustomPool == false)
9535+
if(!m_ExplicitBlockSize)
95279536
{
95289537
while(res < 0 && newBlockSizeShift < NEW_BLOCK_SIZE_SHIFT_MAX)
95299538
{
@@ -10918,6 +10927,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
1091810927
GetBufferImageGranularity(),
1091910928
pCreateInfo->frameInUseCount,
1092010929
false, // isCustomPool
10930+
false, // explicitBlockSize
1092110931
false); // linearAlgorithm
1092210932
// No need to call m_pBlockVectors[memTypeIndex][blockVectorTypeIndex]->CreateMinBlocks here,
1092310933
// becase minBlockCount is 0.
@@ -11809,12 +11819,10 @@ VkResult VmaAllocator_T::CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPoo
1180911819
{
1181011820
return VK_ERROR_INITIALIZATION_FAILED;
1181111821
}
11812-
if(newCreateInfo.blockSize == 0)
11813-
{
11814-
newCreateInfo.blockSize = CalcPreferredBlockSize(newCreateInfo.memoryTypeIndex);
11815-
}
1181611822

11817-
*pPool = vma_new(this, VmaPool_T)(this, newCreateInfo);
11823+
const VkDeviceSize preferredBlockSize = CalcPreferredBlockSize(newCreateInfo.memoryTypeIndex);
11824+
11825+
*pPool = vma_new(this, VmaPool_T)(this, newCreateInfo, preferredBlockSize);
1181811826

1181911827
VkResult res = (*pPool)->m_BlockVector.CreateMinBlocks();
1182011828
if(res != VK_SUCCESS)

0 commit comments

Comments
 (0)