1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2022 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // Suballocation.cpp:
7*8975f5c5SAndroid Build Coastguard Worker // Implements class methods for BufferBlock and Suballocation and other related classes
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Worker // #include "libANGLE/renderer/vulkan/vk_utils.h"
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/Suballocation.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Context.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_mem_alloc_wrapper.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_renderer.h"
16*8975f5c5SAndroid Build Coastguard Worker
17*8975f5c5SAndroid Build Coastguard Worker namespace rx
18*8975f5c5SAndroid Build Coastguard Worker {
19*8975f5c5SAndroid Build Coastguard Worker namespace vk
20*8975f5c5SAndroid Build Coastguard Worker {
21*8975f5c5SAndroid Build Coastguard Worker // BufferBlock implementation.
BufferBlock()22*8975f5c5SAndroid Build Coastguard Worker BufferBlock::BufferBlock()
23*8975f5c5SAndroid Build Coastguard Worker : mMemoryPropertyFlags(0),
24*8975f5c5SAndroid Build Coastguard Worker mSize(0),
25*8975f5c5SAndroid Build Coastguard Worker mAllocatedBufferSize(0),
26*8975f5c5SAndroid Build Coastguard Worker mMemoryAllocationType(MemoryAllocationType::InvalidEnum),
27*8975f5c5SAndroid Build Coastguard Worker mMemoryTypeIndex(kInvalidMemoryTypeIndex),
28*8975f5c5SAndroid Build Coastguard Worker mMappedMemory(nullptr)
29*8975f5c5SAndroid Build Coastguard Worker {}
30*8975f5c5SAndroid Build Coastguard Worker
BufferBlock(BufferBlock && other)31*8975f5c5SAndroid Build Coastguard Worker BufferBlock::BufferBlock(BufferBlock &&other)
32*8975f5c5SAndroid Build Coastguard Worker : mVirtualBlock(std::move(other.mVirtualBlock)),
33*8975f5c5SAndroid Build Coastguard Worker mBuffer(std::move(other.mBuffer)),
34*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory(std::move(other.mDeviceMemory)),
35*8975f5c5SAndroid Build Coastguard Worker mMemoryPropertyFlags(other.mMemoryPropertyFlags),
36*8975f5c5SAndroid Build Coastguard Worker mSize(other.mSize),
37*8975f5c5SAndroid Build Coastguard Worker mAllocatedBufferSize(other.mAllocatedBufferSize),
38*8975f5c5SAndroid Build Coastguard Worker mMemoryAllocationType(other.mMemoryAllocationType),
39*8975f5c5SAndroid Build Coastguard Worker mMemoryTypeIndex(other.mMemoryTypeIndex),
40*8975f5c5SAndroid Build Coastguard Worker mMappedMemory(other.mMappedMemory),
41*8975f5c5SAndroid Build Coastguard Worker mSerial(other.mSerial),
42*8975f5c5SAndroid Build Coastguard Worker mCountRemainsEmpty(0)
43*8975f5c5SAndroid Build Coastguard Worker {}
44*8975f5c5SAndroid Build Coastguard Worker
operator =(BufferBlock && other)45*8975f5c5SAndroid Build Coastguard Worker BufferBlock &BufferBlock::operator=(BufferBlock &&other)
46*8975f5c5SAndroid Build Coastguard Worker {
47*8975f5c5SAndroid Build Coastguard Worker std::swap(mVirtualBlock, other.mVirtualBlock);
48*8975f5c5SAndroid Build Coastguard Worker std::swap(mBuffer, other.mBuffer);
49*8975f5c5SAndroid Build Coastguard Worker std::swap(mDeviceMemory, other.mDeviceMemory);
50*8975f5c5SAndroid Build Coastguard Worker std::swap(mMemoryPropertyFlags, other.mMemoryPropertyFlags);
51*8975f5c5SAndroid Build Coastguard Worker std::swap(mSize, other.mSize);
52*8975f5c5SAndroid Build Coastguard Worker std::swap(mAllocatedBufferSize, other.mAllocatedBufferSize);
53*8975f5c5SAndroid Build Coastguard Worker std::swap(mMemoryAllocationType, other.mMemoryAllocationType);
54*8975f5c5SAndroid Build Coastguard Worker std::swap(mMemoryTypeIndex, other.mMemoryTypeIndex);
55*8975f5c5SAndroid Build Coastguard Worker std::swap(mMappedMemory, other.mMappedMemory);
56*8975f5c5SAndroid Build Coastguard Worker std::swap(mSerial, other.mSerial);
57*8975f5c5SAndroid Build Coastguard Worker std::swap(mCountRemainsEmpty, other.mCountRemainsEmpty);
58*8975f5c5SAndroid Build Coastguard Worker return *this;
59*8975f5c5SAndroid Build Coastguard Worker }
60*8975f5c5SAndroid Build Coastguard Worker
~BufferBlock()61*8975f5c5SAndroid Build Coastguard Worker BufferBlock::~BufferBlock()
62*8975f5c5SAndroid Build Coastguard Worker {
63*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mVirtualBlock.valid());
64*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mBuffer.valid());
65*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mDeviceMemory.valid());
66*8975f5c5SAndroid Build Coastguard Worker ASSERT(mDescriptorSetCacheManager.empty());
67*8975f5c5SAndroid Build Coastguard Worker }
68*8975f5c5SAndroid Build Coastguard Worker
destroy(Renderer * renderer)69*8975f5c5SAndroid Build Coastguard Worker void BufferBlock::destroy(Renderer *renderer)
70*8975f5c5SAndroid Build Coastguard Worker {
71*8975f5c5SAndroid Build Coastguard Worker VkDevice device = renderer->getDevice();
72*8975f5c5SAndroid Build Coastguard Worker
73*8975f5c5SAndroid Build Coastguard Worker mDescriptorSetCacheManager.destroyKeys(renderer);
74*8975f5c5SAndroid Build Coastguard Worker if (mMappedMemory)
75*8975f5c5SAndroid Build Coastguard Worker {
76*8975f5c5SAndroid Build Coastguard Worker unmap(device);
77*8975f5c5SAndroid Build Coastguard Worker }
78*8975f5c5SAndroid Build Coastguard Worker
79*8975f5c5SAndroid Build Coastguard Worker renderer->onMemoryDealloc(mMemoryAllocationType, mAllocatedBufferSize, mMemoryTypeIndex,
80*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory.getHandle());
81*8975f5c5SAndroid Build Coastguard Worker
82*8975f5c5SAndroid Build Coastguard Worker mVirtualBlock.destroy(device);
83*8975f5c5SAndroid Build Coastguard Worker mBuffer.destroy(device);
84*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory.destroy(device);
85*8975f5c5SAndroid Build Coastguard Worker }
86*8975f5c5SAndroid Build Coastguard Worker
init(Context * context,Buffer & buffer,uint32_t memoryTypeIndex,vma::VirtualBlockCreateFlags flags,DeviceMemory & deviceMemory,VkMemoryPropertyFlags memoryPropertyFlags,VkDeviceSize size)87*8975f5c5SAndroid Build Coastguard Worker VkResult BufferBlock::init(Context *context,
88*8975f5c5SAndroid Build Coastguard Worker Buffer &buffer,
89*8975f5c5SAndroid Build Coastguard Worker uint32_t memoryTypeIndex,
90*8975f5c5SAndroid Build Coastguard Worker vma::VirtualBlockCreateFlags flags,
91*8975f5c5SAndroid Build Coastguard Worker DeviceMemory &deviceMemory,
92*8975f5c5SAndroid Build Coastguard Worker VkMemoryPropertyFlags memoryPropertyFlags,
93*8975f5c5SAndroid Build Coastguard Worker VkDeviceSize size)
94*8975f5c5SAndroid Build Coastguard Worker {
95*8975f5c5SAndroid Build Coastguard Worker Renderer *renderer = context->getRenderer();
96*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mVirtualBlock.valid());
97*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mBuffer.valid());
98*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mDeviceMemory.valid());
99*8975f5c5SAndroid Build Coastguard Worker
100*8975f5c5SAndroid Build Coastguard Worker VK_RESULT_TRY(mVirtualBlock.init(renderer->getDevice(), flags, size));
101*8975f5c5SAndroid Build Coastguard Worker
102*8975f5c5SAndroid Build Coastguard Worker mBuffer = std::move(buffer);
103*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory = std::move(deviceMemory);
104*8975f5c5SAndroid Build Coastguard Worker mMemoryPropertyFlags = memoryPropertyFlags;
105*8975f5c5SAndroid Build Coastguard Worker mSize = size;
106*8975f5c5SAndroid Build Coastguard Worker mAllocatedBufferSize = size;
107*8975f5c5SAndroid Build Coastguard Worker mMemoryAllocationType = MemoryAllocationType::Buffer;
108*8975f5c5SAndroid Build Coastguard Worker mMemoryTypeIndex = memoryTypeIndex;
109*8975f5c5SAndroid Build Coastguard Worker mMappedMemory = nullptr;
110*8975f5c5SAndroid Build Coastguard Worker mSerial = renderer->getResourceSerialFactory().generateBufferSerial();
111*8975f5c5SAndroid Build Coastguard Worker
112*8975f5c5SAndroid Build Coastguard Worker return VK_SUCCESS;
113*8975f5c5SAndroid Build Coastguard Worker }
114*8975f5c5SAndroid Build Coastguard Worker
initWithoutVirtualBlock(Context * context,Buffer & buffer,MemoryAllocationType memoryAllocationType,uint32_t memoryTypeIndex,DeviceMemory & deviceMemory,VkMemoryPropertyFlags memoryPropertyFlags,VkDeviceSize size,VkDeviceSize allocatedBufferSize)115*8975f5c5SAndroid Build Coastguard Worker void BufferBlock::initWithoutVirtualBlock(Context *context,
116*8975f5c5SAndroid Build Coastguard Worker Buffer &buffer,
117*8975f5c5SAndroid Build Coastguard Worker MemoryAllocationType memoryAllocationType,
118*8975f5c5SAndroid Build Coastguard Worker uint32_t memoryTypeIndex,
119*8975f5c5SAndroid Build Coastguard Worker DeviceMemory &deviceMemory,
120*8975f5c5SAndroid Build Coastguard Worker VkMemoryPropertyFlags memoryPropertyFlags,
121*8975f5c5SAndroid Build Coastguard Worker VkDeviceSize size,
122*8975f5c5SAndroid Build Coastguard Worker VkDeviceSize allocatedBufferSize)
123*8975f5c5SAndroid Build Coastguard Worker {
124*8975f5c5SAndroid Build Coastguard Worker Renderer *renderer = context->getRenderer();
125*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mVirtualBlock.valid());
126*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mBuffer.valid());
127*8975f5c5SAndroid Build Coastguard Worker ASSERT(!mDeviceMemory.valid());
128*8975f5c5SAndroid Build Coastguard Worker
129*8975f5c5SAndroid Build Coastguard Worker mBuffer = std::move(buffer);
130*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory = std::move(deviceMemory);
131*8975f5c5SAndroid Build Coastguard Worker mMemoryPropertyFlags = memoryPropertyFlags;
132*8975f5c5SAndroid Build Coastguard Worker mSize = size;
133*8975f5c5SAndroid Build Coastguard Worker mAllocatedBufferSize = allocatedBufferSize;
134*8975f5c5SAndroid Build Coastguard Worker mMemoryAllocationType = memoryAllocationType;
135*8975f5c5SAndroid Build Coastguard Worker mMemoryTypeIndex = memoryTypeIndex;
136*8975f5c5SAndroid Build Coastguard Worker mMappedMemory = nullptr;
137*8975f5c5SAndroid Build Coastguard Worker mSerial = renderer->getResourceSerialFactory().generateBufferSerial();
138*8975f5c5SAndroid Build Coastguard Worker }
139*8975f5c5SAndroid Build Coastguard Worker
map(const VkDevice device)140*8975f5c5SAndroid Build Coastguard Worker VkResult BufferBlock::map(const VkDevice device)
141*8975f5c5SAndroid Build Coastguard Worker {
142*8975f5c5SAndroid Build Coastguard Worker ASSERT(mMappedMemory == nullptr);
143*8975f5c5SAndroid Build Coastguard Worker return mDeviceMemory.map(device, 0, mSize, 0, &mMappedMemory);
144*8975f5c5SAndroid Build Coastguard Worker }
145*8975f5c5SAndroid Build Coastguard Worker
unmap(const VkDevice device)146*8975f5c5SAndroid Build Coastguard Worker void BufferBlock::unmap(const VkDevice device)
147*8975f5c5SAndroid Build Coastguard Worker {
148*8975f5c5SAndroid Build Coastguard Worker mDeviceMemory.unmap(device);
149*8975f5c5SAndroid Build Coastguard Worker mMappedMemory = nullptr;
150*8975f5c5SAndroid Build Coastguard Worker }
151*8975f5c5SAndroid Build Coastguard Worker
allocate(VkDeviceSize size,VkDeviceSize alignment,VmaVirtualAllocation * allocationOut,VkDeviceSize * offsetOut)152*8975f5c5SAndroid Build Coastguard Worker VkResult BufferBlock::allocate(VkDeviceSize size,
153*8975f5c5SAndroid Build Coastguard Worker VkDeviceSize alignment,
154*8975f5c5SAndroid Build Coastguard Worker VmaVirtualAllocation *allocationOut,
155*8975f5c5SAndroid Build Coastguard Worker VkDeviceSize *offsetOut)
156*8975f5c5SAndroid Build Coastguard Worker {
157*8975f5c5SAndroid Build Coastguard Worker std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
158*8975f5c5SAndroid Build Coastguard Worker mCountRemainsEmpty = 0;
159*8975f5c5SAndroid Build Coastguard Worker return mVirtualBlock.allocate(size, alignment, allocationOut, offsetOut);
160*8975f5c5SAndroid Build Coastguard Worker }
161*8975f5c5SAndroid Build Coastguard Worker
free(VmaVirtualAllocation allocation,VkDeviceSize offset)162*8975f5c5SAndroid Build Coastguard Worker void BufferBlock::free(VmaVirtualAllocation allocation, VkDeviceSize offset)
163*8975f5c5SAndroid Build Coastguard Worker {
164*8975f5c5SAndroid Build Coastguard Worker std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
165*8975f5c5SAndroid Build Coastguard Worker mVirtualBlock.free(allocation, offset);
166*8975f5c5SAndroid Build Coastguard Worker }
167*8975f5c5SAndroid Build Coastguard Worker
getAndIncrementEmptyCounter()168*8975f5c5SAndroid Build Coastguard Worker int32_t BufferBlock::getAndIncrementEmptyCounter()
169*8975f5c5SAndroid Build Coastguard Worker {
170*8975f5c5SAndroid Build Coastguard Worker return ++mCountRemainsEmpty;
171*8975f5c5SAndroid Build Coastguard Worker }
172*8975f5c5SAndroid Build Coastguard Worker
calculateStats(vma::StatInfo * pStatInfo) const173*8975f5c5SAndroid Build Coastguard Worker void BufferBlock::calculateStats(vma::StatInfo *pStatInfo) const
174*8975f5c5SAndroid Build Coastguard Worker {
175*8975f5c5SAndroid Build Coastguard Worker std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
176*8975f5c5SAndroid Build Coastguard Worker mVirtualBlock.calculateStats(pStatInfo);
177*8975f5c5SAndroid Build Coastguard Worker }
178*8975f5c5SAndroid Build Coastguard Worker
179*8975f5c5SAndroid Build Coastguard Worker // BufferSuballocation implementation.
map(Context * context)180*8975f5c5SAndroid Build Coastguard Worker VkResult BufferSuballocation::map(Context *context)
181*8975f5c5SAndroid Build Coastguard Worker {
182*8975f5c5SAndroid Build Coastguard Worker return mBufferBlock->map(context->getDevice());
183*8975f5c5SAndroid Build Coastguard Worker }
184*8975f5c5SAndroid Build Coastguard Worker
185*8975f5c5SAndroid Build Coastguard Worker // BufferSuballocationGarbage implementation.
destroyIfComplete(Renderer * renderer)186*8975f5c5SAndroid Build Coastguard Worker bool BufferSuballocationGarbage::destroyIfComplete(Renderer *renderer)
187*8975f5c5SAndroid Build Coastguard Worker {
188*8975f5c5SAndroid Build Coastguard Worker if (renderer->hasResourceUseFinished(mLifetime))
189*8975f5c5SAndroid Build Coastguard Worker {
190*8975f5c5SAndroid Build Coastguard Worker mBuffer.destroy(renderer->getDevice());
191*8975f5c5SAndroid Build Coastguard Worker mSuballocation.destroy(renderer);
192*8975f5c5SAndroid Build Coastguard Worker return true;
193*8975f5c5SAndroid Build Coastguard Worker }
194*8975f5c5SAndroid Build Coastguard Worker return false;
195*8975f5c5SAndroid Build Coastguard Worker }
196*8975f5c5SAndroid Build Coastguard Worker
hasResourceUseSubmitted(Renderer * renderer) const197*8975f5c5SAndroid Build Coastguard Worker bool BufferSuballocationGarbage::hasResourceUseSubmitted(Renderer *renderer) const
198*8975f5c5SAndroid Build Coastguard Worker {
199*8975f5c5SAndroid Build Coastguard Worker return renderer->hasResourceUseSubmitted(mLifetime);
200*8975f5c5SAndroid Build Coastguard Worker }
201*8975f5c5SAndroid Build Coastguard Worker } // namespace vk
202*8975f5c5SAndroid Build Coastguard Worker } // namespace rx
203