From cf02e5aa4b385b60b9b9d76e0a3b324ddfa25f42 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Tue, 1 Mar 2016 18:54:36 +0100 Subject: [PATCH] Added function for submitting a pre present image memory barrier, moved barrier setup into functions, removed unnecessary vkQueueWaitIdle --- base/vulkanexamplebase.cpp | 48 +++++++++++++++++++++++++++++--------- base/vulkanexamplebase.h | 10 ++++++-- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 7a170a65..99a39111 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -109,10 +109,12 @@ void VulkanExampleBase::createCommandBuffers() VkResult vkRes = vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, drawCmdBuffers.data()); assert(!vkRes); - // Create one command buffer for submitting the - // post present image memory barrier + // Command buffers for submitting present barriers cmdBufAllocateInfo.commandBufferCount = 1; - + // Pre present + vkRes = vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &prePresentCmdBuffer); + assert(!vkRes); + // Post present vkRes = vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &postPresentCmdBuffer); assert(!vkRes); } @@ -120,6 +122,7 @@ void VulkanExampleBase::createCommandBuffers() void VulkanExampleBase::destroyCommandBuffers() { vkFreeCommandBuffers(device, cmdPool, (uint32_t)drawCmdBuffers.size(), drawCmdBuffers.data()); + vkFreeCommandBuffers(device, cmdPool, 1, &prePresentCmdBuffer); vkFreeCommandBuffers(device, cmdPool, 1, &postPresentCmdBuffer); } @@ -347,7 +350,34 @@ void VulkanExampleBase::renderLoop() #endif } -// todo : comment +void VulkanExampleBase::submitPrePresentBarrier(VkImage image) +{ + VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); + + VkResult vkRes = vkBeginCommandBuffer(prePresentCmdBuffer, &cmdBufInfo); + assert(!vkRes); + + VkImageMemoryBarrier prePresentBarrier = vkTools::prePresentBarrier(image); + vkCmdPipelineBarrier( + prePresentCmdBuffer, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, + VK_FLAGS_NONE, + 0, nullptr, // No memory barriers, + 0, nullptr, // No buffer barriers, + 1, &prePresentBarrier); + + vkRes = vkEndCommandBuffer(prePresentCmdBuffer); + assert(!vkRes); + + VkSubmitInfo submitInfo = vkTools::initializers::submitInfo(); + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = &prePresentCmdBuffer; + + vkRes = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE); + assert(!vkRes); +} + void VulkanExampleBase::submitPostPresentBarrier(VkImage image) { VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); @@ -362,23 +392,19 @@ void VulkanExampleBase::submitPostPresentBarrier(VkImage image) VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, - 0, NULL, // No memory barriers, - 0, NULL, // No buffer barriers, + 0, nullptr, // No memory barriers, + 0, nullptr, // No buffer barriers, 1, &postPresentBarrier); vkRes = vkEndCommandBuffer(postPresentCmdBuffer); assert(!vkRes); - VkSubmitInfo submitInfo = {}; - submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + VkSubmitInfo submitInfo = vkTools::initializers::submitInfo(); submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &postPresentCmdBuffer; vkRes = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE); assert(!vkRes); - - vkRes = vkQueueWaitIdle(queue); - assert(!vkRes); } VulkanExampleBase::VulkanExampleBase(bool enableValidation) diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 6ab74418..659feb00 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -68,8 +68,10 @@ protected: VkCommandPool cmdPool; // Command buffer used for setup VkCommandBuffer setupCmdBuffer = VK_NULL_HANDLE; - // Command buffer for submitting a post present barrier + // Command buffer for submitting a post present image barrier VkCommandBuffer postPresentCmdBuffer = VK_NULL_HANDLE; + // Command buffer for submitting a pre present image barrier + VkCommandBuffer prePresentCmdBuffer = VK_NULL_HANDLE; // Command buffers used for rendering std::vector drawCmdBuffers; // Global render pass for frame buffer writes @@ -230,8 +232,12 @@ public: // Start the main render loop void renderLoop(); + // Submit a pre present image barrier to the queue + // Transforms the (framebuffer) image layout from color attachment to present(khr) for presenting to the swap chain + void submitPrePresentBarrier(VkImage image); + // Submit a post present image barrier to the queue - // Transforms image layout back to color attachment layout + // Transforms the (framebuffer) image layout back from present(khr) to color attachment layout void submitPostPresentBarrier(VkImage image); };