Graphics/BakedFrameGraph: Handle resize in a better way

This commit is contained in:
Jérôme Leclercq 2021-07-03 13:57:18 +02:00
parent c42d18243b
commit 1c482bbde6
2 changed files with 27 additions and 8 deletions

View File

@ -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<Texture>& GetAttachmentTexture(std::size_t attachmentIndex) const;
const std::shared_ptr<RenderPass>& 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<TextureTransition> transitions;
FramePass::ExecutionCallback executionCallback;
Recti renderRect;
bool forceCommandBufferRegeneration = true;
};
struct TextureData
@ -91,6 +93,8 @@ namespace Nz
std::vector<TextureData> m_textures;
AttachmentIdToTextureId m_attachmentToTextureMapping;
PassIdToPhysicalPassIndex m_passIdToPhysicalPassMapping;
unsigned int m_height;
unsigned int m_width;
};
}

View File

@ -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>& 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>& 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;
}
}