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 | |
| 8 | #include "GrVkMemory.h" |
| 9 | |
| 10 | #include "GrVkGpu.h" |
| 11 | #include "GrVkUtil.h" |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 12 | #include "vk/GrVkMemoryAllocator.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 13 | |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -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 | 331c266 | 2018-05-30 14:51:53 -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 | 331c266 | 2018-05-30 14:51:53 -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 | 331c266 | 2018-05-30 14:51:53 -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 | 331c266 | 2018-05-30 14:51:53 -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 | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 44 | if (!allocator->allocateMemoryForBuffer(buffer, usage, AllocationPropertyFlags::kNone, |
| 45 | &memory)) { |
| 46 | return false; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 47 | } |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 48 | allocator->getAllocInfo(memory, alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 49 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 50 | // Bind buffer |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 51 | VkResult err = GR_VK_CALL(gpu->vkInterface(), BindBufferMemory(gpu->device(), buffer, |
| 52 | alloc->fMemory, |
| 53 | alloc->fOffset)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 54 | if (err) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 55 | FreeBufferMemory(gpu, type, *alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 56 | return false; |
| 57 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 58 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 62 | void GrVkMemory::FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, |
| 63 | const GrVkAlloc& alloc) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 64 | if (alloc.fBackendMemory) { |
| 65 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 66 | allocator->freeMemory(alloc.fBackendMemory); |
| 67 | } else { |
| 68 | GR_VK_CALL(gpu->vkInterface(), FreeMemory(gpu->device(), alloc.fMemory, nullptr)); |
| 69 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 70 | } |
| 71 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 72 | const VkDeviceSize kMaxSmallImageSize = 16 * 1024; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 73 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 74 | bool GrVkMemory::AllocAndBindImageMemory(const GrVkGpu* gpu, |
| 75 | VkImage image, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 76 | bool linearTiling, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 77 | GrVkAlloc* alloc) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 78 | SkASSERT(!linearTiling); |
| 79 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 80 | GrVkBackendMemory memory = 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 81 | |
| 82 | VkMemoryRequirements memReqs; |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 83 | GR_VK_CALL(gpu->vkInterface(), GetImageMemoryRequirements(gpu->device(), image, &memReqs)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 84 | |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 85 | AllocationPropertyFlags propFlags; |
| 86 | if (memReqs.size <= kMaxSmallImageSize) { |
| 87 | propFlags = AllocationPropertyFlags::kNone; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 88 | } else { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 89 | propFlags = AllocationPropertyFlags::kDedicatedAllocation; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 92 | if (!allocator->allocateMemoryForImage(image, AllocationPropertyFlags::kDedicatedAllocation, |
| 93 | &memory)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | return false; |
| 95 | } |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 96 | allocator->getAllocInfo(memory, alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 97 | |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 98 | // Bind buffer |
| 99 | VkResult err = GR_VK_CALL(gpu->vkInterface(), BindImageMemory(gpu->device(), image, |
| 100 | alloc->fMemory, alloc->fOffset)); |
| 101 | if (err) { |
| 102 | FreeImageMemory(gpu, linearTiling, *alloc); |
| 103 | return false; |
| 104 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 105 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 109 | void GrVkMemory::FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, |
| 110 | const GrVkAlloc& alloc) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 111 | if (alloc.fBackendMemory) { |
| 112 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 113 | allocator->freeMemory(alloc.fBackendMemory); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 114 | } else { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 115 | GR_VK_CALL(gpu->vkInterface(), FreeMemory(gpu->device(), alloc.fMemory, nullptr)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 116 | } |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 119 | void* GrVkMemory::MapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) { |
| 120 | SkASSERT(GrVkAlloc::kMappable_Flag & alloc.fFlags); |
| 121 | #ifdef SK_DEBUG |
| 122 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
| 123 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 124 | SkASSERT(0 == (alloc.fOffset & (alignment-1))); |
| 125 | SkASSERT(0 == (alloc.fSize & (alignment-1))); |
| 126 | } |
| 127 | #endif |
| 128 | if (alloc.fBackendMemory) { |
| 129 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 130 | return allocator->mapMemory(alloc.fBackendMemory); |
| 131 | } |
| 132 | |
| 133 | void* mapPtr; |
| 134 | VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(), alloc.fMemory, |
| 135 | alloc.fOffset, |
| 136 | alloc.fSize, 0, &mapPtr)); |
| 137 | if (err) { |
| 138 | mapPtr = nullptr; |
| 139 | } |
| 140 | return mapPtr; |
| 141 | } |
| 142 | |
| 143 | void GrVkMemory::UnmapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc) { |
| 144 | if (alloc.fBackendMemory) { |
| 145 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 146 | allocator->unmapMemory(alloc.fBackendMemory); |
| 147 | } else { |
| 148 | GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void GrVkMemory::GetNonCoherentMappedMemoryRange(const GrVkAlloc& alloc, VkDeviceSize offset, |
| 153 | VkDeviceSize size, VkDeviceSize alignment, |
| 154 | VkMappedMemoryRange* range) { |
| 155 | SkASSERT(alloc.fFlags & GrVkAlloc::kNoncoherent_Flag); |
| 156 | offset = offset + alloc.fOffset; |
| 157 | VkDeviceSize offsetDiff = offset & (alignment -1); |
| 158 | offset = offset - offsetDiff; |
| 159 | size = (size + alignment - 1) & ~(alignment - 1); |
| 160 | #ifdef SK_DEBUG |
| 161 | SkASSERT(offset >= alloc.fOffset); |
| 162 | SkASSERT(offset + size <= alloc.fOffset + alloc.fSize); |
| 163 | SkASSERT(0 == (offset & (alignment-1))); |
| 164 | SkASSERT(size > 0); |
| 165 | SkASSERT(0 == (size & (alignment-1))); |
| 166 | #endif |
| 167 | |
| 168 | memset(range, 0, sizeof(VkMappedMemoryRange)); |
| 169 | range->sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; |
| 170 | range->memory = alloc.fMemory; |
| 171 | range->offset = offset; |
| 172 | range->size = size; |
| 173 | } |
| 174 | |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 175 | void GrVkMemory::FlushMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc, VkDeviceSize offset, |
| 176 | VkDeviceSize size) { |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 177 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 178 | SkASSERT(offset == 0); |
| 179 | SkASSERT(size <= alloc.fSize); |
| 180 | if (alloc.fBackendMemory) { |
| 181 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 182 | allocator->flushMappedMemory(alloc.fBackendMemory, offset, size); |
| 183 | } else { |
| 184 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 185 | VkMappedMemoryRange mappedMemoryRange; |
| 186 | GrVkMemory::GetNonCoherentMappedMemoryRange(alloc, offset, size, alignment, |
| 187 | &mappedMemoryRange); |
| 188 | GR_VK_CALL(gpu->vkInterface(), FlushMappedMemoryRanges(gpu->device(), 1, |
| 189 | &mappedMemoryRange)); |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 190 | } |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 194 | void GrVkMemory::InvalidateMappedAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc, |
| 195 | VkDeviceSize offset, VkDeviceSize size) { |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 196 | if (alloc.fFlags & GrVkAlloc::kNoncoherent_Flag) { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 197 | SkASSERT(offset == 0); |
| 198 | SkASSERT(size <= alloc.fSize); |
| 199 | if (alloc.fBackendMemory) { |
| 200 | GrVkMemoryAllocator* allocator = gpu->memoryAllocator(); |
| 201 | allocator->invalidateMappedMemory(alloc.fBackendMemory, offset, size); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 202 | } else { |
Greg Daniel | 331c266 | 2018-05-30 14:51:53 -0400 | [diff] [blame^] | 203 | VkDeviceSize alignment = gpu->physicalDeviceProperties().limits.nonCoherentAtomSize; |
| 204 | VkMappedMemoryRange mappedMemoryRange; |
| 205 | GrVkMemory::GetNonCoherentMappedMemoryRange(alloc, offset, size, alignment, |
| 206 | &mappedMemoryRange); |
| 207 | GR_VK_CALL(gpu->vkInterface(), InvalidateMappedMemoryRanges(gpu->device(), 1, |
| 208 | &mappedMemoryRange)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 209 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 210 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 211 | } |
| 212 | |