diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 269deae1..d4fffa3b 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -898,13 +898,41 @@ public partial class ComputeContextTests using var sampled = Image.Load(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png")); + using ReadOnlyTexture2D source = device.Get().LoadReadOnlyTexture2D(Path.Combine(imagingPath, "city.jpg")); + using ReadWriteTexture2D sourceAsFloat4 = device.Get().AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(sampled.Width, sampled.Height); + + using (var context = device.Get().CreateComputeContext()) + { + context.For(source.Width, source.Height, new ConvertToNonNormalized2DShader(source, sourceAsFloat4)); + context.Transition(sourceAsFloat4, ResourceState.ReadOnly); + context.ForEach(destination, new LinearSampling2DPixelShader(sourceAsFloat4.AsReadOnly())); + context.Transition(sourceAsFloat4, ResourceState.ReadWrite); + } + + using var processed = destination.ToImage(); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000174f); + } + + [CombinatorialTestMethod] + [AllDevices] + public void Transition_ReadWriteTexture2D_Normalized(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, "CityAfter1024x1024Sampling.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.Transition(source, ResourceState.ReadOnly); - context.ForEach(destination, new LinearSampling2DPixelShader(source.AsReadOnly())); + context.ForEach(destination, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); context.Transition(source, ResourceState.ReadWrite); } @@ -927,6 +955,58 @@ public partial class ComputeContextTests using var sampled = Image.Load(Path.Combine(assetsPath, sampledPath)); + using ReadWriteTexture3D sourceAsFloat4 = 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++) + { + TextureView2D layer = upload3D.View.GetDepthView(z); + + for (int y = 0; y < layer.Height; y++) + { + for (int x = 0; x < layer.Width; x++) + { + layer[x, y] = upload2D.View[x, y].ToPixel(); + } + } + } + + upload3D.CopyTo(sourceAsFloat4); + } + + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(sampled.Width, sampled.Height, 2); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(sourceAsFloat4, ResourceState.ReadOnly); + context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(sourceAsFloat4.AsReadOnly(), destination)); + context.Transition(sourceAsFloat4, ResourceState.ReadWrite); + } + + for (int z = 0; z < destination.Depth; z++) + { + using var processed = destination.ToImage(depth: z); + + ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f); + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void Transition_ReadWriteTexture3D_Normalized(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, "CityAfter1024x1024Sampling.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)) @@ -945,7 +1025,7 @@ public partial class ComputeContextTests using (var context = device.Get().CreateComputeContext()) { context.Transition(source, ResourceState.ReadOnly); - context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(source.AsReadOnly(), destination)); + context.For(destination.Width, destination.Height, destination.Depth, new LinearSamplingFromNormalized3DComputeShader(source.AsReadOnly(), destination)); context.Transition(source, ResourceState.ReadWrite); } @@ -959,7 +1039,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - public void TransitionAndWrites_ReadWriteTexture2D(Device device) + public void TransitionAndWrites_ReadWriteTexture2D_Normalized(Device device) { _ = device.Get(); @@ -975,7 +1055,7 @@ public partial class ComputeContextTests { context.For(source.Width, source.Height, new Dotted2DPixelShader(source)); context.Transition(source, ResourceState.ReadOnly); - context.ForEach(destination, new LinearSampling2DPixelShader(source.AsReadOnly())); + context.ForEach(destination, new LinearSamplingFromNormalized2DPixelShader(source.AsReadOnly())); context.Transition(source, ResourceState.ReadWrite); context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } @@ -994,7 +1074,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - public void TransitionAndWrites_ReadWriteTexture3D(Device device) + public void TransitionAndWrites_ReadWriteTexture3D_Normalized(Device device) { _ = device.Get(); @@ -1025,7 +1105,7 @@ public partial class ComputeContextTests { 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.For(destination.Width, destination.Height, destination.Depth, new LinearSamplingFromNormalized3DComputeShader(source.AsReadOnly(), destination)); context.Transition(source, ResourceState.ReadWrite); context.Fill(source, new Rgba32(0xFF, 0x6A, 0x00, 0x00)); } @@ -1203,7 +1283,7 @@ public partial class ComputeContextTests [AutoConstructor] internal readonly partial struct LinearSampling2DPixelShader : IPixelShader { - public readonly IReadOnlyNormalizedTexture2D source; + public readonly IReadOnlyTexture2D source; /// public float4 Execute() @@ -1214,6 +1294,31 @@ public partial class ComputeContextTests [AutoConstructor] internal readonly partial struct LinearSampling3DComputeShader : IComputeShader + { + public readonly IReadOnlyTexture3D source; + public readonly IReadWriteNormalizedTexture3D destination; + + /// + public void Execute() + { + destination[ThreadIds.XYZ] = source[ThreadIds.Normalized.XYZ]; + } + } + + [AutoConstructor] + internal readonly partial struct LinearSamplingFromNormalized2DPixelShader : IPixelShader + { + public readonly IReadOnlyNormalizedTexture2D source; + + /// + public float4 Execute() + { + return source[ThreadIds.Normalized.XY]; + } + } + + [AutoConstructor] + internal readonly partial struct LinearSamplingFromNormalized3DComputeShader : IComputeShader { public readonly IReadOnlyNormalizedTexture3D source; public readonly IReadWriteNormalizedTexture3D destination; @@ -1280,4 +1385,17 @@ public partial class ComputeContextTests } } } + + [AutoConstructor] + internal readonly partial struct ConvertToNonNormalized2DShader : IComputeShader + { + public readonly IReadOnlyNormalizedTexture2D source; + public readonly ReadWriteTexture2D destination; + + /// + public void Execute() + { + destination[ThreadIds.XY] = source[ThreadIds.XY]; + } + } }