-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgbuffer.cpp
72 lines (56 loc) · 2.42 KB
/
mgbuffer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "mgbuffer.h"
/**
* Create the buffer.
*/
VkResult MgBuffer::create(VkDeviceSize size, VkBufferUsageFlags usageMask, const VkcDevice *device)
{
this->device = device;
// Get queue families.
QVector<uint32_t> queueFamilies;
device->getQueueFamilies(queueFamilies);
// Fill buffer info.
VkBufferCreateInfo bufferInfo =
{
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkBufferCreateFlags flags;
size, // VkDeviceSize size;
usageMask, // VkBufferUsageFlags usage;
VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
(uint32_t)queueFamilies.count(), // uint32_t queueFamilyIndexCount;
queueFamilies.data() // const uint32_t* pQueueFamilyIndices;
};
mgAssert(vkCreateBuffer(device->logical, &bufferInfo, nullptr, &handle));
// Get buffer memory requirements.
VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(device->logical, handle, &memoryRequirements);
uint32_t memoryTypeIdx = 0;
VkMemoryPropertyFlags memoryMask = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
mgAssert(device->getMemoryTypeIndex(memoryMask, memoryRequirements, &memoryTypeIdx));
// Fill buffer memory allocate info.
VkMemoryAllocateInfo memoryInfo =
{
VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
memoryRequirements.size, // VkDeviceSize allocationSize;
memoryTypeIdx // uint32_t memoryTypeIndex;
};
// Allocate buffer memory.
mgAssert(vkAllocateMemory(device->logical, &memoryInfo, nullptr, &memory));
// Bind memory to buffer.
mgAssert(vkBindBufferMemory(device->logical, handle, memory, 0));
return VK_SUCCESS;
}
/**
* Destroy the buffer.
*/
void MgBuffer::destroy()
{
if (device != nullptr)
{
if (memory != VK_NULL_HANDLE)
vkFreeMemory(device->logical, memory, nullptr);
if (handle != VK_NULL_HANDLE)
vkDestroyBuffer(device->logical, handle, nullptr);
}
}