From 95d2836c94e44d24058b6dddc7fc1e08d844647c Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Fri, 11 Nov 2022 02:38:05 +0800 Subject: [PATCH] God I felt so much pain writing this - Thank god this botchy/hacky resource linking feature between graphs will only be used for a couple of resources. - Setup environment for UI rendering - "Scene" resource is now used in BOTH world render graph and screen render graph. Layouts are setup accordingly. - A very horrible result of this is that linked resources have to be resized in their original graph and it has to be resized BEFORE the graphs linked to it since the framebuffers use their image views. - sigh. --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 51 +++++++++++++++---- .../MiddleEnd/Interface/SHGraphicsSystem.h | 13 +++-- .../Graphics/RenderGraph/SHRenderGraph.cpp | 11 +++- .../src/Graphics/RenderGraph/SHRenderGraph.h | 5 +- .../RenderGraph/SHRenderGraphNode.cpp | 8 +++ .../RenderGraph/SHRenderGraphNodeCompute.cpp | 1 + .../RenderGraph/SHRenderGraphResource.cpp | 13 +++++ .../RenderGraph/SHRenderGraphResource.h | 9 +++- 8 files changed, 95 insertions(+), 16 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 52476ebd..b9201db6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -156,6 +156,8 @@ namespace SHADE worldCamera->SetLookAt(SHVec3(0.0f, 0.0f, 0.0f), SHVec3(0.0f, 0.0f, -2.0f), SHVec3(0.0f, 1.0f, 0.0f)); worldCamera->SetPerspective(90.0f, static_cast(windowDims.first), static_cast(windowDims.second), 0.0f, 100.0f); + worldCameraDirector = cameraSystem->CreateDirector(); + // Create Default Viewport worldViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast(window->GetWindowSize().first), static_cast(window->GetWindowSize().second), 0.0f, 1.0f)); @@ -169,7 +171,7 @@ namespace SHADE } /*-----------------------------------------------------------------------*/ - /* SCENE RENDER GRAPH RESOURCES */ + /* WORLD RENDER GRAPH RESOURCES */ /*-----------------------------------------------------------------------*/ // Initialize world render graph worldRenderGraph->Init(device, swapchain, &resourceManager); @@ -180,7 +182,7 @@ namespace SHADE worldRenderGraph->AddResource("Depth Buffer", { SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL }, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); worldRenderGraph->AddResource("Entity ID", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); worldRenderGraph->AddResource("Light Layer Indices", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); - worldRenderGraph->AddResource("Scene", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second); + worldRenderGraph->AddResource("Scene", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE, SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED }, windowDims.first, windowDims.second); worldRenderGraph->AddResource("SSAO", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); worldRenderGraph->AddResource("SSAO Blur", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); @@ -257,25 +259,55 @@ namespace SHADE /*-----------------------------------------------------------------------*/ gBufferNode->AddNodeCompute(deferredCompositeShader, { "Position", "Normals", "Albedo", "Light Layer Indices", "SSAO Blur", "Scene" }); - // Dummy Node to transition scene render graph resource - auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Debug Draw" }); // no predecessors - auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); - dummySubpass->AddInput("Scene"); + { + //// Dummy Node to transition scene render graph resource + //auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Debug Draw" }); // no predecessors + //auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); + //dummySubpass->AddInput("Scene"); + } /*-----------------------------------------------------------------------*/ - /* GENERATE RENDER GRAPH */ + /* GENERATE WORLD RENDER GRAPH */ /*-----------------------------------------------------------------------*/ // Generate world render graph worldRenderGraph->Generate(); + + /*-----------------------------------------------------------------------*/ + /* SCREEN RENDER GRAPH */ + /*-----------------------------------------------------------------------*/ + // Initialize screen render graph + screenRenderGraph = resourceManager.Create(); + screenRenderGraph->Init(device, swapchain, &resourceManager); + screenRenderGraph->LinkNonOwningResource(worldRenderGraph, "Scene"); + + auto screenSpaceNode = screenRenderGraph->AddNode("Screen Space Pass", { "Scene" }, {}); + auto uiSubpass = screenSpaceNode->AddSubpass("UI"); + uiSubpass->AddColorOutput("Scene"); + + { + // Dummy Node to transition scene render graph resource + auto dummyNode = screenRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Screen Space Pass" }); // no predecessors + auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); + dummySubpass->AddInput("Scene"); + } + + + screenRenderGraph->Generate(); + /*-----------------------------------------------------------------------*/ /* BIND RENDER GRAPH TO RENDERER */ /*-----------------------------------------------------------------------*/ // Add world renderer to default viewport worldRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph); worldRenderer->SetCamera(worldCamera); + worldRenderer->SetCameraDirector(worldCameraDirector); + + // Add screen renderer to default viewport + screenRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], screenRenderGraph); + screenRenderer->SetCamera(screenCamera); + screenRenderer->SetCameraDirector(worldCameraDirector); - worldRenderer->SetCameraDirector(cameraSystem->CreateDirector()); // Create debug draw pipeline debugDrawPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass); @@ -322,7 +354,7 @@ namespace SHADE textRenderingSubSystem = resourceManager.Create(); // initialize the text renderer - auto uiNode = worldRenderGraph->GetNode("Screen Space Pass"); + //auto uiNode = screenRenderGraph->GetNode("Screen Space Pass"); //textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS); SHFreetypeInstance::Init(); @@ -906,6 +938,7 @@ namespace SHADE worldRenderGraph->HandleResize(resizeWidth, resizeHeight); editorRenderGraph->HandleResize(windowDims.first, windowDims.second); + screenRenderGraph->HandleResize(resizeWidth, resizeHeight); mousePickSystem->HandleResize(); postOffscreenRender->HandleResize(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 54abd5ef..45fd4224 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -32,6 +32,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Interface/SHPostOffscreenRenderSystem.h" #include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" #include "Graphics/MiddleEnd/PostProcessing/SHSSAO.h" +#include "Camera/SHCameraDirector.h" namespace SHADE { @@ -392,20 +393,20 @@ namespace SHADE #ifdef SHEDITOR Handle editorViewport; Handle editorRenderer; - Handle editorRenderGraph; #endif Handle worldViewport; // Whole screen - std::vector> viewports; // Additional viewports + std::vector> viewports; // Additional viewports - // Temp renderers + // Renderers Handle worldRenderer; - Handle screenRenderer; // Temp Cameras Handle worldCamera; Handle screenCamera; + DirectorHandle worldCameraDirector; + // Built-In Shaders Handle defaultVertShader; @@ -432,6 +433,10 @@ namespace SHADE // Render Graphs Handle worldRenderGraph; + Handle screenRenderGraph; +#ifdef SHEDITOR + Handle editorRenderGraph; +#endif // Sub systems Handle mousePickSystem; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index b897eb06..7d2e2d66 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -80,6 +80,12 @@ namespace SHADE { if (vk::ImageLayout layout = resourceToLink->GetInfoTracker()->GetLayout(node, {}); layout != vk::ImageLayout::eUndefined) finalLayout = layout; + + for (auto& compute : node->nodeComputes) + { + if (vk::ImageLayout layout = resourceToLink->GetInfoTracker()->GetLayout(compute); layout != vk::ImageLayout::eUndefined) + finalLayout = layout; + } } renderGraphStorage->graphResources->try_emplace(resourceName, resourceToLink); @@ -588,7 +594,10 @@ namespace SHADE { // resize resources for (auto& [name, resource] : *renderGraphStorage->graphResources) - resource->HandleResize(newWidth, newHeight); + { + if (!renderGraphStorage->nonOwningResourceInitialLayouts.contains (resource.GetId().Raw)) + resource->HandleResize(newWidth, newHeight); + } for (auto& node : nodes) { diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index 6429badf..e0a46c6e 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -111,7 +111,10 @@ namespace SHADE * However, because it was eventually necessary that some resources had to be shared and its contents carried over to * other graphs, the functionality was implemented through a link function in SHRenderGraph.cpp to facilitate this linkage. * This should ideally be replaced by an implementation more self-contained, perhaps through a higher level class like a canvas - * that manage the resources instead and can facilitate such linking of resources. + * that manage the resources instead and can facilitate such linking of resources. Either that, or we allow only 1 render graph, + * but different matrices (SHRenderer) can be used in different nodes. + * - There are also way too many hash maps created for ease of access. This definitely can be cut down. + * - * */ diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp index 9e6b7740..70c9636c 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp @@ -275,10 +275,18 @@ namespace SHADE nodeComputeResources.push_back(resource); } + // need to use for tracking resources + std::vector> temp (nodeComputeResources); + // Create the subpass compute with the resources auto nodeCompute = graphStorage->resourceHub->Create(graphStorage, computeShaderModule, std::move(nodeComputeResources), std::move (dynamicBufferBindings), nodeComputes.empty()); nodeComputes.push_back(nodeCompute); + for (auto& resource : temp) + { + resource->GetInfoTracker()->TrackLayout(nodeCompute); + } + return nodeCompute; } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp index e8822acd..e2d70c39 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp @@ -42,6 +42,7 @@ namespace SHADE // save the resources resources = std::move (subpassComputeResources); + //Get the descriptor set layouts required to allocate. We only want the ones for allocate because //global descriptors are already bound in the main system. auto const& graphResourceLayout = computePipeline->GetPipelineLayout()->GetDescriptorSetLayoutsPipeline()[SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_RESOURCE]; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp index 327a94b6..f431b272 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp @@ -18,6 +18,11 @@ namespace SHADE } + void SHRenderGraphResource::InfoTracker::TrackLayout(Handle compute) noexcept + { + computeTracker.emplace (compute.GetId().Raw); + } + vk::ImageLayout SHRenderGraphResource::InfoTracker::GetLayout(Handle node, Handle subpass) const noexcept { NodeSubpassPair nodeSubpassPair = std::pair(node, subpass); @@ -29,6 +34,14 @@ namespace SHADE return vk::ImageLayout::eUndefined; } + vk::ImageLayout SHRenderGraphResource::InfoTracker::GetLayout(Handle compute) const noexcept + { + if (computeTracker.contains (compute.GetId().Raw)) + return vk::ImageLayout::eGeneral; + else + return vk::ImageLayout::eUndefined; + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h index 0004bb4a..7ac2b824 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h @@ -6,6 +6,7 @@ #include "Resource/SHHandle.h" #include "Graphics/SHVulkanIncludes.h" #include "SH_API.h" +#include namespace SHADE { @@ -18,6 +19,7 @@ namespace SHADE class SHRenderGraphStorage; class SHRenderGraphNode; class SHSubpass; + class SHRenderGraphNodeCompute; static constexpr uint32_t NON_SWAPCHAIN_RESOURCE_INDEX = 0; @@ -33,16 +35,21 @@ namespace SHADE //! key here is the render graph node and subpass name combined, value is the layout of the resource at that node and subpass std::unordered_map layoutTracker; + //! if a resource is involved in a compute process, record it here + std::unordered_set computeTracker; + public: /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ void TrackLayout (Handle node, Handle subpass, vk::ImageLayout layout) noexcept; + void TrackLayout (Handle compute) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ - vk::ImageLayout GetLayout (Handle node, Handle subpass) const noexcept; + vk::ImageLayout GetLayout(Handle node, Handle subpass) const noexcept; + vk::ImageLayout GetLayout(Handle compute) const noexcept; }; private: