From 0395df5c6305d8dcca38b49e48895158da644a73 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Tue, 25 Oct 2022 19:59:16 +0800 Subject: [PATCH] Lights WIP (bug fixes) --- .../GlobalData/SHGraphicsGlobalData.cpp | 35 ++++++++-- .../MiddleEnd/Interface/SHGraphicsConstants.h | 32 +++++++-- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 11 +++- .../MiddleEnd/Interface/SHGraphicsSystem.h | 2 + .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 66 ++++++++++++------- 5 files changed, 108 insertions(+), 38 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp index ed1d3100..94d1d2c5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp @@ -4,6 +4,8 @@ #include "Graphics/Pipeline/SHPipelineState.h" #include "Graphics/Pipeline/SHVkPipelineLayout.h" #include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" +#include "Graphics/MiddleEnd/Lights/SHLightData.h" +#include "Tools/SHUtilities.h" namespace SHADE { @@ -45,16 +47,35 @@ namespace SHADE // For global data (generic data and textures) Handle staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::STATIC_GLOBALS,{ genericDataBinding, texturesBinding }); - SHVkDescriptorSetLayout::Binding lightBinding + std::vector lightBindings{}; + for (uint32_t i = 0; i < SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); ++i) { - .Type = vk::DescriptorType::eStorageBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eFragment, - .BindPoint = SHGraphicsConstants::DescriptorSetBindings::DIRECTIONAL_LIGHT_DATA, - .DescriptorCount = 1, - }; + lightBindings.push_back (SHVkDescriptorSetLayout::Binding + { + .Type = vk::DescriptorType::eStorageBufferDynamic, + .Stage = vk::ShaderStageFlagBits::eFragment, + .BindPoint = i, + .DescriptorCount = 1, + }); + } + //SHVkDescriptorSetLayout::Binding pointLightBinding + //{ + // .Type = vk::DescriptorType::eStorageBufferDynamic, + // .Stage = vk::ShaderStageFlagBits::eFragment, + // .BindPoint = SHGraphicsConstants::DescriptorSetBindings::POINT_LIGHT_DATA, + // .DescriptorCount = 1, + //}; + + //SHVkDescriptorSetLayout::Binding spotLightBinding + //{ + // .Type = vk::DescriptorType::eStorageBufferDynamic, + // .Stage = vk::ShaderStageFlagBits::eFragment, + // .BindPoint = SHGraphicsConstants::DescriptorSetBindings::SPOT_LIGHT_DATA, + // .DescriptorCount = 1, + //}; // For Dynamic global data (lights) - Handle dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, { lightBinding }); + Handle dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, lightBindings); SHVkDescriptorSetLayout::Binding cameraDataBinding { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h index 15f192c0..f31816ce 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h @@ -94,14 +94,32 @@ namespace SHADE /***************************************************************************/ static constexpr uint32_t IMAGE_AND_SAMPLERS_DATA = 1; - /***************************************************************************/ - /*! - \brief - DescriptorSet binding for lights. + ///***************************************************************************/ + ///*! + // \brief + // DescriptorSet binding for directional lights. - */ - /***************************************************************************/ - static constexpr uint32_t DIRECTIONAL_LIGHT_DATA = 0; + //*/ + ///***************************************************************************/ + //static constexpr uint32_t DIRECTIONAL_LIGHT_DATA = 0; + + ///***************************************************************************/ + ///*! + // \brief + // DescriptorSet binding for directional lights. + + //*/ + ///***************************************************************************/ + //static constexpr uint32_t POINT_LIGHT_DATA = 1; + + ///***************************************************************************/ + ///*! + // \brief + // DescriptorSet binding for directional lights. + + //*/ + ///***************************************************************************/ + //static constexpr uint32_t SPOT_LIGHT_DATA = 2; /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 7ecd92d2..b7d4bd2f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -36,6 +36,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/Images/SHVkSampler.h" #include "Assets/Asset Types/SHTextureAsset.h" #include "Graphics/MiddleEnd/Interface/SHMousePickSystem.h" +#include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" namespace SHADE { @@ -231,11 +232,15 @@ namespace SHADE for (uint32_t i = 0; i < swapchain->GetNumImages(); ++i) 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")); // Register the post offscreen render to the system postOffscreenRender = resourceManager.Create(); postOffscreenRender->Init(device, worldRenderGraph->GetRenderGraphResource("Scene"), descPool); + + lightingSubSystem = resourceManager.Create(); + lightingSubSystem->Init(device, descPool); } #ifdef SHEDITOR @@ -353,10 +358,12 @@ 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); + // Force set the pipeline layout currentCmdBuffer->ForceSetPipelineLayout(SHGraphicsGlobalData::GetDummyPipelineLayout(), SH_PIPELINE_TYPE::GRAPHICS); // Bind all the buffers required for meshes @@ -368,6 +375,8 @@ namespace SHADE currentCmdBuffer->BindIndexBuffer(buffer, 0); } + // Bind the descriptor set for lights + lightingSubSystem->Run(currentCmdBuffer, frameIndex); // Bind textures auto textureDescSet = texLibrary.GetTextureDescriptorSetGroup(); @@ -401,7 +410,7 @@ namespace SHADE renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); #endif - // Draw first + // Draw the scene renderers[renIndex]->Draw(frameIndex, descPool); // End the command buffer recording diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 870325ac..ae93bd78 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -32,6 +32,7 @@ of DigiPen Institute of Technology is prohibited. #include "../Textures/SHTextureLibrary.h" #include "../Textures/SHVkSamplerCache.h" #include "Graphics/MiddleEnd/Interface/SHPostOffscreenRenderSystem.h" +#include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" namespace SHADE { @@ -351,6 +352,7 @@ namespace SHADE // Sub systems Handle mousePickSystem; Handle postOffscreenRender; + Handle lightingSubSystem; uint32_t resizeWidth; uint32_t resizeHeight; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 613d50d3..30bbd363 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -94,6 +94,12 @@ namespace SHADE /***************************************************************************/ void SHLightingSubSystem::PerTypeData::Expand(Handle logicalDevice) noexcept { + if (lightDataAlignmentSize == 0) + { + SHLOG_ERROR ("One of the types of lights have not been accounted for. Make sure lightDataAlignmentSize is not nullptr."); + return; + } + // we want to wait for the command buffers to finish using the buffers first logicalDevice->WaitIdle(); @@ -159,15 +165,15 @@ namespace SHADE { case SH_LIGHT_TYPE::DIRECTIONAL: // TOOD: Change after creating point light struct - return 0; + return sizeof(SHDirectionalLightData); case SH_LIGHT_TYPE::POINT: - return sizeof (SHDirectionalLightData); + return 4; case SH_LIGHT_TYPE::SPOT: // TOOD: Change after creating spot light struct - return 0; + return 4; case SH_LIGHT_TYPE::NUM_TYPES: default: - return 0; + return 4; } } @@ -257,15 +263,18 @@ namespace SHADE void SHLightingSubSystem::PerTypeData::WriteToGPU(uint32_t frameIndex) noexcept { - // we want to write to the offset of the current frame - dataBuffer->WriteToMemory(intermediateData.get(), lightDataAlignmentSize * numLights, 0, lightDataAlignmentSize * maxLights * frameIndex); + if (intermediateData) + { + // we want to write to the offset of the current frame + dataBuffer->WriteToMemory(intermediateData.get(), lightDataAlignmentSize * numLights, 0, lightDataAlignmentSize * maxLights * frameIndex); + } } /***************************************************************************/ /*! \brief - Update descriptor sets. We want to call this everytime we expand buffers. + Update descriptor sets. We want to call this every time we expand buffers. \param binding The binding in the set we want to update. @@ -281,27 +290,29 @@ namespace SHADE binding, { &buffer, 1 }, 0, - perTypeData[binding].GetAlignmentSize()); + perTypeData[binding].GetAlignmentSize() * perTypeData[binding].GetMaxLights()); lightingDataDescSet->UpdateDescriptorSetBuffer(SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, binding); } /***************************************************************************/ - /*! - + /*! + \brief - Computes dynamic offsets. - - */ + Computes dynamic offsets. + + */ /***************************************************************************/ void SHLightingSubSystem::ComputeDynamicOffsets(void) noexcept { - for (uint32_t i = 0; i < dynamicOffsets.size(); ++i) + for (uint32_t i = 0; i < SHGraphicsConstants::NUM_FRAME_BUFFERS; ++i) { - auto const& typeData = perTypeData[i]; - for (uint32_t j = 0; j < SHGraphicsConstants::NUM_FRAME_BUFFERS; ++j) + for (uint32_t j = 0; j < dynamicOffsets.size(); ++j) { - dynamicOffsets[i][j] = j * typeData.GetAlignmentSize() * typeData.GetMaxLights(); + auto const& typeData = perTypeData[j]; + { + dynamicOffsets[i][j] = j * typeData.GetAlignmentSize() * typeData.GetMaxLights(); + } } } } @@ -319,17 +330,26 @@ namespace SHADE void SHLightingSubSystem::Init(Handle device, Handle descPool) noexcept { logicalDevice = device; + uint32_t constexpr NUM_LIGHT_TYPES = SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); - std::vector variableSizes{ SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES) }; + std::vector variableSizes{ NUM_LIGHT_TYPES }; std::fill (variableSizes.begin(), variableSizes.end(), 1); // Create the descriptor set lightingDataDescSet = descPool->Allocate({SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS]}, variableSizes); - // initialize all the data first. We add more lights here as we add more types. - perTypeData[SHUtilities::ToUnderlying(SH_LIGHT_TYPE::DIRECTIONAL)].InitializeData(logicalDevice, SH_LIGHT_TYPE::DIRECTIONAL); - UpdateDescSet(SHGraphicsConstants::DescriptorSetBindings::DIRECTIONAL_LIGHT_DATA); + for (uint32_t i = 0; i < NUM_LIGHT_TYPES; ++i) + { + // initialize all the data first. We add more lights here as we add more types. + perTypeData[i].InitializeData(logicalDevice, static_cast(i)); + UpdateDescSet(i); + } + + for (uint32_t i = 0; i < SHGraphicsConstants::NUM_FRAME_BUFFERS; ++i) + { + dynamicOffsets[i].resize(NUM_LIGHT_TYPES); + } } /***************************************************************************/ @@ -378,8 +398,8 @@ namespace SHADE // is a new buffer. If some expansion was detected, update descriptor sets. if (expanded) { - uint32_t numLightTyes = SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); - for (uint32_t i = 0; i < numLightTyes; ++i) + uint32_t constexpr NUM_LIGHT_TYPES = SHUtilities::ToUnderlying(SH_LIGHT_TYPE::NUM_TYPES); + for (uint32_t i = 0; i < NUM_LIGHT_TYPES; ++i) { UpdateDescSet(i); }