Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkMemory.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/vk/GrVkMemoryAllocator.h" |
| 11 | #include "src/gpu/vk/GrVkGpu.h" |
| 12 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 14 | using AllocationPropertyFlags = GrVkMemoryAllocator::AllocationPropertyFlags; |
| 15 | using BufferUsage = GrVkMemoryAllocator::BufferUsage; |
jvanverth | 68c3d30 | 2016-09-23 10:30:04 -0700 | [diff] [blame] | 16 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 17 | static BufferUsage get_buffer_usage(GrVkBuffer::Type type, bool dynamic) { |
| 18 | switch (type) { |
| 19 | case GrVkBuffer::kVertex_Type: // fall through |
| 20 | case GrVkBuffer::kIndex_Type: // fall through |
| 21 | case GrVkBuffer::kTexel_Type: |
| 22 | return dynamic ? BufferUsage::kCpuWritesGpuReads : BufferUsage::kGpuOnly; |
| 23 | case GrVkBuffer::kUniform_Type: |
| 24 | SkASSERT(dynamic); |
| 25 | return BufferUsage::kCpuWritesGpuReads; |
| 26 | case GrVkBuffer::kCopyRead_Type: // fall through |
| 27 | case GrVkBuffer::kCopyWrite_Type: |
| 28 | return BufferUsage::kCpuOnly; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 29 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 30 | SK_ABORT("Invalid GrVkBuffer::Type"); |
| 31 | return BufferUsage::kCpuOnly; // Just returning an arbitrary value. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | bool GrVkMemory::AllocAndBindBufferMemory(const GrVkGpu* gpu, |
| 35 | VkBuffer buffer, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 36 | GrVkBuffer::Type type, |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 37 | bool dynamic, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 38 | GrVkAlloc* alloc) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 39 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 40 | GrVkBackendMemory memory = 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 42 | GrVkMemoryAllocator::BufferUsage usage = get_buffer_usage(type, dynamic); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 43 | |
Greg Daniel | 8683037 | 2018-06-01 13:35:16 -0400 | [diff] [blame] | 44 | AllocationPropertyFlags propFlags; |
| 45 | if (usage == GrVkMemoryAllocator::BufferUsage::kCpuWritesGpuReads) { |
| 46 | // In general it is always fine (and often better) to keep buffers always mapped. |
| 47 | // TODO: According to AMDs guide for the VulkanMemoryAllocator they suggest there are two |
| 48 | // cases when keeping it mapped can hurt. The first is when running on Win7 or Win8 (Win 10 |
| 49 | // is fine). In general, by the time Vulkan ships it is probably less likely to be running |
| 50 | // on non Win10 or newer machines. The second use case is if running on an AMD card and you |
| 51 | // are using the special GPU local and host mappable memory. However, in general we don't |
| 52 | // pick this memory as we've found it slower than using the cached host visible memory. In |
| 53 | // the future if we find the need to special case either of these two issues we can add |
| 54 | // checks for them here. |
| 55 | propFlags = AllocationPropertyFlags::kPersistentlyMapped; |
| 56 | } else { |
| 57 | propFlags = AllocationPropertyFlags::kNone; |
| 58 | } |
| 59 | |
| 60 | if (!allocator->allocateMemoryForBuffer(buffer, usage, propFlags, &memory)) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 61 | return false; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 62 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 63 | allocator->getAllocInfo(memory, alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 64 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 65 | // Bind buffer |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 66 | VkResult err = GR_VK_CALL(gpu->vkInterface(), BindBufferMemory(gpu->device(), buffer, |
| 67 | alloc->fMemory, |
| 68 | alloc->fOffset)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 69 | if (err) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 70 | FreeBufferMemory(gpu, type, *alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | return false; |
| 72 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 73 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 77 | void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, |
| 78 | const GrVkAlloc& alloc) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 79 | if (alloc.fBackendMemory) { |
| 80 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 81 | allocator->freeMemory(alloc.fBackendMemory); |
| 82 | } else { |
| 83 | GR_VK_CALL(gpu->vkInterface(), FreeMemory(gpu->device(), alloc.fMemory, nullptr)); |
| 84 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Greg Daniel | 865dc56 | 2019-04-19 14:48:04 -0400 | [diff] [blame] | 87 | const VkDeviceSize kMaxSmallImageSize = 256 * 1024; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 88 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | bool GrVkMemory::AllocAndBindImageMemory(const GrVkGpu* gpu, |
| 90 | VkImage image, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 91 | bool linearTiling, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 92 | GrVkAlloc* alloc) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 93 | SkASSERT(!linearTiling); |
| 94 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 95 | GrVkBackendMemory memory = 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | |
| 97 | VkMemoryRequirements memReqs; |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 98 | GR_VK_CALL(gpu->vkInterface(), GetImageMemoryRequirements(gpu->device(), image, &memReqs)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 99 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 100 | AllocationPropertyFlags propFlags; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame^] | 101 | if (gpu->protectedContext()) { |
| 102 | propFlags = AllocationPropertyFlags::kProtected; |
| 103 | } else if (memReqs.size > kMaxSmallImageSize || |
| 104 | gpu->vkCaps().shouldAlwaysUseDedicatedImageMemory()) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 105 | propFlags = AllocationPropertyFlags::kDedicatedAllocation; |
Greg Daniel | ddc0c60 | 2018-06-18 11:26:30 -0400 | [diff] [blame] | 106 | } else { |
| 107 | propFlags = AllocationPropertyFlags::kNone; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Greg Daniel | 2f6e069 | 2018-06-15 13:07:20 -0400 | [diff] [blame] | 110 | if (!allocator->allocateMemoryForImage(image, propFlags, &memory)) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame] | 111 | return false; |
| 112 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 113 | allocator->getAllocInfo(memory, alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 114 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 115 | // Bind buffer |
| 116 | VkResult err = GR_VK_CALL(gpu->vkInterface(), BindImageMemory(gpu->device(), image, |
| 117 | alloc->fMemory, alloc->fOffset)); |
| 118 | if (err) { |
| 119 | FreeImageMemory(gpu, linearTiling, *alloc); |
| 120 | return false; |
| 121 | } |
Greg Daniel | a9d3dae | 2018-05-30 22:59:03 +0000 | [diff] [blame] | 122 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 126 | void GrVkMemory::FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, |
| 127 | const GrVkAlloc& alloc) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 128 | if (alloc.fBackendMemory) { |
| 129 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 130 | allocator->freeMemory(alloc.fBackendMemory); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 131 | } else { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 132 | GR_VK_CALL(gpu->vkInterface(), FreeMemory(gpu->device(), alloc.fMemory, nullptr)); |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 136 | void* GrVkMemory::MapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) { |
| 137 | SkASSERT(GrVkAlloc::kMappable_Flag & alloc.fFlags); |
| 138 | #ifdef SK_DEBUG |
| 139 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
| 140 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 141 | SkASSERT(0 == (alloc.fOffset & (alignment-1))); |
| 142 | SkASSERT(0 == (alloc.fSize & (alignment-1))); |
| 143 | } |
| 144 | #endif |
| 145 | if (alloc.fBackendMemory) { |
| 146 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 147 | return allocator->mapMemory(alloc.fBackendMemory); |
| 148 | } |
| 149 | |
| 150 | void* mapPtr; |
| 151 | VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(), alloc.fMemory, |
| 152 | alloc.fOffset, |
| 153 | alloc.fSize, 0, &mapPtr)); |
| 154 | if (err) { |
| 155 | mapPtr = nullptr; |
| 156 | } |
| 157 | return mapPtr; |
| 158 | } |
| 159 | |
| 160 | void GrVkMemory::UnmapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) { |
| 161 | if (alloc.fBackendMemory) { |
| 162 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 163 | allocator->unmapMemory(alloc.fBackendMemory); |
| 164 | } else { |
| 165 | GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory)); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void GrVkMemory::GetNonCoherentMappedMemoryRange(const GrVkAlloc& alloc, VkDeviceSize offset, |
| 170 | VkDeviceSize size, VkDeviceSize alignment, |
| 171 | VkMappedMemoryRange* range) { |
| 172 | SkASSERT(alloc.fFlags & GrVkAlloc::kNoncoherent_Flag); |
| 173 | offset = offset + alloc.fOffset; |
| 174 | VkDeviceSize offsetDiff = offset & (alignment -1); |
| 175 | offset = offset - offsetDiff; |
| 176 | size = (size + alignment - 1) & ~(alignment - 1); |
| 177 | #ifdef SK_DEBUG |
| 178 | SkASSERT(offset >= alloc.fOffset); |
| 179 | SkASSERT(offset + size <= alloc.fOffset + alloc.fSize); |
| 180 | SkASSERT(0 == (offset & (alignment-1))); |
| 181 | SkASSERT(size > 0); |
| 182 | SkASSERT(0 == (size & (alignment-1))); |
| 183 | #endif |
| 184 | |
| 185 | memset(range, 0, sizeof(VkMappedMemoryRange)); |
| 186 | range->sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; |
| 187 | range->memory = alloc.fMemory; |
| 188 | range->offset = offset; |
| 189 | range->size = size; |
| 190 | } |
| 191 | |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 192 | void GrVkMemory::FlushMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc, VkDeviceSize offset, |
| 193 | VkDeviceSize size) { |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 194 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 195 | SkASSERT(offset == 0); |
| 196 | SkASSERT(size <= alloc.fSize); |
| 197 | if (alloc.fBackendMemory) { |
| 198 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 199 | allocator->flushMappedMemory(alloc.fBackendMemory, offset, size); |
| 200 | } else { |
| 201 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 202 | VkMappedMemoryRange mappedMemoryRange; |
| 203 | GrVkMemory::GetNonCoherentMappedMemoryRange(alloc, offset, size, alignment, |
| 204 | &mappedMemoryRange); |
| 205 | GR_VK_CALL(gpu->vkInterface(), FlushMappedMemoryRanges(gpu->device(), 1, |
| 206 | &mappedMemoryRange)); |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 207 | } |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 211 | void GrVkMemory::InvalidateMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc, |
| 212 | VkDeviceSize offset, VkDeviceSize size) { |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 213 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 214 | SkASSERT(offset == 0); |
| 215 | SkASSERT(size <= alloc.fSize); |
| 216 | if (alloc.fBackendMemory) { |
| 217 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 218 | allocator->invalidateMappedMemory(alloc.fBackendMemory, offset, size); |
Greg Daniel | a9d3dae | 2018-05-30 22:59:03 +0000 | [diff] [blame] | 219 | } else { |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 220 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 221 | VkMappedMemoryRange mappedMemoryRange; |
| 222 | GrVkMemory::GetNonCoherentMappedMemoryRange(alloc, offset, size, alignment, |
| 223 | &mappedMemoryRange); |
| 224 | GR_VK_CALL(gpu->vkInterface(), InvalidateMappedMemoryRanges(gpu->device(), 1, |
| 225 | &mappedMemoryRange)); |
Greg Daniel | a9d3dae | 2018-05-30 22:59:03 +0000 | [diff] [blame] | 226 | } |
Greg Daniel | a9d3dae | 2018-05-30 22:59:03 +0000 | [diff] [blame] | 227 | } |
Greg Daniel | a9d3dae | 2018-05-30 22:59:03 +0000 | [diff] [blame] | 228 | } |
| 229 | |