From 44ca317e1da5eba19d778d1c4ca704a76af14e99 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Wed, 28 Dec 2022 10:22:01 +0800 Subject: [PATCH] WIP will update later, afraid for BSOD again --- Assets/Shaders/DebugDrawMesh_VS.glsl | 3 +- Assets/Shaders/DebugDraw_VS.glsl | 3 +- Assets/Shaders/TestCube_Tile_VS.glsl | 3 +- Assets/Shaders/TestCube_VS.glsl | 3 +- Assets/Shaders/Text_VS.glsl | 5 +- Assets/Shaders/UI_VS.glsl | 5 +- SHADE_Engine/src/Editor/SHEditor.cpp | 7 +- .../Graphics/Commands/SHVkCommandBuffer.cpp | 2 +- .../src/Graphics/Commands/SHVkCommandBuffer.h | 2 +- .../GlobalData/SHGlobalDescriptorSets.cpp | 31 +- .../GlobalData/SHGlobalDescriptorSets.h | 24 +- .../MiddleEnd/GlobalData/SHPredefinedData.cpp | 41 +- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 8 +- .../MiddleEnd/Interface/SHGraphicsConstants.h | 1 + .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 359 ++++++++++++------ .../MiddleEnd/Interface/SHGraphicsSystem.h | 30 +- .../MiddleEnd/Interface/SHRenderer.cpp | 89 ++--- .../Graphics/MiddleEnd/Interface/SHRenderer.h | 34 +- .../MiddleEnd/Interface/SHViewport.cpp | 28 -- .../Graphics/MiddleEnd/Interface/SHViewport.h | 14 +- .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 11 +- .../MiddleEnd/Lights/SHLightingSubSystem.h | 11 +- .../SHTextRenderingSubSystem.cpp | 30 +- .../TextRendering/SHTextRenderingSubSystem.h | 8 +- .../Graphics/RenderGraph/SHRenderGraph.cpp | 53 ++- .../src/Graphics/RenderGraph/SHRenderGraph.h | 16 +- .../RenderGraph/SHRenderGraphNode.cpp | 14 +- .../Graphics/RenderGraph/SHRenderGraphNode.h | 6 +- .../RenderGraph/SHRenderGraphNodeCompute.h | 2 + .../src/Graphics/RenderGraph/SHSubpass.cpp | 34 +- .../src/Graphics/RenderGraph/SHSubpass.h | 24 +- 31 files changed, 548 insertions(+), 353 deletions(-) diff --git a/Assets/Shaders/DebugDrawMesh_VS.glsl b/Assets/Shaders/DebugDrawMesh_VS.glsl index 2f035261..19c1a5b9 100644 --- a/Assets/Shaders/DebugDrawMesh_VS.glsl +++ b/Assets/Shaders/DebugDrawMesh_VS.glsl @@ -16,8 +16,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; void main() diff --git a/Assets/Shaders/DebugDraw_VS.glsl b/Assets/Shaders/DebugDraw_VS.glsl index 42a48c01..ce2dd544 100644 --- a/Assets/Shaders/DebugDraw_VS.glsl +++ b/Assets/Shaders/DebugDraw_VS.glsl @@ -15,8 +15,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; void main() diff --git a/Assets/Shaders/TestCube_Tile_VS.glsl b/Assets/Shaders/TestCube_Tile_VS.glsl index 31a448fe..d27805ef 100644 --- a/Assets/Shaders/TestCube_Tile_VS.glsl +++ b/Assets/Shaders/TestCube_Tile_VS.glsl @@ -39,8 +39,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; layout (std430, set = 3, binding = 0) buffer MaterialProperties // For materials diff --git a/Assets/Shaders/TestCube_VS.glsl b/Assets/Shaders/TestCube_VS.glsl index 774bc580..0e055395 100644 --- a/Assets/Shaders/TestCube_VS.glsl +++ b/Assets/Shaders/TestCube_VS.glsl @@ -34,8 +34,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; void main() diff --git a/Assets/Shaders/Text_VS.glsl b/Assets/Shaders/Text_VS.glsl index 3501a13d..0498ae39 100644 --- a/Assets/Shaders/Text_VS.glsl +++ b/Assets/Shaders/Text_VS.glsl @@ -30,8 +30,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; // push constants @@ -96,6 +95,6 @@ void main() Out2.textColor = testPushConstant.textColor; // transform the vertex position to font space - gl_Position = cameraData.orthoMat * localModel * vec4(vertexPos, 1.0f); + gl_Position = cameraData.projMat * localModel * vec4(vertexPos, 1.0f); // gl_Position = vec4(vertexPos, 1.0f); } \ No newline at end of file diff --git a/Assets/Shaders/UI_VS.glsl b/Assets/Shaders/UI_VS.glsl index c4393b98..622cf827 100644 --- a/Assets/Shaders/UI_VS.glsl +++ b/Assets/Shaders/UI_VS.glsl @@ -34,8 +34,7 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 perspectiveMat; - mat4 orthoMat; + mat4 projMat; } cameraData; void main() @@ -60,7 +59,7 @@ void main() Out.normal.rgb = normalize (Out.normal.rgb); // clip space for rendering - gl_Position = cameraData.orthoMat * worldTransform * vec4 (aVertexPos, 1.0f); + gl_Position = cameraData.projMat * worldTransform * vec4 (aVertexPos, 1.0f); gl_Position.z += 0.1f; // HAX // gl_Position = vec4 (aVertexPos, 1.0f); } \ No newline at end of file diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index 4f1586ac..a12a19f7 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -500,10 +500,7 @@ namespace SHADE imguiCommandBuffer = imguiCommandPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); //auto const& renderers = gfxSystem->GetDefaultViewport()->GetRenderers(); - auto const& renderers = gfxSystem->GetEditorViewport()->GetRenderers(); - - SHASSERT(!renderers.empty(), "No Renderers available") - auto renderGraph = renderers[SHGraphicsConstants::RenderGraphIndices::EDITOR]->GetRenderGraph(); + auto renderGraph = gfxSystem->GetRenderGraph(); auto renderPass = renderGraph->GetNode("ImGui Node")->GetRenderpass(); if(ImGui_ImplVulkan_Init(&initInfo, renderPass->GetVkRenderpass()) == false) @@ -523,7 +520,7 @@ namespace SHADE ImGui_ImplVulkan_DestroyFontUploadObjects(); - renderGraph->GetNode("ImGui Node")->GetSubpass("ImGui Draw")->AddExteriorDrawCalls([](Handle& cmd, uint32_t frameIndex) + renderGraph->GetNode("ImGui Node")->GetSubpass("ImGui Draw")->AddExteriorDrawCalls([](Handle cmd, Handle renderer, uint32_t frameIndex) { cmd->BeginLabeledSegment("ImGui Draw"); ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd->GetVkCommandBuffer()); diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index 05fd4288..37b89883 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -367,7 +367,7 @@ namespace SHADE } } - void SHVkCommandBuffer::BindDescriptorSet(Handle descSetGroup, SH_PIPELINE_TYPE bindPoint, uint32_t firstSet, std::span dynamicOffsets) + void SHVkCommandBuffer::BindDescriptorSet(Handle descSetGroup, SH_PIPELINE_TYPE bindPoint, uint32_t firstSet, std::span const dynamicOffsets) { uint32_t bindPointIndex = static_cast(bindPoint); vkCommandBuffer.bindDescriptorSets(SHVkUtil::GetPipelineBindPointFromType(bindPoint), bindPointData[bindPointIndex].boundPipelineLayoutHdl->GetVkPipelineLayout(), firstSet, descSetGroup->GetVkHandle(), dynamicOffsets); diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index fc348487..c6a17d2a 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -125,7 +125,7 @@ namespace SHADE void BindPipeline (Handle const& pipelineHdl) noexcept; void BindVertexBuffer (uint32_t bindingPoint, Handle const& buffer, vk::DeviceSize offset) noexcept; void BindIndexBuffer (Handle const& buffer, uint32_t startingIndex) const noexcept; - void BindDescriptorSet (Handle descSetGroup, SH_PIPELINE_TYPE bindPoint, uint32_t firstSet, std::span dynamicOffsets); + void BindDescriptorSet (Handle descSetGroup, SH_PIPELINE_TYPE bindPoint, uint32_t firstSet, std::span const dynamicOffsets); // Draw Commands void DrawArrays (uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) const noexcept; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.cpp index 2d6cc9e1..09dbef51 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.cpp @@ -1,10 +1,31 @@ #include "SHpch.h" #include "SHGlobalDescriptorSets.h" +#include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" +#include "Graphics/Commands/SHVkCommandBuffer.h" namespace SHADE { - Handle SHGlobalDescriptorSets::lightDescriptorSet; + Handle SHGlobalDescriptorSets::staticGlobalDataDescriptorSet; + Handle SHGlobalDescriptorSets::lightingSubSystem; + + //void SHGlobalDescriptorSets::BindLightingData(Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t firstSet) noexcept + //{ + // // Bind descriptor set for light data + // cmdBuffer->BindDescriptorSet(SHGlobalDescriptorSets::GetLightDescriptorSet(), SH_PIPELINE_TYPE::COMPUTE, descMappings[SHGraphicsConstants::PredefinedDescSetLayoutTypes::LIGHTS], const std::span{ TEX_DYNAMIC_OFFSET.data(), 1 }); + //} + + void SHGlobalDescriptorSets::BindLightingData(Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex, uint32_t frameIndex) noexcept + { + lightingSubSystem->BindDescSet(cmdBuffer, setIndex, frameIndex); + } + + void SHGlobalDescriptorSets::BindStaticGlobalData(Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex) noexcept + { + // Bind descriptor set for static global data + static constexpr std::array TEX_DYNAMIC_OFFSET{ 0 }; + cmdBuffer->BindDescriptorSet(staticGlobalDataDescriptorSet, pipelineType, setIndex, const std::span{ TEX_DYNAMIC_OFFSET.data(), 1 }); + } /***************************************************************************/ /*! @@ -17,14 +38,14 @@ namespace SHADE */ /***************************************************************************/ - void SHGlobalDescriptorSets::SetLightDescriptorSet(Handle lightDescSet) noexcept + void SHGlobalDescriptorSets::SetLightingSubSystem(Handle system) noexcept { - lightDescriptorSet = lightDescSet; + lightingSubSystem = system; } - Handle SHGlobalDescriptorSets::GetLightDescriptorSet(void) noexcept + void SHGlobalDescriptorSets::SetStaticGlobalDataDescriptorSet(Handle staticGlobalDescSet) noexcept { - return lightDescriptorSet; + staticGlobalDataDescriptorSet = staticGlobalDescSet; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h index ce6c42bb..2e2dca7d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h @@ -2,23 +2,35 @@ #include "Graphics/Descriptors/SHVkDescriptorSetGroup.h" #include "Resource/SHHandle.h" +#include "Graphics/Pipeline/SHPipelineType.h" namespace SHADE { + class SHLightingSubSystem; + class SHVkCommandBuffer; + // This class is mainly for descriptors that are truly global, meaning they only come from 1 place and they are shared between many systems class SHGlobalDescriptorSets { private: - //! Light data descriptor set - static Handle lightDescriptorSet; + + //! Static global descriptor sets for miscellaneous data and textures + static Handle staticGlobalDataDescriptorSet; + //! Lighting sub system required to get information to bind descriptor sets for light data + static Handle lightingSubSystem; + public: + /*-----------------------------------------------------------------------*/ + /* PUBLIC MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + static void BindLightingData (Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex, uint32_t frameIndex) noexcept; + static void BindStaticGlobalData (Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex) noexcept; + /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ - static void SetLightDescriptorSet (Handle lightDescSet) noexcept; - - static Handle GetLightDescriptorSet (void) noexcept; - + static void SetLightingSubSystem (Handle system) noexcept; + static void SetStaticGlobalDataDescriptorSet (Handle staticGlobalDescSet) noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedData.cpp index ac7fac17..ac7ab982 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedData.cpp @@ -125,33 +125,33 @@ namespace SHADE Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding }); SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, materialDataPerInstanceLayout->GetVkHandle(), "[Descriptor Set Layout] Material Globals"); - //// font bitmap data (texture) - //SHVkDescriptorSetLayout::Binding fontBitmapBinding - //{ - // .Type = vk::DescriptorType::eCombinedImageSampler, - // .Stage = vk::ShaderStageFlagBits::eFragment, - // .BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA, - // .DescriptorCount = 1, - //}; + // font bitmap data (texture) + SHVkDescriptorSetLayout::Binding fontBitmapBinding + { + .Type = vk::DescriptorType::eCombinedImageSampler, + .Stage = vk::ShaderStageFlagBits::eFragment, + .BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA, + .DescriptorCount = 1, + }; - //// font data in the form of matrices - //SHVkDescriptorSetLayout::Binding fontMatrixBinding - //{ - // .Type = vk::DescriptorType::eStorageBuffer, - // .Stage = vk::ShaderStageFlagBits::eVertex, - // .BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA, - // .DescriptorCount = 1, - //}; + // font data in the form of matrices + SHVkDescriptorSetLayout::Binding fontMatrixBinding + { + .Type = vk::DescriptorType::eStorageBuffer, + .Stage = vk::ShaderStageFlagBits::eVertex, + .BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA, + .DescriptorCount = 1, + }; - //Handle fontDataDescSetLayout = logicalDevice->CreateDescriptorSetLayout({ fontBitmapBinding, fontMatrixBinding }); - //SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, fontDataDescSetLayout->GetVkHandle(), "[Descriptor Set Layout] Font Data"); + Handle fontDataDescSetLayout = logicalDevice->CreateDescriptorSetLayout({ fontBitmapBinding, fontMatrixBinding }); + SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, fontDataDescSetLayout->GetVkHandle(), "[Descriptor Set Layout] Font Data"); predefinedLayouts.push_back(staticGlobalLayout); predefinedLayouts.push_back(lightDataDescSetLayout); predefinedLayouts.push_back(cameraDataGlobalLayout); predefinedLayouts.push_back(materialDataPerInstanceLayout); - //predefinedLayouts.push_back(fontDataDescSetLayout); + predefinedLayouts.push_back(fontDataDescSetLayout); batchingSystemData.descSetLayouts = GetPredefinedDescSetLayouts ( @@ -163,7 +163,8 @@ namespace SHADE textSystemData.descSetLayouts = GetPredefinedDescSetLayouts ( SHGraphicsConstants::PredefinedDescSetLayoutTypes::STATIC_DATA | - SHGraphicsConstants::PredefinedDescSetLayoutTypes::CAMERA + SHGraphicsConstants::PredefinedDescSetLayoutTypes::CAMERA | + SHGraphicsConstants::PredefinedDescSetLayoutTypes::FONT ); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index d77fbeb0..b57249de 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -99,10 +99,10 @@ namespace SHADE createMeshBatches(); // Register function for subpass - auto const& RENDERERS = gfxSystem->GetDefaultViewport()->GetRenderers(); - auto renderGraph = RENDERERS[SHGraphicsConstants::RenderGraphIndices::WORLD]->GetRenderGraph(); + //auto const& RENDERERS = gfxSystem->GetDefaultViewport()->GetRenderers(); + auto renderGraph = gfxSystem->GetRenderGraph(); auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); - subPass->AddExteriorDrawCalls([this](Handle& cmdBuffer, uint32_t frameIndex) + subPass->AddExteriorDrawCalls([this](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) { const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); cmdBuffer->BeginLabeledSegment("SHDebugDraw (No Depth Test)"); @@ -126,7 +126,7 @@ namespace SHADE cmdBuffer->EndLabeledSegment(); }); auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); - subPassWithDepth->AddExteriorDrawCalls([this](Handle& cmdBuffer, uint32_t frameIndex) + subPassWithDepth->AddExteriorDrawCalls([this](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) { const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); cmdBuffer->BeginLabeledSegment("SHDebugDraw (Depth Tested)"); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h index c80b9de5..06ffe381 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h @@ -34,6 +34,7 @@ namespace SHADE LIGHTS = 0x02, CAMERA = 0x04, MATERIALS = 0x08, + FONT = 0x10, }; // This enum is different from the one above in that it is used to initialize a hash table to diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 6881d7a8..461b8783 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -44,6 +44,7 @@ of DigiPen Institute of Technology is prohibited. #include "../Meshes/SHPrimitiveGenerator.h" #include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h" #include "Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h" +#include "Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h" namespace SHADE { @@ -142,7 +143,7 @@ namespace SHADE static constexpr AssetID RENDER_SC_FS = 36869006; renderToSwapchainFS = SHResourceManager::LoadOrGet(RENDER_SC_FS); } - void SHGraphicsSystem::InitSceneRenderGraph(void) noexcept + void SHGraphicsSystem::InitRenderGraph(void) noexcept { /*-----------------------------------------------------------------------*/ /* MIDDLE END SETUP @@ -158,55 +159,66 @@ namespace SHADE auto cameraSystem = SHSystemManager::GetSystem(); // 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)); - screenCamera->SetOrthographic(static_cast(windowDims.first), static_cast(windowDims.second), 0.01f, 100.0f); + //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)); + //screenCamera->SetOrthographic(static_cast(windowDims.first), static_cast(windowDims.second), 0.01f, 100.0f); - worldCamera = resourceManager.Create(); - 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); + //worldCamera = resourceManager.Create(); + //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(); + /*-----------------------------------------------------------------------*/ + /* PREPARE RENDERERS */ + /*-----------------------------------------------------------------------*/ + // Add world renderer to default viewport + worldRenderer = AddRenderer(SHRenderer::PROJECTION_TYPE::PERSPECTIVE); + worldRenderer->SetCameraDirector(worldCameraDirector); + + // Add screen renderer to default viewport + screenRenderer = AddRenderer(SHRenderer::PROJECTION_TYPE::ORTHOGRAPHIC); + screenRenderer->SetCameraDirector(worldCameraDirector); + // 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)); - // Get render graph from default viewport world renderer - worldRenderGraph = resourceManager.Create(); - std::vector> renderContextCmdPools{ swapchain->GetNumImages() }; for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) { renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; } + // Get render graph from default viewport world renderer + renderGraph = resourceManager.Create(); + /*-----------------------------------------------------------------------*/ /* WORLD RENDER GRAPH RESOURCES */ /*-----------------------------------------------------------------------*/ // Initialize world render graph - worldRenderGraph->Init("World Render Graph", device, swapchain, &resourceManager); - worldRenderGraph->AddResource("Position", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); - worldRenderGraph->AddResource("Normals", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); + renderGraph->Init("World Render Graph", device, swapchain, &resourceManager, renderContextCmdPools); + renderGraph->AddResource("Position", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); + renderGraph->AddResource("Normals", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); //worldRenderGraph->AddResource("Tangents", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); - worldRenderGraph->AddResource("Albedo", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second); - 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, SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED }, 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, 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); + renderGraph->AddResource("Albedo", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second); + renderGraph->AddResource("Depth Buffer", { SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL }, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); + renderGraph->AddResource("Entity ID", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); + renderGraph->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); + renderGraph->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); + renderGraph->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); + renderGraph->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); + renderGraph->AddResource("Present", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); /*-----------------------------------------------------------------------*/ /* MAIN NODE */ /*-----------------------------------------------------------------------*/ - auto gBufferNode = worldRenderGraph->AddNode("G-Buffer", + auto gBufferNode = renderGraph->AddNode("G-Buffer", { "Position", "Entity ID", "Light Layer Indices", "Normals", - //"Tangents", "Albedo", "Depth Buffer", "Scene", @@ -218,20 +230,21 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* G-BUFFER SUBPASS INIT */ /*-----------------------------------------------------------------------*/ - auto gBufferSubpass = gBufferNode->AddSubpass("G-Buffer Write"); + auto gBufferSubpass = gBufferNode->AddSubpass("G-Buffer Write", worldViewport, worldRenderer); gBufferSubpass->AddColorOutput("Position"); gBufferSubpass->AddColorOutput("Entity ID"); gBufferSubpass->AddColorOutput("Light Layer Indices"); gBufferSubpass->AddColorOutput("Normals"); - //gBufferSubpass->AddColorOutput("Tangents"); gBufferSubpass->AddColorOutput("Albedo"); gBufferSubpass->AddDepthOutput("Depth Buffer", SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL); + /*-----------------------------------------------------------------------*/ /* SSAO PASS AND DATA INIT */ /*-----------------------------------------------------------------------*/ ssaoStorage = resourceManager.Create(); + // command buffer operation to transfer data for ssao ssaoTransferCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); SET_VK_OBJ_NAME(device, vk::ObjectType::eCommandBuffer, ssaoTransferCmdBuffer->GetVkCommandBuffer(), "[Command Buffer] SSAO Pass (Graphics)"); ssaoTransferCmdBuffer->BeginRecording(); @@ -240,50 +253,80 @@ namespace SHADE ssaoTransferCmdBuffer->EndRecording(); graphicsQueue->SubmitCommandBuffer({ ssaoTransferCmdBuffer }); - // Set up Debug Draw Passes - // - Depth Tested - auto debugDrawNodeDepth = worldRenderGraph->AddNode("Debug Draw with Depth", { "Scene", "Depth Buffer" }, {"G-Buffer"}); - auto debugDrawDepthSubpass = debugDrawNodeDepth->AddSubpass("Debug Draw with Depth"); - debugDrawDepthSubpass->AddColorOutput("Scene"); - debugDrawDepthSubpass->AddDepthOutput("Depth Buffer"); - // - No Depth Test - auto debugDrawNode = worldRenderGraph->AddNode("Debug Draw", { "Scene" }, { "Debug Draw with Depth" }); - auto debugDrawSubpass = debugDrawNode->AddSubpass("Debug Draw"); - debugDrawSubpass->AddColorOutput("Scene"); + // wait for command buffer to finish executing graphicsQueue->WaitIdle(); - ssaoStorage->PrepareRotationVectorsVkData(device); + // Add the pass to generate an image with just SSAO data Handle ssaoPass = gBufferNode->AddNodeCompute("SSAO", ssaoShader, { "Position", "Normals", "SSAO" }); auto ssaoDataBuffer = ssaoStorage->GetBuffer(); ssaoPass->ModifyWriteDescBufferComputeResource(SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_NODE_COMPUTE_RESOURCE, SHSSAO::DESC_SET_BUFFER_BINDING, { &ssaoDataBuffer, 1 }, 0, ssaoStorage->GetBuffer()->GetSizeStored()); auto viewSamplerLayout = ssaoStorage->GetViewSamplerLayout(); - ssaoPass->ModifyWriteDescImageComputeResource(SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_NODE_COMPUTE_RESOURCE, SHSSAO::DESC_SET_IMAGE_BINDING, {&viewSamplerLayout, 1}); + ssaoPass->ModifyWriteDescImageComputeResource(SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_NODE_COMPUTE_RESOURCE, SHSSAO::DESC_SET_IMAGE_BINDING, { &viewSamplerLayout, 1 }); - - Handle ssaoBlurPass = gBufferNode->AddNodeCompute("SSAO Blur Step", ssaoBlurShader, {"SSAO", "SSAO Blur"}); + // Add another pass to blur SSAO + Handle ssaoBlurPass = gBufferNode->AddNodeCompute("SSAO Blur Step", ssaoBlurShader, { "SSAO", "SSAO Blur" }); /*-----------------------------------------------------------------------*/ /* DEFERRED COMPOSITE SUBPASS INIT */ /*-----------------------------------------------------------------------*/ - gBufferNode->AddNodeCompute("Deferred Composite", deferredCompositeShader, {"Position", "Normals", "Albedo", "Light Layer Indices", "SSAO Blur", "Scene"}); + gBufferNode->AddNodeCompute("Deferred Composite", deferredCompositeShader, { "Position", "Normals", "Albedo", "Light Layer Indices", "SSAO Blur", "Scene" }); + + /*-----------------------------------------------------------------------*/ + /* DEBUG DRAW PASS INIT */ + /*-----------------------------------------------------------------------*/ + // Set up Debug Draw Passes + // - Depth Tested + auto debugDrawNodeDepth = renderGraph->AddNode("Debug Draw with Depth", { "Scene", "Depth Buffer" }, {"G-Buffer"}); + auto debugDrawDepthSubpass = debugDrawNodeDepth->AddSubpass("Debug Draw with Depth", worldViewport, worldRenderer); + debugDrawDepthSubpass->AddColorOutput("Scene"); + debugDrawDepthSubpass->AddDepthOutput("Depth Buffer"); + // - No Depth Test + auto debugDrawNode = renderGraph->AddNode("Debug Draw", { "Scene" }, { "Debug Draw with Depth" }); + auto debugDrawSubpass = debugDrawNode->AddSubpass("Debug Draw", worldViewport, worldRenderer); + debugDrawSubpass->AddColorOutput("Scene"); + + /*-----------------------------------------------------------------------*/ + /* SCREEN SPACE PASS */ + /*-----------------------------------------------------------------------*/ + auto screenSpaceNode = renderGraph->AddNode("Screen Space Pass", { "Scene", "Entity ID" }, {"G-Buffer", "Debug Draw" }); + auto uiSubpass = screenSpaceNode->AddSubpass("UI", worldViewport, screenRenderer); + uiSubpass->AddColorOutput("Scene"); + uiSubpass->AddColorOutput("Entity ID"); + uiSubpass->AddExteriorDrawCalls([=](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) + { + textRenderingSubSystem->Render(cmdBuffer, renderer, frameIndex); + }); + + /*-----------------------------------------------------------------------*/ + /* RENDER TO SWAPCHAIN IMAGE FOR PRESENT PASS */ + /*-----------------------------------------------------------------------*/ +#ifdef SHEDITOR { - //// 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 = renderGraph->AddNode("Dummy Pass", { "Scene" }, { "Screen Space Pass" }); + auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass", {}, {}); + dummySubpass->AddInput("Scene"); + + auto imGuiNode = renderGraph->AddNode("ImGui Node", { "Present" }, {}); + auto imGuiSubpass = imGuiNode->AddSubpass("ImGui Draw", {}, {}); + imGuiSubpass->AddColorOutput("Present"); } +#else + renderGraph->AddRenderToSwapchainNode("Scene", "Present", { "Screen Space Pass" }, { renderToSwapchainVS, renderToSwapchainFS }); +#endif + /*-----------------------------------------------------------------------*/ - /* GENERATE WORLD RENDER GRAPH */ + /* GENERATE RENDER GRAPH */ /*-----------------------------------------------------------------------*/ - // Generate world render graph - worldRenderGraph->Generate(); - + // Generate render graph + renderGraph->Generate(); +#if 0 /*-----------------------------------------------------------------------*/ /* SCREEN RENDER GRAPH */ /*-----------------------------------------------------------------------*/ @@ -317,18 +360,7 @@ namespace SHADE screenRenderGraph->Generate(); - /*-----------------------------------------------------------------------*/ - /* BIND RENDER GRAPH TO RENDERER */ - /*-----------------------------------------------------------------------*/ - // Add world renderer to default viewport - worldRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHPredefinedData::GetPredefinedDescSetLayouts()[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, SHPredefinedData::GetPredefinedDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], screenRenderGraph); - screenRenderer->SetCamera(screenCamera); - screenRenderer->SetCameraDirector(worldCameraDirector); +#endif // Create debug draw pipeline debugDrawPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass, false, false, false); @@ -361,10 +393,12 @@ namespace SHADE { SHPredefinedData::Init(device); - InitSceneRenderGraph(); + InitRenderGraph(); -#ifdef SHEDITOR - InitEditorRenderGraph(); +#if 0 + #ifdef SHEDITOR + InitEditorRenderGraph(); + #endif #endif // Create Semaphore @@ -377,7 +411,7 @@ namespace SHADE void SHGraphicsSystem::InitSubsystems(void) noexcept { - mousePickSystem = resourceManager.Create(); + mousePickSubSystem = resourceManager.Create(); std::vector> cmdPools; cmdPools.reserve(swapchain->GetNumImages()); @@ -385,11 +419,11 @@ namespace SHADE cmdPools.push_back(renderContext.GetFrameData(i).cmdPoolHdls[0]); // Mouse picking system for the editor (Will still run with editor disabled) - mousePickSystem->Init(device, cmdPools, worldRenderGraph->GetRenderGraphResource("Entity ID")); + mousePickSubSystem->Init(device, cmdPools, renderGraph->GetRenderGraphResource("Entity ID")); // Register the post offscreen render to the system - postOffscreenRender = resourceManager.Create(); - postOffscreenRender->Init(device, worldRenderGraph->GetRenderGraphResource("Scene"), descPool); + postOffscreenRenderSubSystem = resourceManager.Create(); + postOffscreenRenderSubSystem->Init(device, renderGraph->GetRenderGraphResource("Scene"), descPool); lightingSubSystem = resourceManager.Create(); lightingSubSystem->Init(device, descPool); @@ -397,11 +431,11 @@ namespace SHADE textRenderingSubSystem = resourceManager.Create(); // initialize the text renderer - auto uiNode = screenRenderGraph->GetNode("Screen Space Pass"); - textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS, [=](Handle cmdBuffer, uint32_t frameIndex) - { - screenRenderer->BindDescSet(cmdBuffer, frameIndex); - }); + auto uiNode = renderGraph->GetNode("Screen Space Pass"); + textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS); + + SHGlobalDescriptorSets::SetStaticGlobalDataDescriptorSet(texLibrary.GetTextureDescriptorSetGroup()); + SHGlobalDescriptorSets::SetLightingSubSystem(lightingSubSystem); } @@ -425,41 +459,42 @@ namespace SHADE defaultMaterial = AddMaterial ( defaultVertShader, defaultFragShader, - worldRenderGraph->GetNode("G-Buffer")->GetSubpass("G-Buffer Write") + renderGraph->GetNode("G-Buffer")->GetSubpass("G-Buffer Write") ); defaultMaterial->SetProperty("data.textureIndex", defaultTexture->TextureArrayIndex); } -#ifdef SHEDITOR - void SHGraphicsSystem::InitEditorRenderGraph(void) noexcept - { - auto windowDims = window->GetWindowSize(); +#if 0 + #ifdef SHEDITOR + void SHGraphicsSystem::InitEditorRenderGraph(void) noexcept + { + auto windowDims = window->GetWindowSize(); - // Create Default Viewport - editorViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast(windowDims.first), static_cast(windowDims.second), 0.0f, 1.0f)); + // Create Default Viewport + editorViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast(windowDims.first), static_cast(windowDims.second), 0.0f, 1.0f)); - // Get render graph from viewport editor renderer - editorRenderGraph = resourceManager.Create(); + // Get render graph from viewport editor renderer + editorRenderGraph = resourceManager.Create(); - std::vector> renderContextCmdPools{ swapchain->GetNumImages() }; - for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) - renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; + std::vector> renderContextCmdPools{ swapchain->GetNumImages() }; + for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) + renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; - editorRenderGraph->Init("Editor Render Graph", device, swapchain, &resourceManager); - editorRenderGraph->AddResource("Present", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); + editorRenderGraph->Init("Editor Render Graph", device, swapchain, &resourceManager); + editorRenderGraph->AddResource("Present", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); - auto imguiNode = editorRenderGraph->AddNode("ImGui Node", { "Present"}, {}); - auto imguiSubpass = imguiNode->AddSubpass("ImGui Draw"); - imguiSubpass->AddColorOutput("Present"); + auto imguiNode = editorRenderGraph->AddNode("ImGui Node", { "Present"}, {}); + auto imguiSubpass = imguiNode->AddSubpass("ImGui Draw"); + imguiSubpass->AddColorOutput("Present"); - // Generate world render graph - editorRenderGraph->Generate(); + // Generate world render graph + editorRenderGraph->Generate(); - // Add world renderer to default viewport - editorRenderer = editorViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHPredefinedData::GetPredefinedDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], editorRenderGraph); - editorRenderer->SetCamera(worldCamera); - } + // Add world renderer to default viewport + editorRenderer = AddRenderer(SHRenderer::PROJECTION_TYPE::PERSPECTIVE); + } + #endif #endif /*---------------------------------------------------------------------------------*/ @@ -547,6 +582,41 @@ namespace SHADE textRenderingSubSystem->Run(frameIndex); + + for (auto renderer : renderers) + { +#ifdef SHEDITOR + if (renderer == worldRenderer) + { + auto editorSystem = SHSystemManager::GetSystem(); + if (editorSystem->editorState != SHEditor::State::PLAY) + worldRenderer->UpdateDataManual(frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix()); + else + renderer->UpdateData(frameIndex); + } + else + renderer->UpdateData(frameIndex); +#else + renderers[renIndex]->UpdateDataAndBind(frameIndex); +#endif + } + + renderGraph->Begin(frameIndex); + auto cmdBuffer = renderGraph->GetCommandBuffer(frameIndex); + + // Bind all the buffers required for meshes + for (auto& [buffer, bindingPoint] : MESH_DATA) + { + if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eVertexBuffer) + cmdBuffer->BindVertexBuffer(bindingPoint, buffer, 0); + else if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eIndexBuffer) + cmdBuffer->BindIndexBuffer(buffer, 0); + } + + renderGraph->Execute(frameIndex, descPool); + renderGraph->End(frameIndex); + +#if 0 // For every viewport for (int vpIndex = 0; vpIndex < static_cast(viewports.size()); ++vpIndex) { @@ -564,10 +634,10 @@ namespace SHADE // Begin recording the command buffer currentCmdBuffer->BeginRecording(); - // set viewport and scissor - uint32_t w = static_cast(viewports[vpIndex]->GetWidth()); - uint32_t h = static_cast(viewports[vpIndex]->GetHeight()); - currentCmdBuffer->SetViewportScissor (static_cast(w), static_cast(h), w, h); + //// set viewport and scissor + //uint32_t w = static_cast(viewports[vpIndex]->GetWidth()); + //uint32_t h = static_cast(viewports[vpIndex]->GetHeight()); + //currentCmdBuffer->SetViewportScissor (static_cast(w), static_cast(h), w, h); // Force set the pipeline layout currentCmdBuffer->ForceSetPipelineLayout(SHPredefinedData::GetDummyPipelineLayout(), SH_PIPELINE_TYPE::GRAPHICS); @@ -601,20 +671,20 @@ namespace SHADE // bind camera data //renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); -#ifdef SHEDITOR - if (renderers[renIndex] == worldRenderer) - { - auto editorSystem = SHSystemManager::GetSystem(); - if (editorSystem->editorState != SHEditor::State::PLAY) - worldRenderer->UpdateDataAndBind(currentCmdBuffer, frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix(), cameraSystem->GetEditorCamera()->GetOrthoMatrix()); - else - renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); - } - else - renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); -#else - renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); -#endif +//#ifdef SHEDITOR +// if (renderers[renIndex] == worldRenderer) +// { +// auto editorSystem = SHSystemManager::GetSystem(); +// if (editorSystem->editorState != SHEditor::State::PLAY) +// worldRenderer->UpdateDataManual(currentCmdBuffer, frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix(), cameraSystem->GetEditorCamera()->GetOrthoMatrix()); +// else +// renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); +// } +// else +// renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); +//#else +// renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); +//#endif // Draw the scene renderers[renIndex]->Draw(frameIndex, descPool); @@ -638,8 +708,10 @@ namespace SHADE semIndex = !semIndex; } } +#endif } + /***************************************************************************/ /*! @@ -677,12 +749,8 @@ namespace SHADE // #BackEndTest: For for the fence initialized by queue submit renderContext.WaitForFence(); - // Finalise all batches - for (auto vp : viewports) - for (auto renderer : vp->GetRenderers()) - { - renderer->GetRenderGraph()->FinaliseBatch(renderContext.GetCurrentFrame(), descPool); - } + // finalize batches for render graph + renderGraph->FinaliseBatch(renderContext.GetCurrentFrame(), descPool); // #BackEndTest: Acquire the next image in the swapchain available renderContext.AcquireNextIamge(); @@ -723,7 +791,7 @@ namespace SHADE const uint32_t CURR_FRAME_IDX = renderContext.GetCurrentFrame(); auto& currFrameData = renderContext.GetCurrentFrameData(); - mousePickSystem->Run(graphicsQueue, CURR_FRAME_IDX); + mousePickSubSystem->Run(graphicsQueue, CURR_FRAME_IDX); // #BackEndTest: queues an image for presentation if (auto result = graphicsQueue->Present(swapchain, { currFrameData.semRenderFinishHdl }, CURR_FRAME_IDX); result != vk::Result::eSuccess) @@ -763,6 +831,40 @@ namespace SHADE viewports.erase(iter); } + /*---------------------------------------------------------------------------------*/ + /* Renderer Registration Functions */ + /*---------------------------------------------------------------------------------*/ + Handle SHGraphicsSystem::AddRenderer(SHRenderer::PROJECTION_TYPE projectionType) + { + std::vector> renderContextCmdPools{ swapchain->GetNumImages() }; + for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) + { + renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; + } + + // Create the renderer + auto renderer = resourceManager.Create(device, swapchain->GetNumImages(), renderContextCmdPools, descPool); + + // Store + renderers.emplace_back(renderer); + + // Return + return renderer; + } + void SHGraphicsSystem::RemoveRenderer(Handle renderer) + { + auto iter = std::find(renderers.begin(), renderers.end(), renderer); + if (iter == renderers.end()) + { + SHLOG_WARNING("Attempted to remove a Renderer that does not belong to a viewport!"); + return; + } + + // Remove it + iter->Free(); + renderers.erase(iter); + } + Handle SHGraphicsSystem::AddMaterial(Handle vertShader, Handle fragShader, Handle subpass) { // Retrieve pipeline from pipeline storage or create if unavailable @@ -1058,7 +1160,7 @@ namespace SHADE renderContext.HandleResize(); - worldRenderGraph->HandleResize(resizeWidth, resizeHeight); + renderGraph->HandleResize(resizeWidth, resizeHeight); #ifdef SHEDITOR @@ -1067,13 +1169,13 @@ namespace SHADE //editorViewport->SetWidth(windowDims.first); //editorViewport->SetHeight(windowDims.second); - editorRenderGraph->HandleResize(windowDims.first, windowDims.second); + //editorRenderGraph->HandleResize(windowDims.first, windowDims.second); #endif - screenRenderGraph->HandleResize(resizeWidth, resizeHeight); + //screenRenderGraph->HandleResize(resizeWidth, resizeHeight); - mousePickSystem->HandleResize(); - postOffscreenRender->HandleResize(); + mousePickSubSystem->HandleResize(); + postOffscreenRenderSubSystem->HandleResize(); worldViewport->SetWidth(static_cast(resizeWidth)); worldViewport->SetHeight(static_cast(resizeHeight)); @@ -1102,9 +1204,14 @@ namespace SHADE } + Handle SHGraphicsSystem::GetRenderGraph(void) const noexcept + { + return renderGraph; + } + Handle SHGraphicsSystem::GetPrimaryRenderpass() const noexcept { - return worldRenderGraph->GetNode(G_BUFFER_RENDER_GRAPH_NODE_NAME.data()); + return renderGraph->GetNode(G_BUFFER_RENDER_GRAPH_NODE_NAME.data()); } Handle SHGraphicsSystem::GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept @@ -1137,7 +1244,7 @@ namespace SHADE device, SHPipelineLayoutParams { .shaderModules = { (instanced ? debugMeshVertShader : debugVertShader) , debugFragShader }, - .globalDescSetLayouts = SHPredefinedData::GetPredefinedDescSetLayouts(SHGraphicsConstants::PredefinedDescSetLayoutTypes::CAMERA) + .predefinedDescSetLayouts = SHPredefinedData::GetPredefinedDescSetLayouts(SHGraphicsConstants::PredefinedDescSetLayoutTypes::CAMERA) } ); auto pipeline = resourceManager.Create(device, pipelineLayout, nullptr, renderPass, subpass); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 75b48c9b..0aa83f21 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -34,6 +34,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/PostProcessing/SHSSAO.h" #include "Camera/SHCameraDirector.h" #include "Graphics/MiddleEnd/TextRendering/SHFontLibrary.h" +#include "Graphics/MiddleEnd/Interface/SHRenderer.h" namespace SHADE { @@ -49,7 +50,6 @@ namespace SHADE class SHVkImage; class SHVkFramebuffer; class SHVkCommandBuffer; - class SHRenderer; class SHViewport; class SHCamera; class SHVkShaderModule; @@ -99,7 +99,7 @@ namespace SHADE { private: void InitBoilerplate (void) noexcept; - void InitSceneRenderGraph (void) noexcept; + void InitRenderGraph (void) noexcept; void InitMiddleEnd (void) noexcept; void InitSubsystems (void) noexcept; void InitBuiltInResources (void); @@ -171,6 +171,13 @@ namespace SHADE Handle AddViewport(const vk::Viewport& viewport); void RemoveViewport(Handle viewport); + /*-----------------------------------------------------------------------------*/ + /* Renderers Registration Functions */ + /*-----------------------------------------------------------------------------*/ + Handle AddRenderer(SHRenderer::PROJECTION_TYPE projectionType); + void RemoveRenderer(Handle renderer); + + /*-----------------------------------------------------------------------------*/ /* Material Functions */ /*-----------------------------------------------------------------------------*/ @@ -382,8 +389,9 @@ namespace SHADE #ifdef SHEDITOR Handle GetEditorViewport () const {return editorViewport;}; #endif - Handle GetMousePickSystem(void) const noexcept {return mousePickSystem;}; - Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRender;}; + Handle GetMousePickSystem(void) const noexcept {return mousePickSubSystem;}; + Handle GetPostOffscreenRenderSystem(void) const noexcept {return postOffscreenRenderSubSystem;}; + Handle GetRenderGraph (void) const noexcept; Handle GetPrimaryRenderpass() const noexcept; Handle GetDebugDrawPipeline(DebugDrawPipelineType type) const noexcept; uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } @@ -441,10 +449,8 @@ namespace SHADE // Renderers Handle worldRenderer; Handle screenRenderer; + std::vector> renderers; - // Temp Cameras - Handle worldCamera; - Handle screenCamera; DirectorHandle worldCameraDirector; @@ -483,15 +489,15 @@ namespace SHADE std::array, MAX_PRIMITIVE_TYPES> primitiveMeshes; // Render Graphs - Handle worldRenderGraph; - Handle screenRenderGraph; + Handle renderGraph; + //Handle screenRenderGraph; #ifdef SHEDITOR - Handle editorRenderGraph; + //Handle editorRenderGraph; #endif // Sub systems - Handle mousePickSystem; - Handle postOffscreenRender; + Handle mousePickSubSystem; + Handle postOffscreenRenderSubSystem; Handle lightingSubSystem; Handle textRenderingSubSystem; Handle ssaoStorage; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 1136a2c9..be9f0482 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -14,7 +14,6 @@ of DigiPen Institute of Technology is prohibited. #include "SHRenderer.h" #include "Graphics/Devices/SHVkLogicalDevice.h" -#include "SHViewport.h" #include "Graphics/Buffers/SHVkBuffer.h" #include "Graphics/Framebuffer/SHVkFramebuffer.h" #include "SHMaterial.h" @@ -22,22 +21,22 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/Descriptors/SHVkDescriptorSetGroup.h" #include "Graphics/Buffers/SHVkBuffer.h" #include "Camera/SHCameraDirector.h" +#include "Graphics/MiddleEnd/GlobalData/SHPredefinedData.h" namespace SHADE { /*-----------------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*-----------------------------------------------------------------------------------*/ - SHRenderer::SHRenderer(Handle logicalDevice, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph) - : viewport { viewport } - , renderGraph { renderGraph } + SHRenderer::SHRenderer(Handle logicalDevice, uint32_t numFrames, Handle descriptorPool, PROJECTION_TYPE type) + : projectionType{type} { - commandBuffers.resize(static_cast(numFrames)); + //commandBuffers.resize(static_cast(numFrames)); - for (uint32_t i = 0; i < commandBuffers.size(); ++i) - commandBuffers[i] = cmdPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + //for (uint32_t i = 0; i < commandBuffers.size(); ++i) + // commandBuffers[i] = cmdPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); - cameraDescriptorSet = descriptorPool->Allocate({ cameraDescLayout }, { 1 }); + cameraDescriptorSet = descriptorPool->Allocate(SHPredefinedData::GetPredefinedDescSetLayouts(SHGraphicsConstants::PredefinedDescSetLayoutTypes::CAMERA), { 1 }); #ifdef _DEBUG const auto& CAM_DESC_SETS = cameraDescriptorSet->GetVkHandle(); @@ -61,18 +60,6 @@ namespace SHADE SHRenderer::~SHRenderer(void) { - //for (auto& cmdBuffer : commandBuffers) - //{ - // cmdBuffer.Free(); - //} - } - - /*-----------------------------------------------------------------------------------*/ - /* Camera Registration */ - /*-----------------------------------------------------------------------------------*/ - void SHRenderer::SetCamera(Handle _camera) - { - camera = _camera; } void SHRenderer::SetCameraDirector(Handle director) noexcept @@ -80,63 +67,55 @@ namespace SHADE cameraDirector = director; } - /*-----------------------------------------------------------------------------------*/ - /* Drawing Functions */ - /*-----------------------------------------------------------------------------------*/ - void SHRenderer::Draw(uint32_t frameIndex, Handle descPool) noexcept + void SHRenderer::UpdateData(uint32_t frameIndex) noexcept { - renderGraph->Execute(frameIndex, commandBuffers[frameIndex], descPool); - } - - void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex) noexcept - { - if (camera && cameraDirector) + if (cameraDirector) { - //UpdateDataAndBind(cmdBuffer, frameIndex, SHMatrix::Transpose(cameraDirector->GetVPMatrix())); - UpdateDataAndBind(cmdBuffer, frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix(), cameraDirector->GetOrthoMatrix()); + switch (projectionType) + { + case PROJECTION_TYPE::DEFAULT: + UpdateDataManual(frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix()); + break; + case PROJECTION_TYPE::PERSPECTIVE: + UpdateDataManual(frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetPerspectiveMatrix()); + break; + case PROJECTION_TYPE::ORTHOGRAPHIC: + UpdateDataManual(frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetOrthoMatrix()); + break; + default: + UpdateDataManual(frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix()); + break; + } } } - void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept + void SHRenderer::UpdateDataManual(uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept { - SetViewProjectionMatrix(viewMatrix, projMatrix, orthoMatrix); + SetViewProjectionMatrix(viewMatrix, projMatrix); - //cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex); - BindDescSet(cmdBuffer, frameIndex); + //BindDescSet(cmdBuffer, frameIndex); } - void SHRenderer::BindDescSet(Handle cmdBuffer, uint32_t frameIndex) noexcept + void SHRenderer::BindDescriptorSet(Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex, uint32_t frameIndex) noexcept { std::array dynamicOffsets{ frameIndex * cameraDataAlignedSize }; - cmdBuffer->BindDescriptorSet(cameraDescriptorSet, SH_PIPELINE_TYPE::GRAPHICS, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 }); - cmdBuffer->BindDescriptorSet(cameraDescriptorSet, SH_PIPELINE_TYPE::COMPUTE, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 }); + cmdBuffer->BindDescriptorSet(cameraDescriptorSet, pipelineType, setIndex, std::span{ dynamicOffsets.data(), 1 }); } - void SHRenderer::UpdateCameraDataToBuffer(void) noexcept + void SHRenderer::SetViewProjectionMatrix(SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept { - } - - void SHRenderer::SetViewProjectionMatrix(SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept - { - //cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); cpuCameraData.viewProjectionMatrix = SHMatrix::Transpose(projMatrix * viewMatrix); cpuCameraData.viewMatrix = SHMatrix::Transpose(viewMatrix); cpuCameraData.projectionMatrix = SHMatrix::Transpose(projMatrix); - cpuCameraData.orthoMatrix = SHMatrix::Transpose (orthoMatrix); } - Handle SHRenderer::GetRenderGraph(void) const noexcept - { - return renderGraph; - } - - Handle SHRenderer::GetCommandBuffer(uint32_t frameIndex) const noexcept - { - return commandBuffers[frameIndex]; - } + //Handle SHRenderer::GetCommandBuffer(uint32_t frameIndex) const noexcept + //{ + // return commandBuffers[frameIndex]; + //} Handle SHRenderer::GetCameraDirector(void) const noexcept { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index 4bd205be..c93050d7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -32,7 +32,6 @@ namespace SHADE class SHVkFramebuffer; class SHMaterial; class SHVkLogicalDevice; - class SHViewport; class SHVkImageView; class SHVkCommandBuffer; class SHCamera; @@ -48,7 +47,6 @@ namespace SHADE SHMatrix viewProjectionMatrix; SHMatrix viewMatrix; SHMatrix projectionMatrix; - SHMatrix orthoMatrix; }; /*---------------------------------------------------------------------------------*/ @@ -64,35 +62,36 @@ namespace SHADE /***********************************************************************************/ class SHRenderer { - public: + enum class PROJECTION_TYPE + { + DEFAULT, + PERSPECTIVE, + ORTHOGRAPHIC + }; + /*-----------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*-----------------------------------------------------------------------------*/ - SHRenderer(Handle logicalDevice, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph); + SHRenderer(Handle logicalDevice, uint32_t numFrames, Handle descriptorPool, PROJECTION_TYPE type); ~SHRenderer(void); /*-----------------------------------------------------------------------------*/ /* Camera Registration */ /*-----------------------------------------------------------------------------*/ - void SetCamera(Handle _camera); void SetCameraDirector (Handle director) noexcept; /*-----------------------------------------------------------------------------*/ /* Drawing Functions */ /*-----------------------------------------------------------------------------*/ - void Draw(uint32_t frameIndex, Handle descPool) noexcept; - void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex) noexcept; - void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept; - void BindDescSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; - void UpdateCameraDataToBuffer (void) noexcept; - void SetViewProjectionMatrix (SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept; + void UpdateData(uint32_t frameIndex) noexcept; + void UpdateDataManual(uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept; + void BindDescriptorSet (Handle cmdBuffer, SH_PIPELINE_TYPE pipelineType, uint32_t setIndex, uint32_t frameIndex) noexcept; + void SetViewProjectionMatrix (SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ /*-----------------------------------------------------------------------------*/ - Handle GetRenderGraph (void) const noexcept; - Handle GetCommandBuffer(uint32_t frameIndex) const noexcept; Handle GetCameraDirector (void) const noexcept; private: @@ -102,9 +101,6 @@ namespace SHADE //! Vulkan UBOs need to be aligned, this is pad SHShaderCameraData struct uint32_t cameraDataAlignedSize; - Handle viewport; - Handle camera; - Handle renderGraph; Handle cameraDescriptorSet; Handle cameraBuffer; @@ -114,10 +110,10 @@ namespace SHADE // GPU. SHShaderCameraData cpuCameraData; - //! Command buffers for the render graph - std::vector> commandBuffers; - + ////! Command buffers for the render graph + //std::vector> commandBuffers; + PROJECTION_TYPE projectionType; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp index 7bd0049f..078261a5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp @@ -46,34 +46,6 @@ namespace SHADE ); } - /*---------------------------------------------------------------------------------*/ - /* Renderer Registration Functions */ - /*---------------------------------------------------------------------------------*/ - Handle SHViewport::AddRenderer(SHResourceHub& resourceManager, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph) - { - // Create the renderer - auto renderer = resourceManager.Create(device, numFrames, cmdPools, descriptorPool, cameraDescLayout, GetHandle(), renderGraph); - - // Store - renderers.emplace_back(renderer); - - // Return - return renderer; - } - void SHViewport::RemoveRenderer(Handle renderer) - { - auto iter = std::find(renderers.begin(), renderers.end(), renderer); - if (iter == renderers.end()) - { - SHLOG_WARNING("Attempted to remove a Renderer that does not belong to a viewport!"); - return; - } - - // Remove it - iter->Free(); - renderers.erase(iter); - } - void SHViewport::SetWidth(float w) noexcept { viewport.width = w; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h index 26c0a6bd..60bd6e95 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h @@ -56,11 +56,11 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ void SetUp(Handle commandBuffer); - /*-----------------------------------------------------------------------------*/ - /* Renderers Registration Functions */ - /*-----------------------------------------------------------------------------*/ - Handle AddRenderer(SHResourceHub& resourceManager, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph); - void RemoveRenderer(Handle renderer); + ///*-----------------------------------------------------------------------------*/ + ///* Renderers Registration Functions */ + ///*-----------------------------------------------------------------------------*/ + //Handle AddRenderer(SHResourceHub& resourceManager, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph); + //void RemoveRenderer(Handle renderer); /*-----------------------------------------------------------------------------*/ /* Setters */ @@ -79,7 +79,7 @@ namespace SHADE float GetHeight() const { return viewport.height; } float GetMinDepth() const { return viewport.minDepth; } float GetMaxDepth() const { return viewport.maxDepth; } - std::vector>& GetRenderers() { return renderers; } + //std::vector>& GetRenderers() { return renderers; } private: @@ -88,7 +88,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ Handle device; vk::Viewport viewport; - std::vector> renderers; + //std::vector> renderers; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 448c3bed..abbf88c3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -385,7 +385,7 @@ namespace SHADE std::fill (variableSizes.begin(), variableSizes.end(), 1); // Create the descriptor set - lightingDataDescSet = descPool->Allocate({ SHPredefinedData::GetPredefinedDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS] }, variableSizes); + lightingDataDescSet = descPool->Allocate({ SHPredefinedData::GetPredefinedDescSetLayouts(SHGraphicsConstants::PredefinedDescSetLayoutTypes::LIGHTS) }, variableSizes); #ifdef _DEBUG const auto& CAM_DESC_SETS = lightingDataDescSet->GetVkHandle(); for (int i = 0; i < static_cast(CAM_DESC_SETS.size()); ++i) @@ -517,11 +517,16 @@ namespace SHADE } - void SHLightingSubSystem::BindDescSet(Handle cmdBuffer, uint32_t frameIndex) noexcept + void SHLightingSubSystem::BindDescSet(Handle cmdBuffer, uint32_t setIndex, uint32_t frameIndex) noexcept { //Bind descriptor set(We bind at an offset because the buffer holds NUM_FRAME_BUFFERS sets of data). - cmdBuffer->BindDescriptorSet(lightingDataDescSet, SH_PIPELINE_TYPE::COMPUTE, SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, { dynamicOffsets[frameIndex] }); + cmdBuffer->BindDescriptorSet(lightingDataDescSet, SH_PIPELINE_TYPE::COMPUTE, setIndex, { dynamicOffsets[frameIndex] }); } + 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 ae6caead..fa103136 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -4,6 +4,7 @@ #include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec4.h" #include "SHLightData.h" +#include #include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h" namespace SHADE @@ -57,8 +58,11 @@ namespace SHADE class SH_API SHLightingSubSystem { - private: + public: + using DynamicOffsetArray = std::array, static_cast(SHGraphicsConstants::NUM_FRAME_BUFFERS)>; + + private: class PerTypeData { private: @@ -130,7 +134,7 @@ namespace SHADE std::array(SH_LIGHT_TYPE::NUM_TYPES)> perTypeData; //! Container to store dynamic offsets for binding descriptor sets - std::array, static_cast(SHGraphicsConstants::NUM_FRAME_BUFFERS)> dynamicOffsets; + DynamicOffsetArray dynamicOffsets; //! holds the data that represents how many lights are in the scene std::array(SH_LIGHT_TYPE::NUM_TYPES)> lightCountsData; @@ -162,7 +166,8 @@ namespace SHADE void Run (SHMatrix const& viewMat, uint32_t frameIndex) noexcept; void Exit (void) noexcept; - void BindDescSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void BindDescSet (Handle cmdBuffer, uint32_t setIndex, uint32_t frameIndex) noexcept; + Handle GetLightDataDescriptorSet (void) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp index f117b26c..f9afa48f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp @@ -11,6 +11,8 @@ #include "Graphics/SHVkUtil.h" #include "Graphics/RenderGraph/SHSubpass.h" #include "Math/Transform/SHTransformComponent.h" +#include "Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h" +#include "Graphics/MiddleEnd/Interface/SHRenderer.h" namespace SHADE { @@ -91,12 +93,10 @@ namespace SHADE } - void SHTextRenderingSubSystem::Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS, std::function, uint32_t)> const& bindFunction) noexcept + void SHTextRenderingSubSystem::Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS) noexcept { SHComponentManager::CreateComponentSparseSet(); - cameraDescSetBind = bindFunction; - logicalDevice = device; // prepare pipeline layout params @@ -174,9 +174,14 @@ namespace SHADE } } - void SHTextRenderingSubSystem::Render(Handle cmdBuffer, uint32_t frameIndex) noexcept + void SHTextRenderingSubSystem::Render(Handle cmdBuffer, Handle renderer, uint32_t frameIndex) noexcept { auto& textRendererComps = SHComponentManager::GetDense(); + auto const& mappings = SHPredefinedData::GetTextSystemData().descMappings; + uint32_t fontSetIndex = mappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::FONT); + uint32_t staticGlobalSetIndex = mappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::STATIC_DATA); + uint32_t cameraSetIndex = mappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::CAMERA); + for (auto& comp : textRendererComps) { auto* transform = SHComponentManager::GetComponent(comp.GetEID()); @@ -187,16 +192,19 @@ namespace SHADE // bind the pipeline cmdBuffer->BindPipeline(pipeline); + // Bind global data + SHGlobalDescriptorSets::BindStaticGlobalData(cmdBuffer, SH_PIPELINE_TYPE::GRAPHICS, staticGlobalSetIndex); + + // Bind camera data + renderer->BindDescriptorSet(cmdBuffer, SH_PIPELINE_TYPE::GRAPHICS, cameraSetIndex, frameIndex); + + // bind descriptors for font (matrices) + cmdBuffer->BindDescriptorSet(fontHandle->GetDescriptorSet(), SH_PIPELINE_TYPE::GRAPHICS, fontSetIndex, {}); + // bind VBO (position and indices) cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::CALCULATED_GLYPH_POSITION, comp.charPositionDataBuffer, 0); cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::GLYPH_INDEX, comp.indexingDataBuffer, 0); - // bind camera desc set (again). Necessary because pipeline layout is not compatible. - cameraDescSetBind(cmdBuffer, frameIndex); - - // bind descriptors for font (matrices) - cmdBuffer->BindDescriptorSet(fontHandle->GetDescriptorSet(), SH_PIPELINE_TYPE::GRAPHICS, SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, {}); - cmdBuffer->SetPushConstantVariable("TestPushConstant.worldTransform", transform->GetTRS(), SH_PIPELINE_TYPE::GRAPHICS); cmdBuffer->SetPushConstantVariable("TestPushConstant.eid", comp.GetEID(), SH_PIPELINE_TYPE::GRAPHICS); cmdBuffer->SetPushConstantVariable("TestPushConstant.textColor", SHVec3 (1.0f, 1.0f, 1.0f), SH_PIPELINE_TYPE::GRAPHICS); @@ -206,9 +214,7 @@ namespace SHADE // call draw call cmdBuffer->DrawArrays(4, comp.text.size(), 0, 0); //glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, static_cast(textComp.lastRenderedCharacterIndex) + 1); - } - } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h index 78b363d4..c9a89129 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h @@ -43,18 +43,14 @@ namespace SHADE //! Descriptor set for font data access in shaders //Handle fontDataDescSetLayout; - //! Super temporary. Global descriptor set needs to be revamped along with - //! entire graphics system. - std::function, uint32_t)> cameraDescSetBind; - private: void RecomputePositions(SHTextRenderableComponent& textComp) noexcept; public: - void Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS, std::function, uint32_t)> const& bindFunction) noexcept; + void Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS) noexcept; void Run(uint32_t frameIndex) noexcept; - void Render (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void Render (Handle cmdBuffer, Handle renderer, uint32_t frameIndex) noexcept; void Exit(void) noexcept; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index fc029161..d431cf47 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -14,6 +14,8 @@ #include "Tools/Utilities/SHUtilities.h" #include "Graphics/MiddleEnd/GlobalData/SHPredefinedData.h" #include "Graphics/RenderGraph/SHRenderToSwapchainImageSystem.h" +#include "Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h" + namespace SHADE { @@ -424,7 +426,7 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::Init(std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub) noexcept + void SHRenderGraph::Init(std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub, std::vector>& cmdPools) noexcept { //resourceHub = std::make_shared(); @@ -437,6 +439,11 @@ namespace SHADE renderGraphStorage->resourceHub = resourceHub; renderGraphStorage->descriptorPool = logicalDevice->CreateDescriptorPools(); + commandBuffers.resize(static_cast(swapchain->GetNumImages())); + + for (uint32_t i = 0; i < commandBuffers.size(); ++i) + commandBuffers[i] = cmdPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + name = std::move(graphName); } @@ -555,16 +562,22 @@ namespace SHADE if (renderGraphStorage->graphResources->contains(toSwapchainResource) && renderGraphStorage->graphResources->contains(swapchainResource)) { auto newNode = AddNode("Render To Present", { ResourceInstruction (toSwapchainResource.c_str()), ResourceInstruction(swapchainResource.c_str()) }, predecessorNodes); - auto newSubpass = newNode->AddSubpass("Render"); + auto newSubpass = newNode->AddSubpass("Render", {}, {}); newSubpass->AddColorOutput(swapchainResource); newSubpass->AddInput(toSwapchainResource); renderToSwapchainImageSystem = renderGraphStorage->resourceHub->Create (newNode, newSubpass, shaderModules); - newSubpass->AddExteriorDrawCalls([=](Handle& cmdBuffer, uint32_t frameIndex) + newSubpass->AddExteriorDrawCalls([=](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) { cmdBuffer->BindPipeline(renderToSwapchainImageSystem->GetPipeline()); - + + // If we are rendering to present image, the width and height will be the dimensions of that image. So we need to set viewport scissor. + auto resource = renderGraphStorage->graphResources->at(swapchainResource); + uint32_t w = static_cast(resource->GetWidth()); + uint32_t h = static_cast(resource->GetHeight()); + cmdBuffer->SetViewportScissor(static_cast(w), static_cast(h), w, h); + newSubpass->BindDescriptorInputDescriptorSets (cmdBuffer, frameIndex); // draw a quad. @@ -616,14 +629,39 @@ namespace SHADE // TODO: The graph scope buffers were meant to bind vertex buffers and index buffers for meshes. Find a // better way to manage these - void SHRenderGraph::Execute(uint32_t frameIndex, Handle cmdBuffer, Handle descPool) noexcept + void SHRenderGraph::Execute(uint32_t frameIndex, Handle descPool) noexcept { + auto cmdBuffer = commandBuffers[frameIndex]; cmdBuffer->BeginLabeledSegment(name); + + // Force bind pipeline layout + cmdBuffer->ForceSetPipelineLayout(SHPredefinedData::GetBatchingSystemData().dummyPipelineLayout, SH_PIPELINE_TYPE::GRAPHICS); + cmdBuffer->ForceSetPipelineLayout(SHPredefinedData::GetBatchingSystemData().dummyPipelineLayout, SH_PIPELINE_TYPE::COMPUTE); + + auto const& descMappings = SHPredefinedData::GetBatchingSystemData().descMappings; + for (auto& node : nodes) + { + // bind static global data + SHGlobalDescriptorSets::BindStaticGlobalData(cmdBuffer, SH_PIPELINE_TYPE::GRAPHICS, descMappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::STATIC_DATA)); + node->Execute(cmdBuffer, descPool, frameIndex); + } + cmdBuffer->EndLabeledSegment(); } + void SHRenderGraph::Begin(uint32_t frameIndex) noexcept + { + commandBuffers[frameIndex]->BeginRecording(); + + } + + void SHRenderGraph::End(uint32_t frameIndex) noexcept + { + commandBuffers[frameIndex]->EndRecording(); + } + void SHRenderGraph::FinaliseBatch(uint32_t frameIndex, Handle descPool) { for (auto& node : nodes) @@ -670,4 +708,9 @@ namespace SHADE return {}; } + Handle SHRenderGraph::GetCommandBuffer(uint32_t frameIndex) const noexcept + { + return commandBuffers[frameIndex]; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index f892483f..c69e83b1 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -74,6 +74,9 @@ namespace SHADE //! For rendering onto the swapchain Handle renderToSwapchainImageSystem; + //! Command buffer to issue rendering commands + std::vector> commandBuffers; + public: /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ @@ -86,7 +89,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub) noexcept; + void Init (std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub, std::vector>& cmdPools) noexcept; void AddResource(std::string resourceName, std::initializer_list typeFlags, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageUsageFlagBits usageFlags = {}, vk::ImageCreateFlagBits createFlags = {}); void LinkNonOwningResource (Handle resourceOrigin, std::string resourceName) noexcept; Handle AddNode (std::string nodeName, std::initializer_list resourceInstruction, std::initializer_list predecessorNodes) noexcept; @@ -94,16 +97,19 @@ namespace SHADE void Generate (void) noexcept; void CheckForNodeComputes (void) noexcept; - void Execute (uint32_t frameIndex, Handle cmdBuffer, Handle descPool) noexcept; + void Execute (uint32_t frameIndex, Handle descPool) noexcept; + void Begin (uint32_t frameIndex) noexcept; + void End (uint32_t frameIndex) noexcept; void FinaliseBatch (uint32_t frameIndex, Handle descPool); void HandleResize (uint32_t newWidth, uint32_t newHeight) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ - Handle GetNode (std::string const& nodeName) const noexcept; - std::vector> const& GetNodes (void) const noexcept; - Handle GetRenderGraphResource (std::string const& resourceName) const noexcept; + Handle GetNode (std::string const& nodeName) const noexcept; + std::vector> const& GetNodes (void) const noexcept; + Handle GetRenderGraphResource (std::string const& resourceName) const noexcept; + Handle GetCommandBuffer (uint32_t frameIndex) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp index 0f9379fe..1d8cea62 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp @@ -9,6 +9,8 @@ #include "SHRenderGraphStorage.h" #include "Graphics/RenderGraph/SHRenderGraphNodeCompute.h" #include "Graphics/SHVkUtil.h" +#include "Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h" +#include "Graphics/MiddleEnd/GlobalData/SHPredefinedData.h" namespace SHADE { @@ -238,7 +240,7 @@ namespace SHADE */ /***************************************************************************/ - Handle SHRenderGraphNode::AddSubpass(std::string subpassName) noexcept + Handle SHRenderGraphNode::AddSubpass(std::string subpassName, Handle viewport, Handle renderer) noexcept { // if subpass already exists, don't add. if (subpassIndexing.contains(subpassName)) @@ -253,6 +255,8 @@ namespace SHADE graphStorage->resourceHub->Create ( subpassName, + viewport, + renderer, graphStorage, GetHandle(), static_cast(subpasses.size()), resourceAttachmentMapping.get() ) @@ -318,7 +322,7 @@ namespace SHADE } // insert them all for a subpass to transition them. This subpass is the last subpass - auto dummySubpass = AddSubpass("dummy"); + auto dummySubpass = AddSubpass("dummy", {}, {}); for (auto& resource : resourcesInvolved) { dummySubpass->AddGeneralInput(resource); @@ -331,7 +335,7 @@ namespace SHADE } } - void SHRenderGraphNode::Execute(Handle& commandBuffer, Handle descPool, uint32_t frameIndex) noexcept + void SHRenderGraphNode::Execute(Handle commandBuffer, Handle descPool, uint32_t frameIndex) noexcept { uint32_t framebufferIndex = (framebuffers.size() > 1) ? frameIndex : 0; commandBuffer->BeginRenderpass(renderpass, framebuffers[framebufferIndex]); @@ -347,10 +351,14 @@ namespace SHADE commandBuffer->EndRenderpass(); + auto const& descMappings = SHPredefinedData::GetBatchingSystemData().descMappings; // Execute all subpass computes for (auto& sbCompute : nodeComputes) { + // bind lighting data + SHGlobalDescriptorSets::BindLightingData(commandBuffer, SH_PIPELINE_TYPE::COMPUTE, descMappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::LIGHTS), frameIndex); + sbCompute->Execute(commandBuffer, frameIndex); } } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h index 2311ee0c..f7e55d4a 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h @@ -22,6 +22,8 @@ namespace SHADE class SHPredefinedData; class SHRenderGraphStorage; class SHRenderGraphNodeCompute; + class SHRenderer; + class SHViewport; class SH_API SHRenderGraphNode : public ISelfHandle { @@ -102,12 +104,12 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - Handle AddSubpass(std::string subpassName) noexcept; + 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; void AddDummySubpassIfNeeded (void) noexcept; // TODO: RemoveSubpass() - void Execute(Handle& commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; + void Execute(Handle commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; Handle GetOrCreatePipeline(std::pair, Handle> const& vsFsPair, Handle subpass) noexcept; void FinaliseBatch(uint32_t frameIndex, Handle descPool); diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h index 580f018c..a9a6ac68 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.h @@ -50,6 +50,8 @@ namespace SHADE //! Compute resources Handle computeResource; + //! + //! vector of resources needed by the subpass compute std::vector> resources; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp index c1d53632..e5052f59 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp @@ -11,6 +11,9 @@ #include "Graphics/Swapchain/SHVkSwapchain.h" #include "Graphics/Images/SHVkSampler.h" #include "SHRenderGraphResource.h" +#include "Graphics/MiddleEnd/Interface/SHViewport.h" +#include "Graphics/MiddleEnd/Interface/SHRenderer.h" +#include "Graphics/MiddleEnd/GlobalData/SHPredefinedData.h" namespace SHADE { @@ -30,7 +33,7 @@ namespace SHADE */ /***************************************************************************/ - SHSubpass::SHSubpass(const std::string& name, Handle renderGraphStorage, Handle const& parent, uint32_t index, std::unordered_map const* mapping) noexcept + SHSubpass::SHSubpass(const std::string& name, Handle inViewport, Handle inRenderer, Handle renderGraphStorage, Handle const& parent, uint32_t index, std::unordered_map const* mapping) noexcept : resourceAttachmentMapping{ mapping } , parentNode{ parent } , subpassIndex{ index } @@ -41,6 +44,8 @@ namespace SHADE , name { name } , graphStorage{ renderGraphStorage } , inputImageDescriptorSets{} + , viewport {inViewport} + , renderer {inRenderer} { } @@ -71,6 +76,8 @@ namespace SHADE , inputDescriptorLayout{ rhs.inputDescriptorLayout } , inputSamplers{ rhs.inputSamplers } , name { rhs.name } + , viewport {rhs.viewport} + , renderer {rhs.renderer} { } @@ -106,6 +113,9 @@ namespace SHADE inputDescriptorLayout = rhs.inputDescriptorLayout; inputSamplers = rhs.inputSamplers; name = std::move(rhs.name); + renderer = rhs.renderer; + viewport = rhs.viewport; + return *this; } @@ -199,21 +209,33 @@ namespace SHADE inputReferences.push_back({ resourceAttachmentMapping->at(graphStorage->graphResources->at(resourceToReference).GetId().Raw), vk::ImageLayout::eGeneral }); } - void SHSubpass::Execute(Handle& commandBuffer, Handle descPool, uint32_t frameIndex) noexcept + void SHSubpass::Execute(Handle commandBuffer, Handle descPool, uint32_t frameIndex) noexcept { commandBuffer->BeginLabeledSegment(name); - // Ensure correct transforms are provided superBatch->UpdateBuffers(frameIndex, descPool); + if (viewport) + { + // set viewport and scissor + uint32_t w = static_cast(viewport->GetWidth()); + uint32_t h = static_cast(viewport->GetHeight()); + commandBuffer->SetViewportScissor(static_cast(w), static_cast(h), w, h); + } + + auto const& descMappings = SHPredefinedData::GetBatchingSystemData().descMappings; + + if (renderer) + renderer->BindDescriptorSet(commandBuffer, SH_PIPELINE_TYPE::GRAPHICS, descMappings.GetMappings().at(SHGraphicsConstants::DescriptorSetTypes::CAMERA), frameIndex); + // Draw all the batches superBatch->Draw(commandBuffer, frameIndex); // Draw all the exterior draw calls for (auto& drawCall : exteriorDrawCalls) { - drawCall(commandBuffer, frameIndex); + drawCall(commandBuffer, renderer, frameIndex); } commandBuffer->EndLabeledSegment(); } @@ -231,7 +253,7 @@ namespace SHADE } } - void SHSubpass::AddExteriorDrawCalls(std::function&, uint32_t)> const& newDrawCall) noexcept + void SHSubpass::AddExteriorDrawCalls(ExteriorDrawCallFunction const& newDrawCall) noexcept { exteriorDrawCalls.push_back(newDrawCall); } @@ -266,7 +288,7 @@ namespace SHADE } // We build a new descriptor set layout to store our images - inputDescriptorLayout = graphStorage->logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_RESOURCE, bindings); + inputDescriptorLayout = graphStorage->logicalDevice->CreateDescriptorSetLayout(bindings); // we store a sampler if its an input attachment. if it is storage image, no need sampler, store an empty handle. for (uint32_t i = 0; i < bindings.size(); ++i) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h index 69b8fd56..b5c5c8b1 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h @@ -19,15 +19,28 @@ namespace SHADE class SHRenderGraphStorage; class SHVkShaderModule; class SHVkSampler; + class SHRenderer; + class SHViewport; class SH_API SHSubpass : public ISelfHandle { + public: + using ExteriorDrawCallFunction = std::function, Handle, uint32_t)>; + private: /*---------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ /*---------------------------------------------------------------------*/ Handle graphStorage; + //! Viewport to specify what part of the screen we want to draw on. This + //! will be used in vkCmdSetViewport/Scissor. + Handle viewport; + + //! Renderer used during the subpass execution. This dictates what matrix gets + //! passed to the shaders. + Handle renderer; + //! The index of the subpass in the render graph uint32_t subpassIndex; @@ -79,8 +92,9 @@ namespace SHADE //! after we draw everything from the batch. Because of this, these draw calls //! are always the last things drawn, so DO NOT USE THIS FUNCTIONALITY FOR ANYTHING //! COMPLEX. - std::vector&, uint32_t)>> exteriorDrawCalls; - /// For identifying subpasses + std::vector exteriorDrawCalls; + + // For identifying subpasses std::string name; @@ -88,7 +102,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHSubpass(const std::string& name, Handle renderGraphStorage, Handle const& parent, uint32_t index, std::unordered_map const* mapping) noexcept; + SHSubpass(const std::string& name, Handle inViewport, Handle inRenderer, Handle renderGraphStorage, Handle const& parent, uint32_t index, std::unordered_map const* mapping) noexcept; SHSubpass(SHSubpass&& rhs) noexcept; SHSubpass& operator=(SHSubpass&& rhs) noexcept; @@ -102,10 +116,10 @@ namespace SHADE void AddGeneralDepthOutput(std::string resourceToReference) noexcept; void AddInput(std::string resourceToReference) noexcept; void AddGeneralInput (std::string resourceToReference) noexcept; - void AddExteriorDrawCalls(std::function&, uint32_t)> const& newDrawCall) noexcept; + void AddExteriorDrawCalls(ExteriorDrawCallFunction const& newDrawCall) noexcept; // Runtime functions - void Execute(Handle& commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; + void Execute(Handle commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; void HandleResize (void) noexcept; void BindDescriptorInputDescriptorSets (Handle cmdBuffer, uint32_t frameIndex) const noexcept;