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 171 172 173
//
// 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.
//
// RingBufferAllocator.h:
// Classes used to implement ring buffer allocators.
//
#ifndef COMMON_RING_BUFFER_ALLOCATOR_H_
#define COMMON_RING_BUFFER_ALLOCATOR_H_
#include "angleutils.h"
#include "common/debug.h"
namespace angle
{
static constexpr uint32_t kMinRingBufferAllocatiorCapacity = 1024;
static constexpr uint32_t kDefaultDecaySpeedFactor = 10;
class RingBufferAllocateListener
{
public:
virtual void onRingBufferNewFragment() = 0;
virtual void onRingBufferFragmentEnd() = 0;
protected:
~RingBufferAllocateListener() = default;
};
class RingBufferAllocatorCheckPoint final
{
public:
void reset() { *this = {}; }
bool valid() const { return (mReleasePtr != nullptr); }
private:
friend class RingBufferAllocator;
uint64_t mBufferId = 0;
uint8_t *mReleasePtr = nullptr;
};
class RingBufferAllocatorBuffer final
{
public:
static constexpr uint32_t kBaseOffset = alignof(std::max_align_t);
void resize(uint32_t size) { mStorage.resize(size + kBaseOffset); }
uint8_t *data() { return mStorage.data() + kBaseOffset; }
uint8_t *decClamped(uint8_t *ptr, uint32_t offset) const
{
ASSERT(ptr >= mStorage.data() + kBaseOffset && ptr <= mStorage.data() + mStorage.size());
return ptr - std::min(offset, static_cast<uint32_t>(ptr - mStorage.data()));
}
uint64_t getId() const { return mId; }
void resetId() { mId = 0; }
void incrementId() { mId++; }
size_t getStorageSize() { return mStorage.size(); }
private:
uint64_t mId = 0;
std::vector<uint8_t> mStorage;
};
class RingBufferAllocator final : angle::NonCopyable
{
public:
// Only called from allocate(). Other function may also change the fragment.
RingBufferAllocator() = default;
RingBufferAllocator(RingBufferAllocator &&other);
RingBufferAllocator &operator=(RingBufferAllocator &&other);
void reset();
bool valid() const { return (getPointer() != nullptr); }
void setListener(RingBufferAllocateListener *listener);
void setDecaySpeedFactor(uint32_t decaySpeedFactor);
void setFragmentReserve(uint32_t reserve);
uint8_t *allocate(uint32_t size)
{
ASSERT(valid());
if (ANGLE_LIKELY(mFragmentEndR - mDataEnd >= static_cast<ptrdiff_t>(size)))
{
uint8_t *const result = mDataEnd;
mDataEnd = result + size;
return result;
}
return allocateInNewFragment(size);
}
uint8_t *getPointer() const { return mDataEnd; }
uint32_t getFragmentSize() const
{
ASSERT(mFragmentEnd >= mDataEnd);
return static_cast<uint32_t>(mFragmentEnd - mDataEnd);
}
RingBufferAllocatorCheckPoint getReleaseCheckPoint() const;
void release(const RingBufferAllocatorCheckPoint &checkPoint);
private:
void release(uint8_t *releasePtr);
uint32_t getNumAllocatedInBuffer() const;
void resetPointers();
uint8_t *allocateInNewFragment(uint32_t size);
void resize(uint32_t newCapacity);
RingBufferAllocateListener *mListener = nullptr;
std::vector<RingBufferAllocatorBuffer> mOldBuffers;
RingBufferAllocatorBuffer mBuffer;
uint8_t *mDataBegin = nullptr;
uint8_t *mDataEnd = nullptr;
uint8_t *mFragmentEnd = nullptr;
uint8_t *mFragmentEndR = nullptr;
uint32_t mFragmentReserve = 0;
uint32_t mMinCapacity = 0;
uint32_t mCurrentCapacity = 0;
uint32_t mAllocationMargin = 0;
// 1 - fastest decay speed.
// 2 - 2x slower than fastest, and so on.
uint32_t mDecaySpeedFactor = 0;
};
class SharedRingBufferAllocatorCheckPoint final : angle::NonCopyable
{
public:
void releaseAndUpdate(RingBufferAllocatorCheckPoint *newValue);
private:
friend class SharedRingBufferAllocator;
RingBufferAllocatorCheckPoint pop();
std::mutex mMutex;
RingBufferAllocatorCheckPoint mValue;
#if defined(ANGLE_ENABLE_ASSERTS)
std::atomic<uint32_t> mRefCount{1};
#endif
};
class SharedRingBufferAllocator final : angle::NonCopyable
{
public:
SharedRingBufferAllocator();
~SharedRingBufferAllocator();
RingBufferAllocator &get() { return mAllocator; }
// Once shared - always shared
bool isShared() const { return mSharedCP != nullptr; }
// Once acquired must be released with releaseAndUpdate().
SharedRingBufferAllocatorCheckPoint *acquireSharedCP();
void releaseToSharedCP();
private:
RingBufferAllocator mAllocator;
SharedRingBufferAllocatorCheckPoint *mSharedCP = nullptr;
};
} // namespace angle
#endif // COMMON_RING_BUFFER_ALLOCATOR_H_