Skip to content

Commit

Permalink
Merge pull request #5849 from peppy/vertex-batch-sanity
Browse files Browse the repository at this point in the history
Avoid initialising `VeldridVertexBuffer` immediately
  • Loading branch information
smoogipoo committed Jun 19, 2023
2 parents f9c72ae + a818d61 commit 7510e3d
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions osu.Framework/Graphics/Veldrid/Buffers/VeldridVertexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal abstract class VeldridVertexBuffer<T> : IVertexBuffer
private IStagingBuffer<DepthWrappingVertex<T>>? stagingBuffer;
private DeviceBuffer? gpuBuffer;

private int lastWrittenVertexIndex = -1;

protected VeldridVertexBuffer(VeldridRenderer renderer, int amountVertices)
{
this.renderer = renderer;
Expand All @@ -44,11 +46,15 @@ public bool SetVertex(int vertexIndex, T vertex)
{
ref var currentVertex = ref getMemory()[vertexIndex];

bool isNewVertex = !currentVertex.Vertex.Equals(vertex) || currentVertex.BackbufferDrawDepth != renderer.BackbufferDrawDepth;
bool isNewVertex = vertexIndex > lastWrittenVertexIndex
|| !currentVertex.Vertex.Equals(vertex)
|| currentVertex.BackbufferDrawDepth != renderer.BackbufferDrawDepth;

currentVertex.Vertex = vertex;
currentVertex.BackbufferDrawDepth = renderer.BackbufferDrawDepth;

lastWrittenVertexIndex = Math.Max(lastWrittenVertexIndex, vertexIndex);

return isNewVertex;
}

Expand All @@ -69,9 +75,6 @@ protected virtual void Initialise()

gpuBuffer = renderer.Factory.CreateBuffer(new BufferDescription((uint)(Size * STRIDE), BufferUsage.VertexBuffer | stagingBuffer.CopyTargetUsageFlags));
memoryLease = NativeMemoryTracker.AddMemory(this, gpuBuffer.SizeInBytes);

// Ensure the device buffer is initialised to 0.
Update();
}

~VeldridVertexBuffer()
Expand Down Expand Up @@ -119,11 +122,6 @@ public virtual void Unbind()

protected abstract PrimitiveTopology Type { get; }

public void Draw()
{
DrawRange(0, Size);
}

public void DrawRange(int startIndex, int endIndex)
{
Bind();
Expand All @@ -134,12 +132,7 @@ public void DrawRange(int startIndex, int endIndex)
Unbind();
}

public void Update()
{
UpdateRange(0, Size);
}

public void UpdateRange(int startIndex, int endIndex)
internal void UpdateRange(int startIndex, int endIndex)
{
if (gpuBuffer == null)
Initialise();
Expand Down

0 comments on commit 7510e3d

Please sign in to comment.