Skip to content

Commit

Permalink
Crop and disable mipmaps on beatmap panel backgrounds
Browse files Browse the repository at this point in the history
This is an effort to improve general performance at song select. At
least on the metal renderer, I can notice very high draw frame overheads
related to texture uploads.

By reducing the size of the texture uploads to roughly match what is
actually being displayed on screen (using a relatively inexpensive crop
operation), we can bastly reduce stuttering both during initial load and
carousel scroll.

You might ask if it's safe to disable mipmapping, but I've tested with
lower resolutions and bilinear filtering seems to handle just fine.
Bilinear without mipmaps only falls apart when you scale below 50% and
we're not going too far past that at minimum game scale, if at all.
  • Loading branch information
peppy committed Jun 8, 2023
1 parent a842f79 commit 3978d4b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
88 changes: 88 additions & 0 deletions osu.Game/Beatmaps/BeatmapPanelBackgroundTextureLoaderStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace osu.Game.Beatmaps
{
// Implementation of this class is based off of `MaxDimensionLimitedTextureLoaderStore`.
// If issues are found it's worth checking to make sure similar issues exist there.
public class BeatmapPanelBackgroundTextureLoaderStore : IResourceStore<TextureUpload>
{
// These numbers are taken from the draw visualiser size requirements for song select panel textures at extreme aspect ratios.
private const int height = 130;
private const int max_width = 1280;

private readonly IResourceStore<TextureUpload>? textureStore;

public BeatmapPanelBackgroundTextureLoaderStore(IResourceStore<TextureUpload>? textureStore)
{
this.textureStore = textureStore;
}

public void Dispose()
{
textureStore?.Dispose();
}

public TextureUpload Get(string name)
{
var textureUpload = textureStore?.Get(name);

// NRT not enabled on framework side classes (IResourceStore / TextureLoaderStore), welp.
if (textureUpload == null)
return null!;

return limitTextureUploadSize(textureUpload);
}

public async Task<TextureUpload> GetAsync(string name, CancellationToken cancellationToken = new CancellationToken())
{
// NRT not enabled on framework side classes (IResourceStore / TextureLoaderStore), welp.
if (textureStore == null)
return null!;

var textureUpload = await textureStore.GetAsync(name, cancellationToken).ConfigureAwait(false);

if (textureUpload == null)
return null!;

return await Task.Run(() => limitTextureUploadSize(textureUpload), cancellationToken).ConfigureAwait(false);
}

private TextureUpload limitTextureUploadSize(TextureUpload textureUpload)
{
var image = Image.LoadPixelData(textureUpload.Data.ToArray(), textureUpload.Width, textureUpload.Height);

// The original texture upload will no longer be returned or used.
textureUpload.Dispose();

Size size = image.Size();
int usableWidth = Math.Min(max_width, size.Width);

// Crop the centre region of the background for now.
Rectangle cropRectangle = new Rectangle(
(size.Width - usableWidth) / 2,
size.Height / 2 - height / 2,
usableWidth,
height
);

image.Mutate(i => i.Crop(cropRectangle));

return new TextureUpload(image);
}

public Stream? GetStream(string name) => textureStore?.GetStream(name);

public IEnumerable<string> GetAvailableResources() => textureStore?.GetAvailableResources() ?? Array.Empty<string>();
}
}
5 changes: 5 additions & 0 deletions osu.Game/Beatmaps/IBeatmapResourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public interface IBeatmapResourceProvider : IStorageResourceProvider
/// </summary>
TextureStore LargeTextureStore { get; }

/// <summary>
/// Retrieve a global large texture store, used specifically for retrieving cropped beatmap panel backgrounds.
/// </summary>
TextureStore BeatmapPanelTextureStore { get; }

/// <summary>
/// Access a global track store for retrieving beatmap tracks from.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions osu.Game/Beatmaps/IWorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public interface IWorkingBeatmap
/// </summary>
Texture GetBackground();

