diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png new file mode 100644 index 00000000..3ee0e09d Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png differ diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png new file mode 100644 index 00000000..32c63715 Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png differ diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 048893d4..9ba08e37 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -957,6 +957,94 @@ public partial class ComputeContextTests } } + [CombinatorialTestMethod] + [AllDevices] + public void TransitionAndWrites_ReadWriteTexture2D(Device device) + { + _ = device.Get(); + + string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + + using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024SamplingAndDashing.png")); + + using ReadWriteTexture2D source = device.Get().LoadReadWriteTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); + + using (var context = device.Get().CreateComputeContext()) + { + context.For(source.Width, source.Height, new Dotted2DPixelShader(source)); + context.Transition(source, ResourceState.ReadOnly); + context.ForEach(destination, new LinearSampling2DPixelShader(source.AsReadOnly())); + context.Transition(source, ResourceState.ReadWrite); + context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); + } + + using var processed = destination.ToImage(); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); + + Rgba32[,] clear = source.ToArray(); + + foreach (Rgba32 value in clear) + { + Assert.AreEqual(value, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void TransitionAndWrites_ReadWriteTexture3D(Device device) + { + _ = device.Get(); + + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + string originalPath = Path.Combine(assetsPath, "CityWith1920x1280Resizing.png"); + string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024SamplingFrom1920x1080AndDashing.png"); + + var imageInfo = Image.Identify(originalPath); + + using var sampled = Image.Load(Path.Combine(assetsPath, sampledPath)); + + using ReadWriteTexture3D source = device.Get().AllocateReadWriteTexture3D(imageInfo.Width, imageInfo.Height, 3); + + using (UploadTexture2D upload2D = device.Get().LoadUploadTexture2D(originalPath)) + using (UploadTexture3D upload3D = device.Get().AllocateUploadTexture3D(upload2D.Width, upload2D.Height, 2)) + { + for (int z = 0; z < upload3D.Depth; z++) + { + upload2D.View.CopyTo(upload3D.View.GetDepthView(z)); + } + + upload3D.CopyTo(source); + } + + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(sampled.Width, sampled.Height, 2); + + using (var context = device.Get().CreateComputeContext()) + { + context.For(source.Width, source.Height, source.Depth, new Dotted3DPixelShader(source)); + context.Transition(source, ResourceState.ReadOnly); + context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(source.AsReadOnly(), destination)); + context.Transition(source, ResourceState.ReadWrite); + context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); + } + + for (int z = 0; z < destination.Depth; z++) + { + using var processed = destination.ToImage(depth: z); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + } + + Rgba32[,,] clear = source.ToArray(); + + foreach (Rgba32 value in clear) + { + Assert.AreEqual(value, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); + } + } + [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] @@ -1136,4 +1224,60 @@ public partial class ComputeContextTests destination[ThreadIds.XYZ] = source[ThreadIds.Normalized.XYZ]; } } + + [AutoConstructor] + internal readonly partial struct Dotted2DPixelShader : IComputeShader + { + public readonly IReadWriteTexture2D source; + + /// + public void Execute() + { + bool isHorizontal192CellEven = (ThreadIds.X / 192) % 2 == 0; + bool isVertical128CellEven = (ThreadIds.Y / 128) % 2 == 0; + + if (isHorizontal192CellEven != isVertical128CellEven) + { + if ((ThreadIds.Normalized.X <= 0.5f && + ThreadIds.Normalized.Y <= 0.5) || + (ThreadIds.Normalized.X > 0.5f && + ThreadIds.Normalized.Y > 0.5)) + { + source[ThreadIds.XY] = float4.UnitW; + } + else + { + source[ThreadIds.XY] = float4.Zero; + } + } + } + } + + [AutoConstructor] + internal readonly partial struct Dotted3DPixelShader : IComputeShader + { + public readonly IReadWriteTexture3D source; + + /// + public void Execute() + { + bool isHorizontal192CellEven = (ThreadIds.X / 192) % 2 == 0; + bool isVertical128CellEven = (ThreadIds.Y / 128) % 2 == 0; + + if (isHorizontal192CellEven != isVertical128CellEven) + { + if ((ThreadIds.Normalized.X <= 0.5f && + ThreadIds.Normalized.Y <= 0.5) || + (ThreadIds.Normalized.X > 0.5f && + ThreadIds.Normalized.Y > 0.5)) + { + source[ThreadIds.XYZ] = float4.UnitW; + } + else + { + source[ThreadIds.XYZ] = float4.Zero; + } + } + } + } } diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index c73f109d..ec714ef8 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -51,6 +51,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + PreserveNewest