Hash :
055123f8
Author :
Date :
2025-02-27T10:45:56
Vulkan: Don't maintain SharedCacheKeyManager for BufferBlock Dynamic descriptor type uses the underlying BufferBlock in the descriptorSet. There could be many BufferHelper objects sub-allocated from the same BufferBlock. And each BufferHelper could combine with other buffers to form a descriptorSet. This means the combination for BufferBlock could potentially be very large, in thousands with some app traces like seeing in honkai_star_rail. The overhead of maintaining mDescriptorSetCacheManager for BufferBlock could be too big. In this CL I have chosen to not maintain mDescriptorSetCacheManager in the BufferBlock. The only downside is that when BufferBlock gets destroyed, we will not able to immediately destroy all cached descriptorSets that it is part of. They will still gets evicted later on if needed (see evictStaleDescriptorSets for detail). After this CL, running with all app traces we have, the max vector size of SharedCacheKeyManager::mEmptySlotBits is no more than 2, versus ~70s before the CL. Bug: b/384839847 Bug: b/293297177 Bug: b/237686097 Change-Id: I7c7c91cd0aeacba4145575ac4270b713bf38b742 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6310837 Reviewed-by: Yuxin Hu <yuxinhu@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Suballocation.cpp:
// Implements class methods for BufferBlock and Suballocation and other related classes
//
// #include "libANGLE/renderer/vulkan/vk_utils.h"
#include "libANGLE/renderer/vulkan/Suballocation.h"
#include "libANGLE/Context.h"
#include "libANGLE/renderer/vulkan/vk_mem_alloc_wrapper.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"
namespace rx
{
namespace vk
{
// BufferBlock implementation.
BufferBlock::BufferBlock()
: mMemoryPropertyFlags(0),
mSize(0),
mAllocatedBufferSize(0),
mMemoryAllocationType(MemoryAllocationType::InvalidEnum),
mMemoryTypeIndex(kInvalidMemoryTypeIndex),
mMappedMemory(nullptr)
{}
BufferBlock::BufferBlock(BufferBlock &&other)
: mVirtualBlock(std::move(other.mVirtualBlock)),
mBuffer(std::move(other.mBuffer)),
mDeviceMemory(std::move(other.mDeviceMemory)),
mMemoryPropertyFlags(other.mMemoryPropertyFlags),
mSize(other.mSize),
mAllocatedBufferSize(other.mAllocatedBufferSize),
mMemoryAllocationType(other.mMemoryAllocationType),
mMemoryTypeIndex(other.mMemoryTypeIndex),
mMappedMemory(other.mMappedMemory),
mSerial(other.mSerial),
mCountRemainsEmpty(0)
{}
BufferBlock &BufferBlock::operator=(BufferBlock &&other)
{
std::swap(mVirtualBlock, other.mVirtualBlock);
std::swap(mBuffer, other.mBuffer);
std::swap(mDeviceMemory, other.mDeviceMemory);
std::swap(mMemoryPropertyFlags, other.mMemoryPropertyFlags);
std::swap(mSize, other.mSize);
std::swap(mAllocatedBufferSize, other.mAllocatedBufferSize);
std::swap(mMemoryAllocationType, other.mMemoryAllocationType);
std::swap(mMemoryTypeIndex, other.mMemoryTypeIndex);
std::swap(mMappedMemory, other.mMappedMemory);
std::swap(mSerial, other.mSerial);
std::swap(mCountRemainsEmpty, other.mCountRemainsEmpty);
return *this;
}
BufferBlock::~BufferBlock()
{
ASSERT(!mVirtualBlock.valid());
ASSERT(!mBuffer.valid());
ASSERT(!mDeviceMemory.valid());
}
void BufferBlock::destroy(Renderer *renderer)
{
VkDevice device = renderer->getDevice();
if (mMappedMemory)
{
unmap(device);
}
renderer->onMemoryDealloc(mMemoryAllocationType, mAllocatedBufferSize, mMemoryTypeIndex,
mDeviceMemory.getHandle());
mVirtualBlock.destroy(device);
mBuffer.destroy(device);
mDeviceMemory.destroy(device);
}
VkResult BufferBlock::init(ErrorContext *context,
Buffer &buffer,
uint32_t memoryTypeIndex,
vma::VirtualBlockCreateFlags flags,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size)
{
Renderer *renderer = context->getRenderer();
ASSERT(!mVirtualBlock.valid());
ASSERT(!mBuffer.valid());
ASSERT(!mDeviceMemory.valid());
VK_RESULT_TRY(mVirtualBlock.init(renderer->getDevice(), flags, size));
mBuffer = std::move(buffer);
mDeviceMemory = std::move(deviceMemory);
mMemoryPropertyFlags = memoryPropertyFlags;
mSize = size;
mAllocatedBufferSize = size;
mMemoryAllocationType = MemoryAllocationType::Buffer;
mMemoryTypeIndex = memoryTypeIndex;
mMappedMemory = nullptr;
mSerial = renderer->getResourceSerialFactory().generateBufferSerial();
return VK_SUCCESS;
}
void BufferBlock::initWithoutVirtualBlock(ErrorContext *context,
Buffer &buffer,
MemoryAllocationType memoryAllocationType,
uint32_t memoryTypeIndex,
DeviceMemory &deviceMemory,
VkMemoryPropertyFlags memoryPropertyFlags,
VkDeviceSize size,
VkDeviceSize allocatedBufferSize)
{
Renderer *renderer = context->getRenderer();
ASSERT(!mVirtualBlock.valid());
ASSERT(!mBuffer.valid());
ASSERT(!mDeviceMemory.valid());
mBuffer = std::move(buffer);
mDeviceMemory = std::move(deviceMemory);
mMemoryPropertyFlags = memoryPropertyFlags;
mSize = size;
mAllocatedBufferSize = allocatedBufferSize;
mMemoryAllocationType = memoryAllocationType;
mMemoryTypeIndex = memoryTypeIndex;
mMappedMemory = nullptr;
mSerial = renderer->getResourceSerialFactory().generateBufferSerial();
}
VkResult BufferBlock::map(const VkDevice device)
{
ASSERT(mMappedMemory == nullptr);
return mDeviceMemory.map(device, 0, mAllocatedBufferSize, 0, &mMappedMemory);
}
void BufferBlock::unmap(const VkDevice device)
{
mDeviceMemory.unmap(device);
mMappedMemory = nullptr;
}
VkResult BufferBlock::allocate(VkDeviceSize size,
VkDeviceSize alignment,
VmaVirtualAllocation *allocationOut,
VkDeviceSize *offsetOut)
{
std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
mCountRemainsEmpty = 0;
return mVirtualBlock.allocate(size, alignment, allocationOut, offsetOut);
}
void BufferBlock::free(VmaVirtualAllocation allocation, VkDeviceSize offset)
{
std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
mVirtualBlock.free(allocation, offset);
}
int32_t BufferBlock::getAndIncrementEmptyCounter()
{
return ++mCountRemainsEmpty;
}
void BufferBlock::calculateStats(vma::StatInfo *pStatInfo) const
{
std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
mVirtualBlock.calculateStats(pStatInfo);
}
// BufferSuballocation implementation.
VkResult BufferSuballocation::map(ErrorContext *context)
{
return mBufferBlock->map(context->getDevice());
}
void BufferSuballocation::flush(Renderer *renderer)
{
if (!isCoherent())
{
const VkDeviceSize nonCoherentAtomSize =
renderer->getPhysicalDeviceProperties().limits.nonCoherentAtomSize;
VkMappedMemoryRange mappedRange = {};
mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
mappedRange.memory = mBufferBlock->getDeviceMemory().getHandle();
mappedRange.offset = getOffset();
mappedRange.size = roundUp<VkDeviceSize>(mSize, nonCoherentAtomSize);
ASSERT(mappedRange.size <= mBufferBlock->getAllocatedBufferSize());
mBufferBlock->getDeviceMemory().flush(renderer->getDevice(), mappedRange);
}
}
void BufferSuballocation::invalidate(Renderer *renderer)
{
if (!isCoherent())
{
const VkDeviceSize nonCoherentAtomSize =
renderer->getPhysicalDeviceProperties().limits.nonCoherentAtomSize;
VkMappedMemoryRange mappedRange = {};
mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
mappedRange.memory = mBufferBlock->getDeviceMemory().getHandle();
mappedRange.offset = getOffset();
mappedRange.size = roundUp<VkDeviceSize>(mSize, nonCoherentAtomSize);
ASSERT(mappedRange.size <= mBufferBlock->getAllocatedBufferSize());
mBufferBlock->getDeviceMemory().invalidate(renderer->getDevice(), mappedRange);
}
}
// BufferSuballocationGarbage implementation.
bool BufferSuballocationGarbage::destroyIfComplete(Renderer *renderer)
{
if (renderer->hasResourceUseFinished(mLifetime))
{
mBuffer.destroy(renderer->getDevice());
mSuballocation.destroy(renderer);
return true;
}
return false;
}
bool BufferSuballocationGarbage::hasResourceUseSubmitted(Renderer *renderer) const
{
return renderer->hasResourceUseSubmitted(mLifetime);
}
} // namespace vk
} // namespace rx