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.cpp:
7 // Hides VMA functions so we can use separate warning sets.
8 //
9
10 #include "vk_mem_alloc_wrapper.h"
11
12 #include <vk_mem_alloc.h>
13
14 namespace vma
15 {
InitAllocator(VkPhysicalDevice physicalDevice,VkDevice device,VkInstance instance,uint32_t apiVersion,VkDeviceSize preferredLargeHeapBlockSize,VmaAllocator * pAllocator)16 VkResult InitAllocator(VkPhysicalDevice physicalDevice,
17 VkDevice device,
18 VkInstance instance,
19 uint32_t apiVersion,
20 VkDeviceSize preferredLargeHeapBlockSize,
21 VmaAllocator *pAllocator)
22 {
23 VmaVulkanFunctions funcs = {};
24 funcs.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties;
25 funcs.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties;
26 funcs.vkAllocateMemory = vkAllocateMemory;
27 funcs.vkFreeMemory = vkFreeMemory;
28 funcs.vkMapMemory = vkMapMemory;
29 funcs.vkUnmapMemory = vkUnmapMemory;
30 funcs.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges;
31 funcs.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges;
32 funcs.vkBindBufferMemory = vkBindBufferMemory;
33 funcs.vkBindImageMemory = vkBindImageMemory;
34 funcs.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements;
35 funcs.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements;
36 funcs.vkCreateBuffer = vkCreateBuffer;
37 funcs.vkDestroyBuffer = vkDestroyBuffer;
38 funcs.vkCreateImage = vkCreateImage;
39 funcs.vkDestroyImage = vkDestroyImage;
40 funcs.vkCmdCopyBuffer = vkCmdCopyBuffer;
41 {
42 #if !defined(ANGLE_SHARED_LIBVULKAN)
43 // When the vulkan-loader is statically linked, we need to use the extension
44 // functions defined in ANGLE's rx namespace. When it's dynamically linked
45 // with volk, this will default to the function definitions with no namespace
46 using rx::vkBindBufferMemory2KHR;
47 using rx::vkBindImageMemory2KHR;
48 using rx::vkGetBufferMemoryRequirements2KHR;
49 using rx::vkGetImageMemoryRequirements2KHR;
50 using rx::vkGetPhysicalDeviceMemoryProperties2KHR;
51 #endif // !defined(ANGLE_SHARED_LIBVULKAN)
52 funcs.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2KHR;
53 funcs.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2KHR;
54 funcs.vkBindBufferMemory2KHR = vkBindBufferMemory2KHR;
55 funcs.vkBindImageMemory2KHR = vkBindImageMemory2KHR;
56 funcs.vkGetPhysicalDeviceMemoryProperties2KHR = vkGetPhysicalDeviceMemoryProperties2KHR;
57 }
58
59 VmaAllocatorCreateInfo allocatorInfo = {};
60 allocatorInfo.physicalDevice = physicalDevice;
61 allocatorInfo.device = device;
62 allocatorInfo.instance = instance;
63 allocatorInfo.pVulkanFunctions = &funcs;
64 allocatorInfo.vulkanApiVersion = apiVersion;
65 allocatorInfo.preferredLargeHeapBlockSize = preferredLargeHeapBlockSize;
66
67 return vmaCreateAllocator(&allocatorInfo, pAllocator);
68 }
69
DestroyAllocator(VmaAllocator allocator)70 void DestroyAllocator(VmaAllocator allocator)
71 {
72 vmaDestroyAllocator(allocator);
73 }
74
FreeMemory(VmaAllocator allocator,VmaAllocation allocation)75 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation)
76 {
77 vmaFreeMemory(allocator, allocation);
78 }
79
CreateBuffer(VmaAllocator allocator,const VkBufferCreateInfo * pBufferCreateInfo,VkMemoryPropertyFlags requiredFlags,VkMemoryPropertyFlags preferredFlags,bool persistentlyMappedBuffers,uint32_t * pMemoryTypeIndexOut,VkBuffer * pBuffer,VmaAllocation * pAllocation)80 VkResult CreateBuffer(VmaAllocator allocator,
81 const VkBufferCreateInfo *pBufferCreateInfo,
82 VkMemoryPropertyFlags requiredFlags,
83 VkMemoryPropertyFlags preferredFlags,
84 bool persistentlyMappedBuffers,
85 uint32_t *pMemoryTypeIndexOut,
86 VkBuffer *pBuffer,
87 VmaAllocation *pAllocation)
88 {
89 VkResult result;
90 VmaAllocationCreateInfo allocationCreateInfo = {};
91 allocationCreateInfo.requiredFlags = requiredFlags;
92 allocationCreateInfo.preferredFlags = preferredFlags;
93 allocationCreateInfo.flags = (persistentlyMappedBuffers) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
94 VmaAllocationInfo allocationInfo = {};
95
96 result = vmaCreateBuffer(allocator, pBufferCreateInfo, &allocationCreateInfo, pBuffer,
97 pAllocation, &allocationInfo);
98 *pMemoryTypeIndexOut = allocationInfo.memoryType;
99
100 return result;
101 }
102
FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,const VkBufferCreateInfo * pBufferCreateInfo,VkMemoryPropertyFlags requiredFlags,VkMemoryPropertyFlags preferredFlags,bool persistentlyMappedBuffers,uint32_t * pMemoryTypeIndexOut)103 VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
104 const VkBufferCreateInfo *pBufferCreateInfo,
105 VkMemoryPropertyFlags requiredFlags,
106 VkMemoryPropertyFlags preferredFlags,
107 bool persistentlyMappedBuffers,
108 uint32_t *pMemoryTypeIndexOut)
109 {
110 VmaAllocationCreateInfo allocationCreateInfo = {};
111 allocationCreateInfo.requiredFlags = requiredFlags;
112 allocationCreateInfo.preferredFlags = preferredFlags;
113 allocationCreateInfo.flags = (persistentlyMappedBuffers) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
114
115 return vmaFindMemoryTypeIndexForBufferInfo(allocator, pBufferCreateInfo, &allocationCreateInfo,
116 pMemoryTypeIndexOut);
117 }
118
GetMemoryTypeProperties(VmaAllocator allocator,uint32_t memoryTypeIndex,VkMemoryPropertyFlags * pFlags)119 void GetMemoryTypeProperties(VmaAllocator allocator,
120 uint32_t memoryTypeIndex,
121 VkMemoryPropertyFlags *pFlags)
122 {
123 vmaGetMemoryTypeProperties(allocator, memoryTypeIndex, pFlags);
124 }
125
MapMemory(VmaAllocator allocator,VmaAllocation allocation,void ** ppData)126 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData)
127 {
128 return vmaMapMemory(allocator, allocation, ppData);
129 }
130
UnmapMemory(VmaAllocator allocator,VmaAllocation allocation)131 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation)
132 {
133 return vmaUnmapMemory(allocator, allocation);
134 }
135
FlushAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)136 void FlushAllocation(VmaAllocator allocator,
137 VmaAllocation allocation,
138 VkDeviceSize offset,
139 VkDeviceSize size)
140 {
141 vmaFlushAllocation(allocator, allocation, offset, size);
142 }
143
InvalidateAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)144 void InvalidateAllocation(VmaAllocator allocator,
145 VmaAllocation allocation,
146 VkDeviceSize offset,
147 VkDeviceSize size)
148 {
149 vmaInvalidateAllocation(allocator, allocation, offset, size);
150 }
151
BuildStatsString(VmaAllocator allocator,char ** statsString,VkBool32 detailedMap)152 void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap)
153 {
154 vmaBuildStatsString(allocator, statsString, detailedMap);
155 }
156
FreeStatsString(VmaAllocator allocator,char * statsString)157 void FreeStatsString(VmaAllocator allocator, char *statsString)
158 {
159 vmaFreeStatsString(allocator, statsString);
160 }
161 } // namespace vma
162