Vulkan: Relax size limit for dynamicBuffer to pick buddy algorithm

If glBufferData's usage is one of the dynamic usage, app may keep
calling glBufferData frequently, which means get into suballocation code
frequently. There are two suballocation algorithms today: buddy
algorithm (faster) and generic (slower). Right now the decision of which
algorithm (i.e, which pool) to use is purely based on size or memory
type. This CL also utilize usage information so that dynamic usage will
pick buddy algorithm with bigger size threshold. mSmallBufferPool is
removed and replaced with the BufferPoolPointerArray that gets picked
based on allocation algorithm.

This CL reduces average frame time of efootball_pes_2021 from 7.518 ms
to 4.670 ms on pixel 7 Pro.

Bug: b/271915956
Change-Id: I1c2f270ac49f56e6f405501d20691cfbab49e7eb
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4313685
Reviewed-by: Amirali Abdolrashidi <[email protected]>
Commit-Queue: Charlie Lao <[email protected]>
Reviewed-by: Shahbaz Youssefi <[email protected]>
This commit is contained in:
Charlie Lao authored and Angle LUCI CQ committed 2023-03-11 02:08:27 +00:00
1 parent 4edccb15e3
commit 67ad3ddcf9
11 files changed
+104 -86

No files matched your search

+8 -4
View File
@@ -177,10 +177,12 @@ bool ShouldAvoidRenderPassBreakOnUpdate(ContextVk *contextVk,
return RenderPassUsesBufferForReadOnly(contextVk, buffer);
}
bool IsUsageDynamic(gl::BufferUsage usage)
BufferUsageType GetBufferUsageType(gl::BufferUsage usage)
{
return (usage == gl::BufferUsage::DynamicDraw || usage == gl::BufferUsage::DynamicCopy ||
usage == gl::BufferUsage::DynamicRead);
usage == gl::BufferUsage::DynamicRead)
? BufferUsageType::Dynamic
: BufferUsageType::Static;
}
angle::Result GetMemoryTypeIndex(ContextVk *contextVk,
@@ -267,6 +269,7 @@ BufferVk::BufferVk(const gl::BufferState &state)
mIsStagingBufferMapped(false),
mHasValidData(false),
mIsMappedForWrite(false),
mUsageType(BufferUsageType::Static),
mMappedOffset(0),
mMappedLength(0)
{}
@@ -394,6 +397,7 @@ angle::Result BufferVk::setDataWithMemoryType(const gl::Context *context,
{
ContextVk *contextVk = vk::GetImpl(context);
mUsageType = GetBufferUsageType(usage);
// Reset the flag since the buffer contents are being reinitialized. If the caller passed in
// data to fill the buffer, the flag will be updated when the data is copied to the buffer.
mHasValidData = false;
@@ -856,7 +860,7 @@ angle::Result BufferVk::directUpdate(ContextVk *contextVk,
// If the buffer has dynamic usage then the intent is frequent client side updates to the
// buffer. Don't CPU unmap the buffer, we will take care of unmapping when releasing the buffer
// to either the renderer or mBufferFreeList.
if (!IsUsageDynamic(mState.getUsage()))
if (GetBufferUsageType(mState.getUsage()) == BufferUsageType::Static)
{
mBuffer.unmap(contextVk->getRenderer());
}
@@ -1077,7 +1081,7 @@ angle::Result BufferVk::acquireBufferHelper(ContextVk *contextVk, size_t sizeInB
}
// Allocate the buffer directly
ANGLE_TRY(mBuffer.initSuballocation(contextVk, mMemoryTypeIndex, size, alignment));
ANGLE_TRY(mBuffer.initSuballocation(contextVk, mMemoryTypeIndex, size, alignment, mUsageType));
// Tell the observers (front end) that a new buffer was created, so the necessary
// dirty bits can be set. This allows the buffer views pointing to the old buffer to
+2
View File
@@ -221,6 +221,8 @@ class BufferVk : public BufferImpl
// Otherwise it is mapped from ANGLE internal and will not be consistent with mState access
// bits, so we have to keep record of it.
bool mIsMappedForWrite;
// True if usage is dynamic. May affect how we allocate memory.
BufferUsageType mUsageType;
// Similar as mIsMappedForWrite, this maybe different from mState's getMapOffset/getMapLength if
// mapped from angle internal.
VkDeviceSize mMappedOffset;
+4 -2
View File
@@ -716,9 +716,11 @@ class ContextVk : public ContextImpl, public vk::Context, public MultisampleText
void flushDescriptorSetUpdates();
vk::BufferPool *getDefaultBufferPool(VkDeviceSize size, uint32_t memoryTypeIndex)
vk::BufferPool *getDefaultBufferPool(VkDeviceSize size,
uint32_t memoryTypeIndex,
BufferUsageType usageType)
{
return mShareGroupVk->getDefaultBufferPool(mRenderer, size, memoryTypeIndex);
return mShareGroupVk->getDefaultBufferPool(mRenderer, size, memoryTypeIndex, usageType);
}
angle::Result allocateStreamedVertexBuffer(size_t attribIndex,
+51 -60
View File
@@ -35,6 +35,9 @@ constexpr size_t kDescriptorWriteInfosInitialSize =
kDescriptorBufferInfosInitialSize + kDescriptorImageInfosInitialSize;
constexpr size_t kDescriptorBufferViewsInitialSize = 0;
constexpr VkDeviceSize kMaxStaticBufferSizeToUseBuddyAlgorithm = 256;
constexpr VkDeviceSize kMaxDynamicBufferSizeToUseBuddyAlgorithm = 4096;
// How often monolithic pipelines should be created, if preferMonolithicPipelinesOverLibraries is
// enabled. Pipeline creation is typically O(hundreds of microseconds). A value of 2ms is chosen
// arbitrarily; it ensures that there is always at most a single pipeline job in progress, while
@@ -629,6 +632,9 @@ ShareGroupVk::ShareGroupVk()
mOrphanNonEmptyBufferBlock(false)
{
mLastPruneTime = angle::GetCurrentSystemTime();
mSizeLimitForBuddyAlgorithm[BufferUsageType::Dynamic] =
kMaxDynamicBufferSizeToUseBuddyAlgorithm;
mSizeLimitForBuddyAlgorithm[BufferUsageType::Static] = kMaxStaticBufferSizeToUseBuddyAlgorithm;
}
void ShareGroupVk::addContext(ContextVk *contextVk)
@@ -733,19 +739,17 @@ void ShareGroupVk::onDestroy(const egl::Display *display)
{
RendererVk *renderer = vk::GetImpl(display)->getRenderer();
for (std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
for (vk::BufferPoolPointerArray &array : mDefaultBufferPools)
{
if (pool)
for (std::unique_ptr<vk::BufferPool> &pool : array)
{
pool->destroy(renderer, mOrphanNonEmptyBufferBlock);
if (pool)
{
pool->destroy(renderer, mOrphanNonEmptyBufferBlock);
}
}
}
if (mSmallBufferPool)
{
mSmallBufferPool->destroy(renderer, mOrphanNonEmptyBufferBlock);
}
mPipelineLayoutCache.destroy(renderer);
mDescriptorSetLayoutCache.destroy(renderer);
@@ -955,28 +959,18 @@ uint32_t UpdateDescriptorSetsBuilder::flushDescriptorSetUpdates(VkDevice device)
vk::BufferPool *ShareGroupVk::getDefaultBufferPool(RendererVk *renderer,
VkDeviceSize size,
uint32_t memoryTypeIndex)
uint32_t memoryTypeIndex,
BufferUsageType usageType)
{
if (size <= kMaxSizeToUseSmallBufferPool &&
memoryTypeIndex ==
renderer->getVertexConversionBufferMemoryTypeIndex(vk::MemoryHostVisibility::Visible))
{
if (!mSmallBufferPool)
{
const vk::Allocator &allocator = renderer->getAllocator();
VkBufferUsageFlags usageFlags = GetDefaultBufferUsageFlags(renderer);
// First pick allocation algorithm. Buddy algorithm is faster, but waste more memory
// due to power of two alignment. For smaller size allocation we always use buddy algorithm
// since align to power of two does not waste too much memory. For dynamic usage, the size
// threshold for buddy algorithm is relaxed since the performance is more important.
SuballocationAlgorithm algorithm = size <= mSizeLimitForBuddyAlgorithm[usageType]
? SuballocationAlgorithm::Buddy
: SuballocationAlgorithm::General;
VkMemoryPropertyFlags memoryPropertyFlags;
allocator.getMemoryTypeProperties(memoryTypeIndex, &memoryPropertyFlags);
std::unique_ptr<vk::BufferPool> pool = std::make_unique<vk::BufferPool>();
pool->initWithFlags(renderer, vma::VirtualBlockCreateFlagBits::BUDDY, usageFlags, 0,
memoryTypeIndex, memoryPropertyFlags);
mSmallBufferPool = std::move(pool);
}
return mSmallBufferPool.get();
}
else if (!mDefaultBufferPools[memoryTypeIndex])
if (!mDefaultBufferPools[algorithm][memoryTypeIndex])
{
const vk::Allocator &allocator = renderer->getAllocator();
VkBufferUsageFlags usageFlags = GetDefaultBufferUsageFlags(renderer);
@@ -984,13 +978,16 @@ vk::BufferPool *ShareGroupVk::getDefaultBufferPool(RendererVk *renderer,
VkMemoryPropertyFlags memoryPropertyFlags;
allocator.getMemoryTypeProperties(memoryTypeIndex, &memoryPropertyFlags);
std::unique_ptr<vk::BufferPool> pool = std::make_unique<vk::BufferPool>();
pool->initWithFlags(renderer, vma::VirtualBlockCreateFlagBits::GENERAL, usageFlags, 0,
memoryTypeIndex, memoryPropertyFlags);
mDefaultBufferPools[memoryTypeIndex] = std::move(pool);
std::unique_ptr<vk::BufferPool> pool = std::make_unique<vk::BufferPool>();
vma::VirtualBlockCreateFlags vmaFlags = algorithm == SuballocationAlgorithm::Buddy
? vma::VirtualBlockCreateFlagBits::BUDDY
: vma::VirtualBlockCreateFlagBits::GENERAL;
pool->initWithFlags(renderer, vmaFlags, usageFlags, 0, memoryTypeIndex,
memoryPropertyFlags);
mDefaultBufferPools[algorithm][memoryTypeIndex] = std::move(pool);
}
return mDefaultBufferPools[memoryTypeIndex].get();
return mDefaultBufferPools[algorithm][memoryTypeIndex].get();
}
void ShareGroupVk::pruneDefaultBufferPools(RendererVk *renderer)
@@ -1003,17 +1000,16 @@ void ShareGroupVk::pruneDefaultBufferPools(RendererVk *renderer)
return;
}
for (std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
for (vk::BufferPoolPointerArray &array : mDefaultBufferPools)
{
if (pool)
for (std::unique_ptr<vk::BufferPool> &pool : array)
{
pool->pruneEmptyBuffers(renderer);
if (pool)
{
pool->pruneEmptyBuffers(renderer);
}
}
}
if (mSmallBufferPool)
{
mSmallBufferPool->pruneEmptyBuffers(renderer);
}
renderer->onBufferPoolPrune();
@@ -1045,19 +1041,17 @@ void ShareGroupVk::calculateTotalBufferCount(size_t *bufferCount, VkDeviceSize *
{
*bufferCount = 0;
*totalSize = 0;
for (const std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
for (const vk::BufferPoolPointerArray &array : mDefaultBufferPools)
{
if (pool)
for (const std::unique_ptr<vk::BufferPool> &pool : array)
{
*bufferCount += pool->getBufferCount();
*totalSize += pool->getMemorySize();
if (pool)
{
*bufferCount += pool->getBufferCount();
*totalSize += pool->getMemorySize();
}
}
}
if (mSmallBufferPool)
{
*bufferCount += mSmallBufferPool->getBufferCount();
*totalSize += mSmallBufferPool->getMemorySize();
}
}
void ShareGroupVk::logBufferPools() const
@@ -1068,20 +1062,17 @@ void ShareGroupVk::logBufferPools() const
INFO() << "BufferBlocks count:" << totalBufferCount << " memorySize:" << totalMemorySize / 1024
<< " UnusedBytes/memorySize (KBs):";
for (const std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
for (const vk::BufferPoolPointerArray &array : mDefaultBufferPools)
{
if (pool && pool->getBufferCount() > 0)
for (const std::unique_ptr<vk::BufferPool> &pool : array)
{
std::ostringstream log;
pool->addStats(&log);
INFO() << "\t" << log.str();
if (pool && pool->getBufferCount() > 0)
{
std::ostringstream log;
pool->addStats(&log);
INFO() << "\t" << log.str();
}
}
}
if (mSmallBufferPool && mSmallBufferPool->getBufferCount() > 0)
{
std::ostringstream log;
mSmallBufferPool->addStats(&log);
INFO() << "\t" << log.str();
}
}
} // namespace rx
+11 -6
View File
@@ -93,7 +93,8 @@ class ShareGroupVk : public ShareGroupImpl
vk::BufferPool *getDefaultBufferPool(RendererVk *renderer,
VkDeviceSize size,
uint32_t memoryTypeIndex);
uint32_t memoryTypeIndex,
BufferUsageType usageType);
void pruneDefaultBufferPools(RendererVk *renderer);
bool isDueForBufferPoolPrune(RendererVk *renderer);
@@ -148,11 +149,15 @@ class ShareGroupVk : public ShareGroupImpl
UpdateDescriptorSetsBuilder mUpdateDescriptorSetsBuilder;
// The per shared group buffer pools that all buffers should sub-allocate from.
vk::BufferPoolPointerArray mDefaultBufferPools;
// The pool dedicated for small allocations that uses faster buddy algorithm
std::unique_ptr<vk::BufferPool> mSmallBufferPool;
static constexpr VkDeviceSize kMaxSizeToUseSmallBufferPool = 256;
enum class SuballocationAlgorithm : uint8_t
{
Buddy = 0,
General = 1,
InvalidEnum = 2,
EnumCount = InvalidEnum,
};
angle::PackedEnumMap<SuballocationAlgorithm, vk::BufferPoolPointerArray> mDefaultBufferPools;
angle::PackedEnumMap<BufferUsageType, size_t> mSizeLimitForBuddyAlgorithm;
// The system time when last pruneEmptyBuffer gets called.
double mLastPruneTime;
@@ -115,7 +115,8 @@ angle::Result TransformFeedbackVk::begin(const gl::Context *context,
vk::BufferHelper &bufferHelper = mCounterBufferHelpers[bufferIndex];
ANGLE_TRY(bufferHelper.initSuballocation(
contextVk, contextVk->getRenderer()->getDeviceLocalMemoryTypeIndex(), 16,
contextVk->getRenderer()->getDefaultBufferAlignment()));
contextVk->getRenderer()->getDefaultBufferAlignment(),
BufferUsageType::Static));
mCounterBufferHandles[bufferIndex] = bufferHelper.getBuffer().getHandle();
mCounterBufferOffsets[bufferIndex] = bufferHelper.getOffset();
}
+2 -1
View File
@@ -2731,7 +2731,8 @@ angle::Result UtilsVk::stencilBlitResolveNoShaderExport(ContextVk *contextVk,
ANGLE_TRY(blitBuffer.get().initSuballocation(
contextVk, contextVk->getRenderer()->getDeviceLocalMemoryTypeIndex(),
static_cast<size_t>(bufferSize), contextVk->getRenderer()->getDefaultBufferAlignment()));
static_cast<size_t>(bufferSize), contextVk->getRenderer()->getDefaultBufferAlignment(),
BufferUsageType::Static));
BlitResolveStencilNoExportShaderParams shaderParams;
// Note: adjustments made for pre-rotatation in FramebufferVk::blit() affect these
@@ -335,11 +335,11 @@ angle::Result VertexArrayVk::convertIndexBufferCPU(ContextVk *contextVk,
if (mCachedStreamIndexBuffers.size() < kMaxCachedStreamIndexBuffers)
{
std::unique_ptr<vk::BufferHelper> buffer = std::make_unique<vk::BufferHelper>();
ANGLE_TRY(buffer->initSuballocation(contextVk,
renderer->getVertexConversionBufferMemoryTypeIndex(
vk::MemoryHostVisibility::Visible),
amount,
renderer->getVertexConversionBufferAlignment()));
ANGLE_TRY(buffer->initSuballocation(
contextVk,
renderer->getVertexConversionBufferMemoryTypeIndex(
vk::MemoryHostVisibility::Visible),
amount, renderer->getVertexConversionBufferAlignment(), BufferUsageType::Static));
memcpy(buffer->getMappedMemory(), sourcePointer, amount);
ANGLE_TRY(buffer->flush(renderer));
@@ -664,7 +664,7 @@ angle::Result VertexArrayVk::syncDirtyAttrib(ContextVk *contextVk,
BufferVk *bufferVk = vk::GetImpl(bufferGL);
const angle::Format &intendedFormat = vertexFormat.getIntendedFormat();
bool bindingIsAligned = BindingIsAligned(
binding, intendedFormat, intendedFormat.channelCount, attrib.relativeOffset);
binding, intendedFormat, intendedFormat.channelCount, attrib.relativeOffset);
if (renderer->getFeatures().compressVertexData.enabled &&
gl::IsStaticBufferUsage(bufferGL->getUsage()) &&
+8 -5
View File
@@ -4598,7 +4598,8 @@ angle::Result BufferHelper::initExternal(ContextVk *contextVk,
angle::Result BufferHelper::initSuballocation(ContextVk *contextVk,
uint32_t memoryTypeIndex,
size_t size,
size_t alignment)
size_t alignment,
BufferUsageType usageType)
{
RendererVk *renderer = contextVk->getRenderer();
@@ -4613,7 +4614,7 @@ angle::Result BufferHelper::initSuballocation(ContextVk *contextVk,
size += maxVertexAttribStride;
}
vk::BufferPool *pool = contextVk->getDefaultBufferPool(size, memoryTypeIndex);
vk::BufferPool *pool = contextVk->getDefaultBufferPool(size, memoryTypeIndex, usageType);
ANGLE_TRY(pool->allocateBuffer(contextVk, size, alignment, &mSuballocation));
if (renderer->getFeatures().allocateNonZeroMemory.enabled)
@@ -4631,7 +4632,7 @@ angle::Result BufferHelper::allocateForCopyBuffer(ContextVk *contextVk,
RendererVk *renderer = contextVk->getRenderer();
uint32_t memoryTypeIndex = renderer->getStagingBufferMemoryTypeIndex(coherency);
size_t alignment = renderer->getStagingBufferAlignment();
return initSuballocation(contextVk, memoryTypeIndex, size, alignment);
return initSuballocation(contextVk, memoryTypeIndex, size, alignment, BufferUsageType::Static);
}
angle::Result BufferHelper::allocateForVertexConversion(ContextVk *contextVk,
@@ -4670,7 +4671,8 @@ angle::Result BufferHelper::allocateForVertexConversion(ContextVk *contextVk,
// size anyway.
size_t sizeToAllocate = roundUp(size, alignment);
return initSuballocation(contextVk, memoryTypeIndex, sizeToAllocate, alignment);
return initSuballocation(contextVk, memoryTypeIndex, sizeToAllocate, alignment,
BufferUsageType::Static);
}
angle::Result BufferHelper::allocateForCopyImage(ContextVk *contextVk,
@@ -4693,7 +4695,8 @@ angle::Result BufferHelper::allocateForCopyImage(ContextVk *contextVk,
allocationSize = roundUp(allocationSize, imageCopyAlignment);
size_t stagingAlignment = static_cast<size_t>(renderer->getStagingBufferAlignment());
ANGLE_TRY(initSuballocation(contextVk, memoryTypeIndex, allocationSize, stagingAlignment));
ANGLE_TRY(initSuballocation(contextVk, memoryTypeIndex, allocationSize, stagingAlignment,
BufferUsageType::Static));
*offset = roundUp(getOffset(), static_cast<VkDeviceSize>(imageCopyAlignment));
*dataPtr = getMappedMemory() + (*offset) - getOffset();
+2 -1
View File
@@ -755,7 +755,8 @@ class BufferHelper : public ReadWriteResource
angle::Result initSuballocation(ContextVk *contextVk,
uint32_t memoryTypeIndex,
size_t size,
size_t alignment);
size_t alignment,
BufferUsageType usageType);
// Helper functions to initialize a buffer for a specific usage
// Suballocate a buffer with alignment good for shader storage or copyBuffer .
+8
View File
@@ -108,6 +108,14 @@ enum class TextureDimension
TEX_2D_ARRAY,
};
enum class BufferUsageType
{
Static = 0,
Dynamic = 1,
InvalidEnum = 2,
EnumCount = InvalidEnum,
};
// A maximum offset of 4096 covers almost every Vulkan driver on desktop (80%) and mobile (99%). The
// next highest values to meet native drivers are 16 bits or 32 bits.
constexpr uint32_t kAttributeOffsetMaxBits = 15;