diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs index f9fc316e..4396bd1d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs @@ -170,6 +170,8 @@ partial struct D2D1ResourceTextureManagerImpl return E.E_NOT_VALID_STATE; } + using ComPtr d2D1ResourceTexture = default; + // Create the resource now, as it hasn't been created yet int result = @this->d2D1EffectContext->CreateResourceTexture( resourceId: @this->resourceId, @@ -177,12 +179,19 @@ partial struct D2D1ResourceTextureManagerImpl data: @this->data, strides: @this->strides, dataSize: @this->dataSize, - resourceTexture: resourceTexture); + resourceTexture: d2D1ResourceTexture.GetAddressOf()); // If creation was successful, release the buffered data. Going forwards, // the resource texture will be used directly for all updates requested. if (result == S.S_OK) { + // Store the resource texture for later + d2D1ResourceTexture.CopyTo(&@this->d2D1ResourceTexture); + + // Also return it to callers + d2D1ResourceTexture.CopyTo(resourceTexture); + + // Free the staging buffers NativeMemory.Free(@this->resourceTextureProperties.extents); NativeMemory.Free(@this->resourceTextureProperties.extendModes); NativeMemory.Free(@this->data); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index a10c9b3f..f575d791 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -234,7 +234,6 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl // Enter the lock, and then free the effect context. That is guaranteed to // not be null here, as it is only ever set if ID2D1Multithread is retrieved. this.d2D1Multithread->Enter(); - this.d2D1EffectContext->Release(); // Release the resource too if it has been created if (this.d2D1ResourceTexture is not null) @@ -242,7 +241,14 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl this.d2D1ResourceTexture->Release(); } + // Now that the resource, if any, has been released, the effect context can also + // be released. This must be done only after any associated resource textures have + // been released. Releasing an effect context first, in case it was the last reference + // to it, will otherwise cause the Release() call on the resource texture to explode. + this.d2D1EffectContext->Release(); + this.d2D1Multithread->Leave(); + this.d2D1Multithread->Release(); } if (this.resourceId is not null) diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper0.png b/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper0.png new file mode 100644 index 00000000..8ca378a9 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper0.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper1.png b/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper1.png new file mode 100644 index 00000000..fac9c0c9 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/Wallpaper1.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 2b8891a6..dbe90541 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -38,16 +38,10 @@ PreserveNewest - + PreserveNewest - - PreserveNewest - - - PreserveNewest - - + PreserveNewest diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 3fdd0472..7d103d0d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -214,7 +214,9 @@ public partial class D2D1ResourceTextureManagerTests } [TestMethod] - public unsafe void LoadPixelsFromResourceTexture2D_CreateBeforeGettingEffectContext() + [DataRow(1)] + [DataRow(2)] + public unsafe void LoadPixelsFromResourceTexture2D_CreateBeforeGettingEffectContext(int drawCount) { using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); @@ -242,6 +244,8 @@ public partial class D2D1ResourceTextureManagerTests Assert.Inconclusive(); } + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height); + D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager.Get(), resourceId: Guid.NewGuid(), @@ -253,15 +257,20 @@ public partial class D2D1ResourceTextureManagerTests data: MemoryMarshal.AsBytes(pixels.Span), strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width) }); - D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); + for (int i = 0; i < drawCount; i++) + { + // We need to reassign the resource texture manager before each draw, so the D2D runtime can see + // some effect property has changed, and it will call PrepareForRender. That's where the resource + // texture will be retrieved. When drawCount is more than 1, this test is used to validate that + // getting the resource texture multiple times from the same manager also works correctly. + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); - using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height); - - D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + } using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); - string destinationPath = Path.Combine(assemblyPath, "temp", "IndexedFromResourceTexture2D_Before.png"); + string destinationPath = Path.Combine(assemblyPath, "temp", $"IndexedFromResourceTexture2D_Before_Draw{drawCount}.png"); _ = Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); @@ -328,6 +337,98 @@ public partial class D2D1ResourceTextureManagerTests TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); } + [TestMethod] + public unsafe void LoadPixelsFromResourceTexture2D_AlternatingResourceTextureManagers() + { + 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 _); + + string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; + string wallpaper0Path = Path.Combine(assemblyPath, "Assets", "Wallpaper0.png"); + string wallpaper1Path = Path.Combine(assemblyPath, "Assets", "Wallpaper1.png"); + + using Image expected0 = Image.Load(wallpaper0Path); + using Image expected1 = Image.Load(wallpaper1Path); + + Memory pixels0 = Memory.Empty; + Memory pixels1 = Memory.Empty; + + if (!expected0.DangerousTryGetSinglePixelMemory(out pixels0) || + !expected1.DangerousTryGetSinglePixelMemory(out pixels1)) + { + Assert.Inconclusive(); + } + + using ComPtr resourceTextureManager0 = default; + using ComPtr resourceTextureManager1 = default; + + D2D1ResourceTextureManager.Create((void**)resourceTextureManager0.GetAddressOf()); + D2D1ResourceTextureManager.Create((void**)resourceTextureManager1.GetAddressOf()); + + D2D1ResourceTextureManager.Initialize( + resourceTextureManager: resourceTextureManager0.Get(), + resourceId: Guid.NewGuid(), + extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.Four, + filter: D2D1Filter.MinMagMipPoint, + extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + data: MemoryMarshal.AsBytes(pixels0.Span), + strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) }); + + D2D1ResourceTextureManager.Initialize( + resourceTextureManager: resourceTextureManager1.Get(), + resourceId: Guid.NewGuid(), + extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.Four, + filter: D2D1Filter.MinMagMipPoint, + extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + data: MemoryMarshal.AsBytes(pixels1.Span), + strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) }); + + using (ComPtr d2D1Effect = default) + { + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected0.Width, (uint)expected0.Height); + + for (int i = 0; i < 10; i++) + { + ref readonly ComPtr resourceTextureManager = ref i % 2 == 0 + ? ref resourceTextureManager0 + : ref resourceTextureManager1; + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + + using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); + + string destinationPath = Path.Combine(assemblyPath, "temp", $"LoadPixelsFromResourceTexture2D_Alternating{i}.png"); + + _ = Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); + + ImageHelper.SaveBitmapToFile(destinationPath, (uint)expected0.Width, (uint)expected0.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits); + + string expectedPath = i % 2 == 0 ? wallpaper0Path : wallpaper1Path; + + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + } + } + + _ = resourceTextureManager0.Get()->AddRef(); + + Assert.AreEqual(1u, resourceTextureManager0.Get()->Release()); + + _ = resourceTextureManager1.Get()->AddRef(); + + Assert.AreEqual(1u, resourceTextureManager1.Get()->Release()); + } + [D2DInputCount(0)] [D2DRequiresScenePosition] private partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader