• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // vma_allocator_wrapper.h:
7 //    Hides VMA functions so we can use separate warning sets.
8 //
9 
10 #ifndef LIBANGLE_RENDERER_VULKAN_VK_MEM_ALLOC_WRAPPER_H_
11 #define LIBANGLE_RENDERER_VULKAN_VK_MEM_ALLOC_WRAPPER_H_
12 
13 #include "common/vulkan/vk_headers.h"
14 
15 VK_DEFINE_HANDLE(VmaAllocator)
VK_DEFINE_HANDLE(VmaAllocation)16 VK_DEFINE_HANDLE(VmaAllocation)
17 VK_DEFINE_HANDLE(VmaPool)
18 VK_DEFINE_HANDLE(VmaVirtualBlock)
19 
20 namespace vma
21 {
22 typedef VkFlags VirtualBlockCreateFlags;
23 typedef enum VirtualBlockCreateFlagBits
24 {
25     GENERAL = 0x0000000,
26     LINEAR  = 0x00000001,
27     BUDDY   = 0x00000002
28 } VirtualBlockCreateFlagBits;
29 
30 typedef struct StatInfo
31 {
32     // Number of VkDeviceMemory Vulkan memory blocks allocated.
33     uint32_t blockCount;
34     // Number of VmaAllocation allocation objects allocated.
35     uint32_t allocationCount;
36     // Number of free ranges of memory between allocations.
37     uint32_t unusedRangeCount;
38     // Total number of bytes occupied by all allocations.
39     VkDeviceSize usedBytes;
40     // Total number of bytes occupied by unused ranges.
41     VkDeviceSize unusedBytes;
42     VkDeviceSize allocationSizeMin, allocationSizeAvg, allocationSizeMax;
43     VkDeviceSize unusedRangeSizeMin, unusedRangeSizeAvg, unusedRangeSizeMax;
44 } StatInfo;
45 
46 VkResult InitAllocator(VkPhysicalDevice physicalDevice,
47                        VkDevice device,
48                        VkInstance instance,
49                        uint32_t apiVersion,
50                        VkDeviceSize preferredLargeHeapBlockSize,
51                        VmaAllocator *pAllocator);
52 
53 void DestroyAllocator(VmaAllocator allocator);
54 
55 VkResult CreatePool(VmaAllocator allocator,
56                     uint32_t memoryTypeIndex,
57                     bool buddyAlgorithm,
58                     VkDeviceSize blockSize,
59                     VmaPool *pPool);
60 void DestroyPool(VmaAllocator allocator, VmaPool pool);
61 
62 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation);
63 
64 VkResult CreateBuffer(VmaAllocator allocator,
65                       const VkBufferCreateInfo *pBufferCreateInfo,
66                       VkMemoryPropertyFlags requiredFlags,
67                       VkMemoryPropertyFlags preferredFlags,
68                       bool persistentlyMappedBuffers,
69                       uint32_t *pMemoryTypeIndexOut,
70                       VkBuffer *pBuffer,
71                       VmaAllocation *pAllocation);
72 
73 VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
74                                           const VkBufferCreateInfo *pBufferCreateInfo,
75                                           VkMemoryPropertyFlags requiredFlags,
76                                           VkMemoryPropertyFlags preferredFlags,
77                                           bool persistentlyMappedBuffers,
78                                           uint32_t *pMemoryTypeIndexOut);
79 
80 void GetMemoryTypeProperties(VmaAllocator allocator,
81                              uint32_t memoryTypeIndex,
82                              VkMemoryPropertyFlags *pFlags);
83 
84 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData);
85 
86 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation);
87 
88 void FlushAllocation(VmaAllocator allocator,
89                      VmaAllocation allocation,
90                      VkDeviceSize offset,
91                      VkDeviceSize size);
92 
93 void InvalidateAllocation(VmaAllocator allocator,
94                           VmaAllocation allocation,
95                           VkDeviceSize offset,
96                           VkDeviceSize size);
97 
98 void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap);
99 void FreeStatsString(VmaAllocator allocator, char *statsString);
100 
101 // VMA virtual block
102 VkResult CreateVirtualBlock(VkDeviceSize size,
103                             VirtualBlockCreateFlags flags,
104                             VmaVirtualBlock *pVirtualBlock);
105 void DestroyVirtualBlock(VmaVirtualBlock virtualBlock);
106 VkResult VirtualAllocate(VmaVirtualBlock virtualBlock,
107                          VkDeviceSize size,
108                          VkDeviceSize alignment,
109                          VkDeviceSize *pOffset);
110 void VirtualFree(VmaVirtualBlock virtualBlock, VkDeviceSize offset);
111 VkBool32 IsVirtualBlockEmpty(VmaVirtualBlock virtualBlock);
112 void GetVirtualAllocationInfo(VmaVirtualBlock virtualBlock,
113                               VkDeviceSize offset,
114                               VkDeviceSize *sizeOut,
115                               void **pUserDataOut);
116 void ClearVirtualBlock(VmaVirtualBlock virtualBlock);
117 void SetVirtualAllocationUserData(VmaVirtualBlock virtualBlock,
118                                   VkDeviceSize offset,
119                                   void *pUserData);
120 void CalculateVirtualBlockStats(VmaVirtualBlock virtualBlock, StatInfo *pStatInfo);
121 void BuildVirtualBlockStatsString(VmaVirtualBlock virtualBlock,
122                                   char **ppStatsString,
123                                   VkBool32 detailedMap);
124 void FreeVirtualBlockStatsString(VmaVirtualBlock virtualBlock, char *pStatsString);
125 }  // namespace vma
126 
127 #endif  // LIBANGLE_RENDERER_VULKAN_VK_MEM_ALLOC_WRAPPER_H_
128