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

Reduce VBO buffer count to 2 #5896

Merged
merged 4 commits into from
Jul 5, 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
4 changes: 4 additions & 0 deletions osu.Framework/FrameworkEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class FrameworkEnvironment
public static GraphicsSurfaceType? PreferredGraphicsSurface { get; }
public static string? PreferredGraphicsRenderer { get; }
public static int? StagingBufferType { get; }
public static int? VertexBufferCount { get; }

static FrameworkEnvironment()
{
Expand All @@ -23,6 +24,9 @@ static FrameworkEnvironment()
PreferredGraphicsSurface = Enum.TryParse<GraphicsSurfaceType>(Environment.GetEnvironmentVariable("OSU_GRAPHICS_SURFACE"), true, out var surface) ? surface : null;
PreferredGraphicsRenderer = Environment.GetEnvironmentVariable("OSU_GRAPHICS_RENDERER")?.ToLowerInvariant();

if (int.TryParse(Environment.GetEnvironmentVariable("OSU_GRAPHICS_VBO_COUNT"), out int count))
VertexBufferCount = count;

if (int.TryParse(Environment.GetEnvironmentVariable("OSU_GRAPHICS_STAGING_BUFFER_TYPE"), out int stagingBufferImplementation))
StagingBufferType = stagingBufferImplementation;
}
Expand Down
41 changes: 26 additions & 15 deletions osu.Framework/Graphics/Veldrid/Batches/VeldridVertexBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Vertices;
using osu.Framework.Graphics.Veldrid.Buffers;
using osu.Framework.Platform;
using osu.Framework.Statistics;

namespace osu.Framework.Graphics.Veldrid.Batches
{
internal abstract class VeldridVertexBatch<T> : IVertexBatch<T>
where T : unmanaged, IEquatable<T>, IVertex
{
private readonly List<VeldridVertexBuffer<T>>[] tripleBufferedVertexBuffers = new List<VeldridVertexBuffer<T>>[3];
/// <summary>
/// Most documentation recommends that three buffers are used to avoid contention.
///
/// We already have a triple buffer (see <see cref="GameHost.DrawRoots"/>) at a higher level which guarantees one extra previous buffer,
/// so setting this to two here is ample to guarantee we don't hit any weird edge cases (gives a theoretical buffer count of 4, in the worst scenario).
///
/// Note that due to the higher level triple buffer, the actual number of buffers we are storing is three times as high as this constant.
/// Maintaining this many buffers is a cause of concern from an memory alloc / GPU upload perspective.
/// </summary>
private const int vertex_buffer_count = 2;

public List<VeldridVertexBuffer<T>> CurrentVertexBuffers
{
get => tripleBufferedVertexBuffers[renderer.ResetId % 3];
set => tripleBufferedVertexBuffers[renderer.ResetId % 3] = value;
}
/// <summary>
/// Multiple VBOs in a swap chain to try our best to avoid GPU contention.
/// </summary>
private readonly List<VeldridVertexBuffer<T>>[] vertexBuffers = new List<VeldridVertexBuffer<T>>[FrameworkEnvironment.VertexBufferCount ?? vertex_buffer_count];

private List<VeldridVertexBuffer<T>> currentVertexBuffers => vertexBuffers[renderer.ResetId % (ulong)vertexBuffers.Length];

private VeldridVertexBuffer<T> currentVertexBuffer => currentVertexBuffers[currentBufferIndex];

/// <summary>
/// The number of vertices in each VertexBuffer.
Expand All @@ -34,17 +47,15 @@ public List<VeldridVertexBuffer<T>> CurrentVertexBuffers

private readonly VeldridRenderer renderer;

private VeldridVertexBuffer<T> currentVertexBuffer => CurrentVertexBuffers[currentBufferIndex];

protected VeldridVertexBatch(VeldridRenderer renderer, int bufferSize)
{
Size = bufferSize;
this.renderer = renderer;

AddAction = Add;

for (int i = 0; i < tripleBufferedVertexBuffers.Length; i++)
tripleBufferedVertexBuffers[i] = new List<VeldridVertexBuffer<T>>();
for (int i = 0; i < vertexBuffers.Length; i++)
vertexBuffers[i] = new List<VeldridVertexBuffer<T>>();
}

#region Disposal
Expand All @@ -59,9 +70,9 @@ protected void Dispose(bool disposing)
{
if (disposing)
{
for (int i = 0; i < tripleBufferedVertexBuffers.Length; i++)
for (int i = 0; i < vertexBuffers.Length; i++)
{
foreach (VeldridVertexBuffer<T> vbo in tripleBufferedVertexBuffers[i])
foreach (VeldridVertexBuffer<T> vbo in vertexBuffers[i])
vbo.Dispose();
}
}
Expand All @@ -86,15 +97,15 @@ public void Add(T v)
{
renderer.SetActiveBatch(this);

if (currentBufferIndex < CurrentVertexBuffers.Count && currentVertexIndex >= currentVertexBuffer.Size)
if (currentBufferIndex < currentVertexBuffers.Count && currentVertexIndex >= currentVertexBuffer.Size)
{
Draw();
FrameStatistics.Increment(StatisticsCounterType.VBufOverflow);
}

// currentIndex will change after Draw() above, so this cannot be in an else-condition
while (currentBufferIndex >= CurrentVertexBuffers.Count)
CurrentVertexBuffers.Add(CreateVertexBuffer(renderer));
while (currentBufferIndex >= currentVertexBuffers.Count)
currentVertexBuffers.Add(CreateVertexBuffer(renderer));

if (currentVertexBuffer.SetVertex(currentVertexIndex, v))
{
Expand Down
Loading