mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-24 16:10:14 +00:00
91 lines
2.2 KiB
C++
91 lines
2.2 KiB
C++
#include "rendering/vk_buffer.hpp"
|
|
#include "rendering/vk_context.hpp"
|
|
#include "core/logger.hpp"
|
|
#include <cstring>
|
|
|
|
namespace wowee {
|
|
namespace rendering {
|
|
|
|
VkBuffer::~VkBuffer() {
|
|
destroy();
|
|
}
|
|
|
|
VkBuffer::VkBuffer(VkBuffer&& other) noexcept
|
|
: buf_(other.buf_), allocator_(other.allocator_), size_(other.size_) {
|
|
other.buf_ = {};
|
|
other.allocator_ = VK_NULL_HANDLE;
|
|
other.size_ = 0;
|
|
}
|
|
|
|
VkBuffer& VkBuffer::operator=(VkBuffer&& other) noexcept {
|
|
if (this != &other) {
|
|
destroy();
|
|
buf_ = other.buf_;
|
|
allocator_ = other.allocator_;
|
|
size_ = other.size_;
|
|
other.buf_ = {};
|
|
other.allocator_ = VK_NULL_HANDLE;
|
|
other.size_ = 0;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
bool VkBuffer::uploadToGPU(VkContext& ctx, const void* data, VkDeviceSize size,
|
|
VkBufferUsageFlags usage)
|
|
{
|
|
destroy();
|
|
allocator_ = ctx.getAllocator();
|
|
size_ = size;
|
|
|
|
buf_ = uploadBuffer(ctx, data, size, usage);
|
|
if (!buf_.buffer) {
|
|
LOG_ERROR("Failed to upload buffer (size=", size, ")");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool VkBuffer::createMapped(VmaAllocator allocator, VkDeviceSize size,
|
|
VkBufferUsageFlags usage)
|
|
{
|
|
destroy();
|
|
allocator_ = allocator;
|
|
size_ = size;
|
|
|
|
buf_ = createBuffer(allocator, size, usage, VMA_MEMORY_USAGE_CPU_TO_GPU);
|
|
if (!buf_.buffer) {
|
|
LOG_ERROR("Failed to create mapped buffer (size=", size, ")");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void VkBuffer::updateMapped(const void* data, VkDeviceSize size, VkDeviceSize offset) {
|
|
if (!buf_.info.pMappedData) {
|
|
LOG_ERROR("Attempted to update non-mapped buffer");
|
|
return;
|
|
}
|
|
std::memcpy(static_cast<uint8_t*>(buf_.info.pMappedData) + offset, data, size);
|
|
}
|
|
|
|
void VkBuffer::destroy() {
|
|
if (buf_.buffer && allocator_) {
|
|
destroyBuffer(allocator_, buf_);
|
|
}
|
|
buf_ = {};
|
|
allocator_ = VK_NULL_HANDLE;
|
|
size_ = 0;
|
|
}
|
|
|
|
VkDescriptorBufferInfo VkBuffer::descriptorInfo(VkDeviceSize offset, VkDeviceSize range) const {
|
|
VkDescriptorBufferInfo info{};
|
|
info.buffer = buf_.buffer;
|
|
info.offset = offset;
|
|
info.range = range;
|
|
return info;
|
|
}
|
|
|
|
} // namespace rendering
|
|
} // namespace wowee
|