diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png new file mode 100644 index 00000000..9d50e30a Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png differ diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index c47cd66e..698c66fc 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -941,6 +941,52 @@ public partial class ComputeContextTests TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f); } + [CombinatorialTestMethod] + [AllDevices] + public void Transition_ReadWriteTexture2D_Normalized_WithWriteToo(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 sampled1 = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); + using var sampled2 = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024SamplingInverted.png")); + + using ReadOnlyTexture2D image = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D source = device.Get().AllocateReadWriteTexture2D(image.Width, image.Height); + using ReadWriteTexture2D destination1 = device.Get().AllocateReadWriteTexture2D(sampled1.Width, sampled1.Height); + using ReadWriteTexture2D destination2 = device.Get().AllocateReadWriteTexture2D(sampled1.Width, sampled1.Height); + + using (var context = device.Get().CreateComputeContext()) + { + // Dispatch before any transitions, and UAV barrier test + context.ForEach(source); + context.Barrier(source); + + // Dispatch following a UAV barrier + context.ForEach(source, new CopyToOutputPixelShader(image)); + + // Transition to readonly and first use as readonly resource + context.Transition(source, ResourceState.ReadOnly); + context.ForEach(destination1, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); + + // Transition back and write to it + context.Transition(source, ResourceState.ReadWrite); + context.ForEach(source, new InvertPixelShader(source)); + + // Transition once again to readonly and final use as readonly resource + context.Transition(source, ResourceState.ReadOnly); + context.ForEach(destination2, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); + } + + using var processed1 = destination1.ToImage(); + using var processed2 = destination2.ToImage(); + + TolerantImageComparer.AssertEqual(sampled1, processed1, 0.0000017f); + TolerantImageComparer.AssertEqual(sampled2, processed2, 0.000002f); + } + [CombinatorialTestMethod] [AllDevices] public void Transition_ReadWriteTexture3D(Device device) @@ -1317,6 +1363,30 @@ public partial class ComputeContextTests } } + [AutoConstructor] + internal readonly partial struct CopyToOutputPixelShader : IPixelShader + { + public readonly IReadOnlyNormalizedTexture2D source; + + /// + public float4 Execute() + { + return source[ThreadIds.XY]; + } + } + + [AutoConstructor] + internal readonly partial struct InvertPixelShader : IPixelShader + { + public readonly IReadWriteNormalizedTexture2D source; + + /// + public float4 Execute() + { + return new(1.0f - source[ThreadIds.XY].XYZ, source[ThreadIds.XY].W); + } + } + [AutoConstructor] internal readonly partial struct LinearSamplingFromNormalized3DComputeShader : IComputeShader { diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 62c29afa..c3bd5cec 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -55,6 +55,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest