| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/vk/vulkanmemoryallocator/VulkanAMDMemoryAllocator.h" |
| 9 |
|
| 10 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 11 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/core/SkTraceEvent.h" |
| 15 |
#include "src/gpu/GpuTypesPriv.h" |
| 16 |
#include "src/gpu/vk/VulkanInterface.h" |
| 17 |
#include "src/gpu/vk/VulkanUtilsPriv.h" |
| 18 |
#include "src/gpu/vk/vulkanmemoryallocator/VulkanMemoryAllocatorPriv.h" |
| 19 |
|
| 20 |
#include <cstring> |
| 21 |
|
| 22 |
namespace skgpu { |
| 23 |
|
| 24 |
sk_sp<VulkanMemoryAllocator> VulkanAMDMemoryAllocator::Make(VkInstance instance, |
| 25 |
VkPhysicalDevice physicalDevice, |
| 26 |
VkDevice device, |
| 27 |
uint32_t physicalDeviceVersion, |
| 28 |
const VulkanExtensions* extensions, |
| 29 |
const VulkanInterface* interface, |
| 30 |
ThreadSafe threadSafe) { |
| 31 |
#define SKGPU_COPY_FUNCTION(NAME) functions.vk##NAME = interface->fFunctions.f##NAME |
| 32 |
#define SKGPU_COPY_FUNCTION_KHR(NAME) functions.vk##NAME##KHR = interface->fFunctions.f##NAME |
| 33 |
|
| 34 |
VmaVulkanFunctions functions; |
| 35 |
// We should be setting all the required functions (at least through vulkan 1.1), but this is |
| 36 |
// just extra belt and suspenders to make sure there isn't unitialized values here. |
| 37 |
std::memset(&functions, 0, sizeof(VmaVulkanFunctions)); |
| 38 |
|
| 39 |
// We don't use dynamic function getting in the allocator so we set the getProc functions to |
| 40 |
// null. |
| 41 |
functions.vkGetInstanceProcAddr = nullptr; |
| 42 |
functions.vkGetDeviceProcAddr = nullptr; |
| 43 |
SKGPU_COPY_FUNCTION(GetPhysicalDeviceProperties); |
| 44 |
SKGPU_COPY_FUNCTION(GetPhysicalDeviceMemoryProperties); |
| 45 |
SKGPU_COPY_FUNCTION(AllocateMemory); |
| 46 |
SKGPU_COPY_FUNCTION(FreeMemory); |
| 47 |
SKGPU_COPY_FUNCTION(MapMemory); |
| 48 |
SKGPU_COPY_FUNCTION(UnmapMemory); |
| 49 |
SKGPU_COPY_FUNCTION(FlushMappedMemoryRanges); |
| 50 |
SKGPU_COPY_FUNCTION(InvalidateMappedMemoryRanges); |
| 51 |
SKGPU_COPY_FUNCTION(BindBufferMemory); |
| 52 |
SKGPU_COPY_FUNCTION(BindImageMemory); |
| 53 |
SKGPU_COPY_FUNCTION(GetBufferMemoryRequirements); |
| 54 |
SKGPU_COPY_FUNCTION(GetImageMemoryRequirements); |
| 55 |
SKGPU_COPY_FUNCTION(CreateBuffer); |
| 56 |
SKGPU_COPY_FUNCTION(DestroyBuffer); |
| 57 |
SKGPU_COPY_FUNCTION(CreateImage); |
| 58 |
SKGPU_COPY_FUNCTION(DestroyImage); |
| 59 |
SKGPU_COPY_FUNCTION(CmdCopyBuffer); |
| 60 |
SKGPU_COPY_FUNCTION_KHR(GetBufferMemoryRequirements2); |
| 61 |
SKGPU_COPY_FUNCTION_KHR(GetImageMemoryRequirements2); |
| 62 |
SKGPU_COPY_FUNCTION_KHR(BindBufferMemory2); |
| 63 |
SKGPU_COPY_FUNCTION_KHR(BindImageMemory2); |
| 64 |
SKGPU_COPY_FUNCTION_KHR(GetPhysicalDeviceMemoryProperties2); |
| 65 |
|
| 66 |
VmaAllocatorCreateInfo info; |
| 67 |
info.flags = 0; |
| 68 |
if (threadSafe == ThreadSafe::kNo) { |
| 69 |
info.flags |= VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT; |
| 70 |
} |
| 71 |
// Dedicated allocation is always available since Vulkan 1.1 is the minimum requirement. |
| 72 |
info.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT; |
| 73 |
|
| 74 |
info.physicalDevice = physicalDevice; |
| 75 |
info.device = device; |
| 76 |
// 4MB was picked for the size here by looking at memory usage of Android apps and runs of DM. |
| 77 |
// It seems to be a good compromise of not wasting unused allocated space and not making too |
| 78 |
// many small allocations. The AMD allocator will start making blocks at 1/8 the max size and |
| 79 |
// builds up block size as needed before capping at the max set here. |
| 80 |
constexpr size_t kBlockSize = 4 * 1024 * 1024; |
| 81 |
info.preferredLargeHeapBlockSize = kBlockSize; |
| 82 |
info.pAllocationCallbacks = nullptr; |
| 83 |
info.pDeviceMemoryCallbacks = nullptr; |
| 84 |
info.pHeapSizeLimit = nullptr; |
| 85 |
info.pVulkanFunctions = &functions; |
| 86 |
info.instance = instance; |
| 87 |
// TODO: Update our interface and headers to support vulkan 1.3 and add in the new required |
| 88 |
// functions for 1.3 that the allocator needs. Until then we just clamp the version to 1.1, |
| 89 |
// which is also Skia's minimum requirement. |
| 90 |
info.vulkanApiVersion = VK_API_VERSION_1_1; |
| 91 |
info.pTypeExternalMemoryHandleTypes = nullptr; |
| 92 |
|
| 93 |
VmaAllocator allocator; |
| 94 |
vmaCreateAllocator(&info, &allocator); |
| 95 |
|
| 96 |
return sk_sp<VulkanAMDMemoryAllocator>(new VulkanAMDMemoryAllocator(allocator)); |
| 97 |
} |
| 98 |
|
| 99 |
VulkanAMDMemoryAllocator::VulkanAMDMemoryAllocator(VmaAllocator allocator) |
| 100 |
: fAllocator(allocator) {} |
| 101 |
|
| 102 |
VulkanAMDMemoryAllocator::~VulkanAMDMemoryAllocator() { |
| 103 |
vmaDestroyAllocator(fAllocator); |
| 104 |
fAllocator = VK_NULL_HANDLE; |
| 105 |
} |
| 106 |
|
| 107 |
VkResult VulkanAMDMemoryAllocator::allocateImageMemory(VkImage image, |
| 108 |
uint32_t allocationPropertyFlags, |
| 109 |
skgpu::VulkanBackendMemory* backendMemory) { |
| 110 |
TRACE_EVENT0_ALWAYS("skia.gpu", TRACE_FUNC); |
| 111 |
VmaAllocationCreateInfo info; |
| 112 |
info.flags = 0; |
| 113 |
info.usage = VMA_MEMORY_USAGE_UNKNOWN; |
| 114 |
info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 115 |
info.preferredFlags = 0; |
| 116 |
info.memoryTypeBits = 0; |
| 117 |
info.pool = VK_NULL_HANDLE; |
| 118 |
info.pUserData = nullptr; |
| 119 |
|
| 120 |
if (kDedicatedAllocation_AllocationPropertyFlag & allocationPropertyFlags) { |
| 121 |
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; |
| 122 |
} |
| 123 |
if (kLazyAllocation_AllocationPropertyFlag & allocationPropertyFlags) { |
| 124 |
info.requiredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; |
| 125 |
} |
| 126 |
if (kProtected_AllocationPropertyFlag & allocationPropertyFlags) { |
| 127 |
info.requiredFlags |= VK_MEMORY_PROPERTY_PROTECTED_BIT; |
| 128 |
} |
| 129 |
|
| 130 |
VmaAllocation allocation; |
| 131 |
VkResult result = vmaAllocateMemoryForImage(fAllocator, image, &info, &allocation, nullptr); |
| 132 |
if (VK_SUCCESS == result) { |
| 133 |
*backendMemory = (VulkanBackendMemory)allocation; |
| 134 |
} |
| 135 |
return result; |
| 136 |
} |
| 137 |
|
| 138 |
VkResult VulkanAMDMemoryAllocator::allocateBufferMemory(VkBuffer buffer, |
| 139 |
BufferUsage usage, |
| 140 |
uint32_t allocationPropertyFlags, |
| 141 |
skgpu::VulkanBackendMemory* backendMemory) { |
| 142 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 143 |
VmaAllocationCreateInfo info; |
| 144 |
info.flags = 0; |
| 145 |
info.usage = VMA_MEMORY_USAGE_UNKNOWN; |
| 146 |
info.memoryTypeBits = 0; |
| 147 |
info.pool = VK_NULL_HANDLE; |
| 148 |
info.pUserData = nullptr; |
| 149 |
|
| 150 |
switch (usage) { |
| 151 |
case BufferUsage::kGpuOnly: |
| 152 |
info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 153 |
info.preferredFlags = 0; |
| 154 |
break; |
| 155 |
case BufferUsage::kCpuWritesGpuReads: |
| 156 |
// When doing cpu writes and gpu reads the general rule of thumb is to use coherent |
| 157 |
// memory. Though this depends on the fact that we are not doing any cpu reads and the |
| 158 |
// cpu writes are sequential. For sparse writes we'd want cpu cached memory, however we |
| 159 |
// don't do these types of writes in Skia. |
| 160 |
// |
| 161 |
// TODO: In the future there may be times where specific types of memory could benefit |
| 162 |
// from a coherent and cached memory. Typically these allow for the gpu to read cpu |
| 163 |
// writes from the cache without needing to flush the writes throughout the cache. The |
| 164 |
// reverse is not true and GPU writes tend to invalidate the cache regardless. Also |
| 165 |
// these gpu cache read access are typically lower bandwidth than non-cached memory. |
| 166 |
// For now Skia doesn't really have a need or want of this type of memory. But if we |
| 167 |
// ever do we could pass in an AllocationPropertyFlag that requests the cached property. |
| 168 |
info.requiredFlags = |
| 169 |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 170 |
info.preferredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 171 |
break; |
| 172 |
case BufferUsage::kTransfersFromCpuToGpu: |
| 173 |
info.requiredFlags = |
| 174 |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 175 |
info.preferredFlags = 0; |
| 176 |
break; |
| 177 |
case BufferUsage::kTransfersFromGpuToCpu: |
| 178 |
info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 179 |
info.preferredFlags = VK_MEMORY_PROPERTY_HOST_CACHED_BIT; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
if (kDedicatedAllocation_AllocationPropertyFlag & allocationPropertyFlags) { |
| 184 |
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; |
| 185 |
} |
| 186 |
if ((kLazyAllocation_AllocationPropertyFlag & allocationPropertyFlags) && |
| 187 |
BufferUsage::kGpuOnly == usage) { |
| 188 |
info.preferredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; |
| 189 |
} |
| 190 |
|
| 191 |
if (kPersistentlyMapped_AllocationPropertyFlag & allocationPropertyFlags) { |
| 192 |
SkASSERT(BufferUsage::kGpuOnly != usage); |
| 193 |
info.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT; |
| 194 |
} |
| 195 |
|
| 196 |
if (kProtected_AllocationPropertyFlag & allocationPropertyFlags) { |
| 197 |
info.requiredFlags |= VK_MEMORY_PROPERTY_PROTECTED_BIT; |
| 198 |
} |
| 199 |
|
| 200 |
VmaAllocation allocation; |
| 201 |
VkResult result = vmaAllocateMemoryForBuffer(fAllocator, buffer, &info, &allocation, nullptr); |
| 202 |
if (VK_SUCCESS == result) { |
| 203 |
*backendMemory = (VulkanBackendMemory)allocation; |
| 204 |
} |
| 205 |
|
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
void VulkanAMDMemoryAllocator::freeMemory(const VulkanBackendMemory& memoryHandle) { |
| 210 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 211 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 212 |
vmaFreeMemory(fAllocator, allocation); |
| 213 |
} |
| 214 |
|
| 215 |
void VulkanAMDMemoryAllocator::getAllocInfo(const VulkanBackendMemory& memoryHandle, |
| 216 |
VulkanAlloc* alloc) const { |
| 217 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 218 |
VmaAllocationInfo vmaInfo; |
| 219 |
vmaGetAllocationInfo(fAllocator, allocation, &vmaInfo); |
| 220 |
|
| 221 |
VkMemoryPropertyFlags memFlags; |
| 222 |
vmaGetMemoryTypeProperties(fAllocator, vmaInfo.memoryType, &memFlags); |
| 223 |
|
| 224 |
uint32_t flags = 0; |
| 225 |
if (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT & memFlags) { |
| 226 |
flags |= VulkanAlloc::kMappable_Flag; |
| 227 |
} |
| 228 |
if (!SkToBool(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT & memFlags)) { |
| 229 |
flags |= VulkanAlloc::kNoncoherent_Flag; |
| 230 |
} |
| 231 |
if (VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT & memFlags) { |
| 232 |
flags |= VulkanAlloc::kLazilyAllocated_Flag; |
| 233 |
} |
| 234 |
|
| 235 |
alloc->fMemory = vmaInfo.deviceMemory; |
| 236 |
alloc->fOffset = vmaInfo.offset; |
| 237 |
alloc->fSize = vmaInfo.size; |
| 238 |
alloc->fFlags = flags; |
| 239 |
alloc->fBackendMemory = memoryHandle; |
| 240 |
} |
| 241 |
|
| 242 |
VkResult VulkanAMDMemoryAllocator::mapMemory(const VulkanBackendMemory& memoryHandle, void** data) { |
| 243 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 244 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 245 |
return vmaMapMemory(fAllocator, allocation, data); |
| 246 |
} |
| 247 |
|
| 248 |
void VulkanAMDMemoryAllocator::unmapMemory(const VulkanBackendMemory& memoryHandle) { |
| 249 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 250 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 251 |
vmaUnmapMemory(fAllocator, allocation); |
| 252 |
} |
| 253 |
|
| 254 |
VkResult VulkanAMDMemoryAllocator::flushMemory(const VulkanBackendMemory& memoryHandle, |
| 255 |
VkDeviceSize offset, |
| 256 |
VkDeviceSize size) { |
| 257 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 258 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 259 |
return vmaFlushAllocation(fAllocator, allocation, offset, size); |
| 260 |
} |
| 261 |
|
| 262 |
VkResult VulkanAMDMemoryAllocator::invalidateMemory(const VulkanBackendMemory& memoryHandle, |
| 263 |
VkDeviceSize offset, |
| 264 |
VkDeviceSize size) { |
| 265 |
TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 266 |
const VmaAllocation allocation = (VmaAllocation)memoryHandle; |
| 267 |
return vmaInvalidateAllocation(fAllocator, allocation, offset, size); |
| 268 |
} |
| 269 |
|
| 270 |
std::pair<uint64_t, uint64_t> VulkanAMDMemoryAllocator::totalAllocatedAndUsedMemory() const { |
| 271 |
VmaTotalStatistics stats; |
| 272 |
vmaCalculateStatistics(fAllocator, &stats); |
| 273 |
return {stats.total.statistics.blockBytes, stats.total.statistics.allocationBytes}; |
| 274 |
} |
| 275 |
|
| 276 |
namespace VulkanMemoryAllocators { |
| 277 |
sk_sp<VulkanMemoryAllocator> Make(const skgpu::VulkanBackendContext& backendContext, |
| 278 |
ThreadSafe threadSafe) { |
| 279 |
SkASSERT(backendContext.fInstance != VK_NULL_HANDLE); |
| 280 |
SkASSERT(backendContext.fPhysicalDevice != VK_NULL_HANDLE); |
| 281 |
SkASSERT(backendContext.fDevice != VK_NULL_HANDLE); |
| 282 |
SkASSERT(backendContext.fQueue != VK_NULL_HANDLE); |
| 283 |
SkASSERT(backendContext.fGetProc); |
| 284 |
|
| 285 |
skgpu::VulkanExtensions ext; |
| 286 |
const skgpu::VulkanExtensions* extensions = &ext; |
| 287 |
if (backendContext.fVkExtensions) { |
| 288 |
extensions = backendContext.fVkExtensions; |
| 289 |
} |
| 290 |
|
| 291 |
// It is a bit superfluous to create a VulkanInterface here just to create a memory allocator |
| 292 |
// given that Ganesh and Graphite will create their own. However, there's not a clean way to |
| 293 |
// have the interface created here persist for potential re-use without refactoring |
| 294 |
// VulkanMemoryAllocator to hold onto its interface as opposed to "borrowing" it. |
| 295 |
// Such a refactor could get messy without much actual benefit since interface creation is |
| 296 |
// not too expensive and this cost is only paid once during initialization. |
| 297 |
uint32_t physDevVersion = 0; |
| 298 |
sk_sp<const skgpu::VulkanInterface> interface = |
| 299 |
skgpu::MakeInterface(backendContext, extensions, &physDevVersion, nullptr); |
| 300 |
if (!interface) { |
| 301 |
return nullptr; |
| 302 |
} |
| 303 |
|
| 304 |
return VulkanAMDMemoryAllocator::Make(backendContext.fInstance, |
| 305 |
backendContext.fPhysicalDevice, |
| 306 |
backendContext.fDevice, |
| 307 |
physDevVersion, |
| 308 |
extensions, |
| 309 |
interface.get(), |
| 310 |
threadSafe); |
| 311 |
} |
| 312 |
|
| 313 |
} // namespace VulkanMemoryAllocators |
| 314 |
} // namespace skgpu |