project-aster/aster/buffer.cpp

43 lines
1.4 KiB
C++

// =============================================
// Aster: buffer.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "buffer.h"
void
Buffer::Destroy(const Device *device)
{
if (!m_Buffer)
return;
vmaDestroyBuffer(device->m_Allocator, m_Buffer, m_Allocation);
m_Buffer = nullptr;
}
void
UniformBuffer::Init(const Device *device, usize size, cstr name)
{
vk::BufferCreateInfo bufferCreateInfo = {
.size = size,
.usage = vk::BufferUsageFlagBits::eUniformBuffer,
.sharingMode = vk::SharingMode::eExclusive,
};
constexpr VmaAllocationCreateInfo allocationCreateInfo = {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkBuffer buffer;
VmaAllocation allocation;
auto result = Cast<vk::Result>(vmaCreateBuffer(device->m_Allocator, Recast<VkBufferCreateInfo *>(&bufferCreateInfo),
&allocationCreateInfo, &buffer, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate buffer. Cause: {}", result) THEN_ABORT(result);
m_Buffer = buffer;
m_Size = size;
m_Allocation = allocation;
device->SetName(m_Buffer, name);
}