diff --git a/aster/CMakeLists.txt b/aster/CMakeLists.txt index 9522cf3..4df1a5b 100644 --- a/aster/CMakeLists.txt +++ b/aster/CMakeLists.txt @@ -21,7 +21,8 @@ set(HEADER_FILES device.h swapchain.h "pipeline.h" - queue_allocation.h) + queue_allocation.h + buffer.h) set(SOURCE_FILES logger.cpp @@ -31,7 +32,8 @@ set(SOURCE_FILES physical_device.cpp device.cpp swapchain.cpp - pipeline.cpp) + pipeline.cpp + buffer.cpp) add_library(aster_core STATIC ${SOURCE_FILES} ${HEADER_FILES}) set_property(TARGET aster_core PROPERTY CXX_STANDARD 20) diff --git a/aster/buffer.cpp b/aster/buffer.cpp new file mode 100644 index 0000000..2e12793 --- /dev/null +++ b/aster/buffer.cpp @@ -0,0 +1,43 @@ +// ============================================= +// 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(vmaCreateBuffer(device->m_Allocator, Recast(&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); +} \ No newline at end of file diff --git a/aster/buffer.h b/aster/buffer.h new file mode 100644 index 0000000..5317b79 --- /dev/null +++ b/aster/buffer.h @@ -0,0 +1,25 @@ +// ============================================= +// Aster: buffer.h +// Copyright (c) 2020-2024 Anish Bhobe +// ============================================= + +#pragma once + +#include "device.h" +#include "global.h" + +struct Device; + +struct Buffer +{ + vk::Buffer m_Buffer = nullptr; + VmaAllocation m_Allocation = nullptr; + usize m_Size = 0; + + void Destroy(const Device *device); +}; + +struct UniformBuffer : Buffer +{ + void Init(const Device *device, usize size, cstr name = nullptr); +}; \ No newline at end of file diff --git a/aster/device.h b/aster/device.h index b1082c6..ba5a298 100644 --- a/aster/device.h +++ b/aster/device.h @@ -43,6 +43,8 @@ template void Device::SetName(const T &object, cstr name) const { + if (!name) + return; auto handle = Recast(Cast(object)); const vk::DebugUtilsObjectNameInfoEXT objectNameInfo = { .objectType = object.objectType, diff --git a/samples/01_triangle/triangle.cpp b/samples/01_triangle/triangle.cpp index f0c0992..696e284 100644 --- a/samples/01_triangle/triangle.cpp +++ b/samples/01_triangle/triangle.cpp @@ -76,33 +76,26 @@ main(int, char **) .extent = swapchain.m_Extent, }; + vk::ImageSubresourceRange subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }; vk::ImageMemoryBarrier topOfThePipeBarrier = { .oldLayout = vk::ImageLayout::eUndefined, .newLayout = vk::ImageLayout::eColorAttachmentOptimal, .srcQueueFamilyIndex = queueAllocation.m_Family, .dstQueueFamilyIndex = queueAllocation.m_Family, - .subresourceRange = - { - .aspectMask = vk::ImageAspectFlagBits::eColor, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }, + .subresourceRange = subresourceRange, }; vk::ImageMemoryBarrier renderToPresentBarrier = { .oldLayout = vk::ImageLayout::eColorAttachmentOptimal, .newLayout = vk::ImageLayout::ePresentSrcKHR, .srcQueueFamilyIndex = queueAllocation.m_Family, .dstQueueFamilyIndex = queueAllocation.m_Family, - .subresourceRange = - { - .aspectMask = vk::ImageAspectFlagBits::eColor, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }, + .subresourceRange = subresourceRange, }; // Frames