From f5745c8cbfd802c6b9e73b5f5c9c9e303047bc9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksi=20Gr=C3=B6n?= Date: Thu, 24 Oct 2024 11:07:34 +0300 Subject: [PATCH] Experimental terrain shadows are enabled with debug setting --- editor/src/Views/DebugView.cpp | 4 +++ engine/src/Graphics/TerrainSystem.cpp | 38 +++++++++++++-------- engine/src/Rendering/RenderDebugFeature.hpp | 3 +- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/editor/src/Views/DebugView.cpp b/editor/src/Views/DebugView.cpp index cfdc70d8..4e66c98b 100644 --- a/editor/src/Views/DebugView.cpp +++ b/editor/src/Views/DebugView.cpp @@ -58,6 +58,10 @@ void DebugView::Update(EditorContext& context) if (ImGui::Checkbox("Draw terrain tiles", &drawTerrain)) features.SetFeatureEnabled(kokko::RenderDebugFeatureFlag::DrawTerrainTiles, drawTerrain); + bool terrainShadows = features.IsFeatureEnabled(kokko::RenderDebugFeatureFlag::ExperimentalTerrainShadows); + if (ImGui::Checkbox("Experimental terrain shadows", &terrainShadows)) + features.SetFeatureEnabled(kokko::RenderDebugFeatureFlag::ExperimentalTerrainShadows, terrainShadows); + if (ImGui::Button("Capture profile")) { debug->RequestBeginProfileSession(); diff --git a/engine/src/Graphics/TerrainSystem.cpp b/engine/src/Graphics/TerrainSystem.cpp index 80c3b76a..560396c1 100644 --- a/engine/src/Graphics/TerrainSystem.cpp +++ b/engine/src/Graphics/TerrainSystem.cpp @@ -295,22 +295,23 @@ void TerrainSystem::SetRoughnessValue(TerrainId id, float roughness) instances[id.i].roughnessValue = roughness; } -void TerrainSystem::Upload(const UploadParameters& parameters) +void TerrainSystem::Upload(const UploadParameters& params) { uniformBlocksRendered = 0; + const bool drawShadows = params.renderDebug.IsFeatureEnabled(RenderDebugFeatureFlag::ExperimentalTerrainShadows); for (size_t i = 1, count = instances.GetCount(); i < count; ++i) { // FIXME: Rendering will not be correct if multiple terrain instances exist in the world // All uniform block info needs to be made instance-specific - const RenderViewport& fullscreenViewport = parameters.viewports[parameters.fullscreenViewportIndex]; + const RenderViewport& fullscreenViewport = params.viewports[params.fullscreenViewportIndex]; // Select tiles to render TerrainInstance& instance = instances[i]; TerrainQuadTree& quadTree = instance.quadTree; - quadTree.UpdateTilesToRender(fullscreenViewport.frustum, fullscreenViewport.position, parameters.renderDebug); + quadTree.UpdateTilesToRender(fullscreenViewport.frustum, fullscreenViewport.position, params.renderDebug); ArrayView tiles = quadTree.GetTilesToRender(); uint32_t tileCount = static_cast(tiles.GetCount()); @@ -330,7 +331,8 @@ void TerrainSystem::Upload(const UploadParameters& parameters) uniforms.metalness = 0.0f; uniforms.roughness = instance.roughnessValue; - int viewportCount = parameters.shadowViewportsEndIndex - parameters.shadowViewportsBeginIndex + 1; + int shadowViewports = drawShadows ? params.shadowViewportsEndIndex - params.shadowViewportsBeginIndex : 0; + int viewportCount = shadowViewports + 1; uint32_t uniformBlockCount = tileCount * viewportCount; uniformStagingBuffer.Resize(uniformBlockCount * uniformBlockStride); @@ -371,16 +373,19 @@ void TerrainSystem::Upload(const UploadParameters& parameters) }; // Shadow viewports - uint32_t vpEnd = parameters.shadowViewportsEndIndex; - for (uint32_t vpIdx = parameters.shadowViewportsBeginIndex; vpIdx != vpEnd; ++vpIdx) + if (drawShadows) { - uniforms.MVP = parameters.viewports[vpIdx].viewProjection; - uniforms.MV = parameters.viewports[vpIdx].view.inverse; - - for (const auto& tile : tiles) + uint32_t vpEnd = params.shadowViewportsEndIndex; + for (uint32_t vpIdx = params.shadowViewportsBeginIndex; vpIdx != vpEnd; ++vpIdx) { - updateTileInfo(tile); - writeToBuffer(); + uniforms.MVP = params.viewports[vpIdx].viewProjection; + uniforms.MV = params.viewports[vpIdx].view.inverse; + + for (const auto& tile : tiles) + { + updateTileInfo(tile); + writeToBuffer(); + } } } @@ -409,10 +414,13 @@ void TerrainSystem::Submit(const SubmitParameters& params) { params.commandList->AddToViewport(params.fullscreenViewportIndex, RenderPassType::OpaqueGeometry, depth, i); - const size_t vpEnd = params.shadowViewportsEndIndex; - for (size_t vpIdx = params.shadowViewportsBeginIndex; vpIdx != vpEnd; ++vpIdx) + if (params.renderDebug.IsFeatureEnabled(RenderDebugFeatureFlag::ExperimentalTerrainShadows)) { - params.commandList->AddToViewport(vpIdx, RenderPassType::OpaqueGeometry, depth, i); + const size_t vpEnd = params.shadowViewportsEndIndex; + for (size_t vpIdx = params.shadowViewportsBeginIndex; vpIdx != vpEnd; ++vpIdx) + { + params.commandList->AddToViewport(vpIdx, RenderPassType::OpaqueGeometry, depth, i); + } } } } diff --git a/engine/src/Rendering/RenderDebugFeature.hpp b/engine/src/Rendering/RenderDebugFeature.hpp index 42976f82..3429a174 100644 --- a/engine/src/Rendering/RenderDebugFeature.hpp +++ b/engine/src/Rendering/RenderDebugFeature.hpp @@ -10,7 +10,8 @@ enum class RenderDebugFeatureFlag : uint32_t None = 0, DrawBounds = 1 << 0, DrawNormals = 1 << 1, - DrawTerrainTiles = 1 << 2 + DrawTerrainTiles = 1 << 2, + ExperimentalTerrainShadows = 1 << 3 }; }