/// <summary>
/// Retrieves a cropped background for this <see cref="IWorkingBeatmap"/> used for display on panels.
/// </summary>
Texture GetPanelBackground();

/// <summary>
/// Retrieves the <see cref="Waveform"/> for the <see cref="Track"/> of this <see cref="IWorkingBeatmap"/>.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions osu.Game/Beatmaps/WorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected WorkingBeatmap(BeatmapInfo beatmapInfo, AudioManager audioManager)

protected abstract IBeatmap GetBeatmap();
public abstract Texture GetBackground();
public virtual Texture GetPanelBackground() => GetBackground();
protected abstract Track GetBeatmapTrack();

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions osu.Game/Beatmaps/WorkingBeatmapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class WorkingBeatmapCache : IBeatmapResourceProvider, IWorkingBeatmapCach
private readonly AudioManager audioManager;
private readonly IResourceStore<byte[]> resources;
private readonly LargeTextureStore largeTextureStore;
private readonly LargeTextureStore beatmapPanelTextureStore;
private readonly ITrackStore trackStore;
private readonly IResourceStore<byte[]> files;

Expand All @@ -58,6 +59,7 @@ public WorkingBeatmapCache(ITrackStore trackStore, AudioManager audioManager, IR
this.host = host;
this.files = files;
largeTextureStore = new LargeTextureStore(host?.Renderer ?? new DummyRenderer(), host?.CreateTextureLoaderStore(files));
beatmapPanelTextureStore = new LargeTextureStore(host?.Renderer ?? new DummyRenderer(), new BeatmapPanelBackgroundTextureLoaderStore(host?.CreateTextureLoaderStore(files)));
this.trackStore = trackStore;
}

Expand Down Expand Up @@ -110,6 +112,7 @@ public virtual WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo)
#region IResourceStorageProvider

TextureStore IBeatmapResourceProvider.LargeTextureStore => largeTextureStore;
TextureStore IBeatmapResourceProvider.BeatmapPanelTextureStore => beatmapPanelTextureStore;
ITrackStore IBeatmapResourceProvider.Tracks => trackStore;
IRenderer IStorageResourceProvider.Renderer => host?.Renderer ?? new DummyRenderer();
AudioManager IStorageResourceProvider.AudioManager => audioManager;
Expand Down Expand Up @@ -160,15 +163,19 @@ protected override IBeatmap GetBeatmap()
}
}

public override Texture GetBackground()
public override Texture GetPanelBackground() => getBackgroundFromStore(resources.BeatmapPanelTextureStore);

public override Texture GetBackground() => getBackgroundFromStore(resources.LargeTextureStore);

private Texture getBackgroundFromStore(TextureStore store)
{
if (string.IsNullOrEmpty(Metadata?.BackgroundFile))
return null;

try
{
string fileStorePath = BeatmapSetInfo.GetPathForFile(Metadata.BackgroundFile);
var texture = resources.LargeTextureStore.Get(fileStorePath);
var texture = store.Get(fileStorePath);

if (texture == null)
{
Expand Down
24 changes: 22 additions & 2 deletions osu.Game/Screens/Select/Carousel/SetPanelBackground.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osuTK;
using osuTK.Graphics;

Expand All @@ -21,7 +23,7 @@ public SetPanelBackground(IWorkingBeatmap working)

Children = new Drawable[]
{
new BeatmapBackgroundSprite(working)
new PanelBeatmapBackground(working)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Expand Down Expand Up @@ -68,5 +70,23 @@ public SetPanelBackground(IWorkingBeatmap working)
},
};
}

public partial class PanelBeatmapBackground : Sprite
{
private readonly IWorkingBeatmap working;

public PanelBeatmapBackground(IWorkingBeatmap working)
{
ArgumentNullException.ThrowIfNull(working);

this.working = working;
}

[BackgroundDependencyLoader]
private void load()
{
Texture = working.GetPanelBackground();
}
}
}
}

0 comments on commit 3978d4b

Please sign in to comment.