-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crop and disable mipmaps on beatmap panel backgrounds
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
Showing
6 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
osu.Game/Beatmaps/BeatmapPanelBackgroundTextureLoaderStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -21,7 +23,7 @@ public SetPanelBackground(IWorkingBeatmap working) | |
|
||
Children = new Drawable[] | ||
{ | ||
new BeatmapBackgroundSprite(working) | ||
new PanelBeatmapBackground(working) | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Anchor = Anchor.Centre, | ||
|
@@ -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(); | ||
} | ||
} | ||
} | ||
} |