From 53750df002b1296236cddf56acff69f271d39736 Mon Sep 17 00:00:00 2001 From: Razakhel Date: Sat, 2 Nov 2024 15:10:18 +0100 Subject: [PATCH] [Render/RenderSystem] The copied frame is the last executed pass' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The render pass having been executed last is stored in the render graph and used afterward - This is something that will be extremely useful later, e.g. for gamma correction, although it may be a bit too naïve at the moment - The last executed pass may not be the one the user wants to be displayed: there may be several final passes, one debug detached pass that's executed after the main ones, etc --- examples/xrDemo.cpp | 1 + include/RaZ/Render/RenderGraph.hpp | 1 + src/RaZ/Render/RenderGraph.cpp | 2 ++ src/RaZ/Render/RenderSystem.cpp | 19 +++++++++++++------ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/xrDemo.cpp b/examples/xrDemo.cpp index 8ce89884..a0f0b7ff 100644 --- a/examples/xrDemo.cpp +++ b/examples/xrDemo.cpp @@ -37,6 +37,7 @@ int main() { const auto colorBuffer = Raz::Texture2D::create(2468, 2584, Raz::TextureColorspace::RGBA); const auto depthBuffer = Raz::Texture2D::create(2468, 2584, Raz::TextureColorspace::DEPTH); + // The last executed pass *MUST* have a write color buffer, and at least the geometry pass *MUST* have a write depth buffer Raz::RenderPass& geomPass = render.getGeometryPass(); geomPass.addWriteColorTexture(colorBuffer, 0); geomPass.setWriteDepthTexture(depthBuffer); diff --git a/include/RaZ/Render/RenderGraph.hpp b/include/RaZ/Render/RenderGraph.hpp index 079b7fda..3e4a0752 100644 --- a/include/RaZ/Render/RenderGraph.hpp +++ b/include/RaZ/Render/RenderGraph.hpp @@ -52,6 +52,7 @@ class RenderGraph : public Graph { RenderPass m_geometryPass {}; std::vector> m_renderProcesses {}; std::unordered_set m_executedPasses {}; + const RenderPass* m_lastExecutedPass {}; }; } // namespace Raz diff --git a/src/RaZ/Render/RenderGraph.cpp b/src/RaZ/Render/RenderGraph.cpp index cd5ccb38..bab87107 100644 --- a/src/RaZ/Render/RenderGraph.cpp +++ b/src/RaZ/Render/RenderGraph.cpp @@ -44,6 +44,7 @@ void RenderGraph::execute(RenderSystem& renderSystem) { } executeGeometryPass(renderSystem); + m_lastExecutedPass = &m_geometryPass; m_executedPasses.reserve(m_nodes.size() + 1); m_executedPasses.emplace(&m_geometryPass); @@ -110,6 +111,7 @@ void RenderGraph::executePass(const RenderPass& renderPass) { executePass(*parentPass); renderPass.execute(); + m_lastExecutedPass = &renderPass; m_executedPasses.emplace(&renderPass); } diff --git a/src/RaZ/Render/RenderSystem.cpp b/src/RaZ/Render/RenderSystem.cpp index 6d80ce75..48115469 100644 --- a/src/RaZ/Render/RenderSystem.cpp +++ b/src/RaZ/Render/RenderSystem.cpp @@ -336,18 +336,25 @@ void RenderSystem::renderXrFrame() { m_renderGraph.execute(*this); - // TODO: the returned color buffer must be the one from the render pass executed last - const Framebuffer& geomFramebuffer = m_renderGraph.m_geometryPass.getFramebuffer(); - return std::make_pair(std::cref(geomFramebuffer.getColorBuffer(0)), std::cref(geomFramebuffer.getDepthBuffer())); + assert("Error: There is no valid last executed pass." && m_renderGraph.m_lastExecutedPass); + const Framebuffer& finalFramebuffer = m_renderGraph.m_lastExecutedPass->getFramebuffer(); + assert("Error: The last executed pass must have at least one write color buffer." && finalFramebuffer.getColorBufferCount() >= 1); + assert("Error: Either the last executed pass or the geometry pass must have a write depth buffer." + && (finalFramebuffer.hasDepthBuffer() || m_renderGraph.m_geometryPass.getFramebuffer().hasDepthBuffer())); + + const Texture2D& depthBuffer = (finalFramebuffer.hasDepthBuffer() ? finalFramebuffer.getDepthBuffer() + : m_renderGraph.m_geometryPass.getFramebuffer().getDepthBuffer()); + return std::make_pair(std::cref(finalFramebuffer.getColorBuffer(0)), std::cref(depthBuffer)); }); #if !defined(RAZ_NO_WINDOW) if (!hasRendered) return; - // TODO: the copied color buffer must be the one from the render pass executed last - const Framebuffer& geomFramebuffer = m_renderGraph.m_geometryPass.getFramebuffer(); - copyToWindow(geomFramebuffer.getColorBuffer(0), geomFramebuffer.getDepthBuffer(), m_window->getWidth(), m_window->getHeight()); + const Framebuffer& finalFramebuffer = m_renderGraph.m_lastExecutedPass->getFramebuffer(); + const Texture2D& depthBuffer = (finalFramebuffer.hasDepthBuffer() ? finalFramebuffer.getDepthBuffer() + : m_renderGraph.m_geometryPass.getFramebuffer().getDepthBuffer()); + copyToWindow(finalFramebuffer.getColorBuffer(0), depthBuffer, m_window->getWidth(), m_window->getHeight()); #endif } #endif