From eef8d4a2455068e242dac72469230d3030ab1704 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Mon, 1 Sep 2025 18:22:14 +0200 Subject: [PATCH] refactor: finish final methods --- framegen/.old/include/utils.hpp | 63 -------- framegen/.old/src/utils.cpp | 140 ------------------ framegen/{.old => .old2}/public/lsfg_3_1.hpp | 0 framegen/{.old => .old2}/public/lsfg_3_1p.hpp | 0 framegen/include/vk/core/commandbuffer.hpp | 21 ++- framegen/src/vk/core/commandbuffer.cpp | 38 +++++ 6 files changed, 57 insertions(+), 205 deletions(-) delete mode 100644 framegen/.old/include/utils.hpp delete mode 100644 framegen/.old/src/utils.cpp rename framegen/{.old => .old2}/public/lsfg_3_1.hpp (100%) rename framegen/{.old => .old2}/public/lsfg_3_1p.hpp (100%) diff --git a/framegen/.old/include/utils.hpp b/framegen/.old/include/utils.hpp deleted file mode 100644 index 99a6f2d..0000000 --- a/framegen/.old/include/utils.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#include "core/commandbuffer.hpp" -#include "core/commandpool.hpp" -#include "core/descriptorpool.hpp" -#include "core/image.hpp" -#include "core/device.hpp" -#include "pool/resourcepool.hpp" -#include "pool/shaderpool.hpp" - -#include - -#include -#include -#include -#include -#include -#include - -namespace LSFG::Utils { - - /// - /// Upload a DDS file to a Vulkan image. - /// - /// @param device The Vulkan device - /// @param commandPool The command pool - /// @param image The Vulkan image to upload to - /// @param path The path to the DDS file. - /// - /// @throws std::system_error If the file cannot be opened or read. - /// @throws ls:vulkan_error If the Vulkan image cannot be created or updated. - /// - void uploadImage(const Core::Device& device, - const Core::CommandPool& commandPool, - Core::Image& image, const std::string& path); - - /// - /// Clear a texture to white during setup. - /// - /// @param device The Vulkan device. - /// @param image The image to clear. - /// @param white If true, the image will be cleared to white, otherwise to black. - /// - /// @throws LSFG::vulkan_error If the Vulkan image cannot be cleared. - /// - void clearImage(const Core::Device& device, Core::Image& image, bool white = false); - -} - -namespace LSFG { - struct Vulkan { - Core::Device device; - Core::CommandPool commandPool; - Core::DescriptorPool descriptorPool; - - uint64_t generationCount; - float flowScale; - bool isHdr; - - Pool::ShaderPool shaders; - Pool::ResourcePool resources; - }; -} diff --git a/framegen/.old/src/utils.cpp b/framegen/.old/src/utils.cpp deleted file mode 100644 index f210a5a..0000000 --- a/framegen/.old/src/utils.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include -#include - -#include "common/utils.hpp" -#include "core/buffer.hpp" -#include "core/image.hpp" -#include "core/device.hpp" -#include "core/commandpool.hpp" -#include "core/fence.hpp" -#include "common/exception.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace LSFG; -using namespace LSFG::Utils; - -void Utils::uploadImage(const Core::Device& device, const Core::CommandPool& commandPool, - Core::Image& image, const std::string& path) { - // read image bytecode - std::ifstream file(path.data(), std::ios::binary | std::ios::ate); - if (!file.is_open()) - throw std::system_error(errno, std::generic_category(), "Failed to open image: " + path); - - std::streamsize size = file.tellg(); - size -= 124 + 4; // dds header and magic bytes - std::vector code(static_cast(size)); - - file.seekg(124 + 4, std::ios::beg); - if (!file.read(code.data(), size)) - throw std::system_error(errno, std::generic_category(), "Failed to read image: " + path); - - file.close(); - - // copy data to buffer - const Core::Buffer stagingBuffer( - device, code.data(), static_cast(code.size()), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT - ); - - // perform the upload - Core::CommandBuffer commandBuffer(device, commandPool); - commandBuffer.begin(); - - const VkImageMemoryBarrier barrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .srcAccessMask = VK_ACCESS_NONE, - .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .oldLayout = image.getLayout(), - .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .image = image.handle(), - .subresourceRange = { - .aspectMask = image.getAspectFlags(), - .levelCount = 1, - .layerCount = 1 - } - }; - image.setLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); - vkCmdPipelineBarrier( - commandBuffer.handle(), - VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, - 0, 0, nullptr, 0, nullptr, 1, &barrier - ); - - auto extent = image.getExtent(); - const VkBufferImageCopy region{ - .bufferImageHeight = 0, - .imageSubresource = { - .aspectMask = image.getAspectFlags(), - .layerCount = 1 - }, - .imageExtent = { extent.width, extent.height, 1 } - }; - vkCmdCopyBufferToImage( - commandBuffer.handle(), - stagingBuffer.handle(), image.handle(), - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion - ); - - commandBuffer.end(); - - Core::Fence fence(device); - commandBuffer.submit(device.getComputeQueue(), fence); - - // wait for the upload to complete - if (!fence.wait(device)) - throw LSFG::vulkan_error(VK_TIMEOUT, "Upload operation timed out"); -} - -void Utils::clearImage(const Core::Device& device, Core::Image& image, bool white) { - Core::Fence fence(device); - const Core::CommandPool cmdPool(device); - Core::CommandBuffer cmdBuf(device, cmdPool); - cmdBuf.begin(); - - const VkImageMemoryBarrier2 barrier{ - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, - .dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT, - .dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, - .oldLayout = image.getLayout(), - .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .image = image.handle(), - .subresourceRange = { - .aspectMask = image.getAspectFlags(), - .levelCount = 1, - .layerCount = 1 - } - }; - const VkDependencyInfo dependencyInfo = { - .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, - .imageMemoryBarrierCount = 1, - .pImageMemoryBarriers = &barrier - }; - image.setLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); - vkCmdPipelineBarrier2(cmdBuf.handle(), &dependencyInfo); - - const float clearValue = white ? 1.0F : 0.0F; - const VkClearColorValue clearColor = {{ clearValue, clearValue, clearValue, clearValue }}; - const VkImageSubresourceRange subresourceRange = { - .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, - .levelCount = 1, - .layerCount = 1 - }; - vkCmdClearColorImage(cmdBuf.handle(), - image.handle(), image.getLayout(), - &clearColor, - 1, &subresourceRange); - - cmdBuf.end(); - - cmdBuf.submit(device.getComputeQueue(), fence); - if (!fence.wait(device)) - throw LSFG::vulkan_error(VK_TIMEOUT, "Failed to wait for clearing fence."); -} diff --git a/framegen/.old/public/lsfg_3_1.hpp b/framegen/.old2/public/lsfg_3_1.hpp similarity index 100% rename from framegen/.old/public/lsfg_3_1.hpp rename to framegen/.old2/public/lsfg_3_1.hpp diff --git a/framegen/.old/public/lsfg_3_1p.hpp b/framegen/.old2/public/lsfg_3_1p.hpp similarity index 100% rename from framegen/.old/public/lsfg_3_1p.hpp rename to framegen/.old2/public/lsfg_3_1p.hpp diff --git a/framegen/include/vk/core/commandbuffer.hpp b/framegen/include/vk/core/commandbuffer.hpp index f1a0f1e..2d87438 100644 --- a/framegen/include/vk/core/commandbuffer.hpp +++ b/framegen/include/vk/core/commandbuffer.hpp @@ -97,8 +97,25 @@ namespace VK::Core { const std::vector& readableImages, const std::vector& writableImages) const; - // TODO: Method for copying a buffer to an image - // TODO: Method for clearing an image to a color + /// + /// Copy a buffer to an image. + /// + /// @param buffer Vulkan buffer + /// @param image Vulkan image + /// + /// @throws std::logic_error if the command buffer is not in Recording state + /// + void copyBufferToImage(const Buffer& buffer, const Image& image) const; + + /// + /// Clear an image to a color. + /// + /// @param image Vulkan image + /// @param white If true, clear to white; otherwise, clear to black + /// + /// @throws std::logic_error if the command buffer is not in Recording state + /// + void clearImage(const Image& image, bool white) const; /// /// Dispatch a compute command. diff --git a/framegen/src/vk/core/commandbuffer.cpp b/framegen/src/vk/core/commandbuffer.cpp index 35a5614..e859972 100644 --- a/framegen/src/vk/core/commandbuffer.cpp +++ b/framegen/src/vk/core/commandbuffer.cpp @@ -6,8 +6,10 @@ #include "vk/core/commandpool.hpp" #include "vk/core/semaphore.hpp" #include "vk/core/pipeline.hpp" +#include "vk/core/buffer.hpp" #include "vk/core/device.hpp" #include "vk/core/fence.hpp" +#include "vk/core/image.hpp" #include "vk/exception.hpp" #include @@ -147,6 +149,42 @@ void CommandBuffer::insertBarrier( vkCmdPipelineBarrier2(*this->commandBuffer, &dependencyInfo); } +void CommandBuffer::copyBufferToImage(const Buffer& buffer, const Image& image) const { + if (*this->state != CommandBufferState::Recording) + throw std::logic_error("Command buffer is not in Recording state"); + + const auto extent = image.getExtent(); + const VkBufferImageCopy region{ + .imageSubresource = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .layerCount = 1 + }, + .imageExtent = { extent.width, extent.height, 1 } + }; + vkCmdCopyBufferToImage( + *this->commandBuffer, + buffer.handle(), image.handle(), + VK_IMAGE_LAYOUT_GENERAL, 1, ®ion + ); +} + +void CommandBuffer::clearImage(const Image& image, bool white) const { + if (*this->state != CommandBufferState::Recording) + throw std::logic_error("Command buffer is not in Recording state"); + + const float clearValue = white ? 1.0F : 0.0F; + const VkClearColorValue clearColor = {{ clearValue, clearValue, clearValue, clearValue }}; + const VkImageSubresourceRange subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .levelCount = 1, + .layerCount = 1 + }; + vkCmdClearColorImage(*this->commandBuffer, + image.handle(), VK_IMAGE_LAYOUT_GENERAL, + &clearColor, + 1, &subresourceRange); +} + void CommandBuffer::dispatch(uint32_t x, uint32_t y, uint32_t z) const { if (*this->state != CommandBufferState::Recording) throw std::logic_error("Command buffer is not in Recording state");