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.
This commit is contained in:
Brandon Mak 2022-10-30 14:17:36 +08:00
parent 94f579e8e3
commit fad9d37cd4
6 changed files with 86 additions and 80 deletions

View File

@ -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);

View File

@ -358,8 +358,8 @@ namespace SHADE
Handle<SHPostOffscreenRenderSystem> postOffscreenRender;
Handle<SHLightingSubSystem> lightingSubSystem;
uint32_t resizeWidth;
uint32_t resizeHeight;
uint32_t resizeWidth = 1;
uint32_t resizeHeight = 1;
bool restoredFromMinimize = false;
};

View File

@ -9,9 +9,9 @@ namespace SHADE
{
lightData.Reset();
SetType(SH_LIGHT_TYPE::DIRECTIONAL);
indexInBuffer = std::numeric_limits<uint32_t>::max();
//indexInBuffer = std::numeric_limits<uint32_t>::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<uint32_t>::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
{

View File

@ -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()

View File

@ -240,7 +240,7 @@ namespace SHADE
*/
/***************************************************************************/
void SHLightingSubSystem::PerTypeData::AddLight(Handle<SHVkLogicalDevice> logicalDevice, SHLightComponent* unboundLight, bool expanded) noexcept
void SHLightingSubSystem::PerTypeData::AddLight(Handle<SHVkLogicalDevice> 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<uint8_t*>(intermediateData.get()) + (lightDataAlignedSize * lightComp->GetIndexInBuffer());
WriteLightToAddress(writeLocation, lightComp);
}
//void SHLightingSubSystem::PerTypeData::ModifyLight(SHLightComponent* lightComp) noexcept
//{
// void* writeLocation = reinterpret_cast<uint8_t*>(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<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept
void SHLightingSubSystem::Run(Handle<SHVkCommandBuffer> 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<SHLightComponent>();
bool expanded = false;
bool rewrite = false;
if (numLightComponents > lightComps.size())
{
rewrite = true;
// 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();
}
}

View File

@ -102,8 +102,8 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
void InitializeData (Handle<SHVkLogicalDevice> logicalDevice, SH_LIGHT_TYPE type) noexcept;
void Expand (Handle<SHVkLogicalDevice> logicalDevice) noexcept;
void AddLight (Handle<SHVkLogicalDevice> logicalDevice, SHLightComponent* unboundLight, bool expanded) noexcept;
void ModifyLight (SHLightComponent* lightComp) noexcept;
void AddLight (Handle<SHVkLogicalDevice> 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<SHVkLogicalDevice> device, Handle<SHVkDescriptorPool> descPool) noexcept;
void Run (Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept;
void Run (Handle<SHVkCommandBuffer> cmdBuffer, /*SHMatrix const& viewMat,*/ uint32_t frameIndex) noexcept;
void Exit (void) noexcept;
};