From 046d0f2c422048bb7ff87846e97dd1f300c2e330 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Sun, 5 Jun 2016 17:56:53 +0200 Subject: [PATCH] Additional semaphore to synchronize offscreen and final render (Refs #70) --- bloom/bloom.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bloom/bloom.cpp b/bloom/bloom.cpp index 3949d657..3bceec8a 100644 --- a/bloom/bloom.cpp +++ b/bloom/bloom.cpp @@ -124,12 +124,16 @@ public: FrameBufferAttachment color, depth; // Texture target for framebuffer blit vkTools::VulkanTexture textureTarget; + VkSampler colorSampler; } offScreenFrameBuf, offScreenFrameBufB; // Used to store commands for rendering and blitting // the offscreen scene VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE; + // Semaphore used to synchronize between offscreen and final scene rendering + VkSemaphore offscreenSemaphore = VK_NULL_HANDLE; + VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { zoom = -10.25f; @@ -192,6 +196,7 @@ public: vkTools::destroyUniformData(device, &uniformData.fsHorzBlur); vkFreeCommandBuffers(device, cmdPool, 1, &offScreenCmdBuffer); + vkDestroySemaphore(device, offscreenSemaphore, nullptr); textureLoader->destroyTexture(textures.cubemap); } @@ -394,18 +399,22 @@ public: VulkanExampleBase::flushCommandBuffer(cmdBuffer, queue, true); } - void createOffscreenCommandBuffer() - { - VkCommandBufferAllocateInfo cmd = vkTools::initializers::commandBufferAllocateInfo( - cmdPool, - VK_COMMAND_BUFFER_LEVEL_PRIMARY, - 1); - VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmd, &offScreenCmdBuffer)); - } - - // Render the 3D scene into a texture target + // Sets up the command buffer that renders the scene to the offscreen frame buffer + // The blur method used in this example is multi pass and renders the vertical + // blur first and then the horizontal one. + // While it's possible to blur in one pass, this method is widely used as it + // requires far less samples to generate the blur void buildOffscreenCommandBuffer() { + if (offScreenCmdBuffer == VK_NULL_HANDLE) + { + offScreenCmdBuffer = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false); + } + + // Create a semaphore used to synchronize offscreen rendering and usage + VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo(); + VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenSemaphore)); + VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); // Horizontal blur @@ -1207,7 +1216,6 @@ public: preparePipelines(); setupDescriptorPool(); setupDescriptorSet(); - createOffscreenCommandBuffer(); buildCommandBuffers(); prepared = true; }