2021-01-16 22:22:31 +01:00
|
|
|
#include "vk_buffer.h"
|
|
|
|
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
qboolean createBuffer(vk_buffer_t *buf, uint32_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags flags)
|
|
|
|
{
|
|
|
|
VkBufferCreateInfo bci = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
|
|
|
.size = size,
|
|
|
|
.usage = usage,
|
|
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
|
|
|
};
|
|
|
|
VkMemoryRequirements memreq;
|
|
|
|
XVK_CHECK(vkCreateBuffer(vk_core.device, &bci, NULL, &buf->buffer));
|
|
|
|
|
|
|
|
vkGetBufferMemoryRequirements(vk_core.device, buf->buffer, &memreq);
|
2021-01-17 00:45:10 +01:00
|
|
|
gEngine.Con_Reportf("memreq: memoryTypeBits=0x%x alignment=%zu size=%zu\n", memreq.memoryTypeBits, memreq.alignment, memreq.size);
|
2021-01-16 22:22:31 +01:00
|
|
|
|
2021-01-17 00:45:10 +01:00
|
|
|
buf->device_memory = allocateDeviceMemory(memreq, flags);
|
|
|
|
XVK_CHECK(vkBindBufferMemory(vk_core.device, buf->buffer, buf->device_memory.device_memory, buf->device_memory.offset));
|
2021-01-16 22:22:31 +01:00
|
|
|
|
2021-01-17 00:45:10 +01:00
|
|
|
// FIXME when there are many allocation per VkDeviceMemory, fix this
|
|
|
|
XVK_CHECK(vkMapMemory(vk_core.device, buf->device_memory.device_memory, 0, bci.size, 0, &buf->mapped));
|
2021-01-16 22:22:31 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyBuffer(vk_buffer_t *buf) {
|
2021-01-17 00:45:10 +01:00
|
|
|
// FIXME when there are many allocation per VkDeviceMemory, fix this
|
2021-01-23 19:54:44 +01:00
|
|
|
if (buf->buffer)
|
|
|
|
{
|
|
|
|
vkDestroyBuffer(vk_core.device, buf->buffer, NULL);
|
|
|
|
buf->buffer = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->device_memory.device_memory)
|
|
|
|
{
|
|
|
|
vkUnmapMemory(vk_core.device, buf->device_memory.device_memory);
|
|
|
|
freeDeviceMemory(&buf->device_memory);
|
|
|
|
buf->device_memory.device_memory = VK_NULL_HANDLE;
|
|
|
|
buf->device_memory.offset = 0;
|
|
|
|
buf->mapped = 0;
|
|
|
|
buf->size = 0;
|
|
|
|
}
|
2021-01-16 22:22:31 +01:00
|
|
|
}
|