From edfed9359bec5280fc247a47691820f1aa9137ef Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Aug 2022 16:11:25 +0200 Subject: [PATCH] Add tests to update 1D resource textures, bug fixes --- ...ManagerImpl.ID2D1ResourceTextureManager.cs | 4 +- .../D2D1ResourceTextureManagerTests.cs | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs index e48e170e..8fb721b3 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs @@ -377,7 +377,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl // Either both extents are available, or none is, and the other values must not be null if (minimumExtents is null && maximimumExtents is not null || minimumExtents is not null && maximimumExtents is null || - strides is null || + (dimensions > 1 && strides is null) || data is null) { return E.E_POINTER; @@ -506,7 +506,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl } } - return E.E_NOTIMPL; + return S.S_OK; } /// diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 9343d4ae..80dcb69e 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -2,6 +2,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using System.Security.Cryptography; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Helpers; using ComputeSharp.Tests.Helpers; @@ -363,4 +364,85 @@ public partial class D2D1ResourceTextureManagerTests return this.source[x, y, z]; } } + + [TestMethod] + [DataRow(128, 0, 26)] + [DataRow(128, 0, 67)] + [DataRow(128, 0, 128)] + [DataRow(128, 32, 64)] + [DataRow(1024, 311, 489)] + [DataRow(1024, 907, 117)] + public unsafe void UpdateResourceTexture1D_RCW(int width, int startOffset, int updateLength) + { + 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 d2D1Effect = default; + + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); + + CopyFromResourceTexture1DShader shader = new(width); + + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + + byte[] texture = new byte[width]; + + D2D1ResourceTextureManager resourceTextureManager = new( + extents: stackalloc[] { (uint)width }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.One, + filter: D2D1Filter.MinMagMipPoint, + extendModes: stackalloc[] { D2D1ExtendMode.Clamp }, + data: texture, + strides: null); + + byte[] data = RandomNumberGenerator.GetBytes(updateLength); + + resourceTextureManager.Update( + minimumExtents: stackalloc uint[] { (uint)startOffset }, + maximimumExtents: stackalloc uint[] { (uint)(startOffset + updateLength) }, + strides: ReadOnlySpan.Empty, + data: data); + + data.CopyTo(texture.AsSpan(startOffset)); + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, 0); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, 1); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + + using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); + + byte[] resultingBytes = new byte[width]; + int i = 0; + + foreach (Bgra32 pixel in new ReadOnlySpan(d2D1MappedRect.bits, width)) + { + resultingBytes[i++] = pixel.B; + } + + Assert.IsTrue(texture.AsSpan().SequenceEqual(resultingBytes)); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + [AutoConstructor] + private partial struct CopyFromResourceTexture1DShader : ID2D1PixelShader + { + private int width; + + [D2DResourceTextureIndex(0)] + private D2D1ResourceTexture1D source; + + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + + return this.source[(int)((uint)xy.X % (uint)this.width)]; + } + } } \ No newline at end of file