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_NON_DISPATCHABLE_HANDLE(VmaVirtualAllocation)
19 VK_DEFINE_HANDLE(VmaVirtualBlock)
20
21 namespace vma
22 {
23 typedef VkFlags VirtualBlockCreateFlags;
24 typedef enum VirtualBlockCreateFlagBits
25 {
26 GENERAL = 0x0000000,
27 LINEAR = 0x00000001,
28 BUDDY = 0x00000002
29 } VirtualBlockCreateFlagBits;
30
31 typedef struct StatInfo
32 {
33 // Number of VkDeviceMemory Vulkan memory blocks allocated.
34 uint32_t blockCount;
35 // Number of VmaAllocation allocation objects allocated.
36 uint32_t allocationCount;
37 // Number of free ranges of memory between allocations.
38 uint32_t unusedRangeCount;
39 // Total number of bytes occupied by all allocations.
40 VkDeviceSize usedBytes;
41 // Total number of bytes occupied by unused ranges.
42 VkDeviceSize unusedBytes;
43 VkDeviceSize allocationSizeMin, allocationSizeAvg, allocationSizeMax;
44 VkDeviceSize unusedRangeSizeMin, unusedRangeSizeAvg, unusedRangeSizeMax;
45 } StatInfo;
46
47 VkResult InitAllocator(VkPhysicalDevice physicalDevice,
48 VkDevice device,
49 VkInstance instance,
50 uint32_t apiVersion,
51 VkDeviceSize preferredLargeHeapBlockSize,
52 VmaAllocator *pAllocator);
53
54 void DestroyAllocator(VmaAllocator allocator);
55
56 VkResult CreatePool(VmaAllocator allocator,
57 uint32_t memoryTypeIndex,
58 #if ANGLE_VMA_VERSION < 3000000
59 bool buddyAlgorithm,
60 #endif // ANGLE_VMA_VERSION < 3000000
61 VkDeviceSize blockSize,
62 VmaPool *pPool);
63 void DestroyPool(VmaAllocator allocator, VmaPool pool);
64
65 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation);
66
67 VkResult CreateBuffer(VmaAllocator allocator,
68 const VkBufferCreateInfo *pBufferCreateInfo,
69 VkMemoryPropertyFlags requiredFlags,
70 VkMemoryPropertyFlags preferredFlags,
71 bool persistentlyMapped,
72 uint32_t *pMemoryTypeIndexOut,
73 VkBuffer *pBuffer,
74 VmaAllocation *pAllocation);
75
76 VkResult AllocateAndBindMemoryForImage(VmaAllocator allocator,
77 VkImage *pImage,
78 VkMemoryPropertyFlags requiredFlags,
79 VkMemoryPropertyFlags preferredFlags,
80 bool allocateDedicatedMemory,
81 VmaAllocation *pAllocationOut,
82 uint32_t *pMemoryTypeIndexOut,
83 VkDeviceSize *sizeOut);
84
85 VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
86 const VkBufferCreateInfo *pBufferCreateInfo,
87 VkMemoryPropertyFlags requiredFlags,
88 VkMemoryPropertyFlags preferredFlags,
89 bool persistentlyMappedBuffers,
90 uint32_t *pMemoryTypeIndexOut);
91
92 VkResult FindMemoryTypeIndexForImageInfo(VmaAllocator allocator,
93 const VkImageCreateInfo *pImageCreateInfo,
94 VkMemoryPropertyFlags requiredFlags,
95 VkMemoryPropertyFlags preferredFlags,
96 bool allocateDedicatedMemory,
97 uint32_t *pMemoryTypeIndexOut);
98
99 void GetMemoryTypeProperties(VmaAllocator allocator,
100 uint32_t memoryTypeIndex,
101 VkMemoryPropertyFlags *pFlags);
102
103 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData);
104
105 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation);
106
107 void FlushAllocation(VmaAllocator allocator,
108 VmaAllocation allocation,
109 VkDeviceSize offset,
110 VkDeviceSize size);
111
112 void InvalidateAllocation(VmaAllocator allocator,
113 VmaAllocation allocation,
114 VkDeviceSize offset,
115 VkDeviceSize size);
116
117 void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap);
118 void FreeStatsString(VmaAllocator allocator, char *statsString);
119
120 // VMA virtual block
121 VkResult CreateVirtualBlock(VkDeviceSize size,
122 VirtualBlockCreateFlags flags,
123 VmaVirtualBlock *pVirtualBlock);
124 void DestroyVirtualBlock(VmaVirtualBlock virtualBlock);
125 VkResult VirtualAllocate(VmaVirtualBlock virtualBlock,
126 VkDeviceSize size,
127 VkDeviceSize alignment,
128 VmaVirtualAllocation *pAllocation,
129 VkDeviceSize *pOffset);
130 void VirtualFree(VmaVirtualBlock virtualBlock,
131 VmaVirtualAllocation allocation,
132 VkDeviceSize offset);
133 VkBool32 IsVirtualBlockEmpty(VmaVirtualBlock virtualBlock);
134 void GetVirtualAllocationInfo(VmaVirtualBlock virtualBlock,
135 VmaVirtualAllocation allocation,
136 VkDeviceSize offset,
137 VkDeviceSize *sizeOut,
138 void **pUserDataOut);
139 void ClearVirtualBlock(VmaVirtualBlock virtualBlock);
140 void SetVirtualAllocationUserData(VmaVirtualBlock virtualBlock,
141 VkDeviceSize offset,
142 void *pUserData);
143 void CalculateVirtualBlockStats(VmaVirtualBlock virtualBlock, StatInfo *pStatInfo);
144 void BuildVirtualBlockStatsString(VmaVirtualBlock virtualBlock,
145 char **ppStatsString,
146 VkBool32 detailedMap);
147 void FreeVirtualBlockStatsString(VmaVirtualBlock virtualBlock, char *pStatsString);
148 } // namespace vma
149
150 #endif // LIBANGLE_RENDERER_VULKAN_VK_MEM_ALLOC_WRAPPER_H_
151