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