From 17b71393f34974c64a55285518c0044a1889033f Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Mon, 17 Oct 2022 16:50:53 +0800 Subject: [PATCH] Restructured abit --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 207 ++++++++++-------- .../MiddleEnd/Interface/SHGraphicsSystem.h | 15 +- 2 files changed, 132 insertions(+), 90 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 21d7569e..d5a45cf7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -36,11 +36,9 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { +#pragma region INIT_EXIT - /*---------------------------------------------------------------------------------*/ - /* Constructor/Destructors */ - /*---------------------------------------------------------------------------------*/ - void SHGraphicsSystem::Init(void) + void SHGraphicsSystem::InitBoilerplate(void) noexcept { /*-----------------------------------------------------------------------*/ /* BACKEND BOILERPLATE */ @@ -51,7 +49,7 @@ namespace SHADE // Get Physical Device and Construct Logical Device physicalDevice = SHVkInstance::CreatePhysicalDevice(SH_PHYSICAL_DEVICE_TYPE::BEST); device = SHVkInstance::CreateLogicalDevice({ SHQueueParams(SH_Q_FAM::GRAPHICS, SH_QUEUE_SELECT::DEDICATED), SHQueueParams(SH_Q_FAM::TRANSFER, SH_QUEUE_SELECT::DEDICATED) }, physicalDevice); - + // Construct surface surface = device->CreateSurface(window->GetHWND()); @@ -106,11 +104,12 @@ namespace SHADE graphicsCmdPool = device->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); transferCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); graphicsTexCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + } - - + void SHGraphicsSystem::InitSceneRenderGraph(void) noexcept + { /*-----------------------------------------------------------------------*/ - /* MIDDLE END SETUP + /* MIDDLE END SETUP - Viewports - Renderer - Render graph in renderers @@ -118,6 +117,7 @@ namespace SHADE - Default vertex input state - Global data /*-----------------------------------------------------------------------*/ + auto windowDims = window->GetWindowSize(); SHGraphicsGlobalData::Init(device); @@ -132,63 +132,49 @@ namespace SHADE // Create Default Viewport defaultViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast(window->GetWindowSize().first), static_cast(window->GetWindowSize().second), 0.0f, 1.0f)); - + // Get render graph from default viewport world renderer - worldRenderGraph = resourceManager.Create(); - - std::vector> renderContextCmdPools{swapchain->GetNumImages()}; + sceneRenderGraph = resourceManager.Create(); + + std::vector> renderContextCmdPools{ swapchain->GetNumImages() }; for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) { renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; } // Initialize world render graph - worldRenderGraph->Init(device, swapchain); - worldRenderGraph->AddResource("Present", {SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT}, windowDims.first, windowDims.second); - worldRenderGraph->AddResource("Scene", {SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT}, windowDims.first, windowDims.second); - worldRenderGraph->AddResource("Depth Buffer", {SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL}, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); - worldRenderGraph->AddResource("Entity ID", {SH_ATT_DESC_TYPE_FLAGS::COLOR}, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); + sceneRenderGraph->Init(device, swapchain); + sceneRenderGraph->AddResource("Present", { SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); + sceneRenderGraph->AddResource("Scene", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT }, windowDims.first, windowDims.second); + sceneRenderGraph->AddResource("Depth Buffer", { SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL }, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); + sceneRenderGraph->AddResource("Entity ID", { SH_ATT_DESC_TYPE_FLAGS::COLOR }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); - //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); - //worldRenderGraph->AddResource("Scene", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eB8G8R8A8Unorm); - auto node = worldRenderGraph->AddNode("G-Buffer", { "Entity ID", "Depth Buffer", "Scene"}, {}); // no predecessors + auto node = sceneRenderGraph->AddNode("G-Buffer", { "Entity ID", "Depth Buffer", "Scene" }, {}); // no predecessors //First subpass to write to G-Buffer auto gBufferWriteSubpass = node->AddSubpass("G-Buffer Write"); gBufferWriteSubpass->AddColorOutput("Scene"); gBufferWriteSubpass->AddColorOutput("Entity ID"); - gBufferWriteSubpass->AddDepthOutput ("Depth Buffer", SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL); + gBufferWriteSubpass->AddDepthOutput("Depth Buffer", SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL); // We do this to just transition our scene layout to shader read auto sceneLayoutTransitionSubpass = node->AddSubpass("Scene Layout Transition"); sceneLayoutTransitionSubpass->AddInput("Scene"); #ifdef SHEDITOR - auto imguiNode = worldRenderGraph->AddNode("ImGui Node", { "Present" }, {"G-Buffer"}); + auto imguiNode = sceneRenderGraph->AddNode("ImGui Node", { "Present" }, { "G-Buffer" }); auto imguiSubpass = imguiNode->AddSubpass("ImGui Draw"); imguiSubpass->AddColorOutput("Present"); #endif - worldRenderGraph->Generate(); - - // Create Semaphore - for (auto& semaHandle : graphSemaphores) - { - semaHandle = device->CreateSemaphore(); - } - - // Create Debug Renderers - /*debugScreenRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph); - debugScreenRenderer->SetCamera(screenCamera); - debugWorldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph); - debugWorldRenderer->SetCamera(worldCamera);*/ + // Generate world render graph + sceneRenderGraph->Generate(); // Add world renderer to default viewport - worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph); + worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], sceneRenderGraph); worldRenderer->SetCamera(worldCamera); + // TODO: This is VERY temporarily here until a more solid resource management system is implemented shaderSourceLibrary.Init("../../TempShaderFolder/"); @@ -202,21 +188,59 @@ namespace SHADE cubeFS->Reflect(); defaultMaterial = AddMaterial(cubeVS, cubeFS, gBufferWriteSubpass); + } + + void SHGraphicsSystem::InitMiddleEnd(void) noexcept + { + InitSceneRenderGraph(); + + // Create Semaphore + for (auto& semaHandle : graphSemaphores) + { + semaHandle = device->CreateSemaphore(); + } + } + + void SHGraphicsSystem::InitSubsystems(void) noexcept + { mousePickSystem = resourceManager.Create(); std::vector> cmdPools; cmdPools.reserve(swapchain->GetNumImages()); for (uint32_t i = 0; i < swapchain->GetNumImages(); ++i) - cmdPools.push_back(renderContext.GetFrameData(i).cmdPoolHdls[0]); + cmdPools.push_back(renderContext.GetFrameData(i).cmdPoolHdls[0]); - mousePickSystem->Init(device, cmdPools, worldRenderGraph->GetRenderGraphResource("Entity ID")); + mousePickSystem->Init(device, cmdPools, sceneRenderGraph->GetRenderGraphResource("Entity ID")); // Register the post offscreen render to the system postOffscreenRender = resourceManager.Create(); - postOffscreenRender->Init(device, worldRenderGraph->GetRenderGraphResource("Scene"), descPool); + postOffscreenRender->Init(device, sceneRenderGraph->GetRenderGraphResource("Scene"), descPool); } + /*---------------------------------------------------------------------------------*/ + /* Constructor/Destructors */ + /*---------------------------------------------------------------------------------*/ + void SHGraphicsSystem::Init(void) + { + InitBoilerplate(); + InitMiddleEnd(); + InitSubsystems(); + + + } + + void SHGraphicsSystem::Exit(void) + { + } + +#pragma endregion INIT_EXIT + +#pragma region LIFECYCLE + + /*---------------------------------------------------------------------------------*/ + /* Lifecycle Functions */ + /*---------------------------------------------------------------------------------*/ /***************************************************************************/ /*! @@ -336,14 +360,7 @@ namespace SHADE } } } - - void SHGraphicsSystem::Exit(void) - { - } - - /*---------------------------------------------------------------------------------*/ - /* Lifecycle Functions */ - /*---------------------------------------------------------------------------------*/ + /***************************************************************************/ /*! @@ -435,6 +452,10 @@ namespace SHADE renderContext.AdvanceFrame(); } +#pragma endregion LIFECYCLE + +#pragma region ADD_REMOVE_BUILD + Handle SHGraphicsSystem::AddViewport(const vk::Viewport& viewport) { // Create the viewport @@ -545,47 +566,9 @@ namespace SHADE ); } - void SHGraphicsSystem::PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept - { - resizeWidth = newWidth; - resizeHeight = newHeight; - - renderContext.SetIsResized(true); - } - - void SHGraphicsSystem::HandleResize(void) noexcept - { - if (window->IsMinimized() || renderContext.GetWindowIsDead()) - return; - - auto windowDims = window->GetWindowSize(); - - // Resize the swapchain - swapchain->Resize(surface, windowDims.first, windowDims.second); - - renderContext.HandleResize(); - - worldRenderGraph->HandleResize(resizeWidth, resizeHeight); - - mousePickSystem->HandleResize(); - postOffscreenRender->HandleResize(); - - defaultViewport->SetWidth(static_cast(resizeWidth)); - defaultViewport->SetHeight(static_cast(resizeHeight)); - - worldCamera->SetPerspective(90.0f, static_cast(resizeWidth), static_cast(resizeHeight), 0.0f, 100.0f); - } - - void SHGraphicsSystem::AwaitGraphicsExecution() - { - device->WaitIdle(); - } - - void SHGraphicsSystem::SetWindow(SHWindow* wind) noexcept - { - window = wind; - } +#pragma endregion ADD_REMOVE +#pragma region ROUTINES /*-----------------------------------------------------------------------------------*/ /* System Routine Functions - BeginRoutine */ /*-----------------------------------------------------------------------------------*/ @@ -657,5 +640,51 @@ namespace SHADE renderable.ResetChangedFlag(); } } +#pragma endregion ROUTINES +#pragma region MISC + + void SHGraphicsSystem::PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept + { + resizeWidth = newWidth; + resizeHeight = newHeight; + + renderContext.SetIsResized(true); + } + + void SHGraphicsSystem::HandleResize(void) noexcept + { + if (window->IsMinimized() || renderContext.GetWindowIsDead()) + return; + + auto windowDims = window->GetWindowSize(); + + // Resize the swapchain + swapchain->Resize(surface, windowDims.first, windowDims.second); + + renderContext.HandleResize(); + + sceneRenderGraph->HandleResize(resizeWidth, resizeHeight); + + mousePickSystem->HandleResize(); + postOffscreenRender->HandleResize(); + + defaultViewport->SetWidth(static_cast(resizeWidth)); + defaultViewport->SetHeight(static_cast(resizeHeight)); + + worldCamera->SetPerspective(90.0f, static_cast(resizeWidth), static_cast(resizeHeight), 0.0f, 100.0f); + } + + void SHGraphicsSystem::AwaitGraphicsExecution() + { + device->WaitIdle(); + } + + void SHGraphicsSystem::SetWindow(SHWindow* wind) noexcept + { + window = wind; + } + + +#pragma endregion MISC } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index c3b39514..e6739715 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -67,6 +67,12 @@ namespace SHADE /***********************************************************************************/ class SH_API SHGraphicsSystem : public SHSystem { + private: + void InitBoilerplate (void) noexcept; + void InitSceneRenderGraph(void) noexcept; + void InitMiddleEnd (void) noexcept; + void InitSubsystems (void) noexcept; + public: class SH_API BeginRoutine final : public SHSystemRoutine { @@ -279,6 +285,7 @@ namespace SHADE private: + /*-----------------------------------------------------------------------------*/ /* Data Members */ /*-----------------------------------------------------------------------------*/ @@ -305,6 +312,12 @@ namespace SHADE SHSamplerCache samplerCache; SHMaterialInstanceCache materialInstanceCache; // Viewports +#ifdef SHEDITOR + Handle editorViewport; + Handle editorRenderer; + Handle editorRenderGraph; +#endif + Handle defaultViewport; // Whole screen std::vector> viewports; // Additional viewports @@ -326,7 +339,7 @@ namespace SHADE // Temp Materials Handle defaultMaterial; - Handle worldRenderGraph; + Handle sceneRenderGraph; // Sub systems Handle mousePickSystem;