-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmm.hpp
220 lines (112 loc) · 3.47 KB
/
vmm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#include <vulkan\vulkan.hpp>
namespace vmm {
enum class Size : uint32_t {
Kb = 1000,
Mb = 1000000,
Gb = 1000000000
};
struct Block {
vk::DeviceSize
size = 0,
offset = 0;
bool isFree = false;
};
class Pool {
public:
Pool();
Pool(vk::Device device, vk::DeviceSize size, uint32_t memoryTypeIndex);
void create(vk::Device device, vk::DeviceSize size, uint32_t memoryTypeIndex);
void destroy();
vk::DeviceSize/*offset*/ alloc(vk::DeviceSize size, vk::DeviceSize alignment);
void free(vk::DeviceSize offset);
inline bool hasUniqueFreeBlock() const noexcept {
return _blocks.size() == 1 ? _blocks[0].isFree : false;
}
inline uint32_t getMemoryTypeIndex() const noexcept {
return _memoryTypeIndex;
}
inline vk::DeviceMemory getMemory() const noexcept {
return _memory;
}
inline vk::Device getDevice() const noexcept {
return _device;
}
void print();
private:
void _create();
std::vector<Block> _blocks;
vk::DeviceMemory _memory;
vk::DeviceSize _size;
uint32_t _memoryTypeIndex;
vk::Device _device;
};
struct Allocation {
vk::DeviceSize offset = 0, size = 0;
Pool* pool = nullptr;
};
class MemoryObject {
public:
MemoryObject();
inline void* map() {
return _allocation.pool->getDevice().mapMemory(
_allocation.pool->getMemory(), _allocation.offset, _allocation.size);
}
inline void unmap() {
_allocation.pool->getDevice().unmapMemory(_allocation.pool->getMemory());
}
inline void copy(void* mapped, const void* data, vk::DeviceSize offset, vk::DeviceSize size) {
memcpy(static_cast<char*>(mapped) + offset, data, size);
}
void singleCopy(const void* data, vk::DeviceSize offset, vk::DeviceSize size);
virtual void destroy();
inline vk::DeviceSize size() const noexcept {
return _allocation.size;
}
protected:
Allocation _allocation;
};
class Buffer : public MemoryObject {
public:
Buffer();
Buffer(vk::Buffer buffer, Allocation allocation);
void destroy() override;
operator vk::Buffer();
inline vk::Buffer getVkBuffer() const noexcept {
return _buffer;
}
private:
vk::Buffer _buffer;
};
class Image : public MemoryObject {
public:
Image();
Image(vk::Image image, Allocation allocation);
void destroy() override;
operator vk::Image();
inline vk::Image getVkImage() const noexcept {
return _image;
}
private:
vk::Image _image;
};
class Allocator {
public:
Allocator();
Allocator(vk::Device device, vk::PhysicalDevice physicalDevice);
void create(vk::Device device, vk::PhysicalDevice physicalDevice);
void destroy();
Buffer createBuffer(const vk::BufferCreateInfo& bufferInfo, vk::MemoryPropertyFlags memoryProperties);
Image createImage(const vk::ImageCreateInfo& imageInfo, vk::MemoryPropertyFlags memoryProperties);
void clean();
void print();
private:
uint32_t _findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) const;
Allocation _alloc(vk::MemoryRequirements memRequirements, uint32_t memoryTypeIndex);
static vk::DeviceSize _POOL_SIZE;
vk::PhysicalDevice _physicalDevice;
vk::PhysicalDeviceMemoryProperties _physicalDeviceMemoryProperties;
vk::Device _device;
std::vector<Pool*> _pools;
};
}