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 | #ifndef GrVkMemory_DEFINED |
| 9 | #define GrVkMemory_DEFINED |
| 10 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 11 | #include "GrVkBuffer.h" |
| 12 | #include "SkTArray.h" |
| 13 | #include "SkTLList.h" |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 14 | #include "vk/GrVkDefines.h" |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 15 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 16 | |
| 17 | class GrVkGpu; |
| 18 | |
| 19 | namespace GrVkMemory { |
| 20 | /** |
| 21 | * Allocates vulkan device memory and binds it to the gpu's device for the given object. |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 22 | * Returns true if allocation succeeded. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 23 | */ |
| 24 | bool AllocAndBindBufferMemory(const GrVkGpu* gpu, |
| 25 | VkBuffer buffer, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 26 | GrVkBuffer::Type type, |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 27 | bool dynamic, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 28 | GrVkAlloc* alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 29 | void FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, const GrVkAlloc& alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 30 | |
| 31 | bool AllocAndBindImageMemory(const GrVkGpu* gpu, |
| 32 | VkImage image, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 33 | bool linearTiling, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 34 | GrVkAlloc* alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 35 | void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc& alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 36 | |
| 37 | VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); |
| 38 | |
| 39 | VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
| 40 | } |
| 41 | |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 42 | class GrVkFreeListAlloc { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 43 | public: |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 44 | GrVkFreeListAlloc(VkDeviceSize size, VkDeviceSize alignment) |
| 45 | : fSize(size) |
| 46 | , fAlignment(alignment) |
| 47 | , fFreeSize(size) |
| 48 | , fLargestBlockSize(size) |
| 49 | , fLargestBlockOffset(0) { |
| 50 | Block* block = fFreeList.addToTail(); |
| 51 | block->fOffset = 0; |
| 52 | block->fSize = fSize; |
| 53 | } |
| 54 | ~GrVkFreeListAlloc() { |
| 55 | this->reset(); |
| 56 | } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 57 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 58 | VkDeviceSize size() const { return fSize; } |
| 59 | VkDeviceSize alignment() const { return fAlignment; } |
| 60 | VkDeviceSize freeSize() const { return fFreeSize; } |
| 61 | VkDeviceSize largestBlockSize() const { return fLargestBlockSize; } |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 62 | |
| 63 | bool unallocated() const { return fSize == fFreeSize; } |
| 64 | |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 65 | protected: |
| 66 | bool alloc(VkDeviceSize requestedSize, VkDeviceSize* allocOffset, VkDeviceSize* allocSize); |
| 67 | void free(VkDeviceSize allocOffset, VkDeviceSize allocSize); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 68 | |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 69 | void reset() { |
| 70 | fSize = 0; |
| 71 | fAlignment = 0; |
| 72 | fFreeSize = 0; |
| 73 | fLargestBlockSize = 0; |
| 74 | fFreeList.reset(); |
| 75 | } |
| 76 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 77 | struct Block { |
| 78 | VkDeviceSize fOffset; |
| 79 | VkDeviceSize fSize; |
| 80 | }; |
| 81 | typedef SkTLList<Block, 16> FreeList; |
| 82 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 83 | VkDeviceSize fSize; |
| 84 | VkDeviceSize fAlignment; |
| 85 | VkDeviceSize fFreeSize; |
| 86 | VkDeviceSize fLargestBlockSize; |
| 87 | VkDeviceSize fLargestBlockOffset; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 88 | FreeList fFreeList; |
| 89 | }; |
| 90 | |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 91 | class GrVkSubHeap : public GrVkFreeListAlloc { |
| 92 | public: |
| 93 | GrVkSubHeap(const GrVkGpu* gpu, uint32_t memoryTypeIndex, |
| 94 | VkDeviceSize size, VkDeviceSize alignment); |
| 95 | ~GrVkSubHeap(); |
| 96 | |
| 97 | uint32_t memoryTypeIndex() const { return fMemoryTypeIndex; } |
| 98 | VkDeviceMemory memory() { return fAlloc; } |
| 99 | |
| 100 | bool alloc(VkDeviceSize requestedSize, GrVkAlloc* alloc); |
| 101 | void free(const GrVkAlloc& alloc); |
| 102 | |
| 103 | private: |
| 104 | const GrVkGpu* fGpu; |
| 105 | uint32_t fMemoryTypeIndex; |
| 106 | VkDeviceMemory fAlloc; |
| 107 | |
| 108 | typedef GrVkFreeListAlloc INHERITED; |
| 109 | }; |
| 110 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 111 | class GrVkHeap { |
| 112 | public: |
| 113 | enum Strategy { |
| 114 | kSubAlloc_Strategy, // alloc large subheaps and suballoc within them |
| 115 | kSingleAlloc_Strategy // alloc/recycle an individual subheap per object |
| 116 | }; |
| 117 | |
| 118 | GrVkHeap(const GrVkGpu* gpu, Strategy strategy, VkDeviceSize subHeapSize) |
| 119 | : fGpu(gpu) |
| 120 | , fSubHeapSize(subHeapSize) |
| 121 | , fAllocSize(0) |
| 122 | , fUsedSize(0) { |
| 123 | if (strategy == kSubAlloc_Strategy) { |
| 124 | fAllocFunc = &GrVkHeap::subAlloc; |
| 125 | } else { |
| 126 | fAllocFunc = &GrVkHeap::singleAlloc; |
| 127 | } |
| 128 | } |
| 129 | |
jvanverth | 82356cc | 2016-07-07 07:16:42 -0700 | [diff] [blame] | 130 | ~GrVkHeap() {} |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 131 | |
jvanverth | d6f8034 | 2016-06-16 04:42:30 -0700 | [diff] [blame] | 132 | VkDeviceSize allocSize() const { return fAllocSize; } |
| 133 | VkDeviceSize usedSize() const { return fUsedSize; } |
| 134 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 135 | bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeIndex, |
| 136 | GrVkAlloc* alloc) { |
jvanverth | 6dc3af4 | 2016-06-16 14:05:09 -0700 | [diff] [blame] | 137 | SkASSERT(size > 0); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 138 | return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); |
| 139 | } |
| 140 | bool free(const GrVkAlloc& alloc); |
| 141 | |
| 142 | private: |
| 143 | typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignment, |
| 144 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 145 | |
| 146 | bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 147 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 148 | bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 149 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 150 | |
| 151 | const GrVkGpu* fGpu; |
| 152 | VkDeviceSize fSubHeapSize; |
| 153 | VkDeviceSize fAllocSize; |
| 154 | VkDeviceSize fUsedSize; |
| 155 | AllocFunc fAllocFunc; |
| 156 | SkTArray<SkAutoTDelete<GrVkSubHeap>> fSubHeaps; |
| 157 | }; |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 158 | #endif |