From 82a6737ce2521d6a17f56b3e63f3ab353fa3a794 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 5 Jan 2021 17:06:11 +0100 Subject: [PATCH] Switched gaussian blur to normalized 2D textures --- .../Processors/HlslGaussianBlurProcessor.cs | 21 +++---- .../HlslGaussianBlurProcessor{TPixel}.cs | 57 +++++++++---------- .../ComputeSharp.ImageProcessing/Program.cs | 3 +- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index b0d22c81..8bdfa0ef 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -60,13 +60,12 @@ namespace ComputeSharp.BokehBlur.Processors [AutoConstructor] internal readonly partial struct VerticalConvolutionProcessor : IComputeShader { - public readonly int width; public readonly int maxY; public readonly int maxX; public readonly int kernelLength; - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// @@ -80,13 +79,12 @@ namespace ComputeSharp.BokehBlur.Processors { int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); - Vector4 color = source[offsetY * width + offsetX]; + Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color; } - int offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; + target[ids.XY] = result; } } @@ -96,13 +94,12 @@ namespace ComputeSharp.BokehBlur.Processors [AutoConstructor] internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader { - public readonly int width; public readonly int maxY; public readonly int maxX; public readonly int kernelLength; - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// @@ -117,14 +114,12 @@ namespace ComputeSharp.BokehBlur.Processors for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - offsetXY = offsetY * width + offsetX; - Vector4 color = source[offsetXY]; + Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color; } - offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; + target[ids.XY] = result; } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 4ea8671c..f9529bf2 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -1,11 +1,12 @@ using System; -using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.BokehBlur.Processors { @@ -86,46 +87,41 @@ namespace ComputeSharp.BokehBlur.Processors /// protected override void OnFrameApply(ImageFrame source) { - foreach (Memory memory in source.GetPixelMemoryGroup()) + ImageFrame frame = (ImageFrame)(object)source; + + if (!frame.TryGetSinglePixelSpan(out Span pixelSpan)) { - Span spanTPixel = memory.Span; - - using IMemoryOwner group4 = Configuration.MemoryAllocator.Allocate(spanTPixel.Length); - - Span spanVector4 = group4.Memory.Span; - - PixelOperations.Instance.ToVector4(Configuration, spanTPixel, spanVector4); - - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(spanVector4); - using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); - using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); - - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); - - sourceBuffer.GetData(spanVector4); - - PixelOperations.Instance.FromVector4Destructive(Configuration, spanVector4, spanTPixel); + ThrowHelper.ThrowInvalidOperationException("Cannot process image frames wrapping discontiguous memory"); } + + Span span = MemoryMarshal.Cast(pixelSpan); + + using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(span, frame.Width, frame.Height); + using ReadWriteTexture2D firstPassTexture = Gpu.Default.AllocateReadWriteTexture2D(frame.Width, frame.Height); + using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); + + ApplyVerticalConvolution(sourceTexture, firstPassTexture, kernelBuffer); + ApplyHorizontalConvolution(firstPassTexture, sourceTexture, kernelBuffer); + + sourceTexture.GetData(span); } /// /// Performs a vertical 1D complex convolution with the specified parameters. /// - /// The source to read data from. - /// The target to write the results to. + /// The source to read data from. + /// The target to write the results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, + ReadWriteTexture2D source, + ReadWriteTexture2D target, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; HlslGaussianBlurProcessor.VerticalConvolutionProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernel.Length, @@ -139,20 +135,19 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Performs an horizontal 1D complex convolution with the specified parameters. /// - /// The source to read data from. - /// The target to write the results to. + /// The source to read data from. + /// The target to write the results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyHorizontalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, + ReadWriteTexture2D source, + ReadWriteTexture2D target, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; HlslGaussianBlurProcessor.HorizontalConvolutionProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernel.Length, diff --git a/samples/ComputeSharp.ImageProcessing/Program.cs b/samples/ComputeSharp.ImageProcessing/Program.cs index 91f52ca5..34af8cb3 100644 --- a/samples/ComputeSharp.ImageProcessing/Program.cs +++ b/samples/ComputeSharp.ImageProcessing/Program.cs @@ -5,6 +5,7 @@ using ComputeSharp.BokehBlur.Processors; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.ImageProcessing { @@ -14,7 +15,7 @@ namespace ComputeSharp.ImageProcessing { Console.WriteLine(">> Loading image"); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg"); - using Image image = Image.Load(path); + using Image image = Image.Load(path); // Apply a series of processors and save the results foreach (var effect in new (string Name, IImageProcessor Processor)[]