Skip to content

Commit

Permalink
Traktor: Terrain GPU occlusion culling.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Mar 5, 2024
1 parent 3809b55 commit 66d7a15
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 114 deletions.
9 changes: 4 additions & 5 deletions code/Terrain/Editor/TerrainEntityPipeline.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand All @@ -75,5 +75,4 @@ bool TerrainEntityPipeline::buildDependencies(
return true;
}

}
}
15 changes: 11 additions & 4 deletions code/Terrain/TerrainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -433,6 +435,9 @@ void TerrainComponent::build(
snapshotOffset.storeUnaligned(data->surfaceOffset);
}

visiblePatch.patchAabb.mn.storeAligned(data->patchBoundingBoxMn);
visiblePatch.patchAabb.mx.storeAligned(data->patchBoundingBoxMx);

data++;
}

Expand Down Expand Up @@ -834,25 +839,27 @@ 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)
return false;

m_culledDrawBuffer = m_renderSystem->createBuffer(
render::BuIndirect,
(uint32_t)m_patches.size() * sizeof(render::IndexedIndirectDraw),
alignedPatchCount * sizeof(render::IndexedIndirectDraw),
false
);
if (!m_culledDrawBuffer)
return false;

m_dataBuffer = m_renderSystem->createBuffer(
render::BuStructured,
(uint32_t)m_patches.size() * sizeof(DrawData),
alignedPatchCount * sizeof(DrawData),
true
);
if (!m_dataBuffer)
Expand Down
1 change: 1 addition & 0 deletions code/Terrain/TerrainComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 66d7a15

Please sign in to comment.