xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/vulkan/Suballocation.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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.h:
7*8975f5c5SAndroid Build Coastguard Worker //    Defines class interface for BufferBlock and Suballocation and other related classes.
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_
11*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_
12*8975f5c5SAndroid Build Coastguard Worker 
13*8975f5c5SAndroid Build Coastguard Worker #include "common/SimpleMutex.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "common/debug.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/angletypes.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/serial_utils.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_cache_utils.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_resource.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_utils.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/vulkan/vk_wrapper.h"
21*8975f5c5SAndroid Build Coastguard Worker 
22*8975f5c5SAndroid Build Coastguard Worker namespace rx
23*8975f5c5SAndroid Build Coastguard Worker {
24*8975f5c5SAndroid Build Coastguard Worker enum class MemoryAllocationType;
25*8975f5c5SAndroid Build Coastguard Worker 
26*8975f5c5SAndroid Build Coastguard Worker namespace vk
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker class Context;
29*8975f5c5SAndroid Build Coastguard Worker 
30*8975f5c5SAndroid Build Coastguard Worker // BufferBlock
31*8975f5c5SAndroid Build Coastguard Worker class BufferBlock final : angle::NonCopyable
32*8975f5c5SAndroid Build Coastguard Worker {
33*8975f5c5SAndroid Build Coastguard Worker   public:
34*8975f5c5SAndroid Build Coastguard Worker     BufferBlock();
35*8975f5c5SAndroid Build Coastguard Worker     BufferBlock(BufferBlock &&other);
36*8975f5c5SAndroid Build Coastguard Worker     ~BufferBlock();
37*8975f5c5SAndroid Build Coastguard Worker 
38*8975f5c5SAndroid Build Coastguard Worker     void destroy(Renderer *renderer);
39*8975f5c5SAndroid Build Coastguard Worker     VkResult init(Context *context,
40*8975f5c5SAndroid Build Coastguard Worker                   Buffer &buffer,
41*8975f5c5SAndroid Build Coastguard Worker                   uint32_t memoryTypeIndex,
42*8975f5c5SAndroid Build Coastguard Worker                   vma::VirtualBlockCreateFlags flags,
43*8975f5c5SAndroid Build Coastguard Worker                   DeviceMemory &deviceMemory,
44*8975f5c5SAndroid Build Coastguard Worker                   VkMemoryPropertyFlags memoryPropertyFlags,
45*8975f5c5SAndroid Build Coastguard Worker                   VkDeviceSize size);
46*8975f5c5SAndroid Build Coastguard Worker     void initWithoutVirtualBlock(Context *context,
47*8975f5c5SAndroid Build Coastguard Worker                                  Buffer &buffer,
48*8975f5c5SAndroid Build Coastguard Worker                                  MemoryAllocationType memoryAllocationType,
49*8975f5c5SAndroid Build Coastguard Worker                                  uint32_t memoryTypeIndex,
50*8975f5c5SAndroid Build Coastguard Worker                                  DeviceMemory &deviceMemory,
51*8975f5c5SAndroid Build Coastguard Worker                                  VkMemoryPropertyFlags memoryPropertyFlags,
52*8975f5c5SAndroid Build Coastguard Worker                                  VkDeviceSize size,
53*8975f5c5SAndroid Build Coastguard Worker                                  VkDeviceSize allocatedBufferSize);
54*8975f5c5SAndroid Build Coastguard Worker 
55*8975f5c5SAndroid Build Coastguard Worker     BufferBlock &operator=(BufferBlock &&other);
56*8975f5c5SAndroid Build Coastguard Worker 
getBuffer()57*8975f5c5SAndroid Build Coastguard Worker     const Buffer &getBuffer() const { return mBuffer; }
getDeviceMemory()58*8975f5c5SAndroid Build Coastguard Worker     const DeviceMemory &getDeviceMemory() const { return mDeviceMemory; }
getDeviceMemory()59*8975f5c5SAndroid Build Coastguard Worker     DeviceMemory &getDeviceMemory() { return mDeviceMemory; }
getBufferSerial()60*8975f5c5SAndroid Build Coastguard Worker     BufferSerial getBufferSerial() const { return mSerial; }
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker     VkMemoryPropertyFlags getMemoryPropertyFlags() const;
63*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize getMemorySize() const;
64*8975f5c5SAndroid Build Coastguard Worker 
65*8975f5c5SAndroid Build Coastguard Worker     VkResult allocate(VkDeviceSize size,
66*8975f5c5SAndroid Build Coastguard Worker                       VkDeviceSize alignment,
67*8975f5c5SAndroid Build Coastguard Worker                       VmaVirtualAllocation *allocationOut,
68*8975f5c5SAndroid Build Coastguard Worker                       VkDeviceSize *offsetOut);
69*8975f5c5SAndroid Build Coastguard Worker     void free(VmaVirtualAllocation allocation, VkDeviceSize offset);
70*8975f5c5SAndroid Build Coastguard Worker     VkBool32 isEmpty();
71*8975f5c5SAndroid Build Coastguard Worker 
hasVirtualBlock()72*8975f5c5SAndroid Build Coastguard Worker     bool hasVirtualBlock() const { return mVirtualBlock.valid(); }
73*8975f5c5SAndroid Build Coastguard Worker     bool isHostVisible() const;
74*8975f5c5SAndroid Build Coastguard Worker     bool isCoherent() const;
75*8975f5c5SAndroid Build Coastguard Worker     bool isCached() const;
76*8975f5c5SAndroid Build Coastguard Worker     bool isMapped() const;
77*8975f5c5SAndroid Build Coastguard Worker     VkResult map(const VkDevice device);
78*8975f5c5SAndroid Build Coastguard Worker     void unmap(const VkDevice device);
79*8975f5c5SAndroid Build Coastguard Worker     uint8_t *getMappedMemory() const;
80*8975f5c5SAndroid Build Coastguard Worker 
81*8975f5c5SAndroid Build Coastguard Worker     // This should be called whenever this found to be empty. The total number of count of empty is
82*8975f5c5SAndroid Build Coastguard Worker     // returned.
83*8975f5c5SAndroid Build Coastguard Worker     int32_t getAndIncrementEmptyCounter();
84*8975f5c5SAndroid Build Coastguard Worker     void calculateStats(vma::StatInfo *pStatInfo) const;
85*8975f5c5SAndroid Build Coastguard Worker 
onNewDescriptorSet(const SharedDescriptorSetCacheKey & sharedCacheKey)86*8975f5c5SAndroid Build Coastguard Worker     void onNewDescriptorSet(const SharedDescriptorSetCacheKey &sharedCacheKey)
87*8975f5c5SAndroid Build Coastguard Worker     {
88*8975f5c5SAndroid Build Coastguard Worker         mDescriptorSetCacheManager.addKey(sharedCacheKey);
89*8975f5c5SAndroid Build Coastguard Worker     }
releaseAllCachedDescriptorSetCacheKeys(Renderer * renderer)90*8975f5c5SAndroid Build Coastguard Worker     void releaseAllCachedDescriptorSetCacheKeys(Renderer *renderer)
91*8975f5c5SAndroid Build Coastguard Worker     {
92*8975f5c5SAndroid Build Coastguard Worker         if (!mDescriptorSetCacheManager.empty())
93*8975f5c5SAndroid Build Coastguard Worker         {
94*8975f5c5SAndroid Build Coastguard Worker             mDescriptorSetCacheManager.releaseKeys(renderer);
95*8975f5c5SAndroid Build Coastguard Worker         }
96*8975f5c5SAndroid Build Coastguard Worker     }
97*8975f5c5SAndroid Build Coastguard Worker 
98*8975f5c5SAndroid Build Coastguard Worker   private:
99*8975f5c5SAndroid Build Coastguard Worker     mutable angle::SimpleMutex mVirtualBlockMutex;
100*8975f5c5SAndroid Build Coastguard Worker     VirtualBlock mVirtualBlock;
101*8975f5c5SAndroid Build Coastguard Worker 
102*8975f5c5SAndroid Build Coastguard Worker     Buffer mBuffer;
103*8975f5c5SAndroid Build Coastguard Worker     DeviceMemory mDeviceMemory;
104*8975f5c5SAndroid Build Coastguard Worker     VkMemoryPropertyFlags mMemoryPropertyFlags;
105*8975f5c5SAndroid Build Coastguard Worker 
106*8975f5c5SAndroid Build Coastguard Worker     // Memory size that user of this object thinks we have.
107*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize mSize;
108*8975f5c5SAndroid Build Coastguard Worker     // Memory size that was actually allocated for this object.
109*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize mAllocatedBufferSize;
110*8975f5c5SAndroid Build Coastguard Worker     // Memory allocation type used for this object.
111*8975f5c5SAndroid Build Coastguard Worker     MemoryAllocationType mMemoryAllocationType;
112*8975f5c5SAndroid Build Coastguard Worker     // Memory type index used for the allocation. It can be used to determine the heap index.
113*8975f5c5SAndroid Build Coastguard Worker     uint32_t mMemoryTypeIndex;
114*8975f5c5SAndroid Build Coastguard Worker 
115*8975f5c5SAndroid Build Coastguard Worker     uint8_t *mMappedMemory;
116*8975f5c5SAndroid Build Coastguard Worker     BufferSerial mSerial;
117*8975f5c5SAndroid Build Coastguard Worker     // Heuristic information for pruneEmptyBuffer. This tracks how many times (consecutively) this
118*8975f5c5SAndroid Build Coastguard Worker     // buffer block is found to be empty when pruneEmptyBuffer is called. This gets reset whenever
119*8975f5c5SAndroid Build Coastguard Worker     // it becomes non-empty.
120*8975f5c5SAndroid Build Coastguard Worker     int32_t mCountRemainsEmpty;
121*8975f5c5SAndroid Build Coastguard Worker     // Manages the descriptorSet cache that created with this BufferBlock.
122*8975f5c5SAndroid Build Coastguard Worker     DescriptorSetCacheManager mDescriptorSetCacheManager;
123*8975f5c5SAndroid Build Coastguard Worker };
124*8975f5c5SAndroid Build Coastguard Worker using BufferBlockPointer       = std::unique_ptr<BufferBlock>;
125*8975f5c5SAndroid Build Coastguard Worker using BufferBlockPointerVector = std::vector<BufferBlockPointer>;
126*8975f5c5SAndroid Build Coastguard Worker 
127*8975f5c5SAndroid Build Coastguard Worker class BufferBlockGarbageList final : angle::NonCopyable
128*8975f5c5SAndroid Build Coastguard Worker {
129*8975f5c5SAndroid Build Coastguard Worker   public:
BufferBlockGarbageList()130*8975f5c5SAndroid Build Coastguard Worker     BufferBlockGarbageList() : mBufferBlockQueue(kInitialQueueCapacity) {}
~BufferBlockGarbageList()131*8975f5c5SAndroid Build Coastguard Worker     ~BufferBlockGarbageList() { ASSERT(mBufferBlockQueue.empty()); }
132*8975f5c5SAndroid Build Coastguard Worker 
add(BufferBlock * bufferBlock)133*8975f5c5SAndroid Build Coastguard Worker     void add(BufferBlock *bufferBlock)
134*8975f5c5SAndroid Build Coastguard Worker     {
135*8975f5c5SAndroid Build Coastguard Worker         std::unique_lock<angle::SimpleMutex> lock(mMutex);
136*8975f5c5SAndroid Build Coastguard Worker         if (mBufferBlockQueue.full())
137*8975f5c5SAndroid Build Coastguard Worker         {
138*8975f5c5SAndroid Build Coastguard Worker             size_t newCapacity = mBufferBlockQueue.capacity() << 1;
139*8975f5c5SAndroid Build Coastguard Worker             mBufferBlockQueue.updateCapacity(newCapacity);
140*8975f5c5SAndroid Build Coastguard Worker         }
141*8975f5c5SAndroid Build Coastguard Worker         mBufferBlockQueue.push(bufferBlock);
142*8975f5c5SAndroid Build Coastguard Worker     }
143*8975f5c5SAndroid Build Coastguard Worker 
144*8975f5c5SAndroid Build Coastguard Worker     // Number of buffer blocks destroyed is returned.
pruneEmptyBufferBlocks(Renderer * renderer)145*8975f5c5SAndroid Build Coastguard Worker     size_t pruneEmptyBufferBlocks(Renderer *renderer)
146*8975f5c5SAndroid Build Coastguard Worker     {
147*8975f5c5SAndroid Build Coastguard Worker         size_t blocksDestroyed = 0;
148*8975f5c5SAndroid Build Coastguard Worker         if (!mBufferBlockQueue.empty())
149*8975f5c5SAndroid Build Coastguard Worker         {
150*8975f5c5SAndroid Build Coastguard Worker             std::unique_lock<angle::SimpleMutex> lock(mMutex);
151*8975f5c5SAndroid Build Coastguard Worker             size_t count = mBufferBlockQueue.size();
152*8975f5c5SAndroid Build Coastguard Worker             for (size_t i = 0; i < count; i++)
153*8975f5c5SAndroid Build Coastguard Worker             {
154*8975f5c5SAndroid Build Coastguard Worker                 BufferBlock *block = mBufferBlockQueue.front();
155*8975f5c5SAndroid Build Coastguard Worker                 mBufferBlockQueue.pop();
156*8975f5c5SAndroid Build Coastguard Worker                 if (block->isEmpty())
157*8975f5c5SAndroid Build Coastguard Worker                 {
158*8975f5c5SAndroid Build Coastguard Worker                     block->destroy(renderer);
159*8975f5c5SAndroid Build Coastguard Worker                     ++blocksDestroyed;
160*8975f5c5SAndroid Build Coastguard Worker                 }
161*8975f5c5SAndroid Build Coastguard Worker                 else
162*8975f5c5SAndroid Build Coastguard Worker                 {
163*8975f5c5SAndroid Build Coastguard Worker                     mBufferBlockQueue.push(block);
164*8975f5c5SAndroid Build Coastguard Worker                 }
165*8975f5c5SAndroid Build Coastguard Worker             }
166*8975f5c5SAndroid Build Coastguard Worker         }
167*8975f5c5SAndroid Build Coastguard Worker         return blocksDestroyed;
168*8975f5c5SAndroid Build Coastguard Worker     }
169*8975f5c5SAndroid Build Coastguard Worker 
empty()170*8975f5c5SAndroid Build Coastguard Worker     bool empty() const { return mBufferBlockQueue.empty(); }
171*8975f5c5SAndroid Build Coastguard Worker 
172*8975f5c5SAndroid Build Coastguard Worker   private:
173*8975f5c5SAndroid Build Coastguard Worker     static constexpr size_t kInitialQueueCapacity = 4;
174*8975f5c5SAndroid Build Coastguard Worker     angle::SimpleMutex mMutex;
175*8975f5c5SAndroid Build Coastguard Worker     angle::FixedQueue<BufferBlock *> mBufferBlockQueue;
176*8975f5c5SAndroid Build Coastguard Worker };
177*8975f5c5SAndroid Build Coastguard Worker 
178*8975f5c5SAndroid Build Coastguard Worker // BufferSuballocation
179*8975f5c5SAndroid Build Coastguard Worker class BufferSuballocation final : angle::NonCopyable
180*8975f5c5SAndroid Build Coastguard Worker {
181*8975f5c5SAndroid Build Coastguard Worker   public:
182*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocation();
183*8975f5c5SAndroid Build Coastguard Worker 
184*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocation(BufferSuballocation &&other);
185*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocation &operator=(BufferSuballocation &&other);
186*8975f5c5SAndroid Build Coastguard Worker 
187*8975f5c5SAndroid Build Coastguard Worker     void destroy(Renderer *renderer);
188*8975f5c5SAndroid Build Coastguard Worker 
189*8975f5c5SAndroid Build Coastguard Worker     void init(BufferBlock *block,
190*8975f5c5SAndroid Build Coastguard Worker               VmaVirtualAllocation allocation,
191*8975f5c5SAndroid Build Coastguard Worker               VkDeviceSize offset,
192*8975f5c5SAndroid Build Coastguard Worker               VkDeviceSize size);
193*8975f5c5SAndroid Build Coastguard Worker     void initWithEntireBuffer(Context *context,
194*8975f5c5SAndroid Build Coastguard Worker                               Buffer &buffer,
195*8975f5c5SAndroid Build Coastguard Worker                               MemoryAllocationType memoryAllocationType,
196*8975f5c5SAndroid Build Coastguard Worker                               uint32_t memoryTypeIndex,
197*8975f5c5SAndroid Build Coastguard Worker                               DeviceMemory &deviceMemory,
198*8975f5c5SAndroid Build Coastguard Worker                               VkMemoryPropertyFlags memoryPropertyFlags,
199*8975f5c5SAndroid Build Coastguard Worker                               VkDeviceSize size,
200*8975f5c5SAndroid Build Coastguard Worker                               VkDeviceSize allocatedBufferSize);
201*8975f5c5SAndroid Build Coastguard Worker 
202*8975f5c5SAndroid Build Coastguard Worker     const Buffer &getBuffer() const;
203*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize getSize() const;
204*8975f5c5SAndroid Build Coastguard Worker     const DeviceMemory &getDeviceMemory() const;
205*8975f5c5SAndroid Build Coastguard Worker     VkMemoryMapFlags getMemoryPropertyFlags() const;
206*8975f5c5SAndroid Build Coastguard Worker     bool isHostVisible() const;
207*8975f5c5SAndroid Build Coastguard Worker     bool isCoherent() const;
208*8975f5c5SAndroid Build Coastguard Worker     bool isCached() const;
209*8975f5c5SAndroid Build Coastguard Worker     bool isMapped() const;
210*8975f5c5SAndroid Build Coastguard Worker     uint8_t *getMappedMemory() const;
211*8975f5c5SAndroid Build Coastguard Worker     void flush(const VkDevice &device);
212*8975f5c5SAndroid Build Coastguard Worker     void invalidate(const VkDevice &device);
213*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize getOffset() const;
214*8975f5c5SAndroid Build Coastguard Worker     bool valid() const;
215*8975f5c5SAndroid Build Coastguard Worker     VkResult map(Context *context);
216*8975f5c5SAndroid Build Coastguard Worker     BufferSerial getBlockSerial() const;
217*8975f5c5SAndroid Build Coastguard Worker     uint8_t *getBlockMemory() const;
218*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize getBlockMemorySize() const;
isSuballocated()219*8975f5c5SAndroid Build Coastguard Worker     bool isSuballocated() const { return mBufferBlock->hasVirtualBlock(); }
getBufferBlock()220*8975f5c5SAndroid Build Coastguard Worker     BufferBlock *getBufferBlock() const { return mBufferBlock; }
221*8975f5c5SAndroid Build Coastguard Worker 
222*8975f5c5SAndroid Build Coastguard Worker   private:
223*8975f5c5SAndroid Build Coastguard Worker     // Only used by DynamicBuffer where DynamicBuffer does the actual suballocation and pass the
224*8975f5c5SAndroid Build Coastguard Worker     // offset/size to this object. Since DynamicBuffer does not have a VMA virtual allocator, they
225*8975f5c5SAndroid Build Coastguard Worker     // will be ignored at destroy time. The offset/size is set here mainly for easy retrieval when
226*8975f5c5SAndroid Build Coastguard Worker     // the BufferHelper object is passed around.
227*8975f5c5SAndroid Build Coastguard Worker     friend class BufferHelper;
228*8975f5c5SAndroid Build Coastguard Worker     void setOffsetAndSize(VkDeviceSize offset, VkDeviceSize size);
229*8975f5c5SAndroid Build Coastguard Worker 
230*8975f5c5SAndroid Build Coastguard Worker     BufferBlock *mBufferBlock;
231*8975f5c5SAndroid Build Coastguard Worker     VmaVirtualAllocation mAllocation;
232*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize mOffset;
233*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize mSize;
234*8975f5c5SAndroid Build Coastguard Worker };
235*8975f5c5SAndroid Build Coastguard Worker 
236*8975f5c5SAndroid Build Coastguard Worker class BufferSuballocationGarbage
237*8975f5c5SAndroid Build Coastguard Worker {
238*8975f5c5SAndroid Build Coastguard Worker   public:
239*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocationGarbage() = default;
BufferSuballocationGarbage(BufferSuballocationGarbage && other)240*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocationGarbage(BufferSuballocationGarbage &&other)
241*8975f5c5SAndroid Build Coastguard Worker         : mLifetime(other.mLifetime),
242*8975f5c5SAndroid Build Coastguard Worker           mSuballocation(std::move(other.mSuballocation)),
243*8975f5c5SAndroid Build Coastguard Worker           mBuffer(std::move(other.mBuffer))
244*8975f5c5SAndroid Build Coastguard Worker     {}
245*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocationGarbage &operator=(BufferSuballocationGarbage &&other)
246*8975f5c5SAndroid Build Coastguard Worker     {
247*8975f5c5SAndroid Build Coastguard Worker         mLifetime      = other.mLifetime;
248*8975f5c5SAndroid Build Coastguard Worker         mSuballocation = std::move(other.mSuballocation);
249*8975f5c5SAndroid Build Coastguard Worker         mBuffer        = std::move(other.mBuffer);
250*8975f5c5SAndroid Build Coastguard Worker         return *this;
251*8975f5c5SAndroid Build Coastguard Worker     }
BufferSuballocationGarbage(const ResourceUse & use,BufferSuballocation && suballocation,Buffer && buffer)252*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocationGarbage(const ResourceUse &use,
253*8975f5c5SAndroid Build Coastguard Worker                                BufferSuballocation &&suballocation,
254*8975f5c5SAndroid Build Coastguard Worker                                Buffer &&buffer)
255*8975f5c5SAndroid Build Coastguard Worker         : mLifetime(use), mSuballocation(std::move(suballocation)), mBuffer(std::move(buffer))
256*8975f5c5SAndroid Build Coastguard Worker     {}
257*8975f5c5SAndroid Build Coastguard Worker     ~BufferSuballocationGarbage() = default;
258*8975f5c5SAndroid Build Coastguard Worker 
259*8975f5c5SAndroid Build Coastguard Worker     bool destroyIfComplete(Renderer *renderer);
260*8975f5c5SAndroid Build Coastguard Worker     bool hasResourceUseSubmitted(Renderer *renderer) const;
getSize()261*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize getSize() const { return mSuballocation.getSize(); }
isSuballocated()262*8975f5c5SAndroid Build Coastguard Worker     bool isSuballocated() const { return mSuballocation.isSuballocated(); }
263*8975f5c5SAndroid Build Coastguard Worker 
264*8975f5c5SAndroid Build Coastguard Worker   private:
265*8975f5c5SAndroid Build Coastguard Worker     ResourceUse mLifetime;
266*8975f5c5SAndroid Build Coastguard Worker     BufferSuballocation mSuballocation;
267*8975f5c5SAndroid Build Coastguard Worker     Buffer mBuffer;
268*8975f5c5SAndroid Build Coastguard Worker };
269*8975f5c5SAndroid Build Coastguard Worker 
270*8975f5c5SAndroid Build Coastguard Worker // BufferBlock implementation.
getMemoryPropertyFlags()271*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkMemoryPropertyFlags BufferBlock::getMemoryPropertyFlags() const
272*8975f5c5SAndroid Build Coastguard Worker {
273*8975f5c5SAndroid Build Coastguard Worker     return mMemoryPropertyFlags;
274*8975f5c5SAndroid Build Coastguard Worker }
275*8975f5c5SAndroid Build Coastguard Worker 
getMemorySize()276*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkDeviceSize BufferBlock::getMemorySize() const
277*8975f5c5SAndroid Build Coastguard Worker {
278*8975f5c5SAndroid Build Coastguard Worker     return mSize;
279*8975f5c5SAndroid Build Coastguard Worker }
280*8975f5c5SAndroid Build Coastguard Worker 
isEmpty()281*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkBool32 BufferBlock::isEmpty()
282*8975f5c5SAndroid Build Coastguard Worker {
283*8975f5c5SAndroid Build Coastguard Worker     std::unique_lock<angle::SimpleMutex> lock(mVirtualBlockMutex);
284*8975f5c5SAndroid Build Coastguard Worker     return vma::IsVirtualBlockEmpty(mVirtualBlock.getHandle());
285*8975f5c5SAndroid Build Coastguard Worker }
286*8975f5c5SAndroid Build Coastguard Worker 
isHostVisible()287*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferBlock::isHostVisible() const
288*8975f5c5SAndroid Build Coastguard Worker {
289*8975f5c5SAndroid Build Coastguard Worker     return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
290*8975f5c5SAndroid Build Coastguard Worker }
291*8975f5c5SAndroid Build Coastguard Worker 
isCoherent()292*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferBlock::isCoherent() const
293*8975f5c5SAndroid Build Coastguard Worker {
294*8975f5c5SAndroid Build Coastguard Worker     return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
295*8975f5c5SAndroid Build Coastguard Worker }
296*8975f5c5SAndroid Build Coastguard Worker 
isCached()297*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferBlock::isCached() const
298*8975f5c5SAndroid Build Coastguard Worker {
299*8975f5c5SAndroid Build Coastguard Worker     return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) != 0;
300*8975f5c5SAndroid Build Coastguard Worker }
301*8975f5c5SAndroid Build Coastguard Worker 
isMapped()302*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferBlock::isMapped() const
303*8975f5c5SAndroid Build Coastguard Worker {
304*8975f5c5SAndroid Build Coastguard Worker     return mMappedMemory != nullptr;
305*8975f5c5SAndroid Build Coastguard Worker }
306*8975f5c5SAndroid Build Coastguard Worker 
getMappedMemory()307*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE uint8_t *BufferBlock::getMappedMemory() const
308*8975f5c5SAndroid Build Coastguard Worker {
309*8975f5c5SAndroid Build Coastguard Worker     ASSERT(mMappedMemory != nullptr);
310*8975f5c5SAndroid Build Coastguard Worker     return mMappedMemory;
311*8975f5c5SAndroid Build Coastguard Worker }
312*8975f5c5SAndroid Build Coastguard Worker 
313*8975f5c5SAndroid Build Coastguard Worker // BufferSuballocation implementation.
BufferSuballocation()314*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE BufferSuballocation::BufferSuballocation()
315*8975f5c5SAndroid Build Coastguard Worker     : mBufferBlock(nullptr), mAllocation(VK_NULL_HANDLE), mOffset(0), mSize(0)
316*8975f5c5SAndroid Build Coastguard Worker {}
317*8975f5c5SAndroid Build Coastguard Worker 
BufferSuballocation(BufferSuballocation && other)318*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE BufferSuballocation::BufferSuballocation(BufferSuballocation &&other)
319*8975f5c5SAndroid Build Coastguard Worker     : BufferSuballocation()
320*8975f5c5SAndroid Build Coastguard Worker {
321*8975f5c5SAndroid Build Coastguard Worker     *this = std::move(other);
322*8975f5c5SAndroid Build Coastguard Worker }
323*8975f5c5SAndroid Build Coastguard Worker 
324*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE BufferSuballocation &BufferSuballocation::operator=(BufferSuballocation &&other)
325*8975f5c5SAndroid Build Coastguard Worker {
326*8975f5c5SAndroid Build Coastguard Worker     std::swap(mBufferBlock, other.mBufferBlock);
327*8975f5c5SAndroid Build Coastguard Worker     std::swap(mSize, other.mSize);
328*8975f5c5SAndroid Build Coastguard Worker     std::swap(mAllocation, other.mAllocation);
329*8975f5c5SAndroid Build Coastguard Worker     std::swap(mOffset, other.mOffset);
330*8975f5c5SAndroid Build Coastguard Worker     return *this;
331*8975f5c5SAndroid Build Coastguard Worker }
332*8975f5c5SAndroid Build Coastguard Worker 
valid()333*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferSuballocation::valid() const
334*8975f5c5SAndroid Build Coastguard Worker {
335*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock != nullptr;
336*8975f5c5SAndroid Build Coastguard Worker }
337*8975f5c5SAndroid Build Coastguard Worker 
destroy(Renderer * renderer)338*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::destroy(Renderer *renderer)
339*8975f5c5SAndroid Build Coastguard Worker {
340*8975f5c5SAndroid Build Coastguard Worker     if (valid())
341*8975f5c5SAndroid Build Coastguard Worker     {
342*8975f5c5SAndroid Build Coastguard Worker         ASSERT(mBufferBlock);
343*8975f5c5SAndroid Build Coastguard Worker         if (mBufferBlock->hasVirtualBlock())
344*8975f5c5SAndroid Build Coastguard Worker         {
345*8975f5c5SAndroid Build Coastguard Worker             mBufferBlock->free(mAllocation, mOffset);
346*8975f5c5SAndroid Build Coastguard Worker             mBufferBlock = nullptr;
347*8975f5c5SAndroid Build Coastguard Worker         }
348*8975f5c5SAndroid Build Coastguard Worker         else
349*8975f5c5SAndroid Build Coastguard Worker         {
350*8975f5c5SAndroid Build Coastguard Worker             // When virtual block is invalid, this is the standalone buffer that are created by
351*8975f5c5SAndroid Build Coastguard Worker             // BufferSuballocation::initWithEntireBuffer call. In this case, vmaBufferSuballocation
352*8975f5c5SAndroid Build Coastguard Worker             // owns block, we must properly delete the block object.
353*8975f5c5SAndroid Build Coastguard Worker             mBufferBlock->destroy(renderer);
354*8975f5c5SAndroid Build Coastguard Worker             SafeDelete(mBufferBlock);
355*8975f5c5SAndroid Build Coastguard Worker         }
356*8975f5c5SAndroid Build Coastguard Worker         mAllocation = VK_NULL_HANDLE;
357*8975f5c5SAndroid Build Coastguard Worker         mOffset     = 0;
358*8975f5c5SAndroid Build Coastguard Worker         mSize       = 0;
359*8975f5c5SAndroid Build Coastguard Worker     }
360*8975f5c5SAndroid Build Coastguard Worker }
361*8975f5c5SAndroid Build Coastguard Worker 
init(BufferBlock * block,VmaVirtualAllocation allocation,VkDeviceSize offset,VkDeviceSize size)362*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::init(BufferBlock *block,
363*8975f5c5SAndroid Build Coastguard Worker                                             VmaVirtualAllocation allocation,
364*8975f5c5SAndroid Build Coastguard Worker                                             VkDeviceSize offset,
365*8975f5c5SAndroid Build Coastguard Worker                                             VkDeviceSize size)
366*8975f5c5SAndroid Build Coastguard Worker {
367*8975f5c5SAndroid Build Coastguard Worker     ASSERT(!valid());
368*8975f5c5SAndroid Build Coastguard Worker     ASSERT(block != nullptr);
369*8975f5c5SAndroid Build Coastguard Worker     ASSERT(allocation != VK_NULL_HANDLE);
370*8975f5c5SAndroid Build Coastguard Worker     ASSERT(offset != VK_WHOLE_SIZE);
371*8975f5c5SAndroid Build Coastguard Worker     mBufferBlock = block;
372*8975f5c5SAndroid Build Coastguard Worker     mAllocation  = allocation;
373*8975f5c5SAndroid Build Coastguard Worker     mOffset      = offset;
374*8975f5c5SAndroid Build Coastguard Worker     mSize        = size;
375*8975f5c5SAndroid Build Coastguard Worker }
376*8975f5c5SAndroid Build Coastguard Worker 
initWithEntireBuffer(Context * context,Buffer & buffer,MemoryAllocationType memoryAllocationType,uint32_t memoryTypeIndex,DeviceMemory & deviceMemory,VkMemoryPropertyFlags memoryPropertyFlags,VkDeviceSize size,VkDeviceSize allocatedBufferSize)377*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::initWithEntireBuffer(
378*8975f5c5SAndroid Build Coastguard Worker     Context *context,
379*8975f5c5SAndroid Build Coastguard Worker     Buffer &buffer,
380*8975f5c5SAndroid Build Coastguard Worker     MemoryAllocationType memoryAllocationType,
381*8975f5c5SAndroid Build Coastguard Worker     uint32_t memoryTypeIndex,
382*8975f5c5SAndroid Build Coastguard Worker     DeviceMemory &deviceMemory,
383*8975f5c5SAndroid Build Coastguard Worker     VkMemoryPropertyFlags memoryPropertyFlags,
384*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize size,
385*8975f5c5SAndroid Build Coastguard Worker     VkDeviceSize allocatedBufferSize)
386*8975f5c5SAndroid Build Coastguard Worker {
387*8975f5c5SAndroid Build Coastguard Worker     ASSERT(!valid());
388*8975f5c5SAndroid Build Coastguard Worker 
389*8975f5c5SAndroid Build Coastguard Worker     std::unique_ptr<BufferBlock> block = std::make_unique<BufferBlock>();
390*8975f5c5SAndroid Build Coastguard Worker     block->initWithoutVirtualBlock(context, buffer, memoryAllocationType, memoryTypeIndex,
391*8975f5c5SAndroid Build Coastguard Worker                                    deviceMemory, memoryPropertyFlags, size, allocatedBufferSize);
392*8975f5c5SAndroid Build Coastguard Worker 
393*8975f5c5SAndroid Build Coastguard Worker     mBufferBlock = block.release();
394*8975f5c5SAndroid Build Coastguard Worker     mAllocation  = VK_NULL_HANDLE;
395*8975f5c5SAndroid Build Coastguard Worker     mOffset      = 0;
396*8975f5c5SAndroid Build Coastguard Worker     mSize        = mBufferBlock->getMemorySize();
397*8975f5c5SAndroid Build Coastguard Worker }
398*8975f5c5SAndroid Build Coastguard Worker 
getBuffer()399*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE const Buffer &BufferSuballocation::getBuffer() const
400*8975f5c5SAndroid Build Coastguard Worker {
401*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getBuffer();
402*8975f5c5SAndroid Build Coastguard Worker }
403*8975f5c5SAndroid Build Coastguard Worker 
getSize()404*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkDeviceSize BufferSuballocation::getSize() const
405*8975f5c5SAndroid Build Coastguard Worker {
406*8975f5c5SAndroid Build Coastguard Worker     return mSize;
407*8975f5c5SAndroid Build Coastguard Worker }
408*8975f5c5SAndroid Build Coastguard Worker 
getDeviceMemory()409*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE const DeviceMemory &BufferSuballocation::getDeviceMemory() const
410*8975f5c5SAndroid Build Coastguard Worker {
411*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getDeviceMemory();
412*8975f5c5SAndroid Build Coastguard Worker }
413*8975f5c5SAndroid Build Coastguard Worker 
getMemoryPropertyFlags()414*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkMemoryMapFlags BufferSuballocation::getMemoryPropertyFlags() const
415*8975f5c5SAndroid Build Coastguard Worker {
416*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getMemoryPropertyFlags();
417*8975f5c5SAndroid Build Coastguard Worker }
418*8975f5c5SAndroid Build Coastguard Worker 
isHostVisible()419*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferSuballocation::isHostVisible() const
420*8975f5c5SAndroid Build Coastguard Worker {
421*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->isHostVisible();
422*8975f5c5SAndroid Build Coastguard Worker }
isCoherent()423*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferSuballocation::isCoherent() const
424*8975f5c5SAndroid Build Coastguard Worker {
425*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->isCoherent();
426*8975f5c5SAndroid Build Coastguard Worker }
isCached()427*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferSuballocation::isCached() const
428*8975f5c5SAndroid Build Coastguard Worker {
429*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->isCached();
430*8975f5c5SAndroid Build Coastguard Worker }
isMapped()431*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool BufferSuballocation::isMapped() const
432*8975f5c5SAndroid Build Coastguard Worker {
433*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->isMapped();
434*8975f5c5SAndroid Build Coastguard Worker }
getMappedMemory()435*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE uint8_t *BufferSuballocation::getMappedMemory() const
436*8975f5c5SAndroid Build Coastguard Worker {
437*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getMappedMemory() + getOffset();
438*8975f5c5SAndroid Build Coastguard Worker }
439*8975f5c5SAndroid Build Coastguard Worker 
flush(const VkDevice & device)440*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::flush(const VkDevice &device)
441*8975f5c5SAndroid Build Coastguard Worker {
442*8975f5c5SAndroid Build Coastguard Worker     if (!isCoherent())
443*8975f5c5SAndroid Build Coastguard Worker     {
444*8975f5c5SAndroid Build Coastguard Worker         VkMappedMemoryRange mappedRange = {};
445*8975f5c5SAndroid Build Coastguard Worker         mappedRange.sType               = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
446*8975f5c5SAndroid Build Coastguard Worker         mappedRange.memory              = mBufferBlock->getDeviceMemory().getHandle();
447*8975f5c5SAndroid Build Coastguard Worker         mappedRange.offset              = getOffset();
448*8975f5c5SAndroid Build Coastguard Worker         mappedRange.size                = mSize;
449*8975f5c5SAndroid Build Coastguard Worker         mBufferBlock->getDeviceMemory().flush(device, mappedRange);
450*8975f5c5SAndroid Build Coastguard Worker     }
451*8975f5c5SAndroid Build Coastguard Worker }
452*8975f5c5SAndroid Build Coastguard Worker 
invalidate(const VkDevice & device)453*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::invalidate(const VkDevice &device)
454*8975f5c5SAndroid Build Coastguard Worker {
455*8975f5c5SAndroid Build Coastguard Worker     if (!isCoherent())
456*8975f5c5SAndroid Build Coastguard Worker     {
457*8975f5c5SAndroid Build Coastguard Worker         VkMappedMemoryRange mappedRange = {};
458*8975f5c5SAndroid Build Coastguard Worker         mappedRange.sType               = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
459*8975f5c5SAndroid Build Coastguard Worker         mappedRange.memory              = mBufferBlock->getDeviceMemory().getHandle();
460*8975f5c5SAndroid Build Coastguard Worker         mappedRange.offset              = getOffset();
461*8975f5c5SAndroid Build Coastguard Worker         mappedRange.size                = mSize;
462*8975f5c5SAndroid Build Coastguard Worker         mBufferBlock->getDeviceMemory().invalidate(device, mappedRange);
463*8975f5c5SAndroid Build Coastguard Worker     }
464*8975f5c5SAndroid Build Coastguard Worker }
465*8975f5c5SAndroid Build Coastguard Worker 
getOffset()466*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkDeviceSize BufferSuballocation::getOffset() const
467*8975f5c5SAndroid Build Coastguard Worker {
468*8975f5c5SAndroid Build Coastguard Worker     return mOffset;
469*8975f5c5SAndroid Build Coastguard Worker }
470*8975f5c5SAndroid Build Coastguard Worker 
setOffsetAndSize(VkDeviceSize offset,VkDeviceSize size)471*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE void BufferSuballocation::setOffsetAndSize(VkDeviceSize offset, VkDeviceSize size)
472*8975f5c5SAndroid Build Coastguard Worker {
473*8975f5c5SAndroid Build Coastguard Worker     mOffset = offset;
474*8975f5c5SAndroid Build Coastguard Worker     mSize   = size;
475*8975f5c5SAndroid Build Coastguard Worker }
476*8975f5c5SAndroid Build Coastguard Worker 
getBlockMemory()477*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE uint8_t *BufferSuballocation::getBlockMemory() const
478*8975f5c5SAndroid Build Coastguard Worker {
479*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getMappedMemory();
480*8975f5c5SAndroid Build Coastguard Worker }
getBlockMemorySize()481*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE VkDeviceSize BufferSuballocation::getBlockMemorySize() const
482*8975f5c5SAndroid Build Coastguard Worker {
483*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getMemorySize();
484*8975f5c5SAndroid Build Coastguard Worker }
getBlockSerial()485*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE BufferSerial BufferSuballocation::getBlockSerial() const
486*8975f5c5SAndroid Build Coastguard Worker {
487*8975f5c5SAndroid Build Coastguard Worker     ASSERT(valid());
488*8975f5c5SAndroid Build Coastguard Worker     return mBufferBlock->getBufferSerial();
489*8975f5c5SAndroid Build Coastguard Worker }
490*8975f5c5SAndroid Build Coastguard Worker }  // namespace vk
491*8975f5c5SAndroid Build Coastguard Worker }  // namespace rx
492*8975f5c5SAndroid Build Coastguard Worker 
493*8975f5c5SAndroid Build Coastguard Worker #endif  // LIBANGLE_RENDERER_VULKAN_SUBALLOCATION_H_
494