Debian Codegraph @

tools/gpu/vk/VkTestMemoryAllocator.cpp

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