diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 376f19a7..fdbf1753 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -96,7 +96,11 @@ namespace SHADE - Render graph in renderers - Render graph command buffer semaphores - Default vertex input state + - Global descriptor set layouts /*-----------------------------------------------------------------------*/ + ConfigureGlobalDescLayouts(); + ConfigureDefaultVertexInputState(); + // Set Up Cameras screenCamera = resourceManager.Create(); screenCamera->SetLookAt(SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 1.0f), SHVec3(0.0f, 1.0f, 0.0f)); @@ -107,22 +111,12 @@ namespace SHADE // Create Default Viewport defaultViewport = AddViewport(vk::Viewport(0.0f, 0.0f, window->GetWindowSize().first, window->GetWindowSize().second, 0.0f, 1.0f)); - - // Create Debug Renderers - debugScreenRenderer = defaultViewport->AddRenderer(resourceManager,); - debugScreenRenderer->SetCamera(screenCamera); - debugWorldRenderer = defaultViewport->AddRenderer(resourceManager, ); - debugWorldRenderer->SetCamera(worldCamera); - - // Add world renderer to default viewport - worldRenderer = defaultViewport->AddRenderer(resourceManager, ); - worldRenderer->SetCamera(worldCamera); // Get render graph from default viewport world renderer - auto worldRenderGraph = worldRenderer->GetRenderGraph(); + auto worldRenderGraph = resourceManager.Create(); // Initialize world render graph - worldRenderGraph->Init(device, swapchain); + worldRenderGraph->Init(device, swapchain, &defaultVertexInputState, &globalDescSetLayouts); worldRenderGraph->AddResource("Position", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); worldRenderGraph->AddResource("Normals", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); worldRenderGraph->AddResource("Composite", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); @@ -152,10 +146,17 @@ namespace SHADE semaHandle = device->CreateSemaphore(); } - + // Create Debug Renderers + debugScreenRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph); + debugScreenRenderer->SetCamera(screenCamera); + debugWorldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph); + debugWorldRenderer->SetCamera(worldCamera); + + // Add world renderer to default viewport + worldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph); + worldRenderer->SetCamera(worldCamera); + - ConfigureDefaultVertexInputState(); - pipelineLibrary.Init(device, &defaultVertexInputState, &globalDescSetLayouts); } /***************************************************************************/ @@ -325,19 +326,13 @@ namespace SHADE { // Retrieve pipeline from pipeline storage or create if unavailable auto shaderPair = std::make_pair(vertShader, fragShader); - Handle pipeline = pipelineLibrary.GetDrawPipline(shaderPair); - if (!pipeline) - { - pipeline = pipelineLibrary.CreateDrawPipeline - ( - shaderPair, - subpass->GetParentNode()->GetRenderpass(), - subpass->GetIndex() - ); - } // Create material auto mat = resourceManager.Create(); + + auto renderGraphNode = subpass->GetParentNode(); + auto pipeline = renderGraphNode->GetOrCreatePipeline(std::make_pair(vertShader, fragShader), subpass->GetIndex()); + mat->SetPipeline(pipeline); return mat; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 8dad518f..af8e98a6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -24,7 +24,6 @@ of DigiPen Institute of Technology is prohibited. #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h" -#include "Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h" #include "Graphics/RenderGraph/SHRenderGraph.h" namespace SHADE @@ -155,7 +154,6 @@ namespace SHADE // Not Owned Resources SHWindow* window; - SHPipelineLibrary pipelineLibrary; std::vector> globalDescSetLayouts; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp index 56265a3f..74bfadff 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp @@ -18,6 +18,7 @@ of DigiPen Institute of Technology is prohibited. #include "Tools/SHLogger.h" #include "SHRenderer.h" #include "Resource/ResourceLibrary.h" +#include "Graphics/RenderGraph/SHRenderGraph.h" namespace SHADE { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h index 0e378c54..6c75861c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h @@ -29,6 +29,7 @@ namespace SHADE class SHVkLogicalDevice; class SHVkImageView; class ResourceManager; + class SHRenderGraph; /*---------------------------------------------------------------------------------*/ /* Type Definitions */ diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index 0181bdad..ba456b78 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -209,10 +209,11 @@ namespace SHADE */ /***************************************************************************/ - SHRenderGraphNode::SHSubpass::SHSubpass(Handle const& parent, std::unordered_map const* mapping, std::unordered_map> const* resources) noexcept + SHRenderGraphNode::SHSubpass::SHSubpass(Handle const& parent, uint32_t index, std::unordered_map const* mapping, std::unordered_map> const* resources) noexcept : resourceAttachmentMapping{ mapping } , ptrToResources{ resources } , parentNode{ parent } + , subpassIndex {index} { } @@ -366,6 +367,11 @@ namespace SHADE return parentNode; } + SHADE::SHSubPassIndex SHRenderGraphNode::SHSubpass::GetIndex() const noexcept + { + return subpassIndex; + } + /***************************************************************************/ /*! @@ -433,7 +439,7 @@ namespace SHADE */ /***************************************************************************/ - SHRenderGraphNode::SHRenderGraphNode(ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources) noexcept + SHRenderGraphNode::SHRenderGraphNode(ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept : logicalDeviceHdl{ logicalDevice } , renderpass{} , framebuffers{} @@ -447,6 +453,9 @@ namespace SHADE , resourceManager{ rm } , ptrToResources{ resources } { + // pipeline library initialization + pipelineLibrary.Init(logicalDeviceHdl, defaultViState, globalLayouts); + attachmentDescriptions.resize(attResources.size()); bool containsSwapchainImage = false; @@ -545,7 +554,7 @@ namespace SHADE } // Add subpass to container and create mapping for it - subpasses.emplace_back(resourceManager.Create(GetHandle(), &resourceAttachmentMapping, ptrToResources)); + subpasses.emplace_back(resourceManager.Create(GetHandle(), subpasses.size(), &resourceAttachmentMapping, ptrToResources)); subpassIndexing.try_emplace(subpassName, static_cast(subpasses.size()) - 1u); return subpasses.at(subpassIndexing[subpassName]); } @@ -567,6 +576,29 @@ namespace SHADE commandBuffer->EndRenderpass(); } + Handle SHRenderGraphNode::GetOrCreatePipeline(std::pair, Handle> const& vsFsPair, uint32_t subpassIndex) noexcept + { + // verify subpass exists + if (subpassIndex >= subpasses.size() - 1) + { + SHLOG_ERROR("Subpass index passed in is not valid. RenderGraphNode does not have that many passes. "); + return {}; + } + + Handle pipeline = pipelineLibrary.GetDrawPipline(vsFsPair); + if (!pipeline) + { + pipeline = pipelineLibrary.CreateDrawPipeline + ( + vsFsPair, + renderpass, + subpassIndex + ); + } + + return pipeline; + } + /***************************************************************************/ /*! @@ -920,10 +952,12 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain) noexcept + void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept { logicalDeviceHdl = logicalDevice; swapchainHdl = swapchain; + vertexInputState = defaultViState; + globalDescSetLayouts = globalLayouts; } /***************************************************************************/ @@ -1001,7 +1035,7 @@ namespace SHADE } } - nodes.emplace_back(resourceManager.Create(resourceManager, logicalDeviceHdl, swapchainHdl, std::move(resources), std::move(predecessors), &graphResources)); + nodes.emplace_back(resourceManager.Create(resourceManager, logicalDeviceHdl, swapchainHdl, std::move(resources), std::move(predecessors), &graphResources, vertexInputState, globalDescSetLayouts)); nodeIndexing.emplace(nodeName, static_cast(nodes.size()) - 1u); return nodes.at(nodeIndexing[nodeName]); } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index 377d4a76..3353e0a0 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -4,6 +4,7 @@ #include "Graphics/Renderpass/SHVkRenderpass.h" #include "Resource/ResourceLibrary.h" #include "SH_API.h" +#include "Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h" #include #include @@ -85,7 +86,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHSubpass(Handle const& parent, std::unordered_map const* mapping, std::unordered_map> const* ptrToResources) noexcept; + SHSubpass(Handle const& parent, uint32_t index, std::unordered_map const* mapping, std::unordered_map> const* ptrToResources) noexcept; SHSubpass(SHSubpass&& rhs) noexcept; SHSubpass& operator=(SHSubpass&& rhs) noexcept; @@ -100,17 +101,20 @@ namespace SHADE // Runtime functions void Execute (Handle& commandBuffer) noexcept; void AddExteriorDrawCalls (std::function&)> const& newDrawCall) noexcept; - + /*-----------------------------------------------------------------------*/ /* GETTERS AND SETTERS */ /*-----------------------------------------------------------------------*/ Handle const& GetParentNode (void) const noexcept; - SHSubPassIndex GetIndex() const noexcept { return 0; } // TODO + SHSubPassIndex GetIndex() const noexcept; private: /*---------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ /*---------------------------------------------------------------------*/ + //! The index of the subpass in the render graph + uint32_t subpassIndex; + //! The parent renderpass that this subpass belongs to Handle parentNode; @@ -137,6 +141,7 @@ namespace SHADE //! COMPLEX. std::vector&)>> exteriorDrawCalls; + friend class SHRenderGraphNode; friend class SHRenderGraph; }; @@ -185,6 +190,9 @@ namespace SHADE //! Pointer to resources in the render graph (for getting handle IDs) std::unordered_map> const* ptrToResources; + //! Every renderpass will require a pipeline library that will contain pipelines compatible with this renderpass + SHPipelineLibrary pipelineLibrary; + //! Whether or not the node has finished execution bool executed; @@ -202,15 +210,16 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHRenderGraphNode (ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources) noexcept; + SHRenderGraphNode (ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept; SHRenderGraphNode(SHRenderGraphNode&& rhs) noexcept; SHRenderGraphNode& operator= (SHRenderGraphNode&& rhs) noexcept; /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - Handle AddSubpass (std::string subpassName) noexcept; - void Execute (Handle& commandBuffer, uint32_t frameIndex) noexcept; + Handle AddSubpass (std::string subpassName) noexcept; + void Execute (Handle& commandBuffer, uint32_t frameIndex) noexcept; + Handle GetOrCreatePipeline (std::pair, Handle> const& vsFsPair, uint32_t subpassIndex) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ @@ -258,6 +267,11 @@ namespace SHADE //! Command buffers for the render graph std::vector> commandBuffers; + //! Default vertex input state for pipeline creation + SHVertexInputState const* vertexInputState; + + //! Global descriptor set layouts to use for pipeline creation + std::vector> const* globalDescSetLayouts; public: /*-----------------------------------------------------------------------*/ @@ -268,7 +282,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle const& logicalDevice, Handle const& swapchain) noexcept; + void Init (Handle const& logicalDevice, Handle const& swapchain, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept; void AddResource (std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; void Generate (void) noexcept;