From 8a097afb1b9692455272563ba8dd7ed70b62ca16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sun, 27 Feb 2022 18:37:05 +0100 Subject: [PATCH] Graphics/FramePass: Replace render rect parameter of CommandCallback by FramePassEnvironment --- examples/DeferredShading/main.cpp | 66 ++++++++++---------- include/Nazara/Graphics/FramePass.hpp | 11 +++- include/Nazara/OpenGLRenderer/Utils.inl | 1 + src/Nazara/Graphics/BakedFrameGraph.cpp | 8 ++- src/Nazara/Graphics/DepthPipelinePass.cpp | 2 +- src/Nazara/Graphics/ForwardFramePipeline.cpp | 6 +- src/Nazara/Graphics/ForwardPipelinePass.cpp | 2 +- 7 files changed, 56 insertions(+), 40 deletions(-) diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index cf339da1f..6fc6d8477 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -840,14 +840,14 @@ int main() return Nz::FramePassExecution::Execute; }); - gbufferPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + gbufferPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetViewport(renderArea); + builder.SetViewport(env.renderRect); std::vector> elements; - spaceshipModel.BuildElement(forwardPassIndex, modelInstance1, elements, renderArea); - spaceshipModel.BuildElement(forwardPassIndex, modelInstance2, elements, renderArea); - planeModel.BuildElement(forwardPassIndex, planeInstance, elements, renderArea); + spaceshipModel.BuildElement(forwardPassIndex, modelInstance1, elements, env.renderRect); + spaceshipModel.BuildElement(forwardPassIndex, modelInstance2, elements, env.renderRect); + planeModel.BuildElement(forwardPassIndex, planeInstance, elements, env.renderRect); std::vector> elementPointers; std::vector renderStates(elements.size()); @@ -866,10 +866,10 @@ int main() return (lightUpdate) ? Nz::FramePassExecution::UpdateAndExecute : Nz::FramePassExecution::Execute; }); - lightingPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + lightingPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); //builder.BindVertexBuffer(0, vertexBuffer.get()); builder.BindIndexBuffer(*coneMeshGfx->GetIndexBuffer(0).get()); @@ -897,10 +897,10 @@ int main() lightingPass.SetDepthStencilInput(depthBuffer1); Nz::FramePass& forwardPass = graph.AddPass("Forward pass"); - forwardPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + forwardPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *skyboxShaderBinding); @@ -911,7 +911,7 @@ int main() builder.DrawIndexed(Nz::SafeCast(cubeMeshGfx->GetIndexCount(0))); std::vector> elements; - flareSprite.BuildElement(forwardPassIndex, flareInstance, elements, renderArea); + flareSprite.BuildElement(forwardPassIndex, flareInstance, elements, env.renderRect); std::vector> elementPointers; std::vector renderStates(elements.size()); @@ -935,12 +935,12 @@ int main() Nz::FramePass& occluderPass = graph.AddPass("Occluder pass"); - occluderPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + occluderPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetViewport(renderArea); + builder.SetViewport(env.renderRect); std::vector> elements; - flareSprite.BuildElement(forwardPassIndex, flareInstance, elements, renderArea); + flareSprite.BuildElement(forwardPassIndex, flareInstance, elements, env.renderRect); std::vector> elementPointers; std::vector renderStates(elements.size()); @@ -959,10 +959,10 @@ int main() occluderPass.SetDepthStencilInput(depthBuffer1); Nz::FramePass& godraysPass = graph.AddPass("Light scattering pass"); - godraysPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + godraysPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *godRaysShaderBinding); @@ -976,10 +976,10 @@ int main() godraysPass.AddOutput(godRaysTexture); Nz::FramePass& bloomBrightPass = graph.AddPass("Bloom pass - extract bright pixels"); - bloomBrightPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + bloomBrightPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *bloomBrightShaderBinding); @@ -1000,10 +1000,10 @@ int main() for (std::size_t i = 0; i < BloomSubdivisionCount; ++i) { Nz::FramePass& bloomBlurPassHorizontal = graph.AddPass("Bloom pass - gaussian blur #" + std::to_string(i) + " - horizontal"); - bloomBlurPassHorizontal.SetCommandCallback([&, i](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + bloomBlurPassHorizontal.SetCommandCallback([&, i](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *gaussianBlurShaderBinding[i * 2 + 0]); builder.BindPipeline(*gaussianBlurPipeline); @@ -1021,10 +1021,10 @@ int main() bloomBlurPassHorizontal.AddOutput(bloomTextures[bloomTextureIndex]); Nz::FramePass& bloomBlurPassVertical = graph.AddPass("Bloom pass - gaussian blur #" + std::to_string(i) + " - vertical"); - bloomBlurPassVertical.SetCommandCallback([&, i](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + bloomBlurPassVertical.SetCommandCallback([&, i](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *gaussianBlurShaderBinding[i * 2 + 1]); builder.BindPipeline(*gaussianBlurPipeline); @@ -1043,10 +1043,10 @@ int main() } Nz::FramePass& bloomBlendPass = graph.AddPass("Bloom pass - blend"); - bloomBlendPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + bloomBlendPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindVertexBuffer(0, *fullscreenVertexBuffer); // Blend bloom @@ -1079,10 +1079,10 @@ int main() Nz::FramePass& toneMappingPass = graph.AddPass("Tone mapping"); toneMappingPass.AddInput(bloomOutput); toneMappingPass.AddOutput(toneMappingOutput); - toneMappingPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::Recti& renderArea) + toneMappingPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderArea); - builder.SetViewport(renderArea); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); builder.BindShaderBinding(0, *toneMappingShaderBinding); builder.BindPipeline(*toneMappingPipeline); diff --git a/include/Nazara/Graphics/FramePass.hpp b/include/Nazara/Graphics/FramePass.hpp index 6e94ec39b..7e9db3101 100644 --- a/include/Nazara/Graphics/FramePass.hpp +++ b/include/Nazara/Graphics/FramePass.hpp @@ -19,8 +19,10 @@ namespace Nz { + class BakedFrameGraph; class CommandBufferBuilder; class FrameGraph; + class RenderFrame; enum class FramePassExecution { @@ -29,10 +31,17 @@ namespace Nz UpdateAndExecute }; + struct FramePassEnvironment + { + BakedFrameGraph& frameGraph; + Recti renderRect; + RenderFrame& renderFrame; + }; + class NAZARA_GRAPHICS_API FramePass { public: - using CommandCallback = std::function; + using CommandCallback = std::function; using ExecutionCallback = std::function; struct DepthStencilClear; struct Input; diff --git a/include/Nazara/OpenGLRenderer/Utils.inl b/include/Nazara/OpenGLRenderer/Utils.inl index d119a3123..07983abe7 100644 --- a/include/Nazara/OpenGLRenderer/Utils.inl +++ b/include/Nazara/OpenGLRenderer/Utils.inl @@ -21,6 +21,7 @@ namespace Nz case PixelFormat::BGRA8: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA }; case PixelFormat::BGRA8_SRGB: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA }; case PixelFormat::Depth16: return GLTextureFormat{ GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_RED, GL_ZERO, GL_ZERO, GL_ZERO }; + case PixelFormat::Depth24: return GLTextureFormat{ GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, GL_RED, GL_ZERO, GL_ZERO, GL_ZERO }; case PixelFormat::Depth24Stencil8: return GLTextureFormat{ GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_RED, GL_GREEN, GL_ZERO, GL_ZERO }; case PixelFormat::Depth32F: return GLTextureFormat{ GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, GL_RED, GL_ZERO, GL_ZERO, GL_ZERO }; case PixelFormat::Depth32FStencil8: return GLTextureFormat{ GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, GL_RED, GL_GREEN, GL_ZERO, GL_ZERO }; diff --git a/src/Nazara/Graphics/BakedFrameGraph.cpp b/src/Nazara/Graphics/BakedFrameGraph.cpp index 0b9dca644..0182185a4 100644 --- a/src/Nazara/Graphics/BakedFrameGraph.cpp +++ b/src/Nazara/Graphics/BakedFrameGraph.cpp @@ -67,6 +67,12 @@ namespace Nz if (!passData.name.empty()) builder.BeginDebugRegion(passData.name, Color::Green); + FramePassEnvironment env{ + *this, + passData.renderRect, + renderFrame + }; + bool first = true; for (auto& subpass : passData.subpasses) { @@ -75,7 +81,7 @@ namespace Nz first = false; - subpass.commandCallback(builder, passData.renderRect); + subpass.commandCallback(builder, env); } if (!passData.name.empty()) diff --git a/src/Nazara/Graphics/DepthPipelinePass.cpp b/src/Nazara/Graphics/DepthPipelinePass.cpp index 56b485dd9..3826b8279 100644 --- a/src/Nazara/Graphics/DepthPipelinePass.cpp +++ b/src/Nazara/Graphics/DepthPipelinePass.cpp @@ -137,7 +137,7 @@ namespace Nz return (m_rebuildCommandBuffer) ? FramePassExecution::UpdateAndExecute : FramePassExecution::Execute; }); - depthPrepass.SetCommandCallback([this](CommandBufferBuilder& builder, const Recti& /*renderRect*/) + depthPrepass.SetCommandCallback([this](CommandBufferBuilder& builder, const FramePassEnvironment& /*env*/) { Recti viewport = m_viewer->GetViewport(); diff --git a/src/Nazara/Graphics/ForwardFramePipeline.cpp b/src/Nazara/Graphics/ForwardFramePipeline.cpp index 73688d3a0..ad6916161 100644 --- a/src/Nazara/Graphics/ForwardFramePipeline.cpp +++ b/src/Nazara/Graphics/ForwardFramePipeline.cpp @@ -532,10 +532,10 @@ namespace Nz mergePass.AddOutput(renderTargetData.finalAttachment); mergePass.SetClearColor(0, Color::Black); - mergePass.SetCommandCallback([&targetViewers](CommandBufferBuilder& builder, const Recti& renderRect) + mergePass.SetCommandCallback([&targetViewers](CommandBufferBuilder& builder, const Nz::FramePassEnvironment& env) { - builder.SetScissor(renderRect); - builder.SetViewport(renderRect); + builder.SetScissor(env.renderRect); + builder.SetViewport(env.renderRect); Graphics* graphics = Graphics::Instance(); builder.BindPipeline(*graphics->GetBlitPipeline(false)); diff --git a/src/Nazara/Graphics/ForwardPipelinePass.cpp b/src/Nazara/Graphics/ForwardPipelinePass.cpp index f7f12640b..dee3e9fa4 100644 --- a/src/Nazara/Graphics/ForwardPipelinePass.cpp +++ b/src/Nazara/Graphics/ForwardPipelinePass.cpp @@ -282,7 +282,7 @@ namespace Nz return (m_rebuildCommandBuffer) ? FramePassExecution::UpdateAndExecute : FramePassExecution::Execute; }); - forwardPass.SetCommandCallback([this](CommandBufferBuilder& builder, const Recti& /*renderRect*/) + forwardPass.SetCommandCallback([this](CommandBufferBuilder& builder, const FramePassEnvironment& /*env*/) { Recti viewport = m_viewer->GetViewport();