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

Add a fence pool for frame tracking fences #5921

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Changes from 2 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
17 changes: 14 additions & 3 deletions osu.Framework/Graphics/Veldrid/VeldridRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ protected internal override bool AllowTearing
/// </summary>
private readonly List<FrameCompletionFence> pendingFramesFences = new List<FrameCompletionFence>();

/// <summary>
/// We are using fences every frame. Construction can be expensive, so let's pool some.
/// </summary>
private readonly Queue<Fence> perFrameFencePool = new Queue<Fence>();

public VeldridIndexData SharedLinearIndex { get; }
public VeldridIndexData SharedQuadIndex { get; }

Expand Down Expand Up @@ -215,6 +220,9 @@ protected override void Initialise(IGraphicsSurface graphicsSurface)
BufferUpdateCommands = Factory.CreateCommandList();

pipeline.Outputs = Device.SwapchainFramebuffer.OutputDescription;

for (int i = 0; i < 16; i++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this 16 a magic number? What happens in the (unlikely) even that the pool runs dry, crash on Dequeue()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. On metal I've never seen usages increase above 1. If we exceed 3 then we're probably going to see texture corruption and stuff elsewhere though? I can use 3 instead of 16 if it feels more robust.

Copy link
Member Author

@peppy peppy Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can change it to add to the pool locally, should also be fine.

diff --git a/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs b/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs
index 341a48ea6..7e0b11a9a 100644
--- a/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs
+++ b/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs
@@ -220,9 +220,6 @@ protected override void Initialise(IGraphicsSurface graphicsSurface)
             BufferUpdateCommands = Factory.CreateCommandList();
 
             pipeline.Outputs = Device.SwapchainFramebuffer.OutputDescription;
-
-            for (int i = 0; i < 16; i++)
-                perFrameFencePool.Enqueue(Factory.CreateFence(false));
         }
 
         private Vector2 currentSize;
@@ -296,7 +293,8 @@ protected internal override void FinishFrame()
 
             // This is returned via the end-of-lifetime tracking in `pendingFrameFences`.
             // See `updateLastCompletedFrameIndex`.
-            Fence fence = perFrameFencePool.Dequeue();
+            if (!perFrameFencePool.TryDequeue(out Fence? fence))
+                fence = Factory.CreateFence(false);
 
             Commands.End();
             Device.SubmitCommands(Commands, fence);

perFrameFencePool.Enqueue(Factory.CreateFence(false));
}

private Vector2 currentSize;
Expand Down Expand Up @@ -261,7 +269,9 @@ private void updateLastCompletedFrameIndex()
}

lastSignalledFenceIndex ??= i;
fence.Fence.Dispose();

Device.ResetFence(fence.Fence);
perFrameFencePool.Enqueue(fence.Fence);
}

if (lastSignalledFenceIndex != null)
Expand All @@ -284,8 +294,9 @@ protected internal override void FinishFrame()
BufferUpdateCommands.End();
Device.SubmitCommands(BufferUpdateCommands);

// This is disposed via the end-of-lifetime tracking in pendingFrameFences.
var fence = Factory.CreateFence(false);
// This is returned via the end-of-lifetime tracking in `pendingFrameFences`.
// See `updateLastCompletedFrameIndex`.
Fence fence = perFrameFencePool.Dequeue();

Commands.End();
Device.SubmitCommands(Commands, fence);
Expand Down