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

Work around D3D11 texture pointer getting lost before disposal #54

Merged
merged 1 commit into from
May 3, 2024

Conversation

smoogipoo
Copy link

The easy-difficulty solution from #53

osu!framework test:

// 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 NUnit.Framework;
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 osuTK;
using osuTK.Graphics;

namespace osu.Framework.Tests.Visual.Graphics
{
    /// <summary>
    /// Continuously allocates graphics objects and leaves it up to the finalizer to clean them up.
    /// </summary>
    public partial class TestSceneResourceFinalisationStressTest : FrameworkTestScene
    {
        private Drawable? lastConsumer;
        private Func<Drawable>? createConsumer;
        private int counter;

        [Test]
        public void TestTextureConsumer()
        {
            AddStep("enable texture consumer", () => createConsumer = () => new TextureConsumer());
        }

        protected override void Update()
        {
            base.Update();

            if (lastConsumer != null)
            {
                // It's important to not dispose and leave it up to the finalizer.
                Remove(lastConsumer, false);

                if (++counter % 100 == 0)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }

            if (createConsumer != null)
                Add(lastConsumer = createConsumer());
        }

        private partial class TextureConsumer : CompositeDrawable
        {
            private Sprite sprite = null!;

            public TextureConsumer()
            {
                Size = new Vector2(100);
                Anchor = Anchor.Centre;
                Origin = Anchor.Centre;
            }

            [BackgroundDependencyLoader]
            private void load(IRenderer renderer)
            {
                InternalChild = sprite = new Sprite
                {
                    RelativeSizeAxes = Axes.Both,
                    Texture = renderer.CreateTexture(512, 512, true, initialisationColour: Color4.Red),
                };

                sprite.Texture.SetData(new TextureUpload());
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant