Diligent Engine  v.2.4.g
VulkanDynamicHeap.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include <mutex>
35 #include "DynamicHeap.hpp"
36 
37 namespace Diligent
38 {
39 
40 // Vulkand dynamic heap implementation consists of a number of dynamic heaps, one per context.
41 // Every dynamic heap suballocates chunk of memory from the global memory manager. Within
42 // every chunk, memory is allocated in a simple lock-free linear fashion. All used allocations are discarded
43 // when FinishFrame() is called
44 
45 class RenderDeviceVkImpl;
46 class VulkanRingBuffer;
47 class VulkanDynamicMemoryManager;
48 
49 // sizeof(VulkanDynamicAllocation) must be at least 16 to avoid false cache line sharing problems
51 {
53 
54  // clang-format off
55  VulkanDynamicAllocation(VulkanDynamicMemoryManager& _DynamicMemMgr, size_t _AlignedOffset, size_t _Size) noexcept :
56  pDynamicMemMgr{&_DynamicMemMgr},
57  AlignedOffset {_AlignedOffset },
58  Size {_Size }
59  {}
60 
63 
65  pDynamicMemMgr{rhs.pDynamicMemMgr},
66  AlignedOffset {rhs.AlignedOffset },
67  Size {rhs.Size }
68 #ifdef DILIGENT_DEVELOPMENT
69  , dvpFrameNumber{rhs.dvpFrameNumber}
70 #endif
71  {
72  rhs.pDynamicMemMgr = nullptr;
73  rhs.AlignedOffset = 0;
74  rhs.Size = 0;
75 #ifdef DILIGENT_DEVELOPMENT
76  rhs.dvpFrameNumber = 0;
77 #endif
78  }
79  // clang-format on
80 
81  VulkanDynamicAllocation& operator=(VulkanDynamicAllocation&& rhs) noexcept // Must be noexcept on MSVC, so can't use = default
82  {
83  pDynamicMemMgr = rhs.pDynamicMemMgr;
84  AlignedOffset = rhs.AlignedOffset;
85  Size = rhs.Size;
86  rhs.pDynamicMemMgr = nullptr;
87  rhs.AlignedOffset = 0;
88  rhs.Size = 0;
89 #ifdef DILIGENT_DEVELOPMENT
90  dvpFrameNumber = rhs.dvpFrameNumber;
91  rhs.dvpFrameNumber = 0;
92 #endif
93  return *this;
94  }
95 
97  size_t AlignedOffset = 0; // Offset from the start of the buffer
98  size_t Size = 0; // Reserved size of this allocation
99 #ifdef DILIGENT_DEVELOPMENT
100  Uint64 dvpFrameNumber = 0;
101 #endif
102 };
103 
104 
105 // VulkanDynamicMemoryManager manages allocation of master blocks from global dynamic buffer
106 //
107 // _______________________________________________________________________
108 // | |
109 // | VulkanDynamicMemoryManager |
110 // | |
111 // | || - - - - - - - - - - Dynamic Memory Buffer- - - - - - - - - -|| |
112 // | || MasterBlock[0] | MasterBlock[1] | ... | MasterBlock[N-1] || |
113 // |_______________________________________________________________________|
114 //
115 // We cannot use global memory manager for dynamic resources because they
116 // need to use the same Vulkan buffer
118 {
119 public:
123 
125  class RenderDeviceVkImpl& DeviceVk,
126  Uint32 Size,
127  Uint64 CommandQueueMask);
129 
130  // clang-format off
135 
136  VkBuffer GetVkBuffer() const{return m_VkBuffer;}
137  Uint8* GetCPUAddress()const{return m_CPUAddress;}
138  // clang-format on
139 
140  void Destroy();
141 
142  static constexpr const Uint32 MasterBlockAlignment = 1024;
143  MasterBlock AllocateMasterBlock(OffsetType SizeInBytes, OffsetType Alignment);
144 
145 private:
146  RenderDeviceVkImpl& m_DeviceVk;
149  Uint8* m_CPUAddress;
150  const VkDeviceSize m_DefaultAlignment;
151  const Uint64 m_CommandQueueMask;
152  OffsetType m_TotalPeakSize = 0;
153 };
154 
155 
156 
157 // Dynamic heap is used by a device context to allocate dynamic space when
158 // mapping a buffer or a texture. This is very similar to upload heap,
159 // however dynamic heap uses special persistently mapped buffer while
160 // upload heap uses global memory manager.
161 //
162 // The heap allocates master blocks from the global dynamic memory manager.
163 // The pages are released and returned to the manager at the end of every frame.
164 //
165 // _______________________________________________________________________________________________________________________________
166 // | |
167 // | VulkanDynamicHeap |
168 // | |
169 // | || - - - - - - - - - MasterBlock[0]- - - - - - - - -|| || - - - - - - - - - MasterBlock[1]- - - - - - - - -|| |
170 // | || Allocation0 | Allocation1 | ... | AllocationN || || Allocation0 | Allocation1 | ... | AllocationM || ... |
171 // |__________|____________________________________________________________________________________________________________________|
172 // | A |
173 // | | |
174 // |Allocate() AllocateMasterBlock()| |FinishFrame()
175 // | ______|___________________V____
176 // V | |
177 // | VulkanDynamicMemoryManager |
178 // | |
179 // | |Global dynamic buffer| |
180 // |______________________________|
181 //
183 {
184 public:
185  // clang-format off
186  VulkanDynamicHeap(VulkanDynamicMemoryManager& DynamicMemMgr, std::string HeapName, Uint32 PageSize) :
187  m_GlobalDynamicMemMgr{DynamicMemMgr},
188  m_HeapName {std::move(HeapName)},
189  m_MasterBlockSize (PageSize)
190  {}
191 
192  VulkanDynamicHeap (const VulkanDynamicHeap&) = delete;
196  // clang-format on
197 
199 
200  VulkanDynamicAllocation Allocate(Uint32 SizeInBytes, Uint32 Alignment);
201 
202  // Releases all master blocks that are later returned to the global dynamic memory manager.
203  // CmdQueueMask indicates which command queues the allocations from this heap were used
204  // with during the last frame.
205  // As global dynamic memory manager is hosted by the render device, the dynamic heap can
206  // be destroyed before the blocks are actually returned to the global dynamic memory manager.
207  void ReleaseMasterBlocks(RenderDeviceVkImpl& DeviceVkImpl, Uint64 CmdQueueMask);
208 
211 
212  static constexpr OffsetType InvalidOffset = static_cast<OffsetType>(-1);
213 
214  size_t GetAllocatedMasterBlockCount() const { return m_MasterBlocks.size(); }
215 
216 private:
217  VulkanDynamicMemoryManager& m_GlobalDynamicMemMgr;
218  const std::string m_HeapName;
219 
220  std::vector<MasterBlock> m_MasterBlocks;
221 
222  OffsetType m_CurrOffset = InvalidOffset;
223  const Uint32 m_MasterBlockSize;
224  Uint32 m_AvailableSize = 0;
225 
226  Uint32 m_CurrAlignedSize = 0;
227  Uint32 m_CurrUsedSize = 0;
228  Uint32 m_PeakAlignedSize = 0;
229  Uint32 m_PeakUsedSize = 0;
230  Uint32 m_CurrAllocatedSize = 0;
231  Uint32 m_PeakAllocatedSize = 0;
232 };
233 
234 } // namespace Diligent
VulkanHeaders.h
Diligent::VulkanDynamicAllocation
Definition: VulkanDynamicHeap.hpp:50
VulkanLogicalDevice.hpp
Diligent::VulkanDynamicAllocation::operator=
VulkanDynamicAllocation & operator=(VulkanDynamicAllocation &&rhs) noexcept
Definition: VulkanDynamicHeap.hpp:81
Diligent::RenderDeviceVkImpl
Render device implementation in Vulkan backend.
Definition: RenderDeviceVkImpl.hpp:58
Diligent::VulkanDynamicHeap::Allocate
VulkanDynamicAllocation Allocate(Uint32 SizeInBytes, Uint32 Alignment)
Definition: VulkanDynamicHeap.cpp:224
Diligent::VulkanDynamicAllocation::operator=
VulkanDynamicAllocation & operator=(const VulkanDynamicAllocation &)=delete
Diligent::VulkanDynamicMemoryManager::GetVkBuffer
VkBuffer GetVkBuffer() const
Definition: VulkanDynamicHeap.hpp:136
Diligent::VulkanDynamicAllocation::VulkanDynamicAllocation
VulkanDynamicAllocation() noexcept
Definition: VulkanDynamicHeap.hpp:52
Diligent::Uint64
uint64_t Uint64
64-bit unsigned integer
Definition: BasicTypes.h:50
Diligent::VulkanDynamicMemoryManager::VulkanDynamicMemoryManager
VulkanDynamicMemoryManager(IMemoryAllocator &Allocator, class RenderDeviceVkImpl &DeviceVk, Uint32 Size, Uint64 CommandQueueMask)
Definition: VulkanDynamicHeap.cpp:47
Diligent::VariableSizeAllocationsManager::Allocation
Definition: VariableSizeAllocationsManager.hpp:156
VulkanUtilities::DeviceMemoryWrapper
DEFINE_VULKAN_OBJECT_WRAPPER(DeviceMemory) DeviceMemoryWrapper
Definition: VulkanLogicalDevice.hpp:73
Diligent::VulkanDynamicHeap::VulkanDynamicHeap
VulkanDynamicHeap(VulkanDynamicMemoryManager &DynamicMemMgr, std::string HeapName, Uint32 PageSize)
Definition: VulkanDynamicHeap.hpp:186
Diligent::VulkanDynamicAllocation::pDynamicMemMgr
VulkanDynamicMemoryManager * pDynamicMemMgr
Definition: VulkanDynamicHeap.hpp:96
Diligent::VulkanDynamicAllocation::VulkanDynamicAllocation
VulkanDynamicAllocation(VulkanDynamicMemoryManager &_DynamicMemMgr, size_t _AlignedOffset, size_t _Size) noexcept
Definition: VulkanDynamicHeap.hpp:55
Diligent::VulkanDynamicAllocation::VulkanDynamicAllocation
VulkanDynamicAllocation(VulkanDynamicAllocation &&rhs) noexcept
Definition: VulkanDynamicHeap.hpp:64
Diligent::VulkanDynamicMemoryManager::MasterBlockAlignment
static constexpr const Uint32 MasterBlockAlignment
Definition: VulkanDynamicHeap.hpp:142
VulkanUtilities::BufferWrapper
DEFINE_VULKAN_OBJECT_WRAPPER(Buffer) BufferWrapper
Definition: VulkanLogicalDevice.hpp:69
Diligent::VulkanDynamicMemoryManager::MasterBlock
TBase::MasterBlock MasterBlock
Definition: VulkanDynamicHeap.hpp:122
Diligent::VulkanDynamicMemoryManager::OffsetType
TBase::OffsetType OffsetType
Definition: VulkanDynamicHeap.hpp:121
Diligent::VulkanDynamicMemoryManager::AllocateMasterBlock
MasterBlock AllocateMasterBlock(OffsetType SizeInBytes, OffsetType Alignment)
Definition: VulkanDynamicHeap.cpp:142
Diligent::VulkanDynamicHeap
Definition: VulkanDynamicHeap.hpp:182
DynamicHeap.hpp
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::VulkanDynamicMemoryManager::Destroy
void Destroy()
Definition: VulkanDynamicHeap.cpp:118
Diligent::VulkanDynamicAllocation::Size
size_t Size
Definition: VulkanDynamicHeap.hpp:98
Diligent::VulkanDynamicHeap::~VulkanDynamicHeap
~VulkanDynamicHeap()
Definition: VulkanDynamicHeap.cpp:301
Diligent::VulkanDynamicHeap::GetAllocatedMasterBlockCount
size_t GetAllocatedMasterBlockCount() const
Definition: VulkanDynamicHeap.hpp:214
Diligent::VulkanDynamicHeap::operator=
VulkanDynamicHeap & operator=(const VulkanDynamicHeap &)=delete
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
Diligent::VulkanDynamicHeap::InvalidOffset
static constexpr OffsetType InvalidOffset
Definition: VulkanDynamicHeap.hpp:212
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
Diligent::VulkanDynamicMemoryManager::~VulkanDynamicMemoryManager
~VulkanDynamicMemoryManager()
Definition: VulkanDynamicHeap.cpp:129
Diligent::VulkanDynamicHeap::OffsetType
VulkanDynamicMemoryManager::OffsetType OffsetType
Definition: VulkanDynamicHeap.hpp:209
VulkanObjectWrappers.hpp
Diligent::DynamicHeap::MasterBlockListBasedManager
Definition: DynamicHeap.hpp:98
Diligent::VulkanDynamicMemoryManager
Definition: VulkanDynamicHeap.hpp:117
Diligent::VulkanDynamicMemoryManager::GetCPUAddress
Uint8 * GetCPUAddress() const
Definition: VulkanDynamicHeap.hpp:137
Diligent::VulkanDynamicAllocation::AlignedOffset
size_t AlignedOffset
Definition: VulkanDynamicHeap.hpp:97
Diligent::VulkanDynamicHeap::ReleaseMasterBlocks
void ReleaseMasterBlocks(RenderDeviceVkImpl &DeviceVkImpl, Uint64 CmdQueueMask)
Definition: VulkanDynamicHeap.cpp:288
Diligent::DynamicHeap::MasterBlockListBasedManager::MasterBlock
VariableSizeAllocationsManager::Allocation MasterBlock
Definition: DynamicHeap.hpp:102
Diligent::VulkanDynamicMemoryManager::operator=
VulkanDynamicMemoryManager & operator=(const VulkanDynamicMemoryManager &)=delete
VulkanMemoryManager.hpp
Diligent::DynamicHeap::MasterBlockListBasedManager::OffsetType
VariableSizeAllocationsManager::OffsetType OffsetType
Definition: DynamicHeap.hpp:101
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37