Hash :
684ff60b
Author :
Date :
2022-06-21T10:52:31
Vulkan: Add shared ring buffer cmd alloc feature
* Added RingBufferAllocator.cpp with implementation.
* Main classes:
* RingBufferAllocator (fast allocation with bulk deallocation)
* SharedRingBufferAllocator (wrapper to help with shared use and
multiple threads)
* Implemented "angle_enable_vulkan_shared_ring_buffer_cmd_alloc"
feature. (Disabled by default)
* Details (from the original CL)
* The angle::PoolAllocator replaced with
angle::RingBufferAllocator.
* Before, there was separate angle::PoolAllocator per each
CommandBufferHelper. Now, a single angle::RingBufferAllocator
is shared between multiple CommandBufferHelper objects.
* Commands data from multiple CommandBufferHelpers is
tightly packed without fragmentation.
* Significantly less memory overhead, observed with enabled
async queue.
* Moved the parts of the code related to the allocators into the
classes in the new AllocatorHelperPool and AllocatorHelperRing files
for better management. The allocator can be switched by changing the
following BUILD flag:
`angle_enable_vulkan_shared_ring_buffer_cmd_alloc`
* It is connected to the following macro:
ANGLE_ENABLE_VULKAN_SHARED_RING_BUFFER_CMD_ALLOC
* The two main allocator classes in each file are aliased as:
* SecondaryCommandBlockAllocator (in CommandBufferHelper objects)
* SecondaryCommandBlockPool (in SecondaryCommandBuffer)
* Also added placeholder functions for VulkanSecondaryCommandBuffer.
* Added descriptions regarding the two allocators.
* renderer/vulkan/doc/Allocators.md
Credit: Original CL authored by Igor Nazarov <i.nazarov@samsung.com>
Bug: angleproject:6401
Bug: b/256666069
Change-Id: I0f24793eef6334bf4ff8e327b9665338807dad37
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3715968
Reviewed-by: Charlie Lao <cclao@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@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
//
// 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.
//
// AllocatorHelperPool:
// Manages the pool allocators used in the command buffers.
//
#ifndef LIBANGLE_RENDERER_VULKAN_ALLOCATORHELPERPOOL_H_
#define LIBANGLE_RENDERER_VULKAN_ALLOCATORHELPERPOOL_H_
#include "common/PoolAlloc.h"
#include "common/vulkan/vk_headers.h"
#include "libANGLE/renderer/vulkan/vk_command_buffer_utils.h"
#include "libANGLE/renderer/vulkan/vk_wrapper.h"
namespace rx
{
namespace vk
{
namespace priv
{
class SecondaryCommandBuffer;
} // namespace priv
using DedicatedCommandMemoryAllocator = angle::PoolAllocator;
// Used in CommandBufferHelperCommon
class DedicatedCommandBlockAllocator
{
public:
DedicatedCommandBlockAllocator() = default;
void resetAllocator();
bool hasAllocatorLinks() { return false; }
static constexpr size_t kDefaultPoolAllocatorPageSize = 16 * 1024;
void init()
{
mAllocator.initialize(kDefaultPoolAllocatorPageSize, 1);
// Push a scope into the pool allocator so we can easily free and re-init on reset()
mAllocator.push();
}
// Placeholder functions for attaching and detaching the allocator.
void attachAllocator(DedicatedCommandMemoryAllocator *allocator) {}
DedicatedCommandMemoryAllocator *detachAllocator(bool isCommandBufferEmpty) { return nullptr; }
DedicatedCommandMemoryAllocator *getAllocator() { return &mAllocator; }
private:
// Using a pool allocator per CBH to avoid threading issues that occur w/ shared allocator
// between multiple CBHs.
DedicatedCommandMemoryAllocator mAllocator;
};
// Used in SecondaryCommandBuffer
class DedicatedCommandBlockPool final
{
public:
DedicatedCommandBlockPool()
: mAllocator(nullptr),
mCurrentWritePointer(nullptr),
mCurrentBytesRemaining(0),
mCommandBuffer(nullptr)
{}
static constexpr size_t kCommandHeaderSize = 4;
using CommandHeaderIDType = uint16_t;
// Make sure the size of command header ID type is less than total command header size.
static_assert(sizeof(CommandHeaderIDType) < kCommandHeaderSize, "Check size of CommandHeader");
// Pool Alloc uses 16kB pages w/ 16byte header = 16368bytes. To minimize waste
// using a 16368/12 = 1364. Also better perf than 1024 due to fewer block allocations
static constexpr size_t kBlockSize = 1364;
// Make sure block size is 4-byte aligned to avoid Android errors
static_assert((kBlockSize % 4) == 0, "Check kBlockSize alignment");
void setCommandBuffer(priv::SecondaryCommandBuffer *commandBuffer)
{
mCommandBuffer = commandBuffer;
}
void resetCommandBuffer() { mCommandBuffer = nullptr; }
void reset(CommandBufferCommandTracker *commandBufferTracker);
// Initialize the SecondaryCommandBuffer by setting the allocator it will use
angle::Result initialize(DedicatedCommandMemoryAllocator *allocator);
bool valid() const { return mAllocator != nullptr; }
bool empty() const;
void getMemoryUsageStats(size_t *usedMemoryOut, size_t *allocatedMemoryOut) const;
void allocateNewBlock(size_t blockSize = kBlockSize);
void onNewVariableSizedCommand(const size_t requiredSize,
const size_t allocationSize,
bool *usesCommandHeaderSizeForOffsetOut,
uint8_t **ptrOut,
uint8_t **headerOut)
{
ASSERT(*ptrOut == nullptr);
if (mCurrentBytesRemaining < requiredSize)
{
// variable size command can potentially exceed default cmd allocation blockSize
if (requiredSize <= kBlockSize)
{
allocateNewBlock();
}
else
{
// Make sure allocation is 4-byte aligned
const size_t alignedSize = roundUpPow2<size_t>(requiredSize, 4);
ASSERT((alignedSize % 4) == 0);
allocateNewBlock(alignedSize);
}
}
*usesCommandHeaderSizeForOffsetOut = true;
*ptrOut = mCurrentWritePointer;
*headerOut = updateHeaderAndAllocatorParams(allocationSize);
}
void onNewCommand(const size_t requiredSize,
const size_t allocationSize,
uint8_t **ptrOut,
uint8_t **header)
{
ASSERT(*ptrOut == nullptr);
if (mCurrentBytesRemaining < requiredSize)
{
ASSERT(requiredSize < kBlockSize);
allocateNewBlock();
*ptrOut = mCurrentWritePointer;
}
*header = updateHeaderAndAllocatorParams(allocationSize);
}
// Placeholder functions
void terminateLastCommandBlock() {}
void attachAllocator(vk::DedicatedCommandMemoryAllocator *source) {}
void detachAllocator(vk::DedicatedCommandMemoryAllocator *destination) {}
private:
uint8_t *updateHeaderAndAllocatorParams(size_t allocationSize)
{
mCurrentBytesRemaining -= allocationSize;
uint8_t *headerPointer = mCurrentWritePointer;
mCurrentWritePointer += allocationSize;
// Set next cmd header to Invalid (0) so cmd sequence will be terminated
reinterpret_cast<CommandHeaderIDType &>(*mCurrentWritePointer) = 0;
return headerPointer;
}
// Using a pool allocator per CBH to avoid threading issues that occur w/ shared allocator
// between multiple CBHs.
DedicatedCommandMemoryAllocator *mAllocator;
uint8_t *mCurrentWritePointer;
size_t mCurrentBytesRemaining;
// Points to the parent command buffer.
priv::SecondaryCommandBuffer *mCommandBuffer;
};
} // namespace vk
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_ALLOCATORHELPERPOOL_H_