OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrVkMemory_DEFINED | 8 #ifndef GrVkMemory_DEFINED |
9 #define GrVkMemory_DEFINED | 9 #define GrVkMemory_DEFINED |
10 | 10 |
| 11 #include "GrVkBuffer.h" |
| 12 #include "SkTArray.h" |
| 13 #include "SkTLList.h" |
11 #include "vk/GrVkDefines.h" | 14 #include "vk/GrVkDefines.h" |
12 #include "vk/GrVkTypes.h" | 15 #include "vk/GrVkTypes.h" |
13 | 16 |
14 class GrVkGpu; | 17 class GrVkGpu; |
15 | 18 |
16 namespace GrVkMemory { | 19 namespace GrVkMemory { |
17 /** | 20 /** |
18 * Allocates vulkan device memory and binds it to the gpu's device for the gi
ven object. | 21 * Allocates vulkan device memory and binds it to the gpu's device for the gi
ven object. |
19 * Returns true of allocation succeeded. | 22 * Returns true if allocation succeeded. |
20 */ | 23 */ |
21 bool AllocAndBindBufferMemory(const GrVkGpu* gpu, | 24 bool AllocAndBindBufferMemory(const GrVkGpu* gpu, |
22 VkBuffer buffer, | 25 VkBuffer buffer, |
23 const VkMemoryPropertyFlags flags, | 26 GrVkBuffer::Type type, |
24 GrVkAlloc* alloc); | 27 GrVkAlloc* alloc); |
25 void FreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc); | 28 void FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, const GrVkA
lloc& alloc); |
26 | 29 |
27 bool AllocAndBindImageMemory(const GrVkGpu* gpu, | 30 bool AllocAndBindImageMemory(const GrVkGpu* gpu, |
28 VkImage image, | 31 VkImage image, |
29 const VkMemoryPropertyFlags flags, | 32 bool linearTiling, |
30 GrVkAlloc* alloc); | 33 GrVkAlloc* alloc); |
31 void FreeImageMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc); | 34 void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc&
alloc); |
32 | 35 |
33 VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); | 36 VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); |
34 | 37 |
35 VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); | 38 VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
36 } | 39 } |
37 | 40 |
| 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 th
em |
| 81 kSingleAlloc_Strategy // alloc/recycle an individual subheap per obj
ect |
| 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 |
| 98 bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeInd
ex, |
| 99 GrVkAlloc* alloc) { |
| 100 return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); |
| 101 } |
| 102 bool free(const GrVkAlloc& alloc); |
| 103 |
| 104 private: |
| 105 typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignmen
t, |
| 106 uint32_t memoryTypeIndex, GrVkAlloc* all
oc); |
| 107 |
| 108 bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 109 uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 110 bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, |
| 111 uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
| 112 |
| 113 const GrVkGpu* fGpu; |
| 114 VkDeviceSize fSubHeapSize; |
| 115 VkDeviceSize fAllocSize; |
| 116 VkDeviceSize fUsedSize; |
| 117 AllocFunc fAllocFunc; |
| 118 SkTArray<SkAutoTDelete<GrVkSubHeap>> fSubHeaps; |
| 119 }; |
38 #endif | 120 #endif |
OLD | NEW |