forked from VisualGMQ/toy2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
57 lines (43 loc) · 1.59 KB
/
buffer.cpp
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
#include "toy2d/buffer.hpp"
namespace toy2d {
Buffer::Buffer(vk::BufferUsageFlags usage, size_t size, vk::MemoryPropertyFlags memProperty) {
auto& device = Context::Instance().device;
this->size = size;
vk::BufferCreateInfo createInfo;
createInfo.setUsage(usage)
.setSize(size)
.setSharingMode(vk::SharingMode::eExclusive);
buffer = device.createBuffer(createInfo);
auto requirements = device.getBufferMemoryRequirements(buffer);
requireSize = requirements.size;
auto index = QueryBufferMemTypeIndex(requirements.memoryTypeBits, memProperty);
vk::MemoryAllocateInfo allocInfo;
allocInfo.setMemoryTypeIndex(index)
.setAllocationSize(requirements.size);
memory = device.allocateMemory(allocInfo);
device.bindBufferMemory(buffer, memory, 0);
if (memProperty & vk::MemoryPropertyFlagBits::eHostVisible) {
map = device.mapMemory(memory, 0, size);
} else {
map = nullptr;
}
}
Buffer::~Buffer() {
auto& device = Context::Instance().device;
if (map) {
device.unmapMemory(memory);
}
device.freeMemory(memory);
device.destroyBuffer(buffer);
}
std::uint32_t QueryBufferMemTypeIndex(std::uint32_t type, vk::MemoryPropertyFlags flag) {
auto property = Context::Instance().phyDevice.getMemoryProperties();
for (std::uint32_t i = 0; i < property.memoryTypeCount; i++) {
if ((1 << i) & type &&
property.memoryTypes[i].propertyFlags & flag) {
return i;
}
}
return 0;
}
}