From 4ccdaea970c0d4bce38d09831d59c2de66e6d0e8 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Wed, 9 Apr 2025 00:12:18 -0700 Subject: [PATCH] Added KRRenderGraphDeferred and KRRenderGraphForward --- kraken/CMakeLists.txt | 2 + kraken/KRRenderGraphDeferred.cpp | 154 +++++++++++++++++++++++++++++++ kraken/KRRenderGraphDeferred.h | 44 +++++++++ kraken/KRRenderGraphForward.cpp | 129 ++++++++++++++++++++++++++ kraken/KRRenderGraphForward.h | 44 +++++++++ kraken/KRSurface.cpp | 142 ++++------------------------ kraken/KRSurface.h | 5 +- 7 files changed, 397 insertions(+), 123 deletions(-) create mode 100644 kraken/KRRenderGraphDeferred.cpp create mode 100644 kraken/KRRenderGraphDeferred.h create mode 100644 kraken/KRRenderGraphForward.cpp create mode 100644 kraken/KRRenderGraphForward.h diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index 1977c53..da4eb6c 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -83,6 +83,8 @@ add_source_and_header(KROctreeNode) add_source_and_header(KRPresentationThread) add_source_and_header(KRRenderGraph) add_source_and_header(KRRenderGraphBlackFrame) +add_source_and_header(KRRenderGraphDeferred) +add_source_and_header(KRRenderGraphForward) add_source_and_header(KRRenderSettings) add_source_and_header(KRPipeline) add_source_and_header(KRPipelineManager) diff --git a/kraken/KRRenderGraphDeferred.cpp b/kraken/KRRenderGraphDeferred.cpp new file mode 100644 index 0000000..1eb2c1a --- /dev/null +++ b/kraken/KRRenderGraphDeferred.cpp @@ -0,0 +1,154 @@ +// +// KRRenderGraphDeferred.cpp +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRREnderGraphDeferred.h" +#include "KRRenderPass.h" +#include "KRSurface.h" +#include "KRDevice.h" + +KRRenderGraphDeferred::KRRenderGraphDeferred(KRContext& context) + : KRRenderGraph(context) +{ + +} + +KRRenderGraphDeferred::~KRRenderGraphDeferred() +{ +} + +KrResult KRRenderGraphDeferred::initialize(KRSurface &surface) +{ + VkFormat depthImageFormat = VK_FORMAT_UNDEFINED; + KrResult res = KR_SUCCESS; + res = surface.getDevice()->selectDepthFormat(depthImageFormat); + if (res != KR_SUCCESS) { + return res; + } + + // ----- Configuration ----- + int shadow_buffer_count = 0; + bool enable_deferred_lighting = true; + // ------------------------- + + int attachment_compositeDepth = addAttachment("Composite Depth", depthImageFormat); + int attachment_compositeColor = addAttachment("Composite Color", surface.getSurfaceFormat()); + int attachment_lightAccumulation = addAttachment("Light Accumulation", VK_FORMAT_B8G8R8A8_UINT); + int attachment_gbuffer = addAttachment("GBuffer", VK_FORMAT_B8G8R8A8_UINT); + int attachment_shadow_cascades[3]; + attachment_shadow_cascades[0] = addAttachment("Shadow Cascade 0", VK_FORMAT_D32_SFLOAT); + attachment_shadow_cascades[1] = addAttachment("Shadow Cascade 1", VK_FORMAT_D32_SFLOAT); + attachment_shadow_cascades[2] = addAttachment("Shadow Cascade 2", VK_FORMAT_D32_SFLOAT); + + RenderPassInfo info{}; + info.finalPass = false; + + info.type = RenderPassType::RENDER_PASS_PRESTREAM; + addRenderPass(*surface.getDevice(), info); + + for (int shadow_index = 0; shadow_index < shadow_buffer_count; shadow_index++) { + info.depthAttachment.id = attachment_shadow_cascades[shadow_index]; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + info.type = RenderPassType::RENDER_PASS_SHADOWMAP; + addRenderPass(*surface.getDevice(), info); + } + + + // ----====---- Opaque Geometry, Deferred rendering Pass 1 ----====---- + + info.depthAttachment.id = attachment_compositeDepth; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + + info.colorAttachments[0].id = attachment_compositeColor; + info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.type = RenderPassType::RENDER_PASS_DEFERRED_GBUFFER; + addRenderPass(*surface.getDevice(), info); + + // ----====---- Opaque Geometry, Deferred rendering Pass 2 ----====---- + + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + + info.colorAttachments[1].id = attachment_lightAccumulation; + info.colorAttachments[1].clearVaue.color.float32[0] = 0.0f; + info.colorAttachments[1].clearVaue.color.float32[1] = 0.0f; + info.colorAttachments[1].clearVaue.color.float32[2] = 0.0f; + info.colorAttachments[1].clearVaue.color.float32[3] = 0.0f; + info.colorAttachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.colorAttachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.type = RenderPassType::RENDER_PASS_DEFERRED_LIGHTS; + addRenderPass(*surface.getDevice(), info); + + // ----====---- Opaque Geometry, Deferred rendering Pass 3 ----====---- + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.colorAttachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.type = RenderPassType::RENDER_PASS_DEFERRED_OPAQUE; + addRenderPass(*surface.getDevice(), info); + + info.colorAttachments[1] = {}; + + // ----====---- Transparent Geometry, Forward Rendering ----====---- + info.depthAttachment.id = attachment_compositeDepth; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + + info.colorAttachments[0].id = attachment_compositeColor; + info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + + info.type = RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT; + addRenderPass(*surface.getDevice(), info); + + info.type = RenderPassType::RENDER_PASS_DEBUG_OVERLAYS; + addRenderPass(*surface.getDevice(), info); + + info.finalPass = true; + info.type = RenderPassType::RENDER_PASS_POST_COMPOSITE; + addRenderPass(*surface.getDevice(), info); + + return KR_SUCCESS; +} diff --git a/kraken/KRRenderGraphDeferred.h b/kraken/KRRenderGraphDeferred.h new file mode 100644 index 0000000..2373336 --- /dev/null +++ b/kraken/KRRenderGraphDeferred.h @@ -0,0 +1,44 @@ +// +// KRRenderGraphDeferred.h +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#pragma once + +#include "KRRenderGraph.h" + +class KRRenderGraphDeferred : public KRRenderGraph +{ +public: + KRRenderGraphDeferred(KRContext& context); + ~KRRenderGraphDeferred(); + + KrResult initialize(KRSurface& surface); + +}; diff --git a/kraken/KRRenderGraphForward.cpp b/kraken/KRRenderGraphForward.cpp new file mode 100644 index 0000000..ca5aacc --- /dev/null +++ b/kraken/KRRenderGraphForward.cpp @@ -0,0 +1,129 @@ +// +// KRRenderGraphForward.cpp +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRREnderGraphForward.h" +#include "KRRenderPass.h" +#include "KRSurface.h" +#include "KRDevice.h" + +KRRenderGraphForward::KRRenderGraphForward(KRContext& context) + : KRRenderGraph(context) +{ + +} + +KRRenderGraphForward::~KRRenderGraphForward() +{ +} + +KrResult KRRenderGraphForward::initialize(KRSurface &surface) +{ + VkFormat depthImageFormat = VK_FORMAT_UNDEFINED; + KrResult res = KR_SUCCESS; + res = surface.getDevice()->selectDepthFormat(depthImageFormat); + if (res != KR_SUCCESS) { + return res; + } + + // ----- Configuration ----- + int shadow_buffer_count = 0; + // ------------------------- + + int attachment_compositeDepth = addAttachment("Composite Depth", depthImageFormat); + int attachment_compositeColor = addAttachment("Composite Color", surface.getSurfaceFormat()); + int attachment_lightAccumulation = addAttachment("Light Accumulation", VK_FORMAT_B8G8R8A8_UINT); + int attachment_gbuffer = addAttachment("GBuffer", VK_FORMAT_B8G8R8A8_UINT); + int attachment_shadow_cascades[3]; + attachment_shadow_cascades[0] = addAttachment("Shadow Cascade 0", VK_FORMAT_D32_SFLOAT); + attachment_shadow_cascades[1] = addAttachment("Shadow Cascade 1", VK_FORMAT_D32_SFLOAT); + attachment_shadow_cascades[2] = addAttachment("Shadow Cascade 2", VK_FORMAT_D32_SFLOAT); + + RenderPassInfo info{}; + info.finalPass = false; + + info.type = RenderPassType::RENDER_PASS_PRESTREAM; + addRenderPass(*surface.getDevice(), info); + + for (int shadow_index = 0; shadow_index < shadow_buffer_count; shadow_index++) { + info.depthAttachment.id = attachment_shadow_cascades[shadow_index]; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + info.type = RenderPassType::RENDER_PASS_SHADOWMAP; + addRenderPass(*surface.getDevice(), info); + } + + // ----====---- Opaque Geometry, Forward Rendering ----====---- + info.depthAttachment.id = attachment_compositeDepth; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + + info.colorAttachments[0].id = attachment_compositeColor; + info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + + info.type = RenderPassType::RENDER_PASS_FORWARD_OPAQUE; + addRenderPass(*surface.getDevice(), info); + + // ----====---- Transparent Geometry, Forward Rendering ----====---- + info.depthAttachment.id = attachment_compositeDepth; + info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; + info.depthAttachment.clearVaue.depthStencil.stencil = 0; + + info.colorAttachments[0].id = attachment_compositeColor; + info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; + info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; + info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; + info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + + info.type = RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT; + addRenderPass(*surface.getDevice(), info); + + info.type = RenderPassType::RENDER_PASS_DEBUG_OVERLAYS; + addRenderPass(*surface.getDevice(), info); + + info.finalPass = true; + info.type = RenderPassType::RENDER_PASS_POST_COMPOSITE; + addRenderPass(*surface.getDevice(), info); + + return KR_SUCCESS; +} diff --git a/kraken/KRRenderGraphForward.h b/kraken/KRRenderGraphForward.h new file mode 100644 index 0000000..8d7a5e6 --- /dev/null +++ b/kraken/KRRenderGraphForward.h @@ -0,0 +1,44 @@ +// +// KRRenderGraphForward.h +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#pragma once + +#include "KRRenderGraph.h" + +class KRRenderGraphForward : public KRRenderGraph +{ +public: + KRRenderGraphForward(KRContext& context); + ~KRRenderGraphForward(); + + KrResult initialize(KRSurface& surface); + +}; diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index 070c368..634b00a 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -33,6 +33,8 @@ #include "KRSwapchain.h" #include "KRRenderPass.h" #include "KRRenderGraphBlackFrame.h" +#include "KRRenderGraphDeferred.h" +#include "KRRenderGraphForward.h" using namespace hydra; @@ -46,7 +48,8 @@ KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle, void* platformH , m_renderFinishedSemaphores{VK_NULL_HANDLE} , m_inFlightFences{VK_NULL_HANDLE} , m_frameIndex(0) - , m_renderGraph(std::make_unique(context)) + , m_renderGraphForward(std::make_unique(context)) + , m_renderGraphDeferred(std::make_unique(context)) , m_blackFrameRenderGraph(std::make_unique(context)) , m_swapChain(std::make_unique(context)) , m_surfaceFormat{} @@ -113,7 +116,8 @@ void KRSurface::destroy() destroySwapChain(); std::unique_ptr& device = m_pContext->getDeviceManager()->getDevice(m_deviceHandle); - m_renderGraph->destroy(*device); + m_renderGraphForward->destroy(*device); + m_renderGraphDeferred->destroy(*device); m_blackFrameRenderGraph->destroy(*device); for (int i=0; i < KRENGINE_MAX_FRAMES_IN_FLIGHT; i++) { @@ -173,132 +177,26 @@ KrResult KRSurface::createSwapChain() imageCount = surfaceCapabilities.maxImageCount; } - // ----- Configuration ----- - int shadow_buffer_count = 0; - bool enable_deferred_lighting = false; - // ------------------------- - int attachment_compositeDepth = m_renderGraph->addAttachment("Composite Depth", depthImageFormat); - int attachment_compositeColor = m_renderGraph->addAttachment("Composite Color", m_surfaceFormat.format); - int attachment_lightAccumulation = m_renderGraph->addAttachment("Light Accumulation", VK_FORMAT_B8G8R8A8_UINT); - int attachment_gbuffer = m_renderGraph->addAttachment("GBuffer", VK_FORMAT_B8G8R8A8_UINT); - int attachment_shadow_cascades[3]; - attachment_shadow_cascades[0] = m_renderGraph->addAttachment("Shadow Cascade 0", VK_FORMAT_D32_SFLOAT); - attachment_shadow_cascades[1] = m_renderGraph->addAttachment("Shadow Cascade 1", VK_FORMAT_D32_SFLOAT); - attachment_shadow_cascades[2] = m_renderGraph->addAttachment("Shadow Cascade 2", VK_FORMAT_D32_SFLOAT); - - RenderPassInfo info{}; - info.finalPass = false; - - info.type = RenderPassType::RENDER_PASS_PRESTREAM; - m_renderGraph->addRenderPass(*device, info); - - for (int shadow_index = 0; shadow_index < shadow_buffer_count; shadow_index++) { - info.depthAttachment.id = attachment_shadow_cascades[shadow_index]; - info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; - info.depthAttachment.clearVaue.depthStencil.stencil = 0; - info.type = RenderPassType::RENDER_PASS_SHADOWMAP; - m_renderGraph->addRenderPass(*device, info); - } - - if (enable_deferred_lighting) { - // ----====---- Opaque Geometry, Deferred rendering Pass 1 ----====---- - - info.depthAttachment.id = attachment_compositeDepth; - info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; - info.depthAttachment.clearVaue.depthStencil.stencil = 0; - - info.colorAttachments[0].id = attachment_compositeColor; - info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; - info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.type = RenderPassType::RENDER_PASS_DEFERRED_GBUFFER; - m_renderGraph->addRenderPass(*device, info); - - // ----====---- Opaque Geometry, Deferred rendering Pass 2 ----====---- - - info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; - info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; - - info.colorAttachments[1].id = attachment_lightAccumulation; - info.colorAttachments[1].clearVaue.color.float32[0] = 0.0f; - info.colorAttachments[1].clearVaue.color.float32[1] = 0.0f; - info.colorAttachments[1].clearVaue.color.float32[2] = 0.0f; - info.colorAttachments[1].clearVaue.color.float32[3] = 0.0f; - info.colorAttachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.colorAttachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.type = RenderPassType::RENDER_PASS_DEFERRED_LIGHTS; - m_renderGraph->addRenderPass(*device, info); - - // ----====---- Opaque Geometry, Deferred rendering Pass 3 ----====---- - info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.colorAttachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; - info.type = RenderPassType::RENDER_PASS_DEFERRED_OPAQUE; - m_renderGraph->addRenderPass(*device, info); - - info.colorAttachments[1] = {}; - - } else { - // !enable_deferred_lighting - - // ----====---- Opaque Geometry, Forward Rendering ----====---- - info.depthAttachment.id = attachment_compositeDepth; - info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; - info.depthAttachment.clearVaue.depthStencil.stencil = 0; - - info.colorAttachments[0].id = attachment_compositeColor; - info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; - info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - - info.type = RenderPassType::RENDER_PASS_FORWARD_OPAQUE; - m_renderGraph->addRenderPass(*device, info); - } - - // ----====---- Transparent Geometry, Forward Rendering ----====---- - info.depthAttachment.id = attachment_compositeDepth; - info.depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - info.depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; - info.depthAttachment.clearVaue.depthStencil.depth = 1.0f; - info.depthAttachment.clearVaue.depthStencil.stencil = 0; - - info.colorAttachments[0].id = attachment_compositeColor; - info.colorAttachments[0].clearVaue.color.float32[0] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[1] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[2] = 0.0f; - info.colorAttachments[0].clearVaue.color.float32[3] = 0.0f; - info.colorAttachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; - info.colorAttachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - - info.type = RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT; - m_renderGraph->addRenderPass(*device, info); - - info.type = RenderPassType::RENDER_PASS_DEBUG_OVERLAYS; - m_renderGraph->addRenderPass(*device, info); - - info.finalPass = true; - info.type = RenderPassType::RENDER_PASS_POST_COMPOSITE; - m_renderGraph->addRenderPass(*device, info); res = m_blackFrameRenderGraph->initialize(*this); if (res != KR_SUCCESS) { return res; } + + res = m_renderGraphForward->initialize(*this); + if (res != KR_SUCCESS) { + return res; + } + + res = m_renderGraphDeferred->initialize(*this); + if (res != KR_SUCCESS) { + return res; + } + - m_swapChain->create(*device, m_surface, m_surfaceFormat, depthImageFormat, swapExtent, imageCount, *m_renderGraph->getFinalRenderPass()); + m_swapChain->create(*device, m_surface, m_surfaceFormat, depthImageFormat, swapExtent, imageCount, *m_renderGraphForward->getFinalRenderPass()); return KR_SUCCESS; } @@ -353,12 +251,12 @@ VkFormat KRSurface::getDepthFormat() const KRRenderPass* KRSurface::getRenderPass(RenderPassType type) { - return m_renderGraph->getRenderPass(type); + return m_renderGraphForward->getRenderPass(type); } void KRSurface::endFrame() { - m_frameIndex++;; + m_frameIndex++; } diff --git a/kraken/KRSurface.h b/kraken/KRSurface.h index 9b31a78..83059ff 100644 --- a/kraken/KRSurface.h +++ b/kraken/KRSurface.h @@ -39,6 +39,8 @@ class KRRenderPass; class KRSwapchain; class KRRenderGraph; class KRRenderGraphBlackFrame; +class KRRenderGraphDeferred; +class KRRenderGraphForward; enum RenderPassType : uint8_t; class KRSurface : public KRContextObject @@ -81,7 +83,8 @@ public: private: void destroySwapChain(); KrResult createSwapChain(); - std::unique_ptr m_renderGraph; + std::unique_ptr m_renderGraphForward; + std::unique_ptr m_renderGraphDeferred; std::unique_ptr m_blackFrameRenderGraph; VkSurfaceFormatKHR m_surfaceFormat;