From 66d7a15a762624457a7c8240abf78c2a53fb08ab Mon Sep 17 00:00:00 2001 From: apistol78 Date: Tue, 5 Mar 2024 10:56:58 +0100 Subject: [PATCH] Traktor: Terrain GPU occlusion culling. --- code/Terrain/Editor/TerrainEntityPipeline.cpp | 9 +- code/Terrain/TerrainComponent.cpp | 15 +- code/Terrain/TerrainComponent.h | 1 + .../Terrain/Terrain/Shaders/Cull/Cull.xdi | 223 +++++++++--------- .../Terrain/Shaders/Templates/Terrain.xdi | 8 + 5 files changed, 142 insertions(+), 114 deletions(-) diff --git a/code/Terrain/Editor/TerrainEntityPipeline.cpp b/code/Terrain/Editor/TerrainEntityPipeline.cpp index a12ef29c0a..dc1ef9a782 100644 --- a/code/Terrain/Editor/TerrainEntityPipeline.cpp +++ b/code/Terrain/Editor/TerrainEntityPipeline.cpp @@ -1,6 +1,6 @@ /* * TRAKTOR - * Copyright (c) 2022 Anders Pistol. + * Copyright (c) 2022-2024 Anders Pistol. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -15,10 +15,8 @@ #include "Terrain/TerrainComponentData.h" #include "Terrain/UndergrowthComponentData.h" -namespace traktor +namespace traktor::terrain { - namespace terrain - { T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.terrain.TerrainEntityPipeline", 0, TerrainEntityPipeline, world::EntityPipeline) @@ -66,6 +64,8 @@ bool TerrainEntityPipeline::buildDependencies( } else if (const TerrainComponentData* terrainComponentData = dynamic_type_cast< const TerrainComponentData* >(sourceAsset)) { + const Guid c_shaderCull(L"{8BA73DD8-0FD9-4C15-A772-EACC14014AEC}"); + pipelineDepends->addDependency(c_shaderCull, editor::PdfBuild | editor::PdfResource); pipelineDepends->addDependency(terrainComponentData->getTerrain(), editor::PdfBuild | editor::PdfResource); } else if (const UndergrowthComponentData* undergrowthComponentData = dynamic_type_cast< const UndergrowthComponentData* >(sourceAsset)) @@ -75,5 +75,4 @@ bool TerrainEntityPipeline::buildDependencies( return true; } - } } diff --git a/code/Terrain/TerrainComponent.cpp b/code/Terrain/TerrainComponent.cpp index 1380777bf3..8a924ab8e8 100644 --- a/code/Terrain/TerrainComponent.cpp +++ b/code/Terrain/TerrainComponent.cpp @@ -51,7 +51,6 @@ const render::Handle c_handleTerrain_DebugPatchIndex(L"Terrain_DebugPatchIndex") const render::Handle c_handleTerrain_DebugMap(L"Terrain_DebugMap"); const render::Handle c_handleTerrain_CutEnable(L"Terrain_CutEnable"); const render::Handle c_handleTerrain_PatchData(L"Terrain_PatchData"); - const render::Handle c_handleTerrain_TargetSize(L"Terrain_TargetSize"); const render::Handle c_handleTerrain_DrawBuffer(L"Terrain_DrawBuffer"); const render::Handle c_handleTerrain_CulledDrawBuffer(L"Terrain_CulledDrawBuffer"); @@ -74,6 +73,8 @@ struct DrawData { float patchOrigin[4]; float surfaceOffset[4]; + float patchBoundingBoxMn[4]; + float patchBoundingBoxMx[4]; }; Ref< render::ITexture > create1x1Texture(render::IRenderSystem* renderSystem, const Color4ub& value) @@ -268,6 +269,7 @@ void TerrainComponent::setup( cp.area = !clipped ? e.x() * e.y() : 1000.0_simd; cp.patchId = patchId; cp.patchOrigin = patchOrigin; + cp.patchAabb = patchAabb; visiblePatches.push_back(cp); } @@ -433,6 +435,9 @@ void TerrainComponent::build( snapshotOffset.storeUnaligned(data->surfaceOffset); } + visiblePatch.patchAabb.mn.storeAligned(data->patchBoundingBoxMn); + visiblePatch.patchAabb.mx.storeAligned(data->patchBoundingBoxMx); + data++; } @@ -834,9 +839,11 @@ bool TerrainComponent::createPatches() m_indexBuffer->unlock(); + const uint32_t alignedPatchCount = alignUp((uint32_t)m_patches.size(), 16); + m_drawBuffer = m_renderSystem->createBuffer( render::BuIndirect, - (uint32_t)m_patches.size() * sizeof(render::IndexedIndirectDraw), + alignedPatchCount * sizeof(render::IndexedIndirectDraw), true ); if (!m_drawBuffer) @@ -844,7 +851,7 @@ bool TerrainComponent::createPatches() m_culledDrawBuffer = m_renderSystem->createBuffer( render::BuIndirect, - (uint32_t)m_patches.size() * sizeof(render::IndexedIndirectDraw), + alignedPatchCount * sizeof(render::IndexedIndirectDraw), false ); if (!m_culledDrawBuffer) @@ -852,7 +859,7 @@ bool TerrainComponent::createPatches() m_dataBuffer = m_renderSystem->createBuffer( render::BuStructured, - (uint32_t)m_patches.size() * sizeof(DrawData), + alignedPatchCount * sizeof(DrawData), true ); if (!m_dataBuffer) diff --git a/code/Terrain/TerrainComponent.h b/code/Terrain/TerrainComponent.h index 7bbad4d44e..3087ed1dd7 100644 --- a/code/Terrain/TerrainComponent.h +++ b/code/Terrain/TerrainComponent.h @@ -99,6 +99,7 @@ class T_DLLCLASS TerrainComponent : public world::IEntityComponent float area; uint32_t patchId; Vector4 patchOrigin; + Aabb3 patchAabb; }; explicit TerrainComponent(resource::IResourceManager* resourceManager, render::IRenderSystem* renderSystem); diff --git a/data/Source/System/Terrain/Terrain/Shaders/Cull/Cull.xdi b/data/Source/System/Terrain/Terrain/Shaders/Cull/Cull.xdi index cbf27656dc..85d6c614d4 100644 --- a/data/Source/System/Terrain/Terrain/Shaders/Cull/Cull.xdi +++ b/data/Source/System/Terrain/Terrain/Shaders/Cull/Cull.xdi @@ -23,8 +23,8 @@ {D49FA81D-23DC-4D88-89DE-2BB75B119F5C} - 291 - 450 + 326 + 380 World_View Matrix @@ -34,8 +34,8 @@ {ABB90199-B133-44AE-BB92-BE5C5C381F39} - 383 - 637 + 294 + 425 World_HiZTexture Texture2D @@ -45,8 +45,8 @@ {EBE8DE46-0230-4EFA-9298-A0D667904DAA} - 679 - 587 + 590 + 375 HiZ @@ -131,8 +131,8 @@ $Output = visible ? 1.0f : 0.0f; {0D8D0651-6390-42B7-B095-5E135EFF0A08} - 401 - 679 + 312 + 467 FtPoint FtPoint @@ -149,60 +149,19 @@ $Output = visible ? 1.0f : 0.0f; {068AB477-6E13-43B5-BC99-858E85F280E4} - 376 - 721 + 294 + 509 Terrain_TargetSize Vector Frame - - {16622FDE-F912-416F-A8F5-639C65BFB41D} - - - -114 - 196 - - InstanceWorld - - - rotation - DtFloat4 - - - translation - DtFloat4 - - - boundingBoxMin - DtFloat4 - - - boundingBoxMax - DtFloat4 - - - - - {99FDC25F-406C-45F8-9C49-CA1ABD37CBF0} - - - 225 - 217 - - - rotation - translation - boundingBoxMin - boundingBoxMax - - {0B4842AF-8301-42BA-8A59-8F749002877B} - -3 - -11 + 459 + 136 Terrain_DrawBuffer @@ -232,8 +191,8 @@ $Output = visible ? 1.0f : 0.0f; {B210749B-2773-47CC-A54F-0C4C950DB6CF} - -15 - 91 + 424 + 174 Terrain_CulledDrawBuffer @@ -263,13 +222,13 @@ $Output = visible ? 1.0f : 0.0f; {6DE9EB4E-1DF3-4579-B9D1-FB65908F81A0} - 520 - 14 + 982 + 161 Copy draw Default - 1 + 16 1 1 @@ -287,17 +246,22 @@ $Output = visible ? 1.0f : 0.0f; {EC05D81B-0CB3-42B8-8FD7-9B25EB804276} CulledDrawBuffer + + {0CD162CF-AD0D-0E40-8799-99EBE074D924} + Visible + @@ -305,11 +269,50 @@ $DrawBuffer[index].firstInstance = $CulledDrawBuffer[index].firstInstance; {C43244FA-782F-46B2-9AA7-D7987F946F96} - 114 - -86 + 528 + 95 Global + + {1C9830DB-621E-714F-810F-84964217FFBB} + + + -165 + 254 + + Terrain_PatchData + + + patchOrigin + DtFloat4 + + + surfaceOffset + DtFloat4 + + + patchBoundingBoxMn + DtFloat4 + + + patchBoundingBoxMx + DtFloat4 + + + + + {222E9977-DDCA-B44B-A43A-9E76BB7E0068} + + + 180 + 264 + + + patchBoundingBoxMn + patchBoundingBoxMx + + @@ -324,112 +327,122 @@ $DrawBuffer[index].firstInstance = $CulledDrawBuffer[index].firstInstance; - - {ACC77B35-91B5-4405-ABC8-D0DA24D68178} + + {1E6639B6-8B58-4694-99E7-C058E3583522} - - {0FF6511C-0293-41A8-830E-81978BD01F7F} + + {38C7084D-4F8D-D749-A614-108D37B2EF5D} - - {ADB4FC1D-3726-4CC5-B4D5-1E2468274325} + + {1E6639B6-8B58-4694-99E7-C058E3583522} - - {0FF6511C-0293-41A8-840E-81978BD01F7F} + + {B53AF0D1-D6CC-9A40-9334-2C4CD642EE5B} - - {0FF6511C-0293-41A8-870E-81978BD01F7F} + + {82C966B2-7B19-48B2-8FE0-B85FF4E3C504} - {38213B60-FC63-A541-BCC2-95E414C2276D} + {6D0649E3-ED26-0646-A195-028038D29BD8} - - {0FF6511C-0293-41A8-880E-81978BD01F7F} + + {1E6639B6-8B58-4694-99E7-C058E3583522} - {484ABC89-3116-A645-8DEB-1DD2AD5B66E9} + {652EFABE-595D-9D4D-BC67-5C63D6168B21} - - {1E6639B6-8B58-4694-99E7-C058E3583522} + + {ACC77B35-91B5-4405-ABC8-D0DA24D68178} - - {38C7084D-4F8D-D749-A614-108D37B2EF5D} + + {EC05D81B-0CB3-42B8-8FD7-9B25EB804276} - - {1E6639B6-8B58-4694-99E7-C058E3583522} + + {ACC77B35-91B5-4405-ABC8-D0DA24D68178} - - {B53AF0D1-D6CC-9A40-9334-2C4CD642EE5B} + + {6A6FC6E9-D058-4351-9FFD-E428F1765CA1} - - {82C966B2-7B19-48B2-8FE0-B85FF4E3C504} + + {AAE82FD3-522F-43C6-A594-2E13D126E5DB} - - {6D0649E3-ED26-0646-A195-028038D29BD8} + + {31C42332-0F95-49D2-9B49-0469F96A3AB2} - - {1E6639B6-8B58-4694-99E7-C058E3583522} + + {ADB4FC1D-3726-4CC5-B4D5-1E2468274325} - - {652EFABE-595D-9D4D-BC67-5C63D6168B21} + + {0FF6511C-0293-41A8-840E-81978BD01F7F} - + {ACC77B35-91B5-4405-ABC8-D0DA24D68178} - - {EC05D81B-0CB3-42B8-8FD7-9B25EB804276} + + {0FF6511C-0293-41A8-830E-81978BD01F7F} - - {ACC77B35-91B5-4405-ABC8-D0DA24D68178} + + {0FF6511C-0293-41A8-850E-81978BD01F7F} - - {6A6FC6E9-D058-4351-9FFD-E428F1765CA1} + + {38213B60-FC63-A541-BCC2-95E414C2276D} - {AAE82FD3-522F-43C6-A594-2E13D126E5DB} + {0FF6511C-0293-41A8-860E-81978BD01F7F} - - {31C42332-0F95-49D2-9B49-0469F96A3AB2} + + {484ABC89-3116-A645-8DEB-1DD2AD5B66E9} + + + + + + {5D1C8949-65AE-2046-A34D-469B22E09B20} + + + + {0CD162CF-AD0D-0E40-8799-99EBE074D924} diff --git a/data/Source/System/Terrain/Terrain/Shaders/Templates/Terrain.xdi b/data/Source/System/Terrain/Terrain/Shaders/Templates/Terrain.xdi index 49de20c103..605fbf963e 100644 --- a/data/Source/System/Terrain/Terrain/Shaders/Templates/Terrain.xdi +++ b/data/Source/System/Terrain/Terrain/Shaders/Templates/Terrain.xdi @@ -936,6 +936,14 @@ $Output = vec4(mix(t, b, f), 0.0f); surfaceOffset DtFloat4 + + patchBoundingBoxMn + DtFloat4 + + + patchBoundingBoxMx + DtFloat4 +