diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp index c23a64b6369c2ef64c35235967433cb67831a972..0712be4887d8d27185bc7ccdd0a4f6c318296ee4 100644 --- a/include/vkcv/Core.hpp +++ b/include/vkcv/Core.hpp @@ -29,6 +29,9 @@ namespace vkcv std::vector<vk::Semaphore> waitSemaphores; std::vector<vk::Semaphore> signalSemaphores; }; + + typedef std::function<void(const vk::CommandBuffer& cmdBuffer)> RecordCommandFunction; + typedef std::function<void(void)> FinishCommandFunction; class Core final { @@ -179,9 +182,15 @@ namespace vkcv vk::Format getSwapchainImageFormat(); - void submitCommands( - const SubmitInfo &submitInfo, - const std::function<void(vk::CommandBuffer cmdBuffer)> recording, - const std::function<void()> finishCallback); + /** + * Submit a command buffer to any queue of selected type. The recording can be customized by a + * custom record-command-function. If the command submission has finished, an optional finish-function + * will be called. + * + * @param submitInfo Submit information + * @param record Record-command-function + * @param finish Finish-command-function or nullptr + */ + void submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish); }; } diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp index d6785892ff2566d81671059cae1313354c17ea2e..e5e847ab8fd112ef8516305fd73e2f2209da0024 100644 --- a/src/vkcv/Core.cpp +++ b/src/vkcv/Core.cpp @@ -173,7 +173,7 @@ namespace vkcv SubmitInfo submitInfo; submitInfo.queueType = QueueType::Graphics; submitInfo.signalSemaphores = { m_SyncResources.renderFinished }; - submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline](const vk::CommandBuffer cmdBuffer) { + submitCommands(submitInfo, [renderpass, renderArea, imageView, framebuffer, pipeline](const vk::CommandBuffer& cmdBuffer) { const std::array<float, 4> clearColor = { 0.f, 0.f, 0.f, 1.f }; const vk::ClearValue clearValues(clearColor); @@ -222,12 +222,9 @@ namespace vkcv /* boilerplate for #34 */ std::cout << "Resized to : " << width << " , " << height << std::endl; } - - void Core::submitCommands( - const SubmitInfo& submitInfo, - const std::function<void(vk::CommandBuffer cmdBuffer)> recording, - const std::function<void()> finishCallback) { - + + void Core::submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish) + { const vk::Device& device = m_Context.getDevice(); const vkcv::Queue queue = getQueueForSubmit(submitInfo.queueType, m_Context.getQueueManager()); @@ -235,16 +232,18 @@ namespace vkcv const vk::CommandBuffer cmdBuffer = allocateCommandBuffer(device, cmdPool); beginCommandBuffer(cmdBuffer, vk::CommandBufferUsageFlagBits::eOneTimeSubmit); - recording(cmdBuffer); + record(cmdBuffer); cmdBuffer.end(); const vk::Fence waitFence = createFence(device); submitCommandBufferToQueue(queue.handle, cmdBuffer, waitFence, submitInfo.waitSemaphores, submitInfo.signalSemaphores); waitForFence(device, waitFence); device.destroyFence(waitFence); - if (finishCallback) { - finishCallback(); - } + device.freeCommandBuffers(cmdPool, cmdBuffer); + + if (finish) { + finish(); + } } }