From 7e9a9c7f01b7caae9eac584d4e3a2336bbb69577 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 19 Aug 2022 14:31:10 +0200 Subject: [PATCH] Add unit test for resource texture manager reference counting --- .../D2D1ResourceTextureManagerTests.cs | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 780f7541..3fdd0472 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -98,6 +98,64 @@ public partial class D2D1ResourceTextureManagerTests Assert.IsTrue(garbage.Get() is null); } + [TestMethod] + public unsafe void VerifyReferenceCounting() + { + using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); + using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); + using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); + + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + + using ComPtr resourceTextureManager = default; + + D2D1ResourceTextureManager.Create((void**)resourceTextureManager.GetAddressOf()); + + _ = resourceTextureManager.Get()->AddRef(); + + // Right after creation, the manager has only 1 ref count + Assert.AreEqual(1u, resourceTextureManager.Get()->Release()); + + using (ComPtr d2D1Effect = default) + { + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); + + _ = resourceTextureManager.Get()->AddRef(); + + // After setting a manager to an effect, the manager also has a reference + Assert.AreEqual(2u, resourceTextureManager.Get()->Release()); + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); + + _ = resourceTextureManager.Get()->AddRef(); + + // Adding the same manager again doesn't increment ref count + Assert.AreEqual(2u, resourceTextureManager.Get()->Release()); + } + + _ = resourceTextureManager.Get()->AddRef(); + + // If the effect is released, that extra manager reference is also released + Assert.AreEqual(1u, resourceTextureManager.Get()->Release()); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + private partial struct DummyShaderWithResourceTexture : ID2D1PixelShader + { + [D2DResourceTextureIndex(0)] + private D2D1ResourceTexture2D source; + + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + + return this.source[xy]; + } + } + [TestMethod] public unsafe void LoadPixelsFromResourceTexture2D_CreateAfterGettingEffectContext() { @@ -399,7 +457,7 @@ public partial class D2D1ResourceTextureManagerTests extendModes: stackalloc[] { D2D1ExtendMode.Clamp }, data: texture, strides: null); - + byte[] data = RandomNumberGenerator.GetBytes(updateLength); resourceTextureManager.Update( @@ -409,7 +467,7 @@ public partial class D2D1ResourceTextureManagerTests data: data); data.CopyTo(texture.AsSpan(startOffset)); - + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, 0); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, 1);