Optimize terrain debug shape drawing by caching lines into a vertex buffer
#2841
This commit is contained in:
@@ -94,6 +94,13 @@ struct DebugLine
|
||||
float TimeLeft;
|
||||
};
|
||||
|
||||
struct DebugGeometryBuffer
|
||||
{
|
||||
GPUBuffer* Buffer;
|
||||
float TimeLeft;
|
||||
Matrix Transform;
|
||||
};
|
||||
|
||||
struct DebugTriangle
|
||||
{
|
||||
Float3 V0;
|
||||
@@ -122,12 +129,9 @@ struct DebugText3D
|
||||
float TimeLeft;
|
||||
};
|
||||
|
||||
PACK_STRUCT(struct Vertex {
|
||||
Float3 Position;
|
||||
Color32 Color;
|
||||
});
|
||||
typedef DebugDraw::Vertex Vertex;
|
||||
|
||||
GPU_CB_STRUCT(Data {
|
||||
GPU_CB_STRUCT(ShaderData {
|
||||
Matrix ViewProjection;
|
||||
Float2 Padding;
|
||||
float ClipPosZBias;
|
||||
@@ -231,6 +235,7 @@ void TeleportList(const Float3& delta, Array<DebugText3D>& list)
|
||||
|
||||
struct DebugDrawData
|
||||
{
|
||||
Array<DebugGeometryBuffer> GeometryBuffers;
|
||||
Array<DebugLine> DefaultLines;
|
||||
Array<Vertex> OneFrameLines;
|
||||
Array<DebugTriangle> DefaultTriangles;
|
||||
@@ -244,7 +249,7 @@ struct DebugDrawData
|
||||
|
||||
inline int32 Count() const
|
||||
{
|
||||
return LinesCount() + TrianglesCount() + TextCount();
|
||||
return LinesCount() + TrianglesCount() + TextCount() + GeometryBuffers.Count();
|
||||
}
|
||||
|
||||
inline int32 LinesCount() const
|
||||
@@ -280,6 +285,7 @@ struct DebugDrawData
|
||||
|
||||
inline void Update(float deltaTime)
|
||||
{
|
||||
UpdateList(deltaTime, GeometryBuffers);
|
||||
UpdateList(deltaTime, DefaultLines);
|
||||
UpdateList(deltaTime, DefaultTriangles);
|
||||
UpdateList(deltaTime, DefaultWireTriangles);
|
||||
@@ -784,7 +790,7 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
|
||||
|
||||
// Update constant buffer
|
||||
const auto cb = DebugDrawShader->GetShader()->GetCB(0);
|
||||
Data data;
|
||||
ShaderData data;
|
||||
Matrix vp;
|
||||
Matrix::Multiply(view.View, view.Projection, vp);
|
||||
Matrix::Transpose(vp, data.ViewProjection);
|
||||
@@ -830,6 +836,22 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
|
||||
context->Draw(depthTestTriangles.StartVertex, depthTestTriangles.VertexCount);
|
||||
}
|
||||
|
||||
// Geometries
|
||||
for (auto& geometry : Context->DebugDrawDepthTest.GeometryBuffers)
|
||||
{
|
||||
auto tmp = data;
|
||||
Matrix mvp;
|
||||
Matrix::Multiply(geometry.Transform, vp, mvp);
|
||||
Matrix::Transpose(mvp, tmp.ViewProjection);
|
||||
context->UpdateCB(cb, &tmp);
|
||||
auto state = data.EnableDepthTest ? &DebugDrawPsLinesDepthTest : &DebugDrawPsLinesDefault;
|
||||
context->SetState(state->Get(enableDepthWrite, true));
|
||||
context->BindVB(ToSpan(&geometry.Buffer, 1));
|
||||
context->Draw(0, geometry.Buffer->GetElementsCount());
|
||||
}
|
||||
if (Context->DebugDrawDepthTest.GeometryBuffers.HasItems())
|
||||
context->UpdateCB(cb, &data);
|
||||
|
||||
if (data.EnableDepthTest)
|
||||
context->UnBindSR(0);
|
||||
}
|
||||
@@ -862,6 +884,19 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
|
||||
context->BindVB(ToSpan(&vb, 1));
|
||||
context->Draw(defaultTriangles.StartVertex, defaultTriangles.VertexCount);
|
||||
}
|
||||
|
||||
// Geometries
|
||||
for (auto& geometry : Context->DebugDrawDefault.GeometryBuffers)
|
||||
{
|
||||
auto tmp = data;
|
||||
Matrix mvp;
|
||||
Matrix::Multiply(geometry.Transform, vp, mvp);
|
||||
Matrix::Transpose(mvp, tmp.ViewProjection);
|
||||
context->UpdateCB(cb, &tmp);
|
||||
context->SetState(DebugDrawPsLinesDefault.Get(false, false));
|
||||
context->BindVB(ToSpan(&geometry.Buffer, 1));
|
||||
context->Draw(0, geometry.Buffer->GetElementsCount());
|
||||
}
|
||||
}
|
||||
|
||||
// Text
|
||||
@@ -1088,6 +1123,24 @@ void DebugDraw::DrawLines(const Span<Float3>& lines, const Matrix& transform, co
|
||||
}
|
||||
}
|
||||
|
||||
void DebugDraw::DrawLines(GPUBuffer* lines, const Matrix& transform, float duration, bool depthTest)
|
||||
{
|
||||
if (lines == nullptr || lines->GetSize() == 0)
|
||||
return;
|
||||
if (lines->GetSize() % (sizeof(Vertex) * 2) != 0)
|
||||
{
|
||||
DebugLog::ThrowException("Cannot draw debug lines with uneven amount of items in array");
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw lines
|
||||
auto& debugDrawData = depthTest ? Context->DebugDrawDepthTest : Context->DebugDrawDefault;
|
||||
auto& geometry = debugDrawData.GeometryBuffers.AddOne();
|
||||
geometry.Buffer = lines;
|
||||
geometry.TimeLeft = duration;
|
||||
geometry.Transform = transform * Matrix::Translation(-Context->Origin);
|
||||
}
|
||||
|
||||
void DebugDraw::DrawLines(const Array<Float3>& lines, const Matrix& transform, const Color& color, float duration, bool depthTest)
|
||||
{
|
||||
DrawLines(Span<Float3>(lines.Get(), lines.Count()), transform, color, duration, depthTest);
|
||||
@@ -2147,6 +2200,7 @@ void DebugDraw::DrawText(const StringView& text, const Transform& transform, con
|
||||
|
||||
void DebugDraw::Clear(void* context)
|
||||
{
|
||||
DebugDraw::UpdateContext(context, MAX_float);
|
||||
UpdateContext(context, MAX_float);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user