1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/vulkan/skia_vk_memory_allocator_impl.h"
#include <vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>
#include <array>
#include "base/feature_list.h"
#include "base/trace_event/trace_event.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_function_pointers.h"
namespace gpu {
SkiaVulkanMemoryAllocator::SkiaVulkanMemoryAllocator(VmaAllocator allocator)
: allocator_(allocator) {}
VkResult SkiaVulkanMemoryAllocator::allocateImageMemory(
VkImage image,
uint32_t flags,
skgpu::VulkanBackendMemory* backend_memory) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::allocateMemoryForImage");
VmaAllocationCreateInfo info = {
.flags = 0,
.usage = VMA_MEMORY_USAGE_UNKNOWN,
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
.preferredFlags = 0,
.memoryTypeBits = 0,
.pool = VK_NULL_HANDLE,
.pUserData = nullptr,
};
if (kDedicatedAllocation_AllocationPropertyFlag & flags) {
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
}
if (kLazyAllocation_AllocationPropertyFlag & flags) {
// If the caller asked for lazy allocation then they already set up the
// VkImage for it so we must require the lazy property.
info.requiredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;
// Make the transient allocation a dedicated allocation for tracking
// memory separately.
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
}
if (kProtected_AllocationPropertyFlag & flags) {
info.requiredFlags |= VK_MEMORY_PROPERTY_PROTECTED_BIT;
}
VmaAllocation allocation;
VkResult result = vma::AllocateMemoryForImage(allocator_, image, &info,
&allocation, nullptr);
if (VK_SUCCESS == result) {
if (info.requiredFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
VmaAllocationInfo vma_info;
vma::GetAllocationInfo(allocator_, allocation, &vma_info);
lazy_allocated_size_ += vma_info.size;
}
*backend_memory = reinterpret_cast<skgpu::VulkanBackendMemory>(allocation);
}
return result;
}
VkResult SkiaVulkanMemoryAllocator::allocateBufferMemory(
VkBuffer buffer,
BufferUsage usage,
uint32_t flags,
skgpu::VulkanBackendMemory* backend_memory) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::allocateMemoryForBuffer");
VmaAllocationCreateInfo info = {
.flags = 0,
.usage = VMA_MEMORY_USAGE_UNKNOWN,
.memoryTypeBits = 0,
.pool = VK_NULL_HANDLE,
.pUserData = nullptr,
};
switch (usage) {
case BufferUsage::kGpuOnly:
info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
info.preferredFlags = 0;
break;
case BufferUsage::kCpuWritesGpuReads:
info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
info.preferredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
break;
case BufferUsage::kTransfersFromCpuToGpu:
info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
info.preferredFlags = 0;
break;
case BufferUsage::kTransfersFromGpuToCpu:
info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
info.preferredFlags = VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
break;
}
if (kDedicatedAllocation_AllocationPropertyFlag & flags) {
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
}
if ((kLazyAllocation_AllocationPropertyFlag & flags) &&
BufferUsage::kGpuOnly == usage) {
info.preferredFlags |= VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;
// Make the transient allocation a dedicated allocation for tracking
// memory separately.
info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
}
if (kPersistentlyMapped_AllocationPropertyFlag & flags) {
SkASSERT(BufferUsage::kGpuOnly != usage);
info.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
}
VmaAllocation allocation;
VkResult result = vma::AllocateMemoryForBuffer(allocator_, buffer, &info,
&allocation, nullptr);
if (VK_SUCCESS == result) {
if (info.preferredFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
VmaAllocationInfo vma_info;
vma::GetAllocationInfo(allocator_, allocation, &vma_info);
lazy_allocated_size_ += vma_info.size;
}
*backend_memory = reinterpret_cast<skgpu::VulkanBackendMemory>(allocation);
}
return result;
}
void SkiaVulkanMemoryAllocator::freeMemory(
const skgpu::VulkanBackendMemory& memory) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::freeMemory");
VmaAllocation allocation = reinterpret_cast<const VmaAllocation>(memory);
// Update `lazy_allocated_size_` tracking.
VmaAllocationInfo vma_info;
vma::GetAllocationInfo(allocator_, allocation, &vma_info);
VkMemoryPropertyFlags mem_flags;
vma::GetMemoryTypeProperties(allocator_, vma_info.memoryType, &mem_flags);
if (mem_flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
lazy_allocated_size_ -= vma_info.size;
}
vma::FreeMemory(allocator_, allocation);
}
void SkiaVulkanMemoryAllocator::getAllocInfo(
const skgpu::VulkanBackendMemory& memory,
skgpu::VulkanAlloc* alloc) const {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::getAllocInfo");
const VmaAllocation allocation =
reinterpret_cast<const VmaAllocation>(memory);
VmaAllocationInfo vma_info;
vma::GetAllocationInfo(allocator_, allocation, &vma_info);
VkMemoryPropertyFlags mem_flags;
vma::GetMemoryTypeProperties(allocator_, vma_info.memoryType, &mem_flags);
uint32_t flags = 0;
if (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT & mem_flags) {
flags |= skgpu::VulkanAlloc::kMappable_Flag;
}
if (!(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT & mem_flags)) {
flags |= skgpu::VulkanAlloc::kNoncoherent_Flag;
}
if (VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT & mem_flags) {
flags |= skgpu::VulkanAlloc::kLazilyAllocated_Flag;
}
alloc->fMemory = vma_info.deviceMemory;
alloc->fOffset = vma_info.offset;
alloc->fSize = vma_info.size;
alloc->fFlags = flags;
alloc->fBackendMemory = memory;
}
VkResult SkiaVulkanMemoryAllocator::mapMemory(
const skgpu::VulkanBackendMemory& memory,
void** data) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::mapMemory");
const VmaAllocation allocation =
reinterpret_cast<const VmaAllocation>(memory);
return vma::MapMemory(allocator_, allocation, data);
}
void SkiaVulkanMemoryAllocator::unmapMemory(
const skgpu::VulkanBackendMemory& memory) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::unmapMemory");
const VmaAllocation allocation =
reinterpret_cast<const VmaAllocation>(memory);
vma::UnmapMemory(allocator_, allocation);
}
VkResult SkiaVulkanMemoryAllocator::flushMemory(
const skgpu::VulkanBackendMemory& memory,
VkDeviceSize offset,
VkDeviceSize size) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::flushMappedMemory");
const VmaAllocation allocation =
reinterpret_cast<const VmaAllocation>(memory);
return vma::FlushAllocation(allocator_, allocation, offset, size);
}
VkResult SkiaVulkanMemoryAllocator::invalidateMemory(
const skgpu::VulkanBackendMemory& memory,
VkDeviceSize offset,
VkDeviceSize size) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("gpu.vulkan.vma"),
"SkiaVulkanMemoryAllocator::invalidateMappedMemory");
const VmaAllocation allocation =
reinterpret_cast<const VmaAllocation>(memory);
return vma::InvalidateAllocation(allocator_, allocation, offset, size);
}
std::pair<uint64_t, uint64_t>
SkiaVulkanMemoryAllocator::totalAllocatedAndUsedMemory() const {
uint64_t total_allocated_memory = 0, total_used_memory = 0;
std::array<VmaBudget, VK_MAX_MEMORY_HEAPS> budget;
vma::GetBudget(allocator_, budget.data());
const VkPhysicalDeviceMemoryProperties* physical_device_memory_properties;
vmaGetMemoryProperties(allocator_, &physical_device_memory_properties);
for (uint32_t i = 0; i < physical_device_memory_properties->memoryHeapCount;
++i) {
total_allocated_memory += budget[i].statistics.blockBytes;
total_used_memory += budget[i].statistics.allocationBytes;
}
DCHECK_LE(total_used_memory, total_allocated_memory);
return {total_allocated_memory, total_used_memory};
}
} // namespace gpu
|