Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 1 | // 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 Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 7 | #include <vk_mem_alloc.h> |
Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 8 | |
| 9 | #include "build/build_config.h" |
| 10 | #include "gpu/vulkan/vulkan_function_pointers.h" |
| 11 | |
| 12 | namespace gpu { |
| 13 | namespace vma { |
| 14 | |
| 15 | VkResult CreateAllocator(VkPhysicalDevice physical_device, |
| 16 | VkDevice device, |
| 17 | VkInstance instance, |
| 18 | VmaAllocator* pAllocator) { |
Peng Huang | 9ce7695 | 2020-05-12 00:24:09 | [diff] [blame] | 19 | auto* function_pointers = gpu::GetVulkanFunctionPointers(); |
Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 20 | VmaVulkanFunctions functions = { |
Peng Huang | dbcbea6 | 2020-05-20 04:50:15 | [diff] [blame] | 21 | 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 Huang | 3e9d1cc | 2020-05-27 17:02:02 | [diff] [blame] | 40 | function_pointers->vkBindBufferMemory2.get(), |
| 41 | function_pointers->vkBindImageMemory2.get(), |
| 42 | function_pointers->vkGetPhysicalDeviceMemoryProperties2.get(), |
Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 43 | }; |
| 44 | |
Peng Huang | 3e9d1cc | 2020-05-27 17:02:02 | [diff] [blame] | 45 | static_assert(kVulkanRequiredApiVersion >= VK_API_VERSION_1_1, ""); |
Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 46 | 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 Huang | 3e9d1cc | 2020-05-27 17:02:02 | [diff] [blame] | 58 | .vulkanApiVersion = kVulkanRequiredApiVersion, |
Peng Huang | 992a855 | 2020-05-04 18:05:30 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | return vmaCreateAllocator(&allocator_info, pAllocator); |
| 62 | } |
| 63 | |
| 64 | void DestroyAllocator(VmaAllocator allocator) { |
| 65 | vmaDestroyAllocator(allocator); |
| 66 | } |
| 67 | |
| 68 | VkResult 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 | |
| 77 | VkResult 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 | |
| 86 | VkResult 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 | |
| 101 | void DestroyBuffer(VmaAllocator allocator, |
| 102 | VkBuffer buffer, |
| 103 | VmaAllocation allocation) { |
| 104 | vmaDestroyBuffer(allocator, buffer, allocation); |
| 105 | } |
| 106 | |
| 107 | VkResult MapMemory(VmaAllocator allocator, |
| 108 | VmaAllocation allocation, |
| 109 | void** data) { |
| 110 | return vmaMapMemory(allocator, allocation, data); |
| 111 | } |
| 112 | |
| 113 | void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation) { |
| 114 | return vmaUnmapMemory(allocator, allocation); |
| 115 | } |
| 116 | |
| 117 | void FreeMemory(VmaAllocator allocator, VmaAllocation allocation) { |
| 118 | vmaFreeMemory(allocator, allocation); |
| 119 | } |
| 120 | |
| 121 | void FlushAllocation(VmaAllocator allocator, |
| 122 | VmaAllocation allocation, |
| 123 | VkDeviceSize offset, |
| 124 | VkDeviceSize size) { |
| 125 | vmaFlushAllocation(allocator, allocation, offset, size); |
| 126 | } |
| 127 | |
| 128 | void InvalidateAllocation(VmaAllocator allocator, |
| 129 | VmaAllocation allocation, |
| 130 | VkDeviceSize offset, |
| 131 | VkDeviceSize size) { |
| 132 | vmaInvalidateAllocation(allocator, allocation, offset, size); |
| 133 | } |
| 134 | |
| 135 | void GetAllocationInfo(VmaAllocator allocator, |
| 136 | VmaAllocation allocation, |
| 137 | VmaAllocationInfo* allocation_info) { |
| 138 | vmaGetAllocationInfo(allocator, allocation, allocation_info); |
| 139 | } |
| 140 | |
| 141 | void GetMemoryTypeProperties(VmaAllocator allocator, |
| 142 | uint32_t memory_type_index, |
| 143 | VkMemoryPropertyFlags* flags) { |
| 144 | vmaGetMemoryTypeProperties(allocator, memory_type_index, flags); |
| 145 | } |
| 146 | |
| 147 | void GetPhysicalDeviceProperties( |
| 148 | VmaAllocator allocator, |
| 149 | const VkPhysicalDeviceProperties** physical_device_properties) { |
| 150 | vmaGetPhysicalDeviceProperties(allocator, physical_device_properties); |
| 151 | } |
| 152 | |
| 153 | void CalculateStats(VmaAllocator allocator, VmaStats* stats) { |
| 154 | vmaCalculateStats(allocator, stats); |
| 155 | } |
| 156 | |
| 157 | } // namespace vma |
| 158 | } // namespace gpu |