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

Allow enabling computed mipmaps in LargeTextureStore #6048

Merged
merged 8 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 97 additions & 0 deletions osu.Framework.Tests/Visual/Sprites/TestSceneLargeTextureStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osuTK;

namespace osu.Framework.Tests.Visual.Sprites
{
public partial class TestSceneLargeTextureStore : FrameworkTestScene
{
private DependencyContainer dependencies = null!;

[BackgroundDependencyLoader]
private void load(IRenderer renderer, GameHost host, Game game)
{
dependencies.CacheAs(new LargeTextureStore(renderer, host.CreateTextureLoaderStore(new NamespacedResourceStore<byte[]>(game.Resources, "Textures")), manualMipmaps: false));
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Setting manualMipamaps: true doesn't change anything in the test.

I'd hope the test shows the difference between the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting manualMipamaps: true doesn't change anything in the test.

That's weird, for me the difference is very clear (I'm on D3D)

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

May have been hot reload failing. But I'd also hope the test can be run in both modes. I don't want to have to change code each time I want to check a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. Was just trying to ensure it works for you and not a renderer issue or something.


Child = new GridContainer
{
RelativeSizeAxes = Axes.Both,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Relative, 0.5f),
new Dimension(GridSizeMode.Relative, 0.5f),
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.Relative, 0.5f),
new Dimension(GridSizeMode.Relative, 0.5f),
},
Content = new[]
{
new Drawable[]
{
new MipmapSprite(0),
new MipmapSprite(1)
},
new Drawable[]
{
new MipmapSprite(2),
new MipmapSprite(3)
}
}
};
}

protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
=> dependencies = new DependencyContainer(base.CreateChildDependencies(parent));

private partial class MipmapSprite : Sprite
{
private readonly int level;

public MipmapSprite(int level)
{
this.level = level;
}

[BackgroundDependencyLoader]
private void load(LargeTextureStore textures)
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
Texture = textures.Get("sample-texture.png");
Scale = new Vector2(0.5f);
}

protected override DrawNode CreateDrawNode() => new SingleMipmapSpriteDrawNode(this, level);

private class SingleMipmapSpriteDrawNode : SpriteDrawNode
{
private readonly int level;

public SingleMipmapSpriteDrawNode(Sprite source, int level)
: base(source)
{
this.level = level;
}

protected override void Blit(IRenderer renderer)
{
Texture.MipLevel = level;
base.Blit(renderer);
renderer.FlushCurrentBatch(null);
Texture.MipLevel = null;
}
}
}
}
}
8 changes: 6 additions & 2 deletions osu.Framework/Graphics/Textures/LargeTextureStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ namespace osu.Framework.Graphics.Textures
/// <summary>
/// A texture store that bypasses atlasing and removes textures from memory after dereferenced by all consumers.
/// </summary>
/// <remarks>
/// It's recommended to use manual mipmaps since large textures are generally rendered at full resolution
/// and computing mipmaps automatically will be unnecessary overhead.
/// </remarks>
public class LargeTextureStore : TextureStore
{
private readonly object referenceCountLock = new object();
private readonly Dictionary<string, TextureWithRefCount.ReferenceCount> referenceCounts = new Dictionary<string, TextureWithRefCount.ReferenceCount>();

public LargeTextureStore(IRenderer renderer, IResourceStore<TextureUpload> store = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear)
: base(renderer, store, false, filteringMode, true)
public LargeTextureStore(IRenderer renderer, IResourceStore<TextureUpload> store = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear, bool manualMipmaps = true)
: base(renderer, store, false, filteringMode, manualMipmaps)
{
}

Expand Down