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 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation);
63
64 VkResult CreateBuffer(VmaAllocator allocator,
65 const VkBufferCreateInfo *pBufferCreateInfo,
66 VkMemoryPropertyFlags requiredFlags,
67 VkMemoryPropertyFlags preferredFlags,
68 bool persistentlyMapped,
69 uint32_t *pMemoryTypeIndexOut,
70 VkBuffer *pBuffer,
71 VmaAllocation *pAllocation);
72
73 VkResult AllocateAndBindMemoryForImage(VmaAllocator allocator,
74 VkImage *pImage,
75 VkMemoryPropertyFlags requiredFlags,
76 VkMemoryPropertyFlags preferredFlags,
77 uint32_t memoryTypeBits,
78 bool allocateDedicatedMemory,
79 VmaAllocation *pAllocationOut,
80 uint32_t *pMemoryTypeIndexOut,
81 VkDeviceSize *sizeOut);
82
83 VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
84 const VkBufferCreateInfo *pBufferCreateInfo,
85 VkMemoryPropertyFlags requiredFlags,
86 VkMemoryPropertyFlags preferredFlags,
87 bool persistentlyMappedBuffers,
88 uint32_t *pMemoryTypeIndexOut);
89
90 VkResult FindMemoryTypeIndexForImageInfo(VmaAllocator allocator,
91 const VkImageCreateInfo *pImageCreateInfo,
92 VkMemoryPropertyFlags requiredFlags,
93 VkMemoryPropertyFlags preferredFlags,
94 bool allocateDedicatedMemory,
95 uint32_t *pMemoryTypeIndexOut);
96
97 void GetMemoryTypeProperties(VmaAllocator allocator,
98 uint32_t memoryTypeIndex,
99 VkMemoryPropertyFlags *pFlags);
100
101 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData);
102
103 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation);
104
105 void FlushAllocation(VmaAllocator allocator,
106 VmaAllocation allocation,
107 VkDeviceSize offset,
108 VkDeviceSize size);
109
110 void InvalidateAllocation(VmaAllocator allocator,
111 VmaAllocation allocation,
112 VkDeviceSize offset,
113 VkDeviceSize size);
114
115 void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap);
116 void FreeStatsString(VmaAllocator allocator, char *statsString);
117
118 // VMA virtual block
119 VkResult CreateVirtualBlock(VkDeviceSize size,
120 VirtualBlockCreateFlags flags,
121 VmaVirtualBlock *pVirtualBlock);
122 void DestroyVirtualBlock(VmaVirtualBlock virtualBlock);
123 VkResult VirtualAllocate(VmaVirtualBlock virtualBlock,
124 VkDeviceSize size,
125 VkDeviceSize alignment,
126 VmaVirtualAllocation *pAllocation,
127 VkDeviceSize *pOffset);
128 void VirtualFree(VmaVirtualBlock virtualBlock,
129 VmaVirtualAllocation allocation,
130 VkDeviceSize offset);
131 VkBool32 IsVirtualBlockEmpty(VmaVirtualBlock virtualBlock);
132 void GetVirtualAllocationInfo(VmaVirtualBlock virtualBlock,
133 VmaVirtualAllocation allocation,
134 VkDeviceSize offset,
135 VkDeviceSize *sizeOut,
136 void **pUserDataOut);
137 void ClearVirtualBlock(VmaVirtualBlock virtualBlock);
138 void SetVirtualAllocationUserData(VmaVirtualBlock virtualBlock,
139 VkDeviceSize offset,
140 void *pUserData);
141 void CalculateVirtualBlockStats(VmaVirtualBlock virtualBlock, StatInfo *pStatInfo);
142 void BuildVirtualBlockStatsString(VmaVirtualBlock virtualBlock,
143 char **ppStatsString,
144 VkBool32 detailedMap);
145 void FreeVirtualBlockStatsString(VmaVirtualBlock virtualBlock, char *pStatsString);
146 } // namespace vma
147
148 #endif // LIBANGLE_RENDERER_VULKAN_VK_MEM_ALLOC_WRAPPER_H_
149