• 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.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 
CreatePool(VmaAllocator allocator,uint32_t memoryTypeIndex,bool buddyAlgorithm,VkDeviceSize blockSize,VmaPool * pPool)75 VkResult CreatePool(VmaAllocator allocator,
76                     uint32_t memoryTypeIndex,
77                     bool buddyAlgorithm,
78                     VkDeviceSize blockSize,
79                     VmaPool *pPool)
80 {
81     VmaPoolCreateInfo poolCreateInfo = {};
82     poolCreateInfo.memoryTypeIndex   = memoryTypeIndex;
83     poolCreateInfo.flags             = VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT;
84     if (buddyAlgorithm)
85     {
86         poolCreateInfo.flags |= VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT;
87     }
88     poolCreateInfo.blockSize     = blockSize;
89     poolCreateInfo.maxBlockCount = -1;  // unlimited
90     return vmaCreatePool(allocator, &poolCreateInfo, pPool);
91 }
92 
DestroyPool(VmaAllocator allocator,VmaPool pool)93 void DestroyPool(VmaAllocator allocator, VmaPool pool)
94 {
95     vmaDestroyPool(allocator, pool);
96 }
97 
FreeMemory(VmaAllocator allocator,VmaAllocation allocation)98 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation)
99 {
100     vmaFreeMemory(allocator, allocation);
101 }
102 
CreateBuffer(VmaAllocator allocator,const VkBufferCreateInfo * pBufferCreateInfo,VkMemoryPropertyFlags requiredFlags,VkMemoryPropertyFlags preferredFlags,bool persistentlyMapped,uint32_t * pMemoryTypeIndexOut,VkBuffer * pBuffer,VmaAllocation * pAllocation)103 VkResult CreateBuffer(VmaAllocator allocator,
104                       const VkBufferCreateInfo *pBufferCreateInfo,
105                       VkMemoryPropertyFlags requiredFlags,
106                       VkMemoryPropertyFlags preferredFlags,
107                       bool persistentlyMapped,
108                       uint32_t *pMemoryTypeIndexOut,
109                       VkBuffer *pBuffer,
110                       VmaAllocation *pAllocation)
111 {
112     VkResult result;
113     VmaAllocationCreateInfo allocationCreateInfo = {};
114     allocationCreateInfo.requiredFlags           = requiredFlags;
115     allocationCreateInfo.preferredFlags          = preferredFlags;
116     allocationCreateInfo.flags       = (persistentlyMapped) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
117     VmaAllocationInfo allocationInfo = {};
118 
119     result = vmaCreateBuffer(allocator, pBufferCreateInfo, &allocationCreateInfo, pBuffer,
120                              pAllocation, &allocationInfo);
121     *pMemoryTypeIndexOut = allocationInfo.memoryType;
122 
123     return result;
124 }
125 
FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,const VkBufferCreateInfo * pBufferCreateInfo,VkMemoryPropertyFlags requiredFlags,VkMemoryPropertyFlags preferredFlags,bool persistentlyMappedBuffers,uint32_t * pMemoryTypeIndexOut)126 VkResult FindMemoryTypeIndexForBufferInfo(VmaAllocator allocator,
127                                           const VkBufferCreateInfo *pBufferCreateInfo,
128                                           VkMemoryPropertyFlags requiredFlags,
129                                           VkMemoryPropertyFlags preferredFlags,
130                                           bool persistentlyMappedBuffers,
131                                           uint32_t *pMemoryTypeIndexOut)
132 {
133     VmaAllocationCreateInfo allocationCreateInfo = {};
134     allocationCreateInfo.requiredFlags           = requiredFlags;
135     allocationCreateInfo.preferredFlags          = preferredFlags;
136     allocationCreateInfo.flags = (persistentlyMappedBuffers) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
137 
138     return vmaFindMemoryTypeIndexForBufferInfo(allocator, pBufferCreateInfo, &allocationCreateInfo,
139                                                pMemoryTypeIndexOut);
140 }
141 
GetMemoryTypeProperties(VmaAllocator allocator,uint32_t memoryTypeIndex,VkMemoryPropertyFlags * pFlags)142 void GetMemoryTypeProperties(VmaAllocator allocator,
143                              uint32_t memoryTypeIndex,
144                              VkMemoryPropertyFlags *pFlags)
145 {
146     vmaGetMemoryTypeProperties(allocator, memoryTypeIndex, pFlags);
147 }
148 
MapMemory(VmaAllocator allocator,VmaAllocation allocation,void ** ppData)149 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData)
150 {
151     return vmaMapMemory(allocator, allocation, ppData);
152 }
153 
UnmapMemory(VmaAllocator allocator,VmaAllocation allocation)154 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation)
155 {
156     return vmaUnmapMemory(allocator, allocation);
157 }
158 
FlushAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)159 void FlushAllocation(VmaAllocator allocator,
160                      VmaAllocation allocation,
161                      VkDeviceSize offset,
162                      VkDeviceSize size)
163 {
164     vmaFlushAllocation(allocator, allocation, offset, size);
165 }
166 
InvalidateAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)167 void InvalidateAllocation(VmaAllocator allocator,
168                           VmaAllocation allocation,
169                           VkDeviceSize offset,
170                           VkDeviceSize size)
171 {
172     vmaInvalidateAllocation(allocator, allocation, offset, size);
173 }
174 
BuildStatsString(VmaAllocator allocator,char ** statsString,VkBool32 detailedMap)175 void BuildStatsString(VmaAllocator allocator, char **statsString, VkBool32 detailedMap)
176 {
177     vmaBuildStatsString(allocator, statsString, detailedMap);
178 }
179 
FreeStatsString(VmaAllocator allocator,char * statsString)180 void FreeStatsString(VmaAllocator allocator, char *statsString)
181 {
182     vmaFreeStatsString(allocator, statsString);
183 }
184 }  // namespace vma
185