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