project-aster/aster/buffer.cpp

36 lines
1.2 KiB
C++

// =============================================
// Aster: buffer.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "buffer.h"
#include "device.h"
Buffer::Buffer(const Device *device, const usize size, const vk::BufferUsageFlags usage, NameString &&name)
: m_Device(device)
, m_Allocation(nullptr)
, m_Size(0)
, m_Name(std::move(name))
{
const VkBufferCreateInfo bufferCreateInfo = vk::BufferCreateInfo{
.size = size,
.usage = usage,
.sharingMode = vk::SharingMode::eExclusive,
};
constexpr VmaAllocationCreateInfo allocationCreateInfo = {.usage = VMA_MEMORY_USAGE_AUTO};
VkBuffer buffer;
vk::Result result = Cast<vk::Result>(vmaCreateBuffer(m_Device->m_Allocator, &bufferCreateInfo,
&allocationCreateInfo, &buffer, &m_Allocation, nullptr));
ERROR_IF(Failed(result), "Could not create buffer. Cause: {}", result) THEN_ABORT(result);
m_Buffer = buffer;
m_Device->SetName(m_Buffer, m_Name.c_str());
}
Buffer::~Buffer()
{
vmaDestroyBuffer(m_Device->m_Allocator, m_Buffer, m_Allocation);
}