diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 79eb4b45..9d8a3089 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -286,12 +286,14 @@ public sealed partial class HlslBokehBlurProcessor kernel.CopyFrom(Kernels[j]); - GraphicsDevice.For( + using var context = GraphicsDevice.CreateComputeContext(); + + context.For( source.Width, source.Height, new(texture, reals, imaginaries, kernel)); - GraphicsDevice.For( + context.For( source.Width, source.Height, new(parameters.Z, parameters.W, reals, imaginaries, temporary, kernel)); diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 5fb59656..59cd2930 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -105,8 +105,11 @@ public sealed partial class HlslGaussianBlurProcessor using ReadWriteTexture2D firstPassTexture = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = GraphicsDevice.AllocateReadOnlyBuffer(Kernel); - GraphicsDevice.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer)); - GraphicsDevice.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer)); + using (var context = GraphicsDevice.CreateComputeContext()) + { + context.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer)); + context.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer)); + } sourceTexture.CopyTo(span); } diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs new file mode 100644 index 00000000..8e25271d --- /dev/null +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -0,0 +1,173 @@ +using System; +using System.Linq; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("ComputeContext")] +public partial class ComputeContextTests +{ + [CombinatorialTestMethod] + [AllDevices] + public void For_Batched(Device device) + { + int[] array = Enumerable.Range(0, 1024).ToArray(); + + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.For(512, new OffsetComputeShader(buffer, 0)); + context.For(64, new OffsetComputeShader(buffer, 512)); + context.For(299, new OffsetComputeShader(buffer, 576)); + context.For(149, new OffsetComputeShader(buffer, 875)); + } + + int[] result = buffer.ToArray(); + + foreach (ref int x in array.AsSpan()) + { + x *= 2; + } + + CollectionAssert.AreEqual(array, result); + } + + [CombinatorialTestMethod] + [AllDevices] + public void ForEach_Batched(Device device) + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.ForEach(texture, default(ClearPixelShader)); + context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); + } + + Rgba32[,] result = texture.ToArray(); + + foreach (Rgba32 pixel in result) + { + Assert.AreEqual(pixel.R, 56); + Assert.AreEqual(pixel.G, 112); + Assert.AreEqual(pixel.B, 168); + Assert.AreEqual(pixel.A, 224); + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void ForAndForEach_Batched(Device device) + { + int[] array = Enumerable.Range(0, 1024).ToArray(); + + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.For(512, new OffsetComputeShader(buffer, 0)); + context.For(64, new OffsetComputeShader(buffer, 512)); + context.ForEach(texture, default(ClearPixelShader)); + context.For(299, new OffsetComputeShader(buffer, 576)); + context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); + context.For(149, new OffsetComputeShader(buffer, 875)); + } + + // Buffer test + { + int[] result = buffer.ToArray(); + + foreach (ref int x in array.AsSpan()) + { + x *= 2; + } + + CollectionAssert.AreEqual(array, result); + } + + // Texture test + { + Rgba32[,] result = texture.ToArray(); + + foreach (Rgba32 pixel in result) + { + Assert.AreEqual(pixel.R, 56); + Assert.AreEqual(pixel.G, 112); + Assert.AreEqual(pixel.B, 168); + Assert.AreEqual(pixel.A, 224); + } + } + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void DefaultContext_For_ThrowsException() + { + using ComputeContext context = default; + + context.For(32, 1, 1, default(OffsetComputeShader)); + + Assert.Fail(); + } + + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException))] + public void DefaultContext_ForEach_ThrowsException(Device device) + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using ComputeContext context = default; + + context.ForEach(texture, default(ColorPixelShader)); + + Assert.Fail(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void DefaultContext_Dispose_ThrowsException() + { + using ComputeContext context = default; + } + + [AutoConstructor] + internal readonly partial struct OffsetComputeShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly int offset; + + /// + public void Execute() + { + buffer[offset + ThreadIds.X] *= 2; + } + } + + [AutoConstructor] + internal readonly partial struct ClearPixelShader : IPixelShader + { + /// + public float4 Execute() + { + return default; + } + } + + [AutoConstructor] + internal readonly partial struct ColorPixelShader : IPixelShader + { + public readonly float4 color; + + /// + public float4 Execute() + { + return color; + } + } +}