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,VmaAllocator * pAllocator)16 VkResult InitAllocator(VkPhysicalDevice physicalDevice,
17 VkDevice device,
18 VkInstance instance,
19 VmaAllocator *pAllocator)
20 {
21 VmaVulkanFunctions funcs;
22 funcs.vkGetPhysicalDeviceProperties = vkGetPhysicalDeviceProperties;
23 funcs.vkGetPhysicalDeviceMemoryProperties = vkGetPhysicalDeviceMemoryProperties;
24 funcs.vkAllocateMemory = vkAllocateMemory;
25 funcs.vkFreeMemory = vkFreeMemory;
26 funcs.vkMapMemory = vkMapMemory;
27 funcs.vkUnmapMemory = vkUnmapMemory;
28 funcs.vkFlushMappedMemoryRanges = vkFlushMappedMemoryRanges;
29 funcs.vkInvalidateMappedMemoryRanges = vkInvalidateMappedMemoryRanges;
30 funcs.vkBindBufferMemory = vkBindBufferMemory;
31 funcs.vkBindImageMemory = vkBindImageMemory;
32 funcs.vkGetBufferMemoryRequirements = vkGetBufferMemoryRequirements;
33 funcs.vkGetImageMemoryRequirements = vkGetImageMemoryRequirements;
34 funcs.vkCreateBuffer = vkCreateBuffer;
35 funcs.vkDestroyBuffer = vkDestroyBuffer;
36 funcs.vkCreateImage = vkCreateImage;
37 funcs.vkDestroyImage = vkDestroyImage;
38 funcs.vkCmdCopyBuffer = vkCmdCopyBuffer;
39 funcs.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2KHR;
40 funcs.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2KHR;
41
42 VmaAllocatorCreateInfo allocatorInfo = {};
43 allocatorInfo.physicalDevice = physicalDevice;
44 allocatorInfo.device = device;
45 allocatorInfo.instance = instance;
46 allocatorInfo.pVulkanFunctions = &funcs;
47
48 return vmaCreateAllocator(&allocatorInfo, pAllocator);
49 }
50
DestroyAllocator(VmaAllocator allocator)51 void DestroyAllocator(VmaAllocator allocator)
52 {
53 vmaDestroyAllocator(allocator);
54 }
55
FreeMemory(VmaAllocator allocator,VmaAllocation allocation)56 void FreeMemory(VmaAllocator allocator, VmaAllocation allocation)
57 {
58 vmaFreeMemory(allocator, allocation);
59 }
60
CreateBuffer(VmaAllocator allocator,const VkBufferCreateInfo * pBufferCreateInfo,VkMemoryPropertyFlags requiredFlags,VkMemoryPropertyFlags preferredFlags,bool persistentlyMappedBuffers,uint32_t * pMemoryTypeIndexOut,VkBuffer * pBuffer,VmaAllocation * pAllocation)61 VkResult CreateBuffer(VmaAllocator allocator,
62 const VkBufferCreateInfo *pBufferCreateInfo,
63 VkMemoryPropertyFlags requiredFlags,
64 VkMemoryPropertyFlags preferredFlags,
65 bool persistentlyMappedBuffers,
66 uint32_t *pMemoryTypeIndexOut,
67 VkBuffer *pBuffer,
68 VmaAllocation *pAllocation)
69 {
70 VkResult result;
71 VmaAllocationCreateInfo allocationCreateInfo = {};
72 allocationCreateInfo.requiredFlags = requiredFlags;
73 allocationCreateInfo.preferredFlags = preferredFlags;
74 allocationCreateInfo.flags = (persistentlyMappedBuffers) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
75 VmaAllocationInfo allocationInfo = {};
76
77 result = vmaCreateBuffer(allocator, pBufferCreateInfo, &allocationCreateInfo, pBuffer,
78 pAllocation, &allocationInfo);
79 *pMemoryTypeIndexOut = allocationInfo.memoryType;
80
81 return result;
82 }
83
GetMemoryTypeProperties(VmaAllocator allocator,uint32_t memoryTypeIndex,VkMemoryPropertyFlags * pFlags)84 void GetMemoryTypeProperties(VmaAllocator allocator,
85 uint32_t memoryTypeIndex,
86 VkMemoryPropertyFlags *pFlags)
87 {
88 vmaGetMemoryTypeProperties(allocator, memoryTypeIndex, pFlags);
89 }
90
MapMemory(VmaAllocator allocator,VmaAllocation allocation,void ** ppData)91 VkResult MapMemory(VmaAllocator allocator, VmaAllocation allocation, void **ppData)
92 {
93 return vmaMapMemory(allocator, allocation, ppData);
94 }
95
UnmapMemory(VmaAllocator allocator,VmaAllocation allocation)96 void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation)
97 {
98 return vmaUnmapMemory(allocator, allocation);
99 }
100
FlushAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)101 void FlushAllocation(VmaAllocator allocator,
102 VmaAllocation allocation,
103 VkDeviceSize offset,
104 VkDeviceSize size)
105 {
106 vmaFlushAllocation(allocator, allocation, offset, size);
107 }
108
InvalidateAllocation(VmaAllocator allocator,VmaAllocation allocation,VkDeviceSize offset,VkDeviceSize size)109 void InvalidateAllocation(VmaAllocator allocator,
110 VmaAllocation allocation,
111 VkDeviceSize offset,
112 VkDeviceSize size)
113 {
114 vmaInvalidateAllocation(allocator, allocation, offset, size);
115 }
116 } // namespace vma
117