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 |
(...skipping 21 matching lines...) Expand all Loading... |
32 VkImage image, | 32 VkImage image, |
33 bool linearTiling, | 33 bool linearTiling, |
34 GrVkAlloc* alloc); | 34 GrVkAlloc* alloc); |
35 void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc&
alloc); | 35 void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc&
alloc); |
36 | 36 |
37 VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); | 37 VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout); |
38 | 38 |
39 VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); | 39 VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
40 } | 40 } |
41 | 41 |
42 class GrVkSubHeap { | 42 class GrVkFreeListAlloc { |
43 public: | 43 public: |
44 GrVkSubHeap(const GrVkGpu* gpu, uint32_t memoryTypeIndex, | 44 GrVkFreeListAlloc(VkDeviceSize size, VkDeviceSize alignment) |
45 VkDeviceSize size, VkDeviceSize alignment); | 45 : fSize(size) |
46 ~GrVkSubHeap(); | 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 } |
47 | 57 |
48 uint32_t memoryTypeIndex() const { return fMemoryTypeIndex; } | |
49 VkDeviceSize size() const { return fSize; } | 58 VkDeviceSize size() const { return fSize; } |
50 VkDeviceSize alignment() const { return fAlignment; } | 59 VkDeviceSize alignment() const { return fAlignment; } |
51 VkDeviceSize freeSize() const { return fFreeSize; } | 60 VkDeviceSize freeSize() const { return fFreeSize; } |
52 VkDeviceSize largestBlockSize() const { return fLargestBlockSize; } | 61 VkDeviceSize largestBlockSize() const { return fLargestBlockSize; } |
53 VkDeviceMemory memory() { return fAlloc; } | |
54 | 62 |
55 bool unallocated() const { return fSize == fFreeSize; } | 63 bool unallocated() const { return fSize == fFreeSize; } |
56 | 64 |
57 bool alloc(VkDeviceSize size, GrVkAlloc* alloc); | 65 protected: |
58 void free(const GrVkAlloc& alloc); | 66 bool alloc(VkDeviceSize requestedSize, VkDeviceSize* allocOffset, VkDeviceSi
ze* allocSize); |
| 67 void free(VkDeviceSize allocOffset, VkDeviceSize allocSize); |
59 | 68 |
60 private: | 69 void reset() { |
| 70 fSize = 0; |
| 71 fAlignment = 0; |
| 72 fFreeSize = 0; |
| 73 fLargestBlockSize = 0; |
| 74 fFreeList.reset(); |
| 75 } |
| 76 |
61 struct Block { | 77 struct Block { |
62 VkDeviceSize fOffset; | 78 VkDeviceSize fOffset; |
63 VkDeviceSize fSize; | 79 VkDeviceSize fSize; |
64 }; | 80 }; |
65 typedef SkTLList<Block, 16> FreeList; | 81 typedef SkTLList<Block, 16> FreeList; |
66 | 82 |
67 const GrVkGpu* fGpu; | |
68 uint32_t fMemoryTypeIndex; | |
69 VkDeviceSize fSize; | 83 VkDeviceSize fSize; |
70 VkDeviceSize fAlignment; | 84 VkDeviceSize fAlignment; |
71 VkDeviceSize fFreeSize; | 85 VkDeviceSize fFreeSize; |
72 VkDeviceSize fLargestBlockSize; | 86 VkDeviceSize fLargestBlockSize; |
73 VkDeviceSize fLargestBlockOffset; | 87 VkDeviceSize fLargestBlockOffset; |
| 88 FreeList fFreeList; |
| 89 }; |
| 90 |
| 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; |
74 VkDeviceMemory fAlloc; | 106 VkDeviceMemory fAlloc; |
75 FreeList fFreeList; | 107 |
| 108 typedef GrVkFreeListAlloc INHERITED; |
76 }; | 109 }; |
77 | 110 |
78 class GrVkHeap { | 111 class GrVkHeap { |
79 public: | 112 public: |
80 enum Strategy { | 113 enum Strategy { |
81 kSubAlloc_Strategy, // alloc large subheaps and suballoc within th
em | 114 kSubAlloc_Strategy, // alloc large subheaps and suballoc within th
em |
82 kSingleAlloc_Strategy // alloc/recycle an individual subheap per obj
ect | 115 kSingleAlloc_Strategy // alloc/recycle an individual subheap per obj
ect |
83 }; | 116 }; |
84 | 117 |
85 GrVkHeap(const GrVkGpu* gpu, Strategy strategy, VkDeviceSize subHeapSize) | 118 GrVkHeap(const GrVkGpu* gpu, Strategy strategy, VkDeviceSize subHeapSize) |
86 : fGpu(gpu) | 119 : fGpu(gpu) |
87 , fSubHeapSize(subHeapSize) | 120 , fSubHeapSize(subHeapSize) |
88 , fAllocSize(0) | 121 , fAllocSize(0) |
89 , fUsedSize(0) { | 122 , fUsedSize(0) { |
90 if (strategy == kSubAlloc_Strategy) { | 123 if (strategy == kSubAlloc_Strategy) { |
91 fAllocFunc = &GrVkHeap::subAlloc; | 124 fAllocFunc = &GrVkHeap::subAlloc; |
92 } else { | 125 } else { |
93 fAllocFunc = &GrVkHeap::singleAlloc; | 126 fAllocFunc = &GrVkHeap::singleAlloc; |
94 } | 127 } |
95 } | 128 } |
96 | 129 |
97 ~GrVkHeap(); | 130 ~GrVkHeap() {} |
98 | 131 |
99 VkDeviceSize allocSize() const { return fAllocSize; } | 132 VkDeviceSize allocSize() const { return fAllocSize; } |
100 VkDeviceSize usedSize() const { return fUsedSize; } | 133 VkDeviceSize usedSize() const { return fUsedSize; } |
101 | 134 |
102 bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeInd
ex, | 135 bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeInd
ex, |
103 GrVkAlloc* alloc) { | 136 GrVkAlloc* alloc) { |
104 SkASSERT(size > 0); | 137 SkASSERT(size > 0); |
105 return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); | 138 return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); |
106 } | 139 } |
107 bool free(const GrVkAlloc& alloc); | 140 bool free(const GrVkAlloc& alloc); |
108 | 141 |
109 private: | 142 private: |
110 typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignmen
t, | 143 typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignmen
t, |
111 uint32_t memoryTypeIndex, GrVkAlloc* all
oc); | 144 uint32_t memoryTypeIndex, GrVkAlloc* all
oc); |
112 | 145 |
113 bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, | 146 bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, |
114 uint32_t memoryTypeIndex, GrVkAlloc* alloc); | 147 uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
115 bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, | 148 bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, |
116 uint32_t memoryTypeIndex, GrVkAlloc* alloc); | 149 uint32_t memoryTypeIndex, GrVkAlloc* alloc); |
117 | 150 |
118 const GrVkGpu* fGpu; | 151 const GrVkGpu* fGpu; |
119 VkDeviceSize fSubHeapSize; | 152 VkDeviceSize fSubHeapSize; |
120 VkDeviceSize fAllocSize; | 153 VkDeviceSize fAllocSize; |
121 VkDeviceSize fUsedSize; | 154 VkDeviceSize fUsedSize; |
122 AllocFunc fAllocFunc; | 155 AllocFunc fAllocFunc; |
123 SkTArray<SkAutoTDelete<GrVkSubHeap>> fSubHeaps; | 156 SkTArray<SkAutoTDelete<GrVkSubHeap>> fSubHeaps; |
124 }; | 157 }; |
125 #endif | 158 #endif |
OLD | NEW |