HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VE_Memory.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: VE_Memory.h ( VE Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __VE_MEMORY_H__
12 #define __VE_MEMORY_H__
13 
14 #include "VE_API.h"
15 #include "VE_Result.h"
16 #include "VE_Instance.h"
17 
18 #include <vk_mem_alloc.h>
19 
20 #include <UT/UT_NonCopyable.h>
21 
23 {
29 };
30 
32  FLAG_COHERENT = 1 << 0,
33  FLAG_DEDICATED = 1 << 1,
34 };
35 
36 using VE_MemFlags = uint32_t;
37 
39 {
40 public:
41  const VmaAllocationInfo &info() const { return myInfo; }
42 
43  bool isValid() const { return myAllocation != nullptr; }
44 
45  void free(VmaAllocator allocator);
46 
47 
48 protected:
49  friend class VE_VmaMemoryAllocator;
50 
51  VmaAllocation myAllocation = VK_NULL_HANDLE;
52  VmaAllocationInfo myInfo;
53 };
54 
56 {
57 protected:
58  VE_Result<void *> map(VmaAllocator allocator);
59  void unmap(VmaAllocator allocator);
60 };
61 
63 {
64 public:
66  VkInstance,
67  VkPhysicalDevice,
68  VkDevice,
69  uint32_t req_ver,
70  VmaAllocatorCreateFlags flags);
71 
72  void destroy();
73 
74  template <typename R>
75  struct Allocation {
78  };
79 
80  template <typename R>
84  };
85 
90 
91  VE_Result<VE_VmaAllocation> allocateMemory(const VkMemoryRequirements&, const VmaAllocationCreateInfo &info);
92  VE_Result<ImageAllocation> allocateImage(const VkImageCreateInfo&, const VmaAllocationCreateInfo &info);
93  VE_Result<BufferAllocation> allocateBuffer(const VkBufferCreateInfo&, const VmaAllocationCreateInfo &info);
94 
95  VE_Result<VE_VmaMappableAllocation> allocateMappableMemory(const VkMemoryRequirements&, const VmaAllocationCreateInfo &info);
96  VE_Result<MappableBufferAllocation> allocateMappableBuffer(const VkBufferCreateInfo&, const VmaAllocationCreateInfo &info);
97  VE_Result<MappableImageAllocation> allocateMappableImage(const VkImageCreateInfo&, const VmaAllocationCreateInfo &info);
98 
99 protected:
100  VE_VoidResult init(
101  VkInstance,
102  VkPhysicalDevice,
103  VkDevice,
104  uint32_t req_ver,
105  VmaAllocatorCreateFlags flags);
106 
107  VmaAllocator myAllocator = VK_NULL_HANDLE;
108 };
109 
110 /// A block of memory as allocated by VE_MemoryAllocator. Will free itself when
111 /// it goes out of scope.
112 /// It represents a block of bytes, and should in general be used to as a
113 /// backing member in some higher level, resource-like class, such as a Buffer
114 /// or Image class.
116 {
117 public:
118  VkDeviceMemory getVkMem() const { return allocInfo().deviceMemory; }
119  VkDeviceSize getOffset() const { return allocInfo().offset; }
120  VkDeviceSize getSize() const { return allocInfo().size; }
121 
122  const VmaAllocationInfo &allocInfo() const { return info(); }
123 
124  // Copying a block of memory should be done with an explicit copy method,
125  // and that method should exist at the resource level instead of here.
127 
128  VE_MemType getType() const { return myData.type; }
129 
130  void *mapMemory();
131  void unmapMemory();
132 
133  void *mappedPointer() { return myData.mappedPtr; }
134  const void *mappedPointer() const { return myData.mappedPtr; }
135 
136  bool isCoherent() const { return myData.flags & FLAG_COHERENT; }
137  bool isDedicated() const { return myData.flags & FLAG_DEDICATED; }
138 
139  VE_Memory(VE_Memory &&other) noexcept
140  {
141  *this = std::move(other);
142  }
143 
144  VE_Memory &operator=(VE_Memory &&other) noexcept
145  {
146  std::swap(myAllocation, other.myAllocation);
147  std::swap(myInfo, other.myInfo);
148  std::swap(myAllocator, other.myAllocator);
149  std::swap(myData, other.myData);
150  return *this;
151  }
152 
153  virtual ~VE_Memory();
154 
155 private:
156  VE_Memory(
157  VmaAllocator allocator,
161  : VE_VmaMappableAllocation(block)
162  , myAllocator(allocator)
163  {
164  myData.type = type;
165  myData.flags = flags;
166  myData.mappedPtr = nullptr;
167  }
168 
169  friend class VE_MemoryAllocator;
170 
171  VmaAllocator myAllocator;
172 
173  struct
174  {
176  void *mappedPtr;
178  } myData;
179 };
180 
181 /// General purpose allocator for vulkan backed memory blocks that tries to
182 /// balance convenience, safety, and performance.
183 ///
184 /// The main convenience offered is that memory blocks allocated from this
185 /// object will free themselves when they go out of scope. The caller does not
186 /// need to explicity free them. This makes this class well suited for
187 /// allocating lots of resources that would be difficult to keep track of
188 /// manually.
189 ///
190 /// However, the memory blocks do this by maintaining references to the
191 /// underlying VmaAllocator object handle, which is owned by and cleaned up by
192 /// this class. Therefore, care must be taken to ensure this object's life time
193 /// extends beyond its allocated blocks. If the memory blocks and this allocator
194 /// are kept as members of a larger structure, ensure proper member ordering so
195 /// that the blocks are destroyed before the allocator.
196 ///
197 /// Thread safety:
198 /// This allocator contains no locks and attempts no cross thread
199 /// synchronization. If you need memory allocations in different threads, you
200 /// should give those threads their own allocator.
202 {
203 public:
205  VkInstance,
206  VkPhysicalDevice,
207  VkDevice,
208  uint32_t req_ver,
209  VmaAllocationCreateFlags flags);
211  const VE_Instance &,
212  VkPhysicalDevice,
213  VkDevice,
214  VmaAllocationCreateFlags flags);
215 
216  static VkExternalMemoryHandleTypeFlags getExternMemHandleType();
217 
219  const
220  {
221  return myMemoryProperties;
222  };
223 
224  UT_Array<VmaBudget> heapInfos() const;
225 
226  // prints memory heap info and usage statistics
227  void printMemoryInfo() const;
228  void printMemoryInfo(std::ostream* out = nullptr) const;
229 
230  // get simplified memory usage statistics
231  void getMemoryUsage(exint* out_device_vma_alloc_size,
232  exint* out_device_vk_alloc_size,
233  exint* out_device_total_size,
234  exint* out_shared_vma_alloc_size,
235  exint* out_shared_vk_alloc_size,
236  exint* out_shared_total_size) const;
237 
240  const VkImageCreateInfo &img_info,
241  VkImage &vk_img);
244  const VkBufferCreateInfo &buf_info,
245  VkBuffer &vk_buf);
248  const VkMemoryRequirements &info);
249 
250  VE_MemoryAllocator() = default;
252 
254 
256  {
257  *this = std::move(other);
258  }
259 
261  {
262  std::swap(myAllocator, other.myAllocator);
263  std::swap(myMemoryProperties, other.myMemoryProperties);
264  std::swap(myGLExportPool, other.myGLExportPool);
265  std::swap(myGLSharedExportPool, other.myGLSharedExportPool);
266  return *this;
267  }
268 
269 private:
270  void createGLExportPool(VkDevice);
271 
272  VE_MemFlags memoryFlags(const VmaAllocationInfo &vma_info, VE_MemType type) const;
273 
274  VkPhysicalDeviceMemoryProperties myMemoryProperties;
275  VmaPool myGLExportPool = VK_NULL_HANDLE;
276  VmaPool myGLSharedExportPool = VK_NULL_HANDLE;
277 };
278 
279 /// This is a low level function to create a raw VmaAllocator object, in case the
280 /// caller wants direct access to the Vma API.
281 /// It is recommended to use VE_MemoryAllocator unless you have good reason not
282 /// to.
283 VE_API
285  VkInstance,
286  VkPhysicalDevice,
287  VkDevice,
288  uint32_t req_ver,
289  VmaAllocatorCreateFlags flags);
290 
291 VE_API
292 void VEdestroyVmaAllocator(VmaAllocator);
293 
294 /// Returns the platform specific external memory handle type
297 
298 #endif
GLbitfield flags
Definition: glcorearb.h:1596
const VmaAllocationInfo & info() const
Definition: VE_Memory.h:41
VkDeviceMemory getVkMem() const
Definition: VE_Memory.h:118
#define VK_NULL_HANDLE
Definition: vulkan_core.h:45
VE_VmaMappableAllocation allocation
Definition: VE_Memory.h:83
VE_MemoryAllocator & operator=(VE_MemoryAllocator &&other) noexcept
Definition: VE_Memory.h:260
VkFlags VkExternalMemoryHandleTypeFlags
Definition: vulkan_core.h:4970
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1639
const void * mappedPointer() const
Definition: VE_Memory.h:134
int64 exint
Definition: SYS_Types.h:125
uint32_t VE_MemFlags
Definition: VE_Memory.h:36
VE_MemType
Definition: VE_Memory.h:22
VE_MemFlagBits
Definition: VE_Memory.h:31
bool isDedicated() const
Definition: VE_Memory.h:137
VE_Memory & operator=(VE_Memory &&other) noexcept
Definition: VE_Memory.h:144
VkDeviceSize getOffset() const
Definition: VE_Memory.h:119
void * mappedPointer()
Definition: VE_Memory.h:133
VE_Memory(VE_Memory &&other) noexcept
Definition: VE_Memory.h:139
#define VE_API
Definition: VE_API.h:20
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
VE_API VkExternalMemoryHandleTypeFlags VEgetExternalMemoryHandleType()
Returns the platform specific external memory handle type.
const VmaAllocationInfo & allocInfo() const
Definition: VE_Memory.h:122
VE_Result< ImageAllocation > allocateImage(const VkImageCreateInfo &, const VmaAllocationCreateInfo &info)
const VkPhysicalDeviceMemoryProperties & physicalDeviceMemoryProperties() const
Definition: VE_Memory.h:218
VE_MemType type
Definition: VE_Memory.h:175
VkDeviceSize getSize() const
Definition: VE_Memory.h:120
VE_API VE_Result< VmaAllocator > VEcreateVmaAllocator(VkInstance, VkPhysicalDevice, VkDevice, uint32_t req_ver, VmaAllocatorCreateFlags flags)
VE_Result< VE_VmaAllocation > allocateMemory(const VkMemoryRequirements &, const VmaAllocationCreateInfo &info)
VE_MemoryAllocator(VE_MemoryAllocator &&other) noexcept
Definition: VE_Memory.h:255
VmaAllocationInfo myInfo
Definition: VE_Memory.h:52
VE_MemFlags flags
Definition: VE_Memory.h:177
bool isValid() const
Definition: VE_Memory.h:43
VE_MemType getType() const
Definition: VE_Memory.h:128
static VE_Result< VE_VmaMemoryAllocator > create(VkInstance, VkPhysicalDevice, VkDevice, uint32_t req_ver, VmaAllocatorCreateFlags flags)
void * mappedPtr
Definition: VE_Memory.h:176
uint64_t VkDeviceSize
Definition: vulkan_core.h:95
bool isCoherent() const
Definition: VE_Memory.h:136
VE_Result< BufferAllocation > allocateBuffer(const VkBufferCreateInfo &, const VmaAllocationCreateInfo &info)
type
Definition: core.h:1059
VE_API void VEdestroyVmaAllocator(VmaAllocator)
VmaAllocator myAllocator
Definition: VE_Memory.h:107