vma_wrapper.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "gpu/vulkan/vma_wrapper.h"
  5. #include <vk_mem_alloc.h>
  6. #include "build/build_config.h"
  7. #include "gpu/vulkan/vulkan_function_pointers.h"
  8. namespace gpu {
  9. namespace vma {
  10. VkResult CreateAllocator(VkPhysicalDevice physical_device,
  11. VkDevice device,
  12. VkInstance instance,
  13. const VkDeviceSize* heap_size_limit,
  14. VmaAllocator* pAllocator) {
  15. auto* function_pointers = gpu::GetVulkanFunctionPointers();
  16. VmaVulkanFunctions functions = {
  17. function_pointers->vkGetPhysicalDeviceProperties.get(),
  18. function_pointers->vkGetPhysicalDeviceMemoryProperties.get(),
  19. function_pointers->vkAllocateMemory.get(),
  20. function_pointers->vkFreeMemory.get(),
  21. function_pointers->vkMapMemory.get(),
  22. function_pointers->vkUnmapMemory.get(),
  23. function_pointers->vkFlushMappedMemoryRanges.get(),
  24. function_pointers->vkInvalidateMappedMemoryRanges.get(),
  25. function_pointers->vkBindBufferMemory.get(),
  26. function_pointers->vkBindImageMemory.get(),
  27. function_pointers->vkGetBufferMemoryRequirements.get(),
  28. function_pointers->vkGetImageMemoryRequirements.get(),
  29. function_pointers->vkCreateBuffer.get(),
  30. function_pointers->vkDestroyBuffer.get(),
  31. function_pointers->vkCreateImage.get(),
  32. function_pointers->vkDestroyImage.get(),
  33. function_pointers->vkCmdCopyBuffer.get(),
  34. function_pointers->vkGetBufferMemoryRequirements2.get(),
  35. function_pointers->vkGetImageMemoryRequirements2.get(),
  36. function_pointers->vkBindBufferMemory2.get(),
  37. function_pointers->vkBindImageMemory2.get(),
  38. function_pointers->vkGetPhysicalDeviceMemoryProperties2.get(),
  39. };
  40. static_assert(kVulkanRequiredApiVersion >= VK_API_VERSION_1_1, "");
  41. VmaAllocatorCreateInfo allocator_info = {
  42. .flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
  43. .physicalDevice = physical_device,
  44. .device = device,
  45. // 4MB was picked for the size here by looking at memory usage of Android
  46. // apps and runs of DM. It seems to be a good compromise of not wasting
  47. // unused allocated space and not making too many small allocations. The
  48. // AMD allocator will start making blocks at 1/8 the max size and builds
  49. // up block size as needed before capping at the max set here.
  50. .preferredLargeHeapBlockSize = 4 * 1024 * 1024,
  51. .pHeapSizeLimit = heap_size_limit,
  52. .pVulkanFunctions = &functions,
  53. .instance = instance,
  54. .vulkanApiVersion = kVulkanRequiredApiVersion,
  55. };
  56. return vmaCreateAllocator(&allocator_info, pAllocator);
  57. }
  58. void DestroyAllocator(VmaAllocator allocator) {
  59. vmaDestroyAllocator(allocator);
  60. }
  61. VkResult AllocateMemoryForImage(VmaAllocator allocator,
  62. VkImage image,
  63. const VmaAllocationCreateInfo* create_info,
  64. VmaAllocation* allocation,
  65. VmaAllocationInfo* allocation_info) {
  66. return vmaAllocateMemoryForImage(allocator, image, create_info, allocation,
  67. allocation_info);
  68. }
  69. VkResult AllocateMemoryForBuffer(VmaAllocator allocator,
  70. VkBuffer buffer,
  71. const VmaAllocationCreateInfo* create_info,
  72. VmaAllocation* allocation,
  73. VmaAllocationInfo* allocation_info) {
  74. return vmaAllocateMemoryForBuffer(allocator, buffer, create_info, allocation,
  75. allocation_info);
  76. }
  77. VkResult CreateBuffer(VmaAllocator allocator,
  78. const VkBufferCreateInfo* buffer_create_info,
  79. VkMemoryPropertyFlags required_flags,
  80. VkMemoryPropertyFlags preferred_flags,
  81. VkBuffer* buffer,
  82. VmaAllocation* allocation) {
  83. VmaAllocationCreateInfo allocation_create_info = {
  84. .requiredFlags = required_flags,
  85. .preferredFlags = preferred_flags,
  86. };
  87. return vmaCreateBuffer(allocator, buffer_create_info, &allocation_create_info,
  88. buffer, allocation, nullptr);
  89. }
  90. void DestroyBuffer(VmaAllocator allocator,
  91. VkBuffer buffer,
  92. VmaAllocation allocation) {
  93. vmaDestroyBuffer(allocator, buffer, allocation);
  94. }
  95. VkResult MapMemory(VmaAllocator allocator,
  96. VmaAllocation allocation,
  97. void** data) {
  98. return vmaMapMemory(allocator, allocation, data);
  99. }
  100. void UnmapMemory(VmaAllocator allocator, VmaAllocation allocation) {
  101. return vmaUnmapMemory(allocator, allocation);
  102. }
  103. void FreeMemory(VmaAllocator allocator, VmaAllocation allocation) {
  104. vmaFreeMemory(allocator, allocation);
  105. }
  106. VkResult FlushAllocation(VmaAllocator allocator,
  107. VmaAllocation allocation,
  108. VkDeviceSize offset,
  109. VkDeviceSize size) {
  110. return vmaFlushAllocation(allocator, allocation, offset, size);
  111. }
  112. VkResult InvalidateAllocation(VmaAllocator allocator,
  113. VmaAllocation allocation,
  114. VkDeviceSize offset,
  115. VkDeviceSize size) {
  116. return vmaInvalidateAllocation(allocator, allocation, offset, size);
  117. }
  118. void GetAllocationInfo(VmaAllocator allocator,
  119. VmaAllocation allocation,
  120. VmaAllocationInfo* allocation_info) {
  121. vmaGetAllocationInfo(allocator, allocation, allocation_info);
  122. }
  123. void GetMemoryTypeProperties(VmaAllocator allocator,
  124. uint32_t memory_type_index,
  125. VkMemoryPropertyFlags* flags) {
  126. vmaGetMemoryTypeProperties(allocator, memory_type_index, flags);
  127. }
  128. void GetPhysicalDeviceProperties(
  129. VmaAllocator allocator,
  130. const VkPhysicalDeviceProperties** physical_device_properties) {
  131. vmaGetPhysicalDeviceProperties(allocator, physical_device_properties);
  132. }
  133. void CalculateStats(VmaAllocator allocator, VmaStats* stats) {
  134. vmaCalculateStats(allocator, stats);
  135. }
  136. uint64_t GetTotalAllocatedMemory(VmaAllocator allocator) {
  137. VmaBudget budget[VK_MAX_MEMORY_HEAPS];
  138. vmaGetBudget(allocator, budget);
  139. const VkPhysicalDeviceMemoryProperties* pPhysicalDeviceMemoryProperties;
  140. vmaGetMemoryProperties(allocator, &pPhysicalDeviceMemoryProperties);
  141. uint64_t total_allocated_memory = 0;
  142. for (uint32_t i = 0; i < pPhysicalDeviceMemoryProperties->memoryHeapCount;
  143. ++i) {
  144. total_allocated_memory +=
  145. std::max(budget[i].blockBytes, budget[i].allocationBytes);
  146. }
  147. return total_allocated_memory;
  148. }
  149. } // namespace vma
  150. } // namespace gpu