Skip to content

Commit

Permalink
Merge pull request #6373 from smoogipoo/reduce-staging-pool-allocations
Browse files Browse the repository at this point in the history
Remove closure allocations in VeldridStagingTexturePool
  • Loading branch information
peppy authored Sep 10, 2024
2 parents 0b21f3f + e44c2a6 commit 0a65ba9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DeviceBufferPool(GraphicsPipeline pipeline, uint bufferSize, BufferUsage

public IPooledDeviceBuffer Get()
{
if (TryGet(_ => true, out IPooledDeviceBuffer? existing))
if (TryGet(out IPooledDeviceBuffer? existing))
return existing;

existing = new PooledDeviceBuffer(Pipeline, bufferSize, usage);
Expand Down
7 changes: 5 additions & 2 deletions osu.Framework/Graphics/Veldrid/VeldridStagingResourcePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ protected VeldridStagingResourcePool(GraphicsPipeline pipeline, string name)
pipeline.ExecutionFinished += executionFinished;
}

protected bool TryGet(Predicate<T> match, [NotNullWhen(true)] out T? resource)
protected bool TryGet([NotNullWhen(true)] out T? resource)
=> TryGet<object>(static (_, _) => true, null, out resource);

protected bool TryGet<TState>(Func<T, TState?, bool> match, TState? state, [NotNullWhen(true)] out T? resource)
{
// Reverse iteration is important to prefer reusing recently returned textures.
// This avoids the case of a large pool being constantly cycled and therefore never
Expand All @@ -41,7 +44,7 @@ protected bool TryGet(Predicate<T> match, [NotNullWhen(true)] out T? resource)
{
var existing = available[i];

if (match(existing.Resource))
if (match(existing.Resource, state))
{
existing.FrameUsageIndex = currentExecutionIndex;

Expand Down
7 changes: 6 additions & 1 deletion osu.Framework/Graphics/Veldrid/VeldridStagingTexturePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ public VeldridStagingTexturePool(GraphicsPipeline pipeline)

public Texture Get(int width, int height, PixelFormat format)
{
if (TryGet(t => t.Width >= width && t.Height >= height && t.Format == format, out var texture))
if (TryGet(match, new TextureLookup(width, height, format), out var texture))
return texture;

texture = Pipeline.Factory.CreateTexture(TextureDescription.Texture2D((uint)width, (uint)height, 1, 1, format, TextureUsage.Staging));
AddNewResource(texture);
return texture;
}

private static bool match(Texture texture, TextureLookup lookup)
=> texture.Width >= lookup.Width && texture.Height >= lookup.Height && texture.Format == lookup.Format;

private readonly record struct TextureLookup(int Width, int Height, PixelFormat Format);
}
}

0 comments on commit 0a65ba9

Please sign in to comment.