From 87b2103f6eb48b02ff95c449b28c52599e33cdf6 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Tue, 3 Jan 2023 22:05:36 +0800 Subject: [PATCH] Shadows WIP --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 7 +++++++ .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 11 ++++++++--- .../MiddleEnd/Lights/SHLightingSubSystem.h | 17 ++++++++++------- .../RenderGraph/SHRenderGraphNode.cpp | 4 ++-- .../Graphics/RenderGraph/SHRenderGraphNode.h | 2 +- .../RenderGraph/SHRenderGraphNodeCompute.cpp | 19 ++++++++++++++++--- .../RenderGraph/SHRenderGraphNodeCompute.h | 2 +- 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 6647ea74..98c897f3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -279,6 +279,13 @@ namespace SHADE }, { SHGraphicsConstants::RenderGraphNodeNames::GBUFFER_PASS .data()}); + // Add subpass with exterior draw call to transition shadow maps + //auto shadowMapTransitionSubpass = deferredCompositeNode->AddSubpass("Shadow Map Transition", {}, {}); + //shadowMapTransitionSubpass->AddExteriorDrawCalls([=](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) + // { + // lightingSubSystem->PrepareShadowMapsForRead(cmdBuffer); + // }); + /*-----------------------------------------------------------------------*/ /* DEFERRED COMPOSITE SUBPASS INIT */ /*-----------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 87b2ab62..997fe68a 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -526,7 +526,6 @@ namespace SHADE // so we do it anyway. #NoteToSelf: if at any point it affects performance, do a check before computing. ComputeDynamicOffsets(); - } /***************************************************************************/ @@ -549,10 +548,10 @@ namespace SHADE } - void SHLightingSubSystem::AddShadowMap(Handle newShadowMap) noexcept + void SHLightingSubSystem::AddShadowMap(Handle newShadowMap, EntityID lightEntity) noexcept { // Add to container of shadow maps - shadowMaps.emplace_back(newShadowMap); + shadowMaps.emplace(lightEntity, newShadowMap); // Just use the image view stored in the resource Handle const NEW_IMAGE_VIEW = newShadowMap->GetImageView(); @@ -597,6 +596,12 @@ namespace SHADE }); } + void SHLightingSubSystem::PrepareShadowMapsForRead(Handle cmdBuffer) noexcept + { + // Issue barrier to transition shadow maps for reading in compute shader + cmdBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eEarlyFragmentTests | vk::PipelineStageFlagBits::eLateFragmentTests, vk::PipelineStageFlagBits::eComputeShader, {}, {}, {}, shadowMapMemoryBarriers); + } + Handle SHLightingSubSystem::GetLightDataDescriptorSet(void) const noexcept { return lightingDataDescSet; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h index ec42193a..65b58174 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -7,6 +7,7 @@ #include #include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h" #include "Graphics/RenderGraph/SHRenderGraphResource.h" +#include "ECS_Base/SHECSMacros.h" namespace SHADE { @@ -162,8 +163,9 @@ namespace SHADE //! Handle to sampler that all shadow map descriptors will use Handle shadowMapSampler; - //! Shadow maps for every light that casts a shadow - std::vector> shadowMaps; + //std::vector> shadowMaps; + //! Shadow maps for every light that casts a shadow Order here doesn't matter. We just want to store it + std::unordered_map> shadowMaps; //! Descriptor sets required to be given to the compute shader for shadow calculation. This will be a descriptor array. //! It will also be preallocated. @@ -189,11 +191,12 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle device, Handle descPool, Handle inShadowMapSampler) noexcept; - void Run (SHMatrix const& viewMat, uint32_t frameIndex) noexcept; - void Exit (void) noexcept; - void BindDescSet (Handle cmdBuffer, uint32_t setIndex, uint32_t frameIndex) noexcept; - void AddShadowMap (Handle newShadowMap) noexcept; + void Init (Handle device, Handle descPool, Handle inShadowMapSampler) noexcept; + void Run (SHMatrix const& viewMat, uint32_t frameIndex) noexcept; + void Exit (void) noexcept; + void BindDescSet (Handle cmdBuffer, uint32_t setIndex, uint32_t frameIndex) noexcept; + void AddShadowMap (Handle newShadowMap, EntityID lightEntity) noexcept; + void PrepareShadowMapsForRead (Handle cmdBuffer) noexcept; //void RemoveShadowMap (uint32_t index) noexcept; /*-----------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp index cc9d61a9..122f41da 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp @@ -431,7 +431,7 @@ namespace SHADE return subpass; } - Handle SHRenderGraphNode::AddNodeCompute(std::string nodeName, Handle computeShaderModule, std::initializer_list resources, std::unordered_set&& dynamicBufferBindings, float numWorkGroupScale/* = 1.0f*/) noexcept + Handle SHRenderGraphNode::AddNodeCompute(std::string nodeName, Handle computeShaderModule, std::initializer_list resources, std::unordered_set&& dynamicBufferBindings, Handle customComputeLayout/* = {}*/, float numWorkGroupScale/* = 1.0f*/) noexcept { // Look for the required resources in the graph std::vector> nodeComputeResources{}; @@ -447,7 +447,7 @@ namespace SHADE std::vector> temp (nodeComputeResources); // Create the subpass compute with the resources - auto nodeCompute = graphStorage->resourceHub->Create(std::move(nodeName), graphStorage, computeShaderModule, std::move(nodeComputeResources), std::move (dynamicBufferBindings), nodeComputes.empty()); + auto nodeCompute = graphStorage->resourceHub->Create(std::move(nodeName), graphStorage, computeShaderModule, std::move(nodeComputeResources), std::move (dynamicBufferBindings), nodeComputes.empty(), customComputeLayout); nodeComputes.push_back(nodeCompute); for (auto& resource : temp) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h index d8e4caa4..8744184f 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h @@ -109,7 +109,7 @@ namespace SHADE /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ Handle AddSubpass(std::string subpassName, Handle viewport, Handle renderer) noexcept; - Handle AddNodeCompute(std::string nodeName, Handle computeShaderModule, std::initializer_list resources, std::unordered_set&& dynamicBufferBindings = {}, float numWorkGroupScale = 1.0f) noexcept; + Handle AddNodeCompute(std::string nodeName, Handle computeShaderModule, std::initializer_list resources, std::unordered_set&& dynamicBufferBindings = {}, Handle customComputeLayout = {}, float numWorkGroupScale = 1.0f) noexcept; void Execute(Handle commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; Handle GetOrCreatePipeline(std::pair, Handle> const& vsFsPair, Handle subpass) noexcept; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp index e1bc3842..fd46cfe7 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp @@ -62,7 +62,8 @@ namespace SHADE followingEndRenderpass = flag; } - SHRenderGraphNodeCompute::SHRenderGraphNodeCompute(std::string nodeName, Handle graphStorage, Handle computeShaderModule, std::vector>&& subpassComputeResources, std::unordered_set&& dynamicBufferBindings, bool followingEndRP, float inNumWorkGroupScale/* = 1.0f*/) noexcept + + SHRenderGraphNodeCompute::SHRenderGraphNodeCompute(std::string nodeName, Handle graphStorage, Handle computeShaderModule, std::vector>&& subpassComputeResources, std::unordered_set&& dynamicBufferBindings, bool followingEndRP, Handle customComputeLayout, float inNumWorkGroupScale/* = 1.0f*/) noexcept : computePipeline{} , pipelineLayout{} , resources{} @@ -116,10 +117,22 @@ namespace SHADE // check if all layouts are there if (layouts.size() == descMappings.at(SHPredefinedDescriptorTypes::RENDER_GRAPH_NODE_COMPUTE_RESOURCE) + 1) { + Handle computeResourceLayout = {}; + uint32_t variableCounts = 0; + if (customComputeLayout) + { + // Just use the descriptor counts in bindings as the variable descriptor counts + auto const& bindings = customComputeLayout->GetBindings(); + variableCounts = bindings.back().DescriptorCount; + computeResourceLayout = customComputeLayout; + } + else + computeResourceLayout = layouts[descMappings.at(SHPredefinedDescriptorTypes::RENDER_GRAPH_NODE_COMPUTE_RESOURCE)]; + + // create compute resources computeResource = graphStorage->resourceHub->Create(); - auto computeResourceLayout = layouts[descMappings.at(SHPredefinedDescriptorTypes::RENDER_GRAPH_NODE_COMPUTE_RESOURCE)]; - computeResource->descSet = graphStorage->descriptorPool->Allocate({ computeResourceLayout }, { 1 }); + computeResource->descSet = graphStorage->descriptorPool->Allocate({ computeResourceLayout }, {variableCounts}); #ifdef _DEBUG for (auto set : computeResource->descSet->GetVkHandle()) SET_VK_OBJ_NAME(graphStorage->logicalDevice, vk::ObjectType::eDescriptorSet, set, "[Descriptor Set] " + name + " Resources"); diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h index 476099c7..5f68cf2c 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h @@ -86,7 +86,7 @@ namespace SHADE void SetFollowingEndRenderpass (uint32_t flag) noexcept; public: - SHRenderGraphNodeCompute(std::string nodeName, Handle graphStorage, Handle computeShaderModule, std::vector>&& subpassComputeResources, std::unordered_set&& dynamicBufferBindings, bool followingEndRP, float inNumWorkGroupScale = 1.0f) noexcept; + SHRenderGraphNodeCompute(std::string nodeName, Handle graphStorage, Handle computeShaderModule, std::vector>&& subpassComputeResources, std::unordered_set&& dynamicBufferBindings, bool followingEndRP, Handle customComputeLayout, float inNumWorkGroupScale = 1.0f) noexcept; void Execute (Handle cmdBuffer, uint32_t frameIndex) noexcept; void HandleResize (void) noexcept;