diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/WallpapersStack.png b/tests/ComputeSharp.D2D1.Tests/Assets/WallpapersStack.png new file mode 100644 index 00000000..c13f05e6 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/WallpapersStack.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index a650719c..2b8891a6 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -38,6 +38,21 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 4fd0f38a..e08dea25 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -66,13 +66,13 @@ public partial class D2D1ResourceTextureManagerTests using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); using ComPtr d2D1Effect = default; - D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(SampleShader), d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(IndexFrom2DResourceTextureShader), d2D1Effect.Get()); using ComPtr resourceTextureManager = default; @@ -115,7 +115,7 @@ public partial class D2D1ResourceTextureManagerTests [D2DInputCount(0)] [D2DRequiresScenePosition] - private partial struct SampleShader : ID2D1PixelShader + private partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader { [D2DResourceTextureIndex(0)] private D2D1ResourceTexture2D source; @@ -124,7 +124,85 @@ public partial class D2D1ResourceTextureManagerTests { int2 xy = (int2)D2D.GetScenePosition().XY; - return source[xy]; + return this.source[xy]; + } + } + + [TestMethod] + public unsafe void LoadPixelsFromResourceTexture3D() + { + 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()); + + string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; + string expectedPath = Path.Combine(assemblyPath, "Assets", "WallpapersStack.png"); + string destinationPath = Path.Combine(assemblyPath, "temp", "IndexedFromResourceTexture3D.png"); + + using Image expected = Image.Load(expectedPath); + + if (!expected.DangerousTryGetSinglePixelMemory(out Memory pixels)) + { + Assert.Inconclusive(); + } + + IndexFrom3DResourceTextureShader shader = new(expected.Height / 4); + + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + + using ComPtr resourceTextureManager = default; + + D2D1ResourceTextureManager.Create((void**)resourceTextureManager.GetAddressOf()); + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0); + + D2D1ResourceTextureManager.Initialize( + resourceTextureManager: resourceTextureManager.Get(), + resourceId: Guid.NewGuid(), + extents: stackalloc[] { (uint)expected.Width, (uint)(expected.Height / 4), 4u }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.Four, + filter: D2D1Filter.MinMagMipPoint, + extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + data: MemoryMarshal.AsBytes(pixels.Span), + strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width), (uint)(sizeof(Rgba32) * expected.Width * (expected.Height / 4)) }); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + + using ComPtr d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect); + + WICHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits); + + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + [AutoConstructor] + private partial struct IndexFrom3DResourceTextureShader : ID2D1PixelShader + { + private int height; + + [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)height); + int z = (int)((uint)xy.Y / (uint)height); + + return this.source[x, y, z]; } } } \ No newline at end of file