blob: c436522a1a885d04f6b0584f45e2bf082922b4e3 [file] [log] [blame]
Peng Huang992a8552020-05-04 18:05:301// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "gpu/vulkan/vma_wrapper.h"
6
Peng Huang992a8552020-05-04 18:05:307#include <vk_mem_alloc.h>
Peng Huang992a8552020-05-04 18:05:308
9#include "build/build_config.h"
10#include "gpu/vulkan/vulkan_function_pointers.h"
11
12namespace gpu {
13namespace vma {
14
15VkResult CreateAllocator(VkPhysicalDevice physical_device,
16 VkDevice device,
17 VkInstance instance,
18 VmaAllocator* pAllocator) {
Peng Huang9ce76952020-05-12 00:24:0919 auto* function_pointers = gpu::GetVulkanFunctionPointers();
Peng Huang992a8552020-05-04 18:05:3020 VmaVulkanFunctions functions = {
Peng Huangdbcbea62020-05-20 04:50:1521 function_pointers->vkGetPhysicalDeviceProperties.get(),
22 function_pointers->vkGetPhysicalDeviceMemoryProperties.get(),
23 function_pointers->vkAllocateMemory.get(),
24 function_pointers->vkFreeMemory.get(),
25 function_pointers->vkMapMemory.get(),
26 function_pointers->vkUnmapMemory.get(),
27 function_pointers->vkFlushMappedMemoryRanges.get(),
28 function_pointers->vkInvalidateMappedMemoryRanges.get(),
29 function_pointers->vkBindBufferMemory.get(),
30 function_pointers->vkBindImageMemory.get(),
31 function_pointers->vkGetBufferMemoryRequirements.get(),
32 function_pointers->vkGetImageMemoryRequirements.get(),
33 function_pointers->vkCreateBuffer.get(),
34 function_pointers->vkDestroyBuffer.get(),
35 function_pointers->vkCreateImage.get(),
36 function_pointers->vkDestroyImage.get(),
37 function_pointers->vkCmdCopyBuffer.get(),
38 function_pointers->vkGetBufferMemoryRequirements2.get(),
39 function_pointers->vkGetImageMemoryRequirements2.get(),
Peng Huang3e9d1cc2020-05-27 17:02:0240 function_pointers->vkBindBufferMemory2.get(),
41 function_pointers->vkBindImageMemory2.get(),
42 function_pointers->vkGetPhysicalDeviceMemoryProperties2.get(),
Peng Huang992a8552020-05-04 18:05:3043 };
44
Peng Huang3e9d1cc2020-05-27 17:02:0245 static_assert(kVulkanRequiredApiVersion >= VK_API_VERSION_1_1, "");
Peng Huang992a8552020-05-04 18:05:3046 VmaAllocatorCreateInfo allocator_info = {
47 .flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
48 .physicalDevice = physical_device,
49 .device = device,
50 // 4MB was picked for the size here by looking at memory usage of Android
51 // apps and runs of DM. It seems to be a good compromise of not wasting
52 // unused allocated space and not making too many small allocations. The
53 // AMD allocator will start making blocks at 1/8 the max size and builds
54 // up block size as needed before capping at the max set here.
55 .preferredLargeHeapBlockSize = 4 * 1024 * 1024,
56 .pVulkanFunctions = &functions,
57 .instance = instance,
Peng Huang3e9d1cc2020-05-27 17:02:0258 .vulkanApiVersion = kVulkanRequiredApiVersion,
Peng Huang992a8552020-05-04 18:05:3059 };
60
61 return vmaCreateAllocator(&allocator_info, pAllocator);
62}
63
64void DestroyAllocator(VmaAllocator allocator) {
65 vmaDestroyAllocator(allocator);
66}
67
68VkResult AllocateMemoryForImage(VmaAllocator allocator,
69 VkImage image,
70 const VmaAllocationCreateInfo* create_info,
71 VmaAllocation* allocation,
72 VmaAllocationInfo* allocation_info) {
73 return vmaAllocateMemoryForImage(allocator, image, create_info, allocation,
74 allocation_info);
75}
76
77VkResult AllocateMemoryForBuffer(VmaAllocator allocator,
78 VkBuffer buffer,
79 const VmaAllocationCreateInfo* create_info,
80 VmaAllocation* allocation,
81 VmaAllocationInfo* allocation_info) {
82 return vmaAllocateMemoryForBuffer(allocator, buffer, create_info, allocation,
83 allocation_info);
84}
85
86VkResult CreateBuffer(VmaAllocator allocator,
87 const VkBufferCreateInfo* buffer_create_info,
88 VkMemoryPropertyFlags required_flags,
89 VkMemoryPropertyFlags preferred_flags,
90 VkBuffer* buffer,
91 VmaAllocation* allocation) {
92 VmaAllocationCreateInfo allocation_create_info = {
93 .requiredFlags = required_flags,
94 .preferredFlags = preferred_flags,
95 };
96
97 return vmaCreateBuffer(allocator, buffer_create_info, &allocation_create_info,
98 buffer, allocation, nullptr);
99}
100
101void DestroyBuffer(VmaAllocator allocator,
102 VkBuffer buffer,
103 VmaAllocation allocation) {
104 vmaDestroyBuffer(allocator, buffer, allocation);
105}
106
107VkResult MapMemory(VmaAllocator allocator,
108 VmaAllocation allocation,
109 void** data) {
110 return vmaMapMemory(allocator, allocation, data);
111}
112
113void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation) {
114 return vmaUnmapMemory(allocator, allocation);
115}
116
117void FreeMemory(VmaAllocator allocator, VmaAllocation allocation) {
118 vmaFreeMemory(allocator, allocation);
119}
120
121void FlushAllocation(VmaAllocator allocator,
122 VmaAllocation allocation,
123 VkDeviceSize offset,
124 VkDeviceSize size) {
125 vmaFlushAllocation(allocator, allocation, offset, size);
126}
127
128void InvalidateAllocation(VmaAllocator allocator,
129 VmaAllocation allocation,
130 VkDeviceSize offset,
131 VkDeviceSize size) {
132 vmaInvalidateAllocation(allocator, allocation, offset, size);
133}
134
135void GetAllocationInfo(VmaAllocator allocator,
136 VmaAllocation allocation,
137 VmaAllocationInfo* allocation_info) {
138 vmaGetAllocationInfo(allocator, allocation, allocation_info);
139}
140
141void GetMemoryTypeProperties(VmaAllocator allocator,
142 uint32_t memory_type_index,
143 VkMemoryPropertyFlags* flags) {
144 vmaGetMemoryTypeProperties(allocator, memory_type_index, flags);
145}
146
147void GetPhysicalDeviceProperties(
148 VmaAllocator allocator,
149 const VkPhysicalDeviceProperties** physical_device_properties) {
150 vmaGetPhysicalDeviceProperties(allocator, physical_device_properties);
151}
152
153void CalculateStats(VmaAllocator allocator, VmaStats* stats) {
154 vmaCalculateStats(allocator, stats);
155}
156
157} // namespace vma
158} // namespace gpu