From 4711a131eb875fcb43706834c42960189e6e4fcf Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 30 Oct 2022 16:35:55 +0800 Subject: [PATCH] Shifted the lighting system run outside the viewport loop. - Since lighting is only calculated in the world render graph for now, this will do just fine - Renderer takes in a view and projection matrix and does the transpose of the multiplication in the renderer --- Assets/Shaders/TestCube_VS.glsl | 1 + Assets/Shaders/TestCube_VS.shshaderb | Bin 3305 -> 3385 bytes .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 16 ++++++++++--- .../MiddleEnd/Interface/SHRenderer.cpp | 17 ++++++++++---- .../Graphics/MiddleEnd/Interface/SHRenderer.h | 8 ++++--- .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 22 ++++++++++++------ .../MiddleEnd/Lights/SHLightingSubSystem.h | 8 ++++--- 7 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Assets/Shaders/TestCube_VS.glsl b/Assets/Shaders/TestCube_VS.glsl index 49f107dd..44c6e956 100644 --- a/Assets/Shaders/TestCube_VS.glsl +++ b/Assets/Shaders/TestCube_VS.glsl @@ -33,6 +33,7 @@ layout(set = 2, binding = 0) uniform CameraData { vec4 position; mat4 vpMat; + mat4 viewMat; } cameraData; void main() diff --git a/Assets/Shaders/TestCube_VS.shshaderb b/Assets/Shaders/TestCube_VS.shshaderb index 03e23af37451b6c4c0791110541c7b6e31d07bbe..2544abd58b576513f2e8185a0b84ef070e6a42fe 100644 GIT binary patch delta 105 zcmaDUxl@Xn!GL!o^CKo(); + { +#ifdef SHEDITOR + auto editorSystem = SHSystemManager::GetSystem(); + if (editorSystem->editorState != SHEditor::State::PLAY) + lightingSubSystem->Run(cameraSystem->GetEditorCamera()->GetViewMatrix(), frameIndex); + else + lightingSubSystem->Run(worldRenderer->GetCameraDirector()->GetViewMatrix(), frameIndex); +#else + lightingSubSystem->Run(worldRenderer->GetCameraDirector()->GetViewMatrix(), frameIndex); +#endif + } // For every viewport for (int vpIndex = 0; vpIndex < static_cast(viewports.size()); ++vpIndex) @@ -394,8 +405,7 @@ namespace SHADE currentCmdBuffer->BindIndexBuffer(buffer, 0); } - // Bind the descriptor set for lights - lightingSubSystem->Run(currentCmdBuffer, frameIndex); + lightingSubSystem->BindDescSet(currentCmdBuffer, frameIndex); // Bind textures auto textureDescSet = texLibrary.GetTextureDescriptorSetGroup(); @@ -419,7 +429,7 @@ namespace SHADE { auto editorSystem = SHSystemManager::GetSystem(); if (editorSystem->editorState != SHEditor::State::PLAY) - worldRenderer->UpdateDataAndBind(currentCmdBuffer, frameIndex, SHMatrix::Transpose(cameraSystem->GetEditorCamera()->GetProjMatrix() * cameraSystem->GetEditorCamera()->GetViewMatrix())); + worldRenderer->UpdateDataAndBind(currentCmdBuffer, frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix()); else renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 962130be..a44e0661 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -83,13 +83,14 @@ namespace SHADE { if (camera && cameraDirector) { - UpdateDataAndBind(cmdBuffer, frameIndex, SHMatrix::Transpose(cameraDirector->GetVPMatrix())); + //UpdateDataAndBind(cmdBuffer, frameIndex, SHMatrix::Transpose(cameraDirector->GetVPMatrix())); + UpdateDataAndBind(cmdBuffer, frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix()); } } - void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix exteriorMatrix) noexcept + void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix viewMatrix, SHMatrix projMatrix) noexcept { - SetViewProjectionMatrix(exteriorMatrix); + SetViewProjectionMatrix(viewMatrix, projMatrix); //cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex); @@ -103,10 +104,11 @@ namespace SHADE { } - void SHRenderer::SetViewProjectionMatrix(SHMatrix const& vpMatrix) noexcept + void SHRenderer::SetViewProjectionMatrix(SHMatrix viewMatrix, SHMatrix projMatrix) noexcept { //cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); - cpuCameraData.viewProjectionMatrix = vpMatrix; + cpuCameraData.viewProjectionMatrix = SHMatrix::Transpose(projMatrix * viewMatrix); + cpuCameraData.viewMatrix = viewMatrix; } Handle SHRenderer::GetRenderGraph(void) const noexcept @@ -119,4 +121,9 @@ namespace SHADE return commandBuffers[frameIndex]; } + Handle SHRenderer::GetCameraDirector(void) const noexcept + { + return cameraDirector; + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index 87cf8ee9..b7c6718c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -46,6 +46,7 @@ namespace SHADE { SHVec4 cameraPosition; SHMatrix viewProjectionMatrix; + SHMatrix viewMatrix; }; /*---------------------------------------------------------------------------------*/ @@ -79,15 +80,16 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ void Draw(uint32_t frameIndex, Handle descPool) noexcept; void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex) noexcept; - void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix exteriorMatrix) noexcept; + void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix viewMatrix, SHMatrix projMatrix) noexcept; void UpdateCameraDataToBuffer (void) noexcept; - void SetViewProjectionMatrix (SHMatrix const& vpMatrix) noexcept; + void SetViewProjectionMatrix (SHMatrix viewMatrix, SHMatrix projMatrix) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ /*-----------------------------------------------------------------------------*/ - Handle GetRenderGraph (void) const noexcept; + Handle GetRenderGraph (void) const noexcept; Handle GetCommandBuffer(uint32_t frameIndex) const noexcept; + Handle GetCameraDirector (void) const noexcept; private: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index c4ba9fa1..ac7212bf 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -8,6 +8,7 @@ #include "SHLightComponent.h" #include "ECS_Base/Managers/SHComponentManager.h" #include "SHLightComponent.h" +#include "Math/Vector/SHVec4.h" namespace SHADE { @@ -31,7 +32,7 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::PerTypeData::WriteLightToAddress(void* address, SHLightComponent* lightComp) noexcept + void SHLightingSubSystem::PerTypeData::WriteLightToAddress(void* address, SHMatrix const& viewMat, SHLightComponent* lightComp) noexcept { auto const& lightData = lightComp->GetLightData(); switch (lightData.type) @@ -41,6 +42,7 @@ namespace SHADE SHDirectionalLightData* lightPtr = reinterpret_cast(address); lightPtr->cullingMask = lightData.cullingMask; + //lightPtr->direction = viewMat * SHVec4 (lightData.direction[0], lightData.direction[1], lightData.direction[2], 0.0f); lightPtr->direction = lightData.direction; lightPtr->diffuseColor = lightData.color; lightPtr->active = lightComp->isActive; @@ -240,7 +242,7 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::PerTypeData::AddLight(Handle logicalDevice, SHLightComponent* unboundLight, bool& expanded) noexcept + void SHLightingSubSystem::PerTypeData::AddLight(Handle logicalDevice, SHLightComponent* unboundLight, SHMatrix const& viewMat, bool& expanded) noexcept { if (unboundLight) { @@ -259,7 +261,7 @@ namespace SHADE void* writeLocation = reinterpret_cast(intermediateData.get()) + (lightDataAlignedSize * numLights); // Write the light data to address - WriteLightToAddress(writeLocation, unboundLight); + WriteLightToAddress(writeLocation, viewMat, unboundLight); // Set the light component to be bound to that location //unboundLight->SetBound(numLights); @@ -419,7 +421,7 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::Run(Handle cmdBuffer, /*SHMatrix const& viewMat,*/ uint32_t frameIndex) noexcept + void SHLightingSubSystem::Run(SHMatrix const& viewMat, uint32_t frameIndex) noexcept { static uint32_t constexpr NUM_LIGHT_TYPES = SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); @@ -447,7 +449,7 @@ namespace SHADE // isn't, we write it to the correct buffer. //if (!light.GetBound() || rewrite) { - perTypeData[enumValue].AddLight(logicalDevice, &light, expanded); + perTypeData[enumValue].AddLight(logicalDevice, &light, viewMat, expanded); // add to light count //++lightCountsData[enumValue]; @@ -492,8 +494,6 @@ namespace SHADE // so we do it anyway. #NoteToSelf: if at any point it affects performance, do a check before computing. ComputeDynamicOffsets(); - // 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]}); } @@ -509,4 +509,12 @@ namespace SHADE { } + + void SHLightingSubSystem::BindDescSet(Handle cmdBuffer, 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] }); + + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h index 4a667ec5..ae6caead 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -95,14 +95,14 @@ namespace SHADE //! to the GPU that stores NUM_FRAME_BUFFERS copies. std::unique_ptr intermediateData; - void WriteLightToAddress (void* address, SHLightComponent* lightComp) noexcept; + void WriteLightToAddress (void* address, SHMatrix const& viewMat, SHLightComponent* lightComp) noexcept; public: /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ void InitializeData (Handle logicalDevice, SH_LIGHT_TYPE type) noexcept; void Expand (Handle logicalDevice) noexcept; - void AddLight (Handle logicalDevice, SHLightComponent* unboundLight, bool& expanded) noexcept; + void AddLight (Handle logicalDevice, SHLightComponent* unboundLight, SHMatrix const& viewMat, bool& expanded) noexcept; //void ModifyLight (SHLightComponent* lightComp) noexcept; void WriteToGPU (uint32_t frameIndex) noexcept; void ResetNumLights (void) noexcept; @@ -159,8 +159,10 @@ namespace SHADE /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ void Init (Handle device, Handle descPool) noexcept; - void Run (Handle cmdBuffer, /*SHMatrix const& viewMat,*/ uint32_t frameIndex) noexcept; + void Run (SHMatrix const& viewMat, uint32_t frameIndex) noexcept; void Exit (void) noexcept; + void BindDescSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; + }; }