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, VkDeviceSize size) ; | |
44 ~GrVkSubHeap(); | |
45 | |
46 uint32_t memoryTypeIndex() { return fMemoryTypeIndex; } | |
47 VkDeviceSize size() { return fSize; } | |
48 VkDeviceSize freeSize() { return fFreeSize; } | |
49 VkDeviceSize largestBlockSize() { return fLargestBlockSize; } | |
50 VkDeviceMemory memory() { return fAlloc; } | |
51 | |
52 bool unallocated() { return fSize == fFreeSize; } | |
53 | |
54 bool alloc(VkDeviceSize size, VkDeviceSize alignment, GrVkAlloc* alloc); | |
55 void free(const GrVkAlloc& alloc); | |
56 | |
57 private: | |
58 struct Block { | |
59 VkDeviceSize fOffset; | |
60 VkDeviceSize fSize; | |
61 }; | |
62 typedef SkTLList<Block, 16> FreeList; | |
63 | |
64 const GrVkGpu* fGpu; | |
65 uint32_t fMemoryTypeIndex; | |
66 VkDeviceSize fSize; | |
67 VkDeviceSize fFreeSize; | |
68 VkDeviceSize fLargestBlockSize; | |
69 VkDeviceSize fLargestBlockOffset; | |
70 VkDeviceMemory fAlloc; | |
71 FreeList fFreeList; | |
72 }; | |
73 | |
74 class GrVkHeap { | |
75 public: | |
76 enum Strategy { | |
77 kSubAlloc_Strategy, // alloc large subheaps and suballoc within th em | |
78 kSingleAlloc_Strategy // alloc/recycle an individual subheap per obj ect | |
79 }; | |
80 | |
81 GrVkHeap(const GrVkGpu* gpu, Strategy strategy, VkDeviceSize subHeapSize) | |
82 : fGpu(gpu) | |
83 , fSubHeapSize(subHeapSize) | |
84 , fAllocSize(0) | |
85 , fUsedSize(0) { | |
86 if (strategy == kSubAlloc_Strategy) { | |
87 fAllocFunc = &GrVkHeap::subAlloc; | |
88 } else { | |
89 fAllocFunc = &GrVkHeap::singleAlloc; | |
90 } | |
91 } | |
92 | |
93 ~GrVkHeap(); | |
94 | |
95 bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeInd ex, GrVkAlloc* alloc) { | |
egdaniel
2016/06/13 16:44:44
100 chars
jvanverth1
2016/06/13 19:59:23
Done.
| |
96 return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc); | |
97 } | |
98 bool free(const GrVkAlloc& alloc); | |
99 | |
100 private: | |
101 typedef bool (GrVkHeap::*AllocFunc)(VkDeviceSize size, VkDeviceSize alignmen t, | |
102 uint32_t memoryTypeIndex, GrVkAlloc* all oc); | |
103 | |
104 bool subAlloc(VkDeviceSize size, VkDeviceSize alignment, | |
105 uint32_t memoryTypeIndex, GrVkAlloc* alloc); | |
106 bool singleAlloc(VkDeviceSize size, VkDeviceSize alignment, | |
107 uint32_t memoryTypeIndex, GrVkAlloc* alloc); | |
108 | |
109 const GrVkGpu* fGpu; | |
110 VkDeviceSize fSubHeapSize; | |
111 VkDeviceSize fAllocSize; | |
112 VkDeviceSize fUsedSize; | |
113 AllocFunc fAllocFunc; | |
114 SkTArray<GrVkSubHeap*> fSubHeaps; | |
115 }; | |
38 #endif | 116 #endif |
OLD | NEW |