From 1c482bbde6d6a56450ad9e258ef528a14a71a8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sat, 3 Jul 2021 13:57:18 +0200 Subject: [PATCH] Graphics/BakedFrameGraph: Handle resize in a better way --- include/Nazara/Graphics/BakedFrameGraph.hpp | 6 ++++- src/Nazara/Graphics/BakedFrameGraph.cpp | 29 ++++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/Nazara/Graphics/BakedFrameGraph.hpp b/include/Nazara/Graphics/BakedFrameGraph.hpp index d3c609f56..7aec5064d 100644 --- a/include/Nazara/Graphics/BakedFrameGraph.hpp +++ b/include/Nazara/Graphics/BakedFrameGraph.hpp @@ -26,6 +26,7 @@ namespace Nz friend class FrameGraph; public: + BakedFrameGraph() = default; BakedFrameGraph(const BakedFrameGraph&) = delete; BakedFrameGraph(BakedFrameGraph&&) noexcept = default; ~BakedFrameGraph() = default; @@ -35,7 +36,7 @@ namespace Nz const std::shared_ptr& GetAttachmentTexture(std::size_t attachmentIndex) const; const std::shared_ptr& GetRenderPass(std::size_t passIndex) const; - void Resize(unsigned int width, unsigned int height); + bool Resize(unsigned int width, unsigned int height); BakedFrameGraph& operator=(const BakedFrameGraph&) = delete; BakedFrameGraph& operator=(BakedFrameGraph&&) noexcept = default; @@ -75,6 +76,7 @@ namespace Nz std::vector transitions; FramePass::ExecutionCallback executionCallback; Recti renderRect; + bool forceCommandBufferRegeneration = true; }; struct TextureData @@ -91,6 +93,8 @@ namespace Nz std::vector m_textures; AttachmentIdToTextureId m_attachmentToTextureMapping; PassIdToPhysicalPassIndex m_passIdToPhysicalPassMapping; + unsigned int m_height; + unsigned int m_width; }; } diff --git a/src/Nazara/Graphics/BakedFrameGraph.cpp b/src/Nazara/Graphics/BakedFrameGraph.cpp index 5688174e9..906d4e7fa 100644 --- a/src/Nazara/Graphics/BakedFrameGraph.cpp +++ b/src/Nazara/Graphics/BakedFrameGraph.cpp @@ -14,7 +14,9 @@ namespace Nz m_passes(std::move(passes)), m_textures(std::move(textures)), m_attachmentToTextureMapping(std::move(attachmentIdToTextureMapping)), - m_passIdToPhysicalPassMapping(std::move(passIdToPhysicalPassMapping)) + m_passIdToPhysicalPassMapping(std::move(passIdToPhysicalPassMapping)), + m_height(0), + m_width(0) { const std::shared_ptr& renderDevice = Graphics::Instance()->GetRenderDevice(); m_commandPool = renderDevice->InstantiateCommandPool(QueueType::Graphics); @@ -24,7 +26,7 @@ namespace Nz { for (auto& passData : m_passes) { - bool regenerateCommandBuffer = (passData.commandBuffer == nullptr); + bool regenerateCommandBuffer = (passData.forceCommandBufferRegeneration || passData.commandBuffer == nullptr); if (passData.executionCallback) { switch (passData.executionCallback()) @@ -33,8 +35,11 @@ namespace Nz break; case FramePassExecution::Skip: - renderFrame.PushForRelease(std::move(passData.commandBuffer)); - passData.commandBuffer.reset(); + if (passData.commandBuffer) + { + renderFrame.PushForRelease(std::move(passData.commandBuffer)); + passData.commandBuffer.reset(); + } continue; //< Skip the pass case FramePassExecution::UpdateAndExecute: @@ -46,7 +51,9 @@ namespace Nz if (!regenerateCommandBuffer) continue; - renderFrame.PushForRelease(std::move(passData.commandBuffer)); + if (passData.commandBuffer) + renderFrame.PushForRelease(std::move(passData.commandBuffer)); + passData.commandBuffer = m_commandPool->BuildCommandBuffer([&](CommandBufferBuilder& builder) { for (auto& textureTransition : passData.transitions) @@ -68,7 +75,7 @@ namespace Nz first = false; - subpass.commandCallback(builder); + subpass.commandCallback(builder, passData.renderRect); } builder.EndRenderPass(); @@ -76,6 +83,8 @@ namespace Nz if (!passData.name.empty()) builder.EndDebugRegion(); }); + + passData.forceCommandBufferRegeneration = false; } //TODO: Submit all commands buffer at once @@ -114,8 +123,11 @@ namespace Nz return m_passes[physicalPassIndex].renderPass; } - void BakedFrameGraph::Resize(unsigned int width, unsigned int height) + bool BakedFrameGraph::Resize(unsigned int width, unsigned int height) { + if (m_width == width && m_height == height) + return false; + const std::shared_ptr& renderDevice = Graphics::Instance()->GetRenderDevice(); // Delete previous textures to make some room in VRAM @@ -162,6 +174,9 @@ namespace Nz passData.renderRect.Set(0, 0, int(framebufferWidth), int(framebufferHeight)); passData.framebuffer = renderDevice->InstantiateFramebuffer(framebufferWidth, framebufferHeight, passData.renderPass, textures); + passData.forceCommandBufferRegeneration = true; } + + return true; } }