diff --git a/config/Sources.cmake b/config/Sources.cmake index dc4d2e85d0d779bf38435905699aff2169989454..11c1388e7429fca514b83eb9ed6e8e08d880bf99 100644 --- a/config/Sources.cmake +++ b/config/Sources.cmake @@ -30,4 +30,7 @@ set(vkcv_sources ${vkcv_source}/vkcv/PipelineManager.hpp ${vkcv_source}/vkcv/PipelineManager.cpp + + ${vkcv_include}/vkcv/CommandResources.hpp + ${vkcv_source}/vkcv/CommandResources.cpp ) diff --git a/include/vkcv/CommandResources.hpp b/include/vkcv/CommandResources.hpp new file mode 100644 index 0000000000000000000000000000000000000000..05e848294935bcf3642e1712072acf607d153611 --- /dev/null +++ b/include/vkcv/CommandResources.hpp @@ -0,0 +1,12 @@ +#pragma once +#include <vulkan/vulkan.hpp> + +namespace vkcv { + struct CommandResources { + vk::CommandPool commandPool; + vk::CommandBuffer commandBuffer; + }; + + CommandResources createDefaultCommandResources(const vk::Device& device, const int graphicFamilyIndex); + void destroyCommandResources(const vk::Device& device, const CommandResources& resources); +} \ No newline at end of file diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index e700ac4a0d7152848a41b0ff19561bba460fb7bc..24f541dfdda2a94603be91d66394d869e0252e49 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -13,6 +13,7 @@ #include "vkcv/PassConfig.hpp" #include "vkcv/Handles.hpp" #include "vkcv/PipelineConfig.hpp" +#include "CommandResources.hpp" namespace vkcv { @@ -32,7 +33,8 @@ namespace vkcv * * @param context encapsulates various Vulkan objects */ - Core(Context &&context, const Window &window, SwapChain swapChain, std::vector<vk::ImageView> imageViews) noexcept; + Core(Context &&context, const Window &window, SwapChain swapChain, std::vector<vk::ImageView> imageViews, + const CommandResources& commandResources) noexcept; // explicit destruction of default constructor Core() = delete; @@ -48,6 +50,7 @@ namespace vkcv std::unique_ptr<PassManager> m_PassManager; std::unique_ptr<PipelineManager> m_PipelineManager; + CommandResources m_CommandResources; public: /** * Destructor of #Core destroys the Vulkan objects contained in the core's context. diff --git a/src/vkcv/CommandResources.cpp b/src/vkcv/CommandResources.cpp new file mode 100644 index 0000000000000000000000000000000000000000..451ec4f27b3bc68e6a787bb79d1dc12f59a086aa --- /dev/null +++ b/src/vkcv/CommandResources.cpp @@ -0,0 +1,23 @@ +#include "vkcv/CommandResources.hpp" + +namespace vkcv { + CommandResources createDefaultCommandResources(const vk::Device& device, const int graphicFamilyIndex) { + CommandResources resources; + vk::CommandPoolCreateFlags flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; + vk::CommandPoolCreateInfo poolCreateInfo(flags, graphicFamilyIndex); + resources.commandPool = device.createCommandPool(poolCreateInfo, nullptr, {}); + + const int commandPoolCount = 1; + vk::CommandBufferAllocateInfo allocateInfo(resources.commandPool, vk::CommandBufferLevel::ePrimary, commandPoolCount); + const std::vector<vk::CommandBuffer> createdBuffers = device.allocateCommandBuffers(allocateInfo, {}); + assert(createdBuffers.size() == 1); + resources.commandBuffer = createdBuffers[0]; + + return resources; + } + + void destroyCommandResources(const vk::Device& device, const CommandResources& resources) { + device.freeCommandBuffers(resources.commandPool, resources.commandBuffer, {}); + device.destroyCommandPool(resources.commandPool, {}); + } +} \ No newline at end of file diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index 07e0081678af075b5ccd57188d003304730ca754..2c9f4e6958a9cac6b0e115fcebe2964532262af3 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -450,7 +450,10 @@ namespace vkcv imageViews.push_back( device.createImageView( imageViewCreateInfo ) ); } - return Core(std::move(context) , window, swapChain, imageViews); + const int graphicQueueFamilyIndex = queuePairsGraphics[0].first; + const auto defaultCommandResources = createDefaultCommandResources(context.getDevice(), graphicQueueFamilyIndex); + + return Core(std::move(context) , window, swapChain, imageViews, defaultCommandResources); } const Context &Core::getContext() const @@ -458,17 +461,19 @@ namespace vkcv return m_Context; } - Core::Core(Context &&context, const Window &window , SwapChain swapChain, std::vector<vk::ImageView> imageViews) noexcept : - m_Context(std::move(context)), - m_window(window), - m_swapchain(swapChain), - m_swapchainImageViews(imageViews), + Core::Core(Context &&context, const Window &window , SwapChain swapChain, std::vector<vk::ImageView> imageViews, + const CommandResources& commandResources) noexcept : + m_Context(std::move(context)), + m_window(window), + m_swapchain(swapChain), + m_swapchainImageViews(imageViews), m_NextPipelineId(0), m_Pipelines{}, m_PipelineLayouts{}, m_PassManager{std::make_unique<PassManager>(m_Context.m_Device)}, - m_PipelineManager{std::make_unique<PipelineManager>(m_Context.m_Device)} - {} + m_PipelineManager{std::make_unique<PipelineManager>(m_Context.m_Device)}, + m_CommandResources(commandResources) + {} Core::~Core() noexcept { std::cout << " Core " << std::endl; @@ -491,6 +496,8 @@ namespace vkcv m_Context.m_Device.destroyImageView(image); } + destroyCommandResources(m_Context.m_Device, m_CommandResources); + m_Context.m_Device.destroySwapchainKHR(m_swapchain.getSwapchain()); m_Context.m_Instance.destroySurfaceKHR(m_swapchain.getSurface()); }