From d33270ea2c977545fcdf2d3961192ad52512bb3b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Aug 2022 23:02:35 +0200 Subject: [PATCH] Add tests to update 3D resource textures, bug fixes --- ...ManagerImpl.ID2D1ResourceTextureManager.cs | 13 +- .../D2D1ResourceTextureManagerTests.cs | 123 +++++++++++++++--- 2 files changed, 109 insertions(+), 27 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 8fb721b3..89fc5479 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs @@ -599,19 +599,8 @@ unsafe partial struct D2D1ResourceTextureManagerImpl } else { - uint stridedExtent01SizeInBytes; - - try - { - stridedExtent01SizeInBytes = checked(extents[1] * strides[1]); - } - catch (OverflowException) - { - return E.E_INVALIDARG; - } - // Validate the 1th stride is not smaller than the byte size of a 2D slice, with padding - if (stridedExtent01SizeInBytes > strides[2]) + if (extent01SizeInBytes > strides[1]) { return E.E_INVALIDARG; } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index dfdd1d69..dcc5e6b2 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -442,15 +442,7 @@ public partial class D2D1ResourceTextureManagerTests { int2 xy = (int2)D2D.GetScenePosition().XY; - float4 result = default; - - if (xy.X < this.width && - xy.Y == 0) - { - result = this.source[xy.X]; - } - - return result; + return this.source[xy.X]; } } @@ -546,15 +538,116 @@ public partial class D2D1ResourceTextureManagerTests { int2 xy = (int2)D2D.GetScenePosition().XY; - float4 result = default; + return this.source[xy]; + } + } - if (xy.X < this.width && - xy.Y < this.height) + [TestMethod] + [DataRow(64, 64, 3, 0, 0, 0, 64, 64, 3)] + [DataRow(64, 64, 3, 10, 22, 1, 44, 33, 1)] + [DataRow(64, 64, 3, 10, 22, 1, 44, 33, 2)] + [DataRow(64, 64, 3, 10, 22, 0, 44, 33, 2)] + [DataRow(512, 512, 6, 245, 0, 2, 188, 1, 4)] + [DataRow(512, 512, 6, 245, 0, 2, 188, 467, 3)] + [DataRow(512, 512, 6, 245, 113, 1, 256, 322, 2)] + public unsafe void UpdateResourceTexture3D( + int width, + int height, + int depth, + int startOffsetX, + int startOffsetY, + int startOffsetZ, + int updateLengthX, + int updateLengthY, + int updateLengthZ) + { + 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()); + + CopyFromResourceTexture3DShader shader = new(width, height, depth); + + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + + byte[] texture = new byte[width * height * depth]; + + D2D1ResourceTextureManager resourceTextureManager = new( + extents: stackalloc[] { (uint)width, (uint)height, (uint)depth }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.One, + filter: D2D1Filter.MinMagMipPoint, + extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + data: texture, + strides: stackalloc uint[] { (uint)width, (uint)(width * height) }); + + byte[] data = RandomNumberGenerator.GetBytes(updateLengthX * updateLengthY * updateLengthZ); + + resourceTextureManager.Update( + minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY, (uint)startOffsetZ }, + maximimumExtents: stackalloc uint[] { (uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY), (uint)(startOffsetZ + updateLengthZ) }, + strides: stackalloc uint[] { (uint)updateLengthX, (uint)(updateLengthX * updateLengthY) }, + data: data); + + for (int z = 0; z < updateLengthZ; z++) + { + for (int y = 0; y < updateLengthY; y++) { - result = this.source[xy]; - } + ReadOnlySpan source = data.AsSpan(z * updateLengthX * updateLengthY + y * updateLengthX, updateLengthX); + Span destination = texture.AsSpan((startOffsetZ + z) * (width * height) + (startOffsetY + y) * width + startOffsetX, updateLengthX); - return result; + source.CopyTo(destination); + } + } + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, 0); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)(height * depth)); + + 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 * height * depth]; + int i = 0; + + for (int y = 0; y < height * depth; y++) + { + foreach (Bgra32 pixel in new ReadOnlySpan(d2D1MappedRect.bits + d2D1MappedRect.pitch * y, width)) + { + resultingBytes[i++] = pixel.B; + } + } + + Assert.IsTrue(texture.AsSpan().SequenceEqual(resultingBytes)); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + [AutoConstructor] + private partial struct CopyFromResourceTexture3DShader : ID2D1PixelShader + { + private int width; + private int height; + private int depth; + + [D2DResourceTextureIndex(0)] + private D2D1ResourceTexture3D source; + + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + + int x = xy.X; + int y = (int)((uint)xy.Y % (uint)this.height); + int z = (int)((uint)xy.Y / (uint)this.height); + + return this.source[x, y, z]; } } } \ No newline at end of file