blob: 44c82867218dc189be44458e47cb7ea0b5dc80f8 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
jvanverth6b6ffc42016-06-13 14:28:07 -070011#include "GrVkBuffer.h"
12#include "SkTArray.h"
13#include "SkTLList.h"
jvanverthe50f3e72016-03-28 07:03:06 -070014#include "vk/GrVkDefines.h"
jvanverth1e305ba2016-06-01 09:39:15 -070015#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016
17class GrVkGpu;
18
19namespace GrVkMemory {
20 /**
21 * Allocates vulkan device memory and binds it to the gpu's device for the given object.
jvanverth6b6ffc42016-06-13 14:28:07 -070022 * Returns true if allocation succeeded.
Greg Daniel164a9f02016-02-22 09:56:40 -050023 */
24 bool AllocAndBindBufferMemory(const GrVkGpu* gpu,
25 VkBuffer buffer,
jvanverth6b6ffc42016-06-13 14:28:07 -070026 GrVkBuffer::Type type,
jvanverth1e305ba2016-06-01 09:39:15 -070027 GrVkAlloc* alloc);
jvanverth6b6ffc42016-06-13 14:28:07 -070028 void FreeBufferMemory(const GrVkGpu* gpu, GrVkBuffer::Type type, const GrVkAlloc& alloc);
Greg Daniel164a9f02016-02-22 09:56:40 -050029
30 bool AllocAndBindImageMemory(const GrVkGpu* gpu,
31 VkImage image,
jvanverth6b6ffc42016-06-13 14:28:07 -070032 bool linearTiling,
jvanverth1e305ba2016-06-01 09:39:15 -070033 GrVkAlloc* alloc);
jvanverth6b6ffc42016-06-13 14:28:07 -070034 void FreeImageMemory(const GrVkGpu* gpu, bool linearTiling, const GrVkAlloc& alloc);
Greg Daniel164a9f02016-02-22 09:56:40 -050035
36 VkPipelineStageFlags LayoutToPipelineStageFlags(const VkImageLayout layout);
37
38 VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout);
39}
40
jvanverth6b6ffc42016-06-13 14:28:07 -070041class GrVkSubHeap {
42public:
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
59private:
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
77class GrVkHeap {
78public:
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
jvanverthd6f80342016-06-16 04:42:30 -070098 VkDeviceSize allocSize() const { return fAllocSize; }
99 VkDeviceSize usedSize() const { return fUsedSize; }
100
jvanverth6b6ffc42016-06-13 14:28:07 -0700101 bool alloc(VkDeviceSize size, VkDeviceSize alignment, uint32_t memoryTypeIndex,
102 GrVkAlloc* alloc) {
jvanverth6dc3af42016-06-16 14:05:09 -0700103 SkASSERT(size > 0);
jvanverth6b6ffc42016-06-13 14:28:07 -0700104 return (*this.*fAllocFunc)(size, alignment, memoryTypeIndex, alloc);
105 }
106 bool free(const GrVkAlloc& alloc);
107
108private:
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};
jvanverthe50f3e72016-03-28 07:03:06 -0700124#endif