diff --git a/lsfg-vk-backend/src/lsfgvk.cpp b/lsfg-vk-backend/src/lsfgvk.cpp index fd5bdf0..1b57f74 100644 --- a/lsfg-vk-backend/src/lsfgvk.cpp +++ b/lsfg-vk-backend/src/lsfgvk.cpp @@ -75,7 +75,7 @@ Context::Context( { width, height }, flowScale, performanceMode, - false /* TODO: HDR */ + false }; this->m_priv = std::make_unique(priv::Context { @@ -158,7 +158,11 @@ void Context::dispatch(uint32_t total) { // Dispatch main passes uint64_t prevInternal{}; for (uint32_t i = 0; i < total; i++) { - const auto& transCmdbuf{ctx.pipeline.getCmdbufs().at(0)}; // FIXME: replace with actual buf + const auto& transCmdbuf{ctx.pipeline.buildTransCmdbuf( + vk.dld, *vk.device, + mapped->iteration, + i, total + )}; // Transition command buffer to next timestamp if (i == 0) { @@ -179,7 +183,7 @@ void Context::dispatch(uint32_t total) { .pWaitSemaphores = i == 0 ? &*internal.first : &*sync.first, .pWaitDstStageMask = &waitStage, .commandBufferCount = 1, - .pCommandBuffers = &*transCmdbuf, + .pCommandBuffers = &transCmdbuf, .signalSemaphoreCount = 1, .pSignalSemaphores = &*internal.first }}, diff --git a/lsfg-vk-backend/src/modules/pipeline.cpp b/lsfg-vk-backend/src/modules/pipeline.cpp index b46f7bc..450c8ac 100644 --- a/lsfg-vk-backend/src/modules/pipeline.cpp +++ b/lsfg-vk-backend/src/modules/pipeline.cpp @@ -805,3 +805,45 @@ Pipeline::Pipeline( LOG_DEBUG(" Recorded command buffers for pipeline execution") LOG_DEBUG("Finished building pipeline") } + +vk::CommandBuffer Pipeline::buildTransCmdbuf( + const vk::detail::DispatchLoaderDynamic& dld, + const vk::Device& device, + uint32_t iteration, + uint32_t index, + uint32_t total +) { + const bool persist{total > 8}; + const uint64_t key{persist ? ((static_cast(index) << 32) | total) : index}; + + if (persist && this->m_transCmdbufs.contains(key)) + return *this->m_transCmdbufs.at(key); + + auto& cmdbuf{this->m_transCmdbufs[key]}; + cmdbuf = vkhelper::createCommandBuffer( + dld, + device, + *this->m_pool + ); + + cmdbuf->begin({ + .flags = persist ? vk::CommandBufferUsageFlagBits::eSimultaneousUse : + vk::CommandBufferUsageFlagBits::eOneTimeSubmit + }, dld); + + const UniformBuffer buf{ + .timestamp = static_cast(index + 1) / static_cast(total + 1), + .iteration = iteration + }; + cmdbuf->updateBuffer( + *this->m_descriptorSet.buffer.first, + 0, + 4, + static_cast(&buf.timestamp), + dld + ); + + cmdbuf->end(dld); + + return *cmdbuf; +} diff --git a/lsfg-vk-backend/src/modules/pipeline.hpp b/lsfg-vk-backend/src/modules/pipeline.hpp index 79e930f..6c81635 100644 --- a/lsfg-vk-backend/src/modules/pipeline.hpp +++ b/lsfg-vk-backend/src/modules/pipeline.hpp @@ -18,8 +18,6 @@ namespace lsfgvk::pipeline { - // TODO: Improve API design - /// Handle to an external image struct ExternalImage { /// Image Extent @@ -60,7 +58,7 @@ namespace lsfgvk::pipeline { /// Create a new pipeline /// /// @param dld Vulkan dispatch loader - /// @param dev Vulkan device + /// @param device Vulkan device /// @param physdev Vulkan physical device /// @param queue Vulkan compute queue /// @param queueFamilyIndex Compute queue family index @@ -74,7 +72,7 @@ namespace lsfgvk::pipeline { /// explicit Pipeline( const vk::detail::DispatchLoaderDynamic& dld, - const vk::Device& dev, + const vk::Device& device, const vk::PhysicalDevice& physdev, const vk::Queue& queue, uint32_t queueFamilyIndex, @@ -118,6 +116,24 @@ namespace lsfgvk::pipeline { return this->m_cmdbufs; } + /// + /// Build a transition command buffer + /// + /// @param dld Vulkan dispatch loader + /// @param device Vulkan device + /// @param iteration Current iteration + /// @param index Index of the iteration + /// @param total Total iterations + /// @return Command buffer handle + /// + vk::CommandBuffer buildTransCmdbuf( + const vk::detail::DispatchLoaderDynamic& dld, + const vk::Device& device, + uint32_t iteration, + uint32_t index, + uint32_t total + ); + private: /// Vulkan descriptor set & pipeline layout struct Layout { @@ -203,6 +219,7 @@ namespace lsfgvk::pipeline { vk::UniqueCommandPool m_pool; std::vector m_cmdbufs; + std::unordered_map m_transCmdbufs; }; }