From fad9d37cd4ca683d27be892b26334046c62f64b5 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 30 Oct 2022 14:17:36 +0800 Subject: [PATCH] Lighting data is now copied to CPU buffer and GPU buffer every frame Since lighting will be done in view space, the camera's constant movement will make it so that the light data is often changing. Keeping track of these changes for optimization might prove to be counter productive. Copying data every frame might just be more ideal. --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 2 + .../MiddleEnd/Interface/SHGraphicsSystem.h | 6 +- .../MiddleEnd/Lights/SHLightComponent.cpp | 82 +++++++++---------- .../MiddleEnd/Lights/SHLightComponent.h | 22 ++--- .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 46 ++++++----- .../MiddleEnd/Lights/SHLightingSubSystem.h | 8 +- 6 files changed, 86 insertions(+), 80 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index bc485b40..5deb83a9 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -110,6 +110,8 @@ namespace SHADE transferCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); graphicsTexCmdBuffer = graphicsCmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + SHAssetManager::CompileAsset("../../Assets/Shaders/TestCube_VS.glsl"); + SHAssetManager::CompileAsset("../../Assets/Shaders/TestCube_FS.glsl"); SHAssetManager::CompileAsset("../../Assets/Shaders/DeferredComposite_CS.glsl"); shaderModuleLibrary.ImportAllShaderSource(device); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 0f9d602a..072a1d99 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -358,9 +358,9 @@ namespace SHADE Handle postOffscreenRender; Handle lightingSubSystem; - uint32_t resizeWidth; - uint32_t resizeHeight; - bool restoredFromMinimize = false; + uint32_t resizeWidth = 1; + uint32_t resizeHeight = 1; + bool restoredFromMinimize = false; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp index fb8795fa..2ea6bc8b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp @@ -9,9 +9,9 @@ namespace SHADE { lightData.Reset(); SetType(SH_LIGHT_TYPE::DIRECTIONAL); - indexInBuffer = std::numeric_limits::max(); + //indexInBuffer = std::numeric_limits::max(); isActive = true; - Unbind(); + //Unbind(); } @@ -23,28 +23,28 @@ namespace SHADE void SHLightComponent::SetPosition(SHVec3 const& position) noexcept { lightData.position = position; - MakeDirty(); + //MakeDirty(); } void SHLightComponent::SetType(SH_LIGHT_TYPE type) noexcept { lightData.type = type; - MakeDirty(); + //MakeDirty(); } void SHLightComponent::SetDirection(SHVec3 const& direction) noexcept { lightData.direction = direction; - MakeDirty(); + //MakeDirty(); } void SHLightComponent::SetColor(SHVec4 const& color) noexcept { lightData.color = color; - MakeDirty(); + //MakeDirty(); } void SHLightComponent::ModifyCullingMask(uint8_t layerIndex, bool value) noexcept @@ -54,7 +54,7 @@ namespace SHADE else lightData.cullingMask &= ~(1u << layerIndex); - MakeDirty(); + //MakeDirty(); } void SHLightComponent::SetCullingMask(uint32_t const& value) noexcept @@ -65,43 +65,43 @@ namespace SHADE void SHLightComponent::SetAllLayers(void) noexcept { lightData.cullingMask = std::numeric_limits::max(); - MakeDirty(); + //MakeDirty(); } void SHLightComponent::ClearAllLayers(void) noexcept { lightData.cullingMask = 0; - MakeDirty(); + //MakeDirty(); } - void SHLightComponent::MakeDirty(void) noexcept - { - dirty = true; - } + //void SHLightComponent::MakeDirty(void) noexcept + //{ + // dirty = true; + //} - void SHLightComponent::ClearDirtyFlag(void) noexcept - { - dirty = false; - } + //void SHLightComponent::ClearDirtyFlag(void) noexcept + //{ + // dirty = false; + //} - void SHLightComponent::Unbind(void) noexcept - { - bound = false; - MakeDirty(); - } - - void SHLightComponent::SetBound(uint32_t inIndexInBuffer) noexcept -{ - bound = true; - indexInBuffer = inIndexInBuffer; - } +// void SHLightComponent::Unbind(void) noexcept +// { +// bound = false; +// MakeDirty(); +// } +// +// void SHLightComponent::SetBound(uint32_t inIndexInBuffer) noexcept +//{ +// bound = true; +// indexInBuffer = inIndexInBuffer; +// } void SHLightComponent::SetStrength(float value) noexcept { lightData.strength = value; - MakeDirty(); + //MakeDirty(); } SHLightData const& SHLightComponent::GetLightData(void) const noexcept @@ -135,20 +135,20 @@ namespace SHADE } - bool SHLightComponent::IsDirty(void) const noexcept - { - return dirty; - } + //bool SHLightComponent::IsDirty(void) const noexcept + //{ + // return dirty; + //} - bool SHLightComponent::GetBound(void) const noexcept - { - return bound; - } + //bool SHLightComponent::GetBound(void) const noexcept + //{ + // return bound; + //} - uint32_t SHLightComponent::GetIndexInBuffer(void) const noexcept - { - return indexInBuffer; - } + //uint32_t SHLightComponent::GetIndexInBuffer(void) const noexcept + //{ + // return indexInBuffer; + //} float SHLightComponent::GetStrength(void) const noexcept { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h index 81eb80f5..6b35559c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h @@ -17,13 +17,13 @@ namespace SHADE //! Since the lighting system is gonna be self contained and light weight, we store this //! so that we only write this to the CPU buffer when this light component change, we don't //! rewrite everything. However we still write to the GPU buffer when everything changes. - uint32_t indexInBuffer; + //uint32_t indexInBuffer; - //! If the light component changed some value we mark this true. - bool dirty; + ////! If the light component changed some value we mark this true. + //bool dirty; - //! If the light's data is already in the buffers, this will be set to true. - bool bound; + ////! If the light's data is already in the buffers, this will be set to true. + //bool bound; public: @@ -44,10 +44,10 @@ namespace SHADE void SetCullingMask (uint32_t const& value) noexcept; void SetAllLayers (void) noexcept; // serialized void ClearAllLayers (void) noexcept; // serialized - void MakeDirty (void) noexcept; - void ClearDirtyFlag (void) noexcept; - void Unbind (void) noexcept; - void SetBound (uint32_t inIndexInBuffer) noexcept; + //void MakeDirty (void) noexcept; + //void ClearDirtyFlag (void) noexcept; + //void Unbind (void) noexcept; + //void SetBound (uint32_t inIndexInBuffer) noexcept; void SetStrength (float value) noexcept; // serialized @@ -57,8 +57,8 @@ namespace SHADE SHVec3 const& GetDirection (void) const noexcept; // serialized SHVec4 const& GetColor (void) const noexcept; // serialized uint32_t const& GetCullingMask (void) const noexcept; // serialized - bool IsDirty (void) const noexcept; - bool GetBound (void) const noexcept; + //bool IsDirty (void) const noexcept; + //bool GetBound (void) const noexcept; uint32_t GetIndexInBuffer (void) const noexcept; float GetStrength (void) const noexcept; RTTR_ENABLE() diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 5ca879c4..c4ba9fa1 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -240,7 +240,7 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::PerTypeData::AddLight(Handle logicalDevice, SHLightComponent* unboundLight, bool expanded) noexcept + void SHLightingSubSystem::PerTypeData::AddLight(Handle logicalDevice, SHLightComponent* unboundLight, bool& expanded) noexcept { if (unboundLight) { @@ -262,7 +262,7 @@ namespace SHADE WriteLightToAddress(writeLocation, unboundLight); // Set the light component to be bound to that location - unboundLight->SetBound(numLights); + //unboundLight->SetBound(numLights); // Increase light count ++numLights; @@ -280,11 +280,11 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::PerTypeData::ModifyLight(SHLightComponent* lightComp) noexcept - { - void* writeLocation = reinterpret_cast(intermediateData.get()) + (lightDataAlignedSize * lightComp->GetIndexInBuffer()); - WriteLightToAddress(writeLocation, lightComp); - } + //void SHLightingSubSystem::PerTypeData::ModifyLight(SHLightComponent* lightComp) noexcept + //{ + // void* writeLocation = reinterpret_cast(intermediateData.get()) + (lightDataAlignedSize * lightComp->GetIndexInBuffer()); + // WriteLightToAddress(writeLocation, lightComp); + //} void SHLightingSubSystem::PerTypeData::WriteToGPU(uint32_t frameIndex) noexcept { @@ -406,7 +406,7 @@ namespace SHADE dynamicOffsets[i].resize(NUM_LIGHT_TYPES + 1); // +1 for the count } - numLightComponents = 0; + //numLightComponents = 0; } /***************************************************************************/ @@ -419,21 +419,25 @@ namespace SHADE */ /***************************************************************************/ - void SHLightingSubSystem::Run(Handle cmdBuffer, uint32_t frameIndex) noexcept + void SHLightingSubSystem::Run(Handle cmdBuffer, /*SHMatrix const& viewMat,*/ uint32_t frameIndex) noexcept { static uint32_t constexpr NUM_LIGHT_TYPES = SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); auto& lightComps = SHComponentManager::GetDense(); bool expanded = false; - bool rewrite = false; - if (numLightComponents > lightComps.size()) - { - rewrite = true; - ResetNumLights(); - } + // First we reset the number of lights. We do this every frame so that we can count how many lights we have + ResetNumLights(); - numLightComponents = lightComps.size(); + //bool rewrite = false; + + //if (numLightComponents > lightComps.size()) + //{ + // rewrite = true; + // ResetNumLights(); + //} + + //numLightComponents = lightComps.size(); for (auto& light : lightComps) { @@ -441,22 +445,22 @@ namespace SHADE // First we want to make sure the light is already bound to the system. if it // isn't, we write it to the correct buffer. - if (!light.GetBound() || rewrite) + //if (!light.GetBound() || rewrite) { perTypeData[enumValue].AddLight(logicalDevice, &light, expanded); - //// add to light count + // add to light count //++lightCountsData[enumValue]; } // if there was modification to the light data - if (light.IsDirty()) + //if (light.IsDirty()) { // Write the data to the CPU - perTypeData[enumValue].ModifyLight(&light); + //perTypeData[enumValue].ModifyLight(&light); // Light is now updated in the container - light.ClearDirtyFlag(); + //light.ClearDirtyFlag(); } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h index fb7aa2de..4a667ec5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -102,8 +102,8 @@ namespace SHADE /*-----------------------------------------------------------------------*/ void InitializeData (Handle logicalDevice, SH_LIGHT_TYPE type) noexcept; void Expand (Handle logicalDevice) noexcept; - void AddLight (Handle logicalDevice, SHLightComponent* unboundLight, bool expanded) noexcept; - void ModifyLight (SHLightComponent* lightComp) noexcept; + void AddLight (Handle logicalDevice, SHLightComponent* unboundLight, bool& expanded) noexcept; + //void ModifyLight (SHLightComponent* lightComp) noexcept; void WriteToGPU (uint32_t frameIndex) noexcept; void ResetNumLights (void) noexcept; @@ -144,7 +144,7 @@ namespace SHADE //! Number of SHLightComponents recorded. If at the beginning of the run function the size returned by the dense //! set is less than the size recorded, rewrite all light components into the its respective buffers. If its more, //! don't do anything. - uint32_t numLightComponents; + //uint32_t numLightComponents; /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER FUNCTIONS */ @@ -159,7 +159,7 @@ namespace SHADE /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ void Init (Handle device, Handle descPool) noexcept; - void Run (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void Run (Handle cmdBuffer, /*SHMatrix const& viewMat,*/ uint32_t frameIndex) noexcept; void Exit (void) noexcept; };