Skip to content

Commit

Permalink
Traktor: Using a "tighter" GPU culling scheme, using bounding box ext…
Browse files Browse the repository at this point in the history
…ents instead of projected sphere.
  • Loading branch information
apistol78 committed Mar 6, 2024
1 parent aa37266 commit 828078d
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 47 deletions.
4 changes: 2 additions & 2 deletions code/Animation/Editor/Cloth/ClothEntityEditor.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 Down Expand Up @@ -35,7 +35,7 @@ bool ClothEntityEditor::handleCommand(const ui::Command& command)
return scene::DefaultEntityEditor::handleCommand(command);
}

void ClothEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer) const
void ClothEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const
{
if (getContext()->shouldDrawGuide(L"Animation.Cloth"))
{
Expand Down
4 changes: 2 additions & 2 deletions code/Animation/Editor/Cloth/ClothEntityEditor.h
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 Down Expand Up @@ -33,7 +33,7 @@ class T_DLLEXPORT ClothEntityEditor : public scene::DefaultEntityEditor

virtual bool handleCommand(const ui::Command& command) override final;

virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer) const override final;
virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const override final;
};

}
2 changes: 2 additions & 0 deletions code/Mesh/Instance/InstanceMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace traktor::mesh
render::Handle s_handleInstanceWorld(L"InstanceWorld");
render::Handle s_handleInstanceWorldLast(L"InstanceWorldLast");
render::Handle s_handleTargetSize(L"InstanceMesh_TargetSize");
render::Handle s_handleViewProjection(L"InstanceMesh_ViewProjection");
render::Handle s_handleVisibility(L"InstanceMesh_Visibility");
render::Handle s_handleCullFrustum(L"InstanceMesh_CullFrustum");
render::Handle s_handleDraw(L"InstanceMesh_Draw");
Expand Down Expand Up @@ -162,6 +163,7 @@ void InstanceMesh::build(
worldRenderPass.setProgramParameters(renderBlock->programParams);

renderBlock->programParams->setVectorParameter(s_handleTargetSize, Vector4(viewSize.x, viewSize.y, 0.0f, 0.0f));
renderBlock->programParams->setMatrixParameter(s_handleViewProjection, worldRenderView.getProjection() * worldRenderView.getView());
renderBlock->programParams->setVectorArrayParameter(s_handleCullFrustum, cullFrustum, sizeof_array(cullFrustum));
renderBlock->programParams->setBufferViewParameter(s_handleInstanceWorld, m_instanceBuffer->getBufferView());
renderBlock->programParams->setBufferViewParameter(s_handleVisibility, visibilityBuffer->getBufferView());
Expand Down
37 changes: 34 additions & 3 deletions code/Scene/Editor/DefaultEntityEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <limits>
#include "Core/Log/Log.h"
#include "Core/Math/Const.h"
#include "Core/Reflection/Reflection.h"
#include "Core/Reflection/RfmArray.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ bool projectSphere(const Vector4& center, const Scalar& radius, const Scalar& zn

aabb = Vector4(minx * P00, miny * P11, maxx * P00, maxy * P11);
//aabb = aabb.xwzy * vec4(0.5f, -0.5f, 0.5f, -0.5f) + vec4(0.5f); // clip space -> uv space
//aabb = aabb.shuffle< 0, 3, 2, 1 >() * Vector4(0.5f, -0.5f, 0.5f, -0.5f) + Vector4(0.5f, 0.5f, 0.5f, 0.5f);
aabb = aabb.shuffle< 0, 3, 2, 1 >() * Vector4(0.5f, -0.5f, 0.5f, -0.5f) + Vector4(0.5f, 0.5f, 0.5f, 0.5f);

return true;
}
Expand Down Expand Up @@ -219,7 +220,7 @@ bool DefaultEntityEditor::handleCommand(const ui::Command& command)
return false;
}

void DefaultEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer) const
void DefaultEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const
{
const Vector4 c_expandBoundingBox(0.001f, 0.001f, 0.001f, 0.0f);

Expand Down Expand Up @@ -264,7 +265,37 @@ void DefaultEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer

primitiveRenderer->pushWorld(Matrix44::identity());
primitiveRenderer->pushView(Matrix44::identity());
primitiveRenderer->setProjection(Matrix44::identity());

// Setup projection as UV space.
primitiveRenderer->setProjection(orthoLh(
-1.0f, 1.0f,
1.0f, -1.0f,
-1.0f, 1.0f
) * translate(-1.0f, -1.0f, 0.0f) * scale(2.0f, 2.0f, 1.0f));

const float w = (aabb.z() - aabb.x()) * clientSize.cx;
const float h = (aabb.w() - aabb.y()) * clientSize.cy;

const int32_t level = int32_t(std::floor(std::log2(std::max(w, h))));
const float step = 4096.0f / std::max(4096 >> level, 1);
for (float y = 0.0f; y < clientSize.cy; y += step)
{
const float fy = float(y) / clientSize.cy;
primitiveRenderer->drawLine(
Vector4(0.0f, fy, 0.0f, 1.0f),
Vector4(1.0f, fy, 0.0f, 1.0f),
Color4ub(255, 255, 0, 120)
);
}
for (float x = 0.0f; x < clientSize.cx; x += step)
{
const float fx = float(x) / clientSize.cx;
primitiveRenderer->drawLine(
Vector4(fx, 0.0f, 0.0f, 1.0f),
Vector4(fx, 1.0f, 0.0f, 1.0f),
Color4ub(255, 255, 0, 120)
);
}

primitiveRenderer->drawLine(Vector4(aabb.x(), aabb.y(), 0.0f, 1.0f), Vector4(aabb.z(), aabb.y(), 0.0f, 1.0f), Color4ub(255, 0, 0, 255));
primitiveRenderer->drawLine(Vector4(aabb.x(), aabb.w(), 0.0f, 1.0f), Vector4(aabb.z(), aabb.w(), 0.0f, 1.0f), Color4ub(255, 0, 0, 255));
Expand Down
4 changes: 2 additions & 2 deletions code/Scene/Editor/DefaultEntityEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class T_DLLCLASS DefaultEntityEditor : public IEntityEditor
T_RTTI_CLASS;

public:
DefaultEntityEditor(SceneEditorContext* context, EntityAdapter* entityAdapter);
explicit DefaultEntityEditor(SceneEditorContext* context, EntityAdapter* entityAdapter);

virtual bool isPickable() const override;

Expand All @@ -50,7 +50,7 @@ class T_DLLCLASS DefaultEntityEditor : public IEntityEditor

virtual bool handleCommand(const ui::Command& command) override;

virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer) const override;
virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const override;

virtual bool getStatusText(std::wstring& outStatusText) const override;

Expand Down
4 changes: 2 additions & 2 deletions code/Scene/Editor/EntityAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,13 @@ AlignedVector< EntityAdapter::SnapPoint > EntityAdapter::getSnapPoints() const
return snapPoints;
}

void EntityAdapter::drawGuides(render::PrimitiveRenderer* primitiveRenderer) const
void EntityAdapter::drawGuides(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const
{
if (!isVisible() || isLocked())
return;

if (m_entityEditor)
m_entityEditor->drawGuide(primitiveRenderer);
m_entityEditor->drawGuide(primitiveRenderer, clientSize);

for (auto componentEditor : m_componentEditors)
componentEditor->drawGuide(primitiveRenderer);
Expand Down
3 changes: 2 additions & 1 deletion code/Scene/Editor/EntityAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Core/Math/Aabb3.h"
#include "Core/Containers/AlignedVector.h"
#include "Core/Containers/SmallMap.h"
#include "Ui/Size.h"

// import/export mechanism.
#undef T_DLLCLASS
Expand Down Expand Up @@ -225,7 +226,7 @@ class T_DLLCLASS EntityAdapter : public Object

AlignedVector< SnapPoint > getSnapPoints() const;

void drawGuides(render::PrimitiveRenderer* primitiveRenderer) const;
void drawGuides(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const;

//@}

Expand Down
5 changes: 3 additions & 2 deletions code/Scene/Editor/IEntityEditor.h
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 @@ -12,6 +12,7 @@
#include "Core/Object.h"
#include "Core/Math/Frustum.h"
#include "Core/Math/Matrix44.h"
#include "Ui/Size.h"

// import/export mechanism.
#undef T_DLLCLASS
Expand Down Expand Up @@ -125,7 +126,7 @@ class T_DLLCLASS IEntityEditor : public Object
*
* \param primitiveRenderer Primitive wire renderer.
*/
virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer) const = 0;
virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const = 0;

/*! Get status text.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ void OrthogonalRenderControl::eventPaint(ui::PaintEvent* event)
if (m_guideEnable)
{
for (auto entityAdapter : m_context->getEntities(SceneEditorContext::GfDefault))
entityAdapter->drawGuides(m_primitiveRenderer);
entityAdapter->drawGuides(m_primitiveRenderer, sz);

// Draw controller guides.
Ref< ISceneControllerEditor > controllerEditor = m_context->getControllerEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ void PerspectiveRenderControl::eventPaint(ui::PaintEvent* event)
if (m_guideEnable)
{
for (auto entityAdapter : m_context->getEntities(SceneEditorContext::GfDefault))
entityAdapter->drawGuides(m_primitiveRenderer);
entityAdapter->drawGuides(m_primitiveRenderer, sz);

// Draw controller guides.
ISceneControllerEditor* controllerEditor = m_context->getControllerEditor();
Expand Down
11 changes: 4 additions & 7 deletions code/Shape/Editor/Solid/SolidEntityEditor.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 @@ -18,10 +18,8 @@
#include "Shape/Editor/Solid/SolidEntityEditor.h"
#include "World/Entity/GroupComponent.h"

namespace traktor
namespace traktor::shape
{
namespace shape
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.shape.SolidEntityEditor", SolidEntityEditor, scene::DefaultEntityEditor)

Expand All @@ -35,7 +33,7 @@ bool SolidEntityEditor::isPickable() const
return false;
}

void SolidEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer) const
void SolidEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const
{
SolidEntity* solidEntity = dynamic_type_cast< SolidEntity* >(getEntityAdapter()->getEntity());
if (!solidEntity)
Expand Down Expand Up @@ -76,7 +74,7 @@ void SolidEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer)
}
for (uint32_t i = 0; i < winding.size(); ++i)
{
uint32_t j = (i + 1) % winding.size();
const uint32_t j = (i + 1) % winding.size();
primitiveRenderer->drawLine(
primitiveEntity->getTransform() * winding[i],
primitiveEntity->getTransform() * winding[j],
Expand All @@ -88,5 +86,4 @@ void SolidEntityEditor::drawGuide(render::PrimitiveRenderer* primitiveRenderer)
}
}

}
}
11 changes: 4 additions & 7 deletions code/Shape/Editor/Solid/SolidEntityEditor.h
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 @@ -18,10 +18,8 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::shape
{
namespace shape
{

/*!
* \ingroup Shape
Expand All @@ -31,12 +29,11 @@ class T_DLLCLASS SolidEntityEditor : public scene::DefaultEntityEditor
T_RTTI_CLASS;

public:
SolidEntityEditor(scene::SceneEditorContext* context, scene::EntityAdapter* entityAdapter);
explicit SolidEntityEditor(scene::SceneEditorContext* context, scene::EntityAdapter* entityAdapter);

virtual bool isPickable() const override final;

virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer) const override final;
virtual void drawGuide(render::PrimitiveRenderer* primitiveRenderer, const ui::Size& clientSize) const override final;
};

}
}
6 changes: 4 additions & 2 deletions code/Terrain/TerrainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const render::Handle c_handleTerrain_ColorMap(L"Terrain_ColorMap");
const render::Handle c_handleTerrain_SplatMap(L"Terrain_SplatMap");
const render::Handle c_handleTerrain_CutMap(L"Terrain_CutMap");
const render::Handle c_handleTerrain_Normals(L"Terrain_Normals");
const render::Handle c_handleTerrain_ViewProjection(L"Terrain_ViewProjection");
const render::Handle c_handleTerrain_Eye(L"Terrain_Eye");
const render::Handle c_handleTerrain_WorldOrigin(L"Terrain_WorldOrigin");
const render::Handle c_handleTerrain_WorldExtent(L"Terrain_WorldExtent");
Expand Down Expand Up @@ -176,8 +177,8 @@ void TerrainComponent::setup(
const Vector4 patchCenterWorld = (patchOrigin + patchDeltaHalf) * Vector4(1.0f, 0.0f, 1.0f, 0.0f) + Vector4(0.0f, (patch.minHeight + patch.maxHeight) * 0.5f, 0.0f, 1.0f);

const Aabb3 patchAabb(
patchCenterWorld * Vector4(1.0f, 0.0f, 1.0f, 1.0f) + Vector4(-patchDeltaHalf.x(), patch.minHeight - FUZZY_EPSILON, -patchDeltaHalf.z(), 0.0f),
patchCenterWorld * Vector4(1.0f, 0.0f, 1.0f, 1.0f) + Vector4( patchDeltaHalf.x(), patch.maxHeight + FUZZY_EPSILON, patchDeltaHalf.z(), 0.0f)
patchCenterWorld * Vector4(1.0f, 0.0f, 1.0f, 1.0f) + Vector4(-patchDeltaHalf.x(), patch.minHeight, -patchDeltaHalf.z(), 0.0f),
patchCenterWorld * Vector4(1.0f, 0.0f, 1.0f, 1.0f) + Vector4( patchDeltaHalf.x(), patch.maxHeight, patchDeltaHalf.z(), 0.0f)
);

if (worldCullFrustum.inside(patchAabb) != Frustum::Result::Outside)
Expand Down Expand Up @@ -460,6 +461,7 @@ void TerrainComponent::build(
worldRenderPass.setProgramParameters(renderBlock->programParams);

renderBlock->programParams->setVectorParameter(c_handleTerrain_TargetSize, Vector4(viewSize.x, viewSize.y, 0.0f, 0.0f));
renderBlock->programParams->setMatrixParameter(c_handleTerrain_ViewProjection, worldRenderView.getProjection() * worldRenderView.getView());
renderBlock->programParams->setBufferViewParameter(c_handleTerrain_DrawBuffer, m_drawBuffer->getBufferView());
renderBlock->programParams->setBufferViewParameter(c_handleTerrain_CulledDrawBuffer, m_culledDrawBuffer->getBufferView());
renderBlock->programParams->setBufferViewParameter(c_handleTerrain_PatchData, m_dataBuffer->getBufferView());
Expand Down
Loading

0 comments on commit 828078d

Please sign in to comment.