Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent re-entrancy into VertexBatch.Draw() #5948

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions osu.Framework/Graphics/OpenGL/Batches/GLVertexBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,27 @@ public void Add(T v)

public int Draw()
{
if (currentVertexIndex == 0)
int countToDraw = currentVertexIndex;
currentVertexIndex = 0;

if (countToDraw == 0)
return 0;

GLVertexBuffer<T> vertexBuffer = currentVertexBuffer;
if (changeBeginIndex >= 0)
vertexBuffer.UpdateRange(changeBeginIndex, changeEndIndex);

vertexBuffer.DrawRange(0, currentVertexIndex);

int count = currentVertexIndex;
vertexBuffer.DrawRange(0, countToDraw);

// When using multiple buffers we advance to the next one with every draw to prevent contention on the same buffer with future vertex updates.
//TODO: let us know if we exceed and roll over to zero here.
currentBufferIndex = (currentBufferIndex + 1) % maxBuffers;
currentVertexIndex = 0;
changeBeginIndex = -1;

FrameStatistics.Increment(StatisticsCounterType.DrawCalls);
FrameStatistics.Add(StatisticsCounterType.VerticesDraw, count);
FrameStatistics.Add(StatisticsCounterType.VerticesDraw, countToDraw);

return count;
return countToDraw;
}
}
}
20 changes: 8 additions & 12 deletions osu.Framework/Graphics/Veldrid/Batches/VeldridVertexBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,33 +144,29 @@ public void Add(T v)

public int Draw()
{
if (currentVertexIndex == currentDrawIndex)
return 0;

var buffers = currentVertexBuffers;
int countToDraw = currentVertexIndex - currentDrawIndex;
int drawStartIndex = currentDrawIndex;
currentDrawIndex = currentVertexIndex;

if (buffers.Count == 0)
if (countToDraw == 0)
return 0;

int verticesCount = currentVertexIndex - currentDrawIndex;

IVeldridVertexBuffer<T> buffer = buffers[currentBufferIndex];
IVeldridVertexBuffer<T> buffer = currentVertexBuffers[currentBufferIndex];

if (synchronisationBeginIndex >= 0)
buffer.UpdateRange(synchronisationBeginIndex, synchronisationEndIndex);

renderer.BindVertexBuffer(buffer);
renderer.BindIndexBuffer(indexLayout, Size);
renderer.DrawVertices(primitiveType, currentDrawIndex, verticesCount);
renderer.DrawVertices(primitiveType, drawStartIndex, countToDraw);

// When using multiple buffers we advance to the next one with every draw to prevent contention on the same buffer with future vertex updates.
currentDrawIndex = currentVertexIndex;
synchronisationBeginIndex = -1;

FrameStatistics.Increment(StatisticsCounterType.DrawCalls);
FrameStatistics.Add(StatisticsCounterType.VerticesDraw, verticesCount);
FrameStatistics.Add(StatisticsCounterType.VerticesDraw, countToDraw);

return verticesCount;
return countToDraw;
}
}
}
Loading