From e44c2a661a2505b44aa0635c10562f0681687f05 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 11 Sep 2024 02:37:44 +0900 Subject: [PATCH] Remove closure allocations in VeldridStagingTexturePool --- .../Rendering/Deferred/Allocation/DeviceBufferPool.cs | 2 +- .../Graphics/Veldrid/VeldridStagingResourcePool.cs | 7 +++++-- .../Graphics/Veldrid/VeldridStagingTexturePool.cs | 7 ++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/osu.Framework/Graphics/Rendering/Deferred/Allocation/DeviceBufferPool.cs b/osu.Framework/Graphics/Rendering/Deferred/Allocation/DeviceBufferPool.cs index 36ec247584..0ee1403d8c 100644 --- a/osu.Framework/Graphics/Rendering/Deferred/Allocation/DeviceBufferPool.cs +++ b/osu.Framework/Graphics/Rendering/Deferred/Allocation/DeviceBufferPool.cs @@ -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); diff --git a/osu.Framework/Graphics/Veldrid/VeldridStagingResourcePool.cs b/osu.Framework/Graphics/Veldrid/VeldridStagingResourcePool.cs index f36486a10a..afe09382b1 100644 --- a/osu.Framework/Graphics/Veldrid/VeldridStagingResourcePool.cs +++ b/osu.Framework/Graphics/Veldrid/VeldridStagingResourcePool.cs @@ -32,7 +32,10 @@ protected VeldridStagingResourcePool(GraphicsPipeline pipeline, string name) pipeline.ExecutionFinished += executionFinished; } - protected bool TryGet(Predicate match, [NotNullWhen(true)] out T? resource) + protected bool TryGet([NotNullWhen(true)] out T? resource) + => TryGet(static (_, _) => true, null, out resource); + + protected bool TryGet(Func 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 @@ -41,7 +44,7 @@ protected bool TryGet(Predicate match, [NotNullWhen(true)] out T? resource) { var existing = available[i]; - if (match(existing.Resource)) + if (match(existing.Resource, state)) { existing.FrameUsageIndex = currentExecutionIndex; diff --git a/osu.Framework/Graphics/Veldrid/VeldridStagingTexturePool.cs b/osu.Framework/Graphics/Veldrid/VeldridStagingTexturePool.cs index 889e16ba73..90cbd28f59 100644 --- a/osu.Framework/Graphics/Veldrid/VeldridStagingTexturePool.cs +++ b/osu.Framework/Graphics/Veldrid/VeldridStagingTexturePool.cs @@ -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); } }