Skip to content

Commit

Permalink
Cascade Shadow Maps Max Distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Shturm0weak committed Sep 18, 2024
1 parent e7fc20f commit 101706e
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 118 deletions.
3 changes: 2 additions & 1 deletion Pengine/Source/Core/EntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ void EntryPoint::Run() const
window,
camera->GetScene(),
camera,
viewport->GetProjectionMat4());
viewport->GetProjectionMat4(),
viewport->GetSize());
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion Pengine/Source/Core/GraphicsSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ namespace Pengine
struct Shadows
{
bool isEnabled = true;
std::vector<float> biases = { 0.01f, 0.03f, 0.1f };
std::vector<float> biases = { 0.2f, 0.5f, 0.2f };
float splitFactor = 0.75f;
float maxDistance = 200.0f;
float fogFactor = 0.2f;
bool pcfEnabled = true;
bool visualize = false;
Expand Down
21 changes: 14 additions & 7 deletions Pengine/Source/Core/RenderPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,14 @@ void RenderPassManager::CreateDeferred()
{
shadowCascadeLevels.emplace_back(glm::vec4(distance));
}
shadowCascadeLevels.emplace_back(camera.GetZFar());

WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.distances", *shadowCascadeLevels.data());

const int cascadeCount = m_CSMRenderer.GetLightSpaceMatrices().size() * renderInfo.scene->GetGraphicsSettings().shadows.isEnabled;
WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.cascadeCount", cascadeCount);

WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.fogFactor", renderInfo.scene->GetGraphicsSettings().shadows.fogFactor);
WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.maxDistance", renderInfo.scene->GetGraphicsSettings().shadows.maxDistance);
WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.pcfRange", renderInfo.scene->GetGraphicsSettings().shadows.pcfRange);

const int pcfEnabled = renderInfo.scene->GetGraphicsSettings().shadows.pcfEnabled;
Expand All @@ -477,7 +477,6 @@ void RenderPassManager::CreateDeferred()
{
biases.emplace_back(glm::vec4(bias));
}
shadowCascadeLevels.emplace_back(camera.GetZFar());
WriterBufferHelper::WriteToBuffer(baseMaterial.get(), lightsBuffer, "Lights", "csm.biases", *biases.data());
}
else
Expand Down Expand Up @@ -1249,7 +1248,8 @@ void RenderPassManager::CreateCSM()

createInfo.renderCallback = [this](const RenderPass::RenderCallbackInfo& renderInfo)
{
if (!renderInfo.scene->GetGraphicsSettings().shadows.isEnabled)
const GraphicsSettings::Shadows& shadowsSettings = renderInfo.scene->GetGraphicsSettings().shadows;
if (!shadowsSettings.isEnabled)
{
return;
}
Expand Down Expand Up @@ -1370,13 +1370,20 @@ void RenderPassManager::CreateCSM()
if (!updatedLightSpaceMatrices)
{
updatedLightSpaceMatrices = true;
// TODO: Camera can be ortho, so need to make for ortho as well.
const glm::mat4 projection = glm::perspective(
camera.GetFov(),
(float)renderInfo.viewportSize.x / (float)renderInfo.viewportSize.y,
camera.GetZNear(),
shadowsSettings.maxDistance);

const bool recreateFrameBuffer = m_CSMRenderer.GenerateLightSpaceMatrices(
renderInfo.projection * camera.GetViewMat4(),
projection * camera.GetViewMat4(),
lightDirection,
camera.GetZNear(),
camera.GetZFar(),
scene->GetGraphicsSettings().shadows.cascadeCount,
scene->GetGraphicsSettings().shadows.splitFactor);
shadowsSettings.maxDistance,
shadowsSettings.cascadeCount,
shadowsSettings.splitFactor);

WriterBufferHelper::WriteToBuffer(
baseMaterial.get(),
Expand Down
6 changes: 6 additions & 0 deletions Pengine/Source/Core/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,7 @@ void Serializer::SerializeGraphicsSettings(const GraphicsSettings& graphicsSetti
out << YAML::Key << "IsEnabled" << YAML::Value << graphicsSettings.shadows.isEnabled;
out << YAML::Key << "CascadeCount" << YAML::Value << graphicsSettings.shadows.cascadeCount;
out << YAML::Key << "SplitFactor" << YAML::Value << graphicsSettings.shadows.splitFactor;
out << YAML::Key << "MaxDistance" << YAML::Value << graphicsSettings.shadows.maxDistance;
out << YAML::Key << "FogFactor" << YAML::Value << graphicsSettings.shadows.fogFactor;
out << YAML::Key << "PcfEnabled" << YAML::Value << graphicsSettings.shadows.pcfEnabled;
out << YAML::Key << "PcfRange" << YAML::Value << graphicsSettings.shadows.pcfRange;
Expand Down Expand Up @@ -2172,6 +2173,11 @@ GraphicsSettings Serializer::DeserializeGraphicsSettings(const std::filesystem::
graphicsSettings.shadows.splitFactor = splitFactorData.as<float>();
}

if (const auto& maxDistanceData = csmData["MaxDistance"])
{
graphicsSettings.shadows.maxDistance = maxDistanceData.as<float>();
}

if (const auto& biasesData = csmData["Biases"])
{
graphicsSettings.shadows.biases = biasesData.as<std::vector<float>>();
Expand Down
1 change: 1 addition & 0 deletions Pengine/Source/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ void Editor::GraphicsSettingsInfo(GraphicsSettings& graphicsSettings)
ImGui::Checkbox("Visualize", &graphicsSettings.shadows.visualize);
isChangedToSerialize += ImGui::SliderInt("Pcf Range", &graphicsSettings.shadows.pcfRange, 1, 5);
isChangedToSerialize += ImGui::SliderFloat("Split Factor", &graphicsSettings.shadows.splitFactor, 0.0f, 1.0f);
isChangedToSerialize += ImGui::SliderFloat("Max Distance", &graphicsSettings.shadows.maxDistance, 0.0f, 1000.0f);
isChangedToSerialize += ImGui::SliderFloat("Fog Factor", &graphicsSettings.shadows.fogFactor, 0.0f, 1.0f);
for (size_t i = 0; i < graphicsSettings.shadows.biases.size(); i++)
{
Expand Down
1 change: 1 addition & 0 deletions Pengine/Source/Graphics/RenderPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace Pengine
std::shared_ptr<Scene> scene;
std::shared_ptr<Entity> camera;
glm::mat4 projection;
glm::ivec2 viewportSize;
void* frame;
};

Expand Down
6 changes: 4 additions & 2 deletions Pengine/Source/Graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void Renderer::Update(
const std::shared_ptr<Window>& window,
const std::shared_ptr<Scene>& scene,
const std::shared_ptr<Entity>& camera,
const glm::mat4& projection)
const glm::mat4& projection,
const glm::ivec2& viewportSize)
{
if (!scene)
{
Expand Down Expand Up @@ -82,7 +83,8 @@ void Renderer::Update(
renderInfo.scene = scene;
renderInfo.renderPass = renderPass;
renderInfo.projection = projection;
renderInfo.frame = frame;;
renderInfo.frame = frame;
renderInfo.viewportSize = viewportSize;

renderPass->Render(renderInfo);
}
Expand Down
3 changes: 2 additions & 1 deletion Pengine/Source/Graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace Pengine
const std::shared_ptr<Window>& window,
const std::shared_ptr<Scene>& scene,
const std::shared_ptr<Entity>& camera,
const glm::mat4& projection);
const glm::mat4& projection,
const glm::ivec2& viewportSize);

std::shared_ptr<UniformWriter> GetUniformWriter(const std::string& renderPassName) const;

Expand Down
19 changes: 7 additions & 12 deletions SandBox/Configs/GraphicsSettings.gs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
SSAO:
IsEnabled: false
IsEnabled: true
AoScale: 2
Bias: 0.0250000004
KernelSize: 16
NoiseSize: 4
Radius: 0.5
CSM:
IsEnabled: true
CascadeCount: 10
SplitFactor: 0
CascadeCount: 4
SplitFactor: 0.899999976
MaxDistance: 200
FogFactor: 0.200000003
PcfEnabled: true
PcfRange: 1
Biases:
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.00999999978
- 0.0299999993
- 0.0399999991
- 0.5
Loading

0 comments on commit 101706e

Please sign in to comment.