-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmm.cpp
More file actions
341 lines (210 loc) · 8.94 KB
/
vmm.cpp
File metadata and controls
341 lines (210 loc) · 8.94 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "vmm.hpp"
#include <iostream>
namespace vmm {
Pool::Pool() : _blocks(), _memory(), _size(), _memoryTypeIndex(), _device() {
}
Pool::Pool(vk::Device device, vk::DeviceSize size, uint32_t memoryTypeIndex)
: _device(device), _size(size), _memoryTypeIndex(memoryTypeIndex) {
_create();
}
void Pool::create(vk::Device device, vk::DeviceSize size, uint32_t memoryTypeIndex) {
_device = device;
_size = size;
_memoryTypeIndex = memoryTypeIndex;
_create();
}
void Pool::destroy() {
_blocks.clear();
_device.freeMemory(_memory);
_size = 0;
_memoryTypeIndex = 0;
}
vk::DeviceSize Pool::alloc(vk::DeviceSize size, vk::DeviceSize alignment) {
for (size_t blockIndex = 0; blockIndex < _blocks.size(); ++blockIndex) {
if (!_blocks[blockIndex].isFree)//si non-libre
continue;
vk::DeviceSize shift = _blocks[blockIndex].offset % alignment == 0
? 0 : alignment - _blocks[blockIndex].offset % alignment;
if (_blocks[blockIndex].size - shift < size) {
++blockIndex;//on peut sauter celui d'après : la "fusion" permet de ne pas avoir de blocks libres adjacents
continue;
}
else if (_blocks[blockIndex].size - shift > size) {
_blocks.insert(
_blocks.begin() + blockIndex + 1,
{ _blocks[blockIndex].size - (shift + size), _blocks[blockIndex].offset + shift + size, true }
);
}
_blocks[blockIndex].size = size;
_blocks[blockIndex].isFree = false;
if (shift > 0) {
Block shiftBlock = { shift, _blocks[blockIndex].offset, true };
_blocks[blockIndex].offset += shift;
_blocks.insert(_blocks.begin() + blockIndex, shiftBlock);
return _blocks[blockIndex + 1].offset;
}
else
return _blocks[blockIndex].offset;
}
return std::numeric_limits<vk::DeviceSize>::max();
}
void Pool::free(vk::DeviceSize offset) {
for (size_t blockIndex = 0; blockIndex < _blocks.size(); ++blockIndex) {
if (_blocks[blockIndex].isFree)
continue;
else if (_blocks[blockIndex].offset < offset)
continue;
else if (_blocks[blockIndex].offset > offset)//on l'a dépassé, c'est mort !
break;
_blocks[blockIndex].isFree = true;
if (
blockIndex > 0 && blockIndex < _blocks.size() - 1 &&
_blocks[blockIndex - 1].isFree && _blocks[blockIndex + 1].isFree) {
_blocks[blockIndex - 1].size += _blocks[blockIndex].size + _blocks[blockIndex + 1].size;
_blocks.erase(_blocks.begin() + blockIndex);
_blocks.erase(_blocks.begin() + blockIndex);
}
else if (blockIndex > 0 && _blocks[blockIndex - 1].isFree) {
_blocks[blockIndex - 1].size += _blocks[blockIndex].size;
_blocks.erase(_blocks.begin() + blockIndex);
}
else if (blockIndex < _blocks.size() - 1 && _blocks[blockIndex + 1].isFree) {
_blocks[blockIndex].size += _blocks[blockIndex + 1].size;
_blocks.erase(_blocks.begin() + blockIndex + 1);
}
return;
}
throw std::exception("failed to deallocate memory");
}
void Pool::_create() {
_memory = _device.allocateMemory({ _size, _memoryTypeIndex });
_blocks = { { _size, 0, true } };
}
void Pool::print() {
for (size_t i = 0; i < _blocks.size(); ++i) {
std::cout << "Block n." << i << " -> offset = " << _blocks[i].offset << " ; size = " << _blocks[i].size
<< " ; state = " << (_blocks[i].isFree ? "free" : "reserved") << std::endl;
}
}
MemoryObject::MemoryObject() : _allocation() {
}
void MemoryObject::singleCopy(const void* data, vk::DeviceSize offset, vk::DeviceSize size) {
void* mapped = _allocation.pool->getDevice().mapMemory(
_allocation.pool->getMemory(), _allocation.offset + offset, size);
memcpy(mapped, data, size);
_allocation.pool->getDevice().unmapMemory(_allocation.pool->getMemory());
}
void MemoryObject::destroy() {
_allocation.pool->free(_allocation.offset);
_allocation.offset = std::numeric_limits<size_t>::max();
_allocation.pool = nullptr;
}
Buffer::Buffer() : MemoryObject(), _buffer() {
}
Buffer::Buffer(vk::Buffer buffer, Allocation allocation) : _buffer(buffer) {
_allocation = allocation;
}
void Buffer::destroy() {
_allocation.pool->getDevice().destroyBuffer(_buffer);
MemoryObject::destroy();
}
Buffer::operator vk::Buffer() {
return _buffer;
}
Image::Image() : MemoryObject(), _image() {
}
Image::Image(vk::Image image, Allocation allocation) : _image(image) {
_allocation = allocation;
}
void Image::destroy() {
_allocation.pool->getDevice().destroyImage(_image);
MemoryObject::destroy();
}
Image::operator vk::Image() {
return _image;
}
Allocator::Allocator() : _physicalDevice(), _device(), _pools(), _physicalDeviceMemoryProperties() {
}
Allocator::Allocator(vk::Device device, vk::PhysicalDevice physicalDevice)
: _physicalDevice(physicalDevice), _device(device), _pools(),
_physicalDeviceMemoryProperties(_physicalDevice.getMemoryProperties()) {
}
void Allocator::create(vk::Device device, vk::PhysicalDevice physicalDevice) {
_physicalDevice = physicalDevice;
_device = device;
_physicalDeviceMemoryProperties = _physicalDevice.getMemoryProperties();
}
void Allocator::destroy() {
for (Pool* pool : _pools) {
pool->destroy();
delete pool;
}
_pools.clear();
}
Buffer Allocator::createBuffer(const vk::BufferCreateInfo& bufferInfo, vk::MemoryPropertyFlags memoryProperties) {
vk::Buffer vkBuffer = _device.createBuffer(bufferInfo);
vk::MemoryRequirements memRequirements = _device.getBufferMemoryRequirements(vkBuffer);
uint32_t memoryTypeIndex = _findMemoryType(memRequirements.memoryTypeBits, memoryProperties);
Allocation allocation = _alloc(memRequirements, memoryTypeIndex);
_device.bindBufferMemory(vkBuffer, allocation.pool->getMemory(), allocation.offset);
return { vkBuffer, allocation };
}
Image Allocator::createImage(const vk::ImageCreateInfo& imageInfo, vk::MemoryPropertyFlags memoryProperties) {
vk::Image vkImage = _device.createImage(imageInfo);
vk::MemoryRequirements memRequirements = _device.getImageMemoryRequirements(vkImage);
uint32_t memoryTypeIndex = _findMemoryType(memRequirements.memoryTypeBits, memoryProperties);
Allocation allocation = _alloc(memRequirements, memoryTypeIndex);
_device.bindImageMemory(vkImage, allocation.pool->getMemory(), allocation.offset);
return { vkImage, allocation };
}
void Allocator::clean() {
for (size_t i = _pools.size() - 1; i < std::numeric_limits<size_t>::max(); --i) {
if (_pools[i]->hasUniqueFreeBlock()) {//s'il n'a qu'un seul bloc libre
_pools[i]->destroy();//le détruire
delete _pools[i];
_pools.erase(_pools.begin() + i);//l'enlever de la liste
}
}
}
uint32_t Allocator::_findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) const {
for (uint32_t i = 0; i < _physicalDeviceMemoryProperties.memoryTypeCount; i++)
if (
(typeFilter & (1 << i)) &&
(_physicalDeviceMemoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
return i;
throw std::exception("failed to find suitable memory type");
}
Allocation Allocator::_alloc(vk::MemoryRequirements memRequirements, uint32_t memoryTypeIndex) {
constexpr size_t lastNotCreatedPoolIndex = std::numeric_limits<size_t>::max();
if (memRequirements.size > _POOL_SIZE) //si plus grand que la taille normale
_pools.push_back(new Pool(_device, memRequirements.size, memoryTypeIndex));
else {
for (size_t i = 0; i < _pools.size(); ++i) {
if (_pools[i]->getMemoryTypeIndex() == memoryTypeIndex) {//si même type de mémoire
vk::DeviceSize offset = _pools[i]->alloc(memRequirements.size, memRequirements.alignment);
if (offset == std::numeric_limits<vk::DeviceSize>::max())//si pas de place dans le pool
continue;
return { offset, memRequirements.size, _pools[i] };
}
}
if (lastNotCreatedPoolIndex != std::numeric_limits<size_t>::max()) {//s'il y a un pool libre
_pools[lastNotCreatedPoolIndex]->create(_device, _POOL_SIZE, memoryTypeIndex);
vk::DeviceSize offset = _pools[lastNotCreatedPoolIndex]->alloc(
memRequirements.size, memRequirements.alignment);
return { offset, memRequirements.size, _pools[lastNotCreatedPoolIndex] };
}
_pools.push_back(new Pool(_device, _POOL_SIZE, memoryTypeIndex));
}
vk::DeviceSize offset = _pools.back()->alloc(memRequirements.size, memRequirements.alignment);
return { offset, memRequirements.size, _pools.back() };
}
void Allocator::print() {
std::cout << "Vulkan Memory (" << _pools.size() << " pools)" << std::endl;
for (size_t i = 0; i < _pools.size(); ++i) {
std::cout << "Pool n." << i << std::endl;
_pools[i]->print();
}
std::cout << std::endl;
}
vk::DeviceSize Allocator::_POOL_SIZE = static_cast<vk::DeviceSize>(Size::Mb) * 50; //50 Mo par pool
}