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 | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 27 | GrVkAlloc* alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 28 | void FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, const GrVkAlloc& alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 29 | |
| 30 | bool AllocAndBindImageMemory(const GrVkGpu* gpu, |
| 31 | VkImage image, |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 32 | bool linearTiling, |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 33 | GrVkAlloc* alloc); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 34 | void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc& alloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 35 | |
| 36 | VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); |
| 37 | |
| 38 | VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
| 39 | } |
| 40 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 41 | class GrVkSubHeap { |
| 42 | public: |
| 43 | GrVkSubHeap(const GrVkGpu* gpu, uint32_t memoryTypeIndex, |
| 44 | VkDeviceSize size, VkDeviceSize alignment); |
| 45 | ~GrVkSubHeap(); |
| 46 | |
| 47 | uint32_t memoryTypeIndex() const { return fMemoryTypeIndex; } |
| 48 | VkDeviceSize size() const { return fSize; } |
| 49 | VkDeviceSize alignment() const { return fAlignment; } |
| 50 | VkDeviceSize freeSize() const { return fFreeSize; } |
| 51 | VkDeviceSize largestBlockSize() const { return fLargestBlockSize; } |
| 52 | VkDeviceMemory memory() { return fAlloc; } |
| 53 | |
| 54 | bool unallocated() const { return fSize == fFreeSize; } |
| 55 | |
| 56 | bool alloc(VkDeviceSize size, GrVkAlloc* alloc); |
| 57 | void free(const GrVkAlloc& alloc); |
| 58 | |
| 59 | private: |
| 60 | struct Block { |
| 61 | VkDeviceSize fOffset; |
| 62 | VkDeviceSize fSize; |
| 63 | }; |
| 64 | typedef SkTLList<Block, 16> FreeList; |
| 65 | |
| 66 | const GrVkGpu* fGpu; |
| 67 | uint32_t fMemoryTypeIndex; |
| 68 | VkDeviceSize fSize; |
| 69 | VkDeviceSize fAlignment; |
| 70 | VkDeviceSize fFreeSize; |
| 71 | VkDeviceSize fLargestBlockSize; |
| 72 | VkDeviceSize fLargestBlockOffset; |
| 73 | VkDeviceMemory fAlloc; |
| 74 | FreeList fFreeList; |
| 75 | }; |
| 76 | |
| 77 | class GrVkHeap { |
| 78 | public: |
| 79 | enum Strategy { |
| 80 | kSubAlloc_Strategy, // alloc large subheaps and suballoc within them |
| 81 | kSingleAlloc_Strategy // alloc/recycle an individual subheap per object |
| 82 | }; |
| 83 | |
| 84 | GrVkHeap(const GrVkGpu* gpu, Strategy strategy, VkDeviceSize subHeapSize) |
| 85 | : fGpu(gpu) |
| 86 | , fSubHeapSize(subHeapSize) |
| 87 | , fAllocSize(0) |
| 88 | , fUsedSize(0) { |
| 89 | if (strategy == kSubAlloc_Strategy) { |
| 90 | fAllocFunc = &GrVkHeap::subAlloc; |
| 91 | } else { |
| 92 | fAllocFunc = &GrVkHeap::singleAlloc; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | ~GrVkHeap(); |
| 97 | |
jvanverth | d6f8034 | 2016-06-16 04:42:30 -0700 | [diff] [blame] | 98 | VkDeviceSize allocSize() const { return fAllocSize; } |
| 99 | VkDeviceSize usedSize() const { return fUsedSize; } |
| 100 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 101 | bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeIndex, |
| 102 | GrVkAlloc* alloc) { |
jvanverth | 6dc3af4 | 2016-06-16 14:05:09 -0700 | [diff] [blame^] | 103 | SkASSERT(size > 0); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 104 | return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); |
| 105 | } |
| 106 | bool free(const GrVkAlloc& alloc); |
| 107 | |
| 108 | private: |
| 109 | typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignment, |
| 110 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 111 | |
| 112 | bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 113 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 114 | bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 115 | uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 116 | |
| 117 | const GrVkGpu* fGpu; |
| 118 | VkDeviceSize fSubHeapSize; |
| 119 | VkDeviceSize fAllocSize; |
| 120 | VkDeviceSize fUsedSize; |
| 121 | AllocFunc fAllocFunc; |
| 122 | SkTArray<SkAutoTDelete<GrVkSubHeap>> fSubHeaps; |
| 123 | }; |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 124 | #endif |