diff --git a/src/libANGLE/renderer/vulkan/BufferVk.cpp b/src/libANGLE/renderer/vulkan/BufferVk.cpp index 0898e7d67..e0704dd2f 100644 --- a/src/libANGLE/renderer/vulkan/BufferVk.cpp +++ b/src/libANGLE/renderer/vulkan/BufferVk.cpp @@ -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 diff --git a/src/libANGLE/renderer/vulkan/BufferVk.h b/src/libANGLE/renderer/vulkan/BufferVk.h index 6f3b4c256..587edfa37 100644 --- a/src/libANGLE/renderer/vulkan/BufferVk.h +++ b/src/libANGLE/renderer/vulkan/BufferVk.h @@ -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; diff --git a/src/libANGLE/renderer/vulkan/ContextVk.h b/src/libANGLE/renderer/vulkan/ContextVk.h index b00955813..5b399dd21 100644 --- a/src/libANGLE/renderer/vulkan/ContextVk.h +++ b/src/libANGLE/renderer/vulkan/ContextVk.h @@ -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, diff --git a/src/libANGLE/renderer/vulkan/DisplayVk.cpp b/src/libANGLE/renderer/vulkan/DisplayVk.cpp index 68c2b921f..932413cb0 100644 --- a/src/libANGLE/renderer/vulkan/DisplayVk.cpp +++ b/src/libANGLE/renderer/vulkan/DisplayVk.cpp @@ -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 &pool : mDefaultBufferPools) + for (vk::BufferPoolPointerArray &array : mDefaultBufferPools) { - if (pool) + for (std::unique_ptr &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 pool = std::make_unique(); - 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 pool = std::make_unique(); - pool->initWithFlags(renderer, vma::VirtualBlockCreateFlagBits::GENERAL, usageFlags, 0, - memoryTypeIndex, memoryPropertyFlags); - mDefaultBufferPools[memoryTypeIndex] = std::move(pool); + std::unique_ptr pool = std::make_unique(); + 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 &pool : mDefaultBufferPools) + for (vk::BufferPoolPointerArray &array : mDefaultBufferPools) { - if (pool) + for (std::unique_ptr &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 &pool : mDefaultBufferPools) + for (const vk::BufferPoolPointerArray &array : mDefaultBufferPools) { - if (pool) + for (const std::unique_ptr &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 &pool : mDefaultBufferPools) + for (const vk::BufferPoolPointerArray &array : mDefaultBufferPools) { - if (pool && pool->getBufferCount() > 0) + for (const std::unique_ptr &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 diff --git a/src/libANGLE/renderer/vulkan/DisplayVk.h b/src/libANGLE/renderer/vulkan/DisplayVk.h index 6930d9622..f1f549408 100644 --- a/src/libANGLE/renderer/vulkan/DisplayVk.h +++ b/src/libANGLE/renderer/vulkan/DisplayVk.h @@ -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 mSmallBufferPool; - static constexpr VkDeviceSize kMaxSizeToUseSmallBufferPool = 256; + enum class SuballocationAlgorithm : uint8_t + { + Buddy = 0, + General = 1, + InvalidEnum = 2, + EnumCount = InvalidEnum, + }; + angle::PackedEnumMap mDefaultBufferPools; + angle::PackedEnumMap mSizeLimitForBuddyAlgorithm; // The system time when last pruneEmptyBuffer gets called. double mLastPruneTime; diff --git a/src/libANGLE/renderer/vulkan/TransformFeedbackVk.cpp b/src/libANGLE/renderer/vulkan/TransformFeedbackVk.cpp index 53ef5a87b..10e11165a 100644 --- a/src/libANGLE/renderer/vulkan/TransformFeedbackVk.cpp +++ b/src/libANGLE/renderer/vulkan/TransformFeedbackVk.cpp @@ -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(); } diff --git a/src/libANGLE/renderer/vulkan/UtilsVk.cpp b/src/libANGLE/renderer/vulkan/UtilsVk.cpp index 25a49ef57..ce06d1061 100644 --- a/src/libANGLE/renderer/vulkan/UtilsVk.cpp +++ b/src/libANGLE/renderer/vulkan/UtilsVk.cpp @@ -2731,7 +2731,8 @@ angle::Result UtilsVk::stencilBlitResolveNoShaderExport(ContextVk *contextVk, ANGLE_TRY(blitBuffer.get().initSuballocation( contextVk, contextVk->getRenderer()->getDeviceLocalMemoryTypeIndex(), - static_cast(bufferSize), contextVk->getRenderer()->getDefaultBufferAlignment())); + static_cast(bufferSize), contextVk->getRenderer()->getDefaultBufferAlignment(), + BufferUsageType::Static)); BlitResolveStencilNoExportShaderParams shaderParams; // Note: adjustments made for pre-rotatation in FramebufferVk::blit() affect these diff --git a/src/libANGLE/renderer/vulkan/VertexArrayVk.cpp b/src/libANGLE/renderer/vulkan/VertexArrayVk.cpp index eb3a29d83..5036d9ab9 100644 --- a/src/libANGLE/renderer/vulkan/VertexArrayVk.cpp +++ b/src/libANGLE/renderer/vulkan/VertexArrayVk.cpp @@ -335,11 +335,11 @@ angle::Result VertexArrayVk::convertIndexBufferCPU(ContextVk *contextVk, if (mCachedStreamIndexBuffers.size() < kMaxCachedStreamIndexBuffers) { std::unique_ptr buffer = std::make_unique(); - 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()) && diff --git a/src/libANGLE/renderer/vulkan/vk_helpers.cpp b/src/libANGLE/renderer/vulkan/vk_helpers.cpp index fa255a083..45d9d97f0 100644 --- a/src/libANGLE/renderer/vulkan/vk_helpers.cpp +++ b/src/libANGLE/renderer/vulkan/vk_helpers.cpp @@ -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(renderer->getStagingBufferAlignment()); - ANGLE_TRY(initSuballocation(contextVk, memoryTypeIndex, allocationSize, stagingAlignment)); + ANGLE_TRY(initSuballocation(contextVk, memoryTypeIndex, allocationSize, stagingAlignment, + BufferUsageType::Static)); *offset = roundUp(getOffset(), static_cast(imageCopyAlignment)); *dataPtr = getMappedMemory() + (*offset) - getOffset(); diff --git a/src/libANGLE/renderer/vulkan/vk_helpers.h b/src/libANGLE/renderer/vulkan/vk_helpers.h index ba620899b..c99e3f5be 100644 --- a/src/libANGLE/renderer/vulkan/vk_helpers.h +++ b/src/libANGLE/renderer/vulkan/vk_helpers.h @@ -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 . diff --git a/src/libANGLE/renderer/vulkan/vk_utils.h b/src/libANGLE/renderer/vulkan/vk_utils.h index c79df5964..d1c2c1d24 100644 --- a/src/libANGLE/renderer/vulkan/vk_utils.h +++ b/src/libANGLE/renderer/vulkan/vk_utils.h @@ -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;