using System; using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; namespace ComputeSharp.BokehBlur.Processors { /// /// Applies Gaussian blur processing to an image. /// /// The pixel format. internal sealed class HlslGaussianBlurProcessor : ImageProcessor where TPixel : unmanaged, IPixel { /// /// The 1D kernel to apply. /// private readonly float[] Kernel; /// /// Initializes a new instance of the class. /// /// The defining the processor parameters. /// The configuration which allows altering default behaviour or extending the library. /// The source for the current processor instance. /// The source area to process for the current processor instance. public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) : base(configuration, source, sourceRectangle) { int kernelSize = definition.Radius * 2 + 1; Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); } /// /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function. /// private static float[] CreateGaussianBlurKernel(int size, float weight) { float[] kernel = new float[size]; ref float rKernel = ref kernel[0]; float sum = 0F; float midpoint = (size - 1) / 2F; for (int i = 0; i < size; i++) { float x = i - midpoint; float gx = Gaussian(x, weight); sum += gx; Unsafe.Add(ref rKernel, i) = gx; } // Normalize kernel so that the sum of all weights equals 1 for (int i = 0; i < size; i++) { Unsafe.Add(ref rKernel, i) /= sum; } return kernel; } /// /// Implementation of 1D Gaussian G(x) function. /// /// The x provided to G(x) /// The spread of the blur. /// The Gaussian G(x) private static float Gaussian(float x, float sigma) { const float Numerator = 1.0f; float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; float exponentNumerator = -x * x; float exponentDenominator = 2 * sigma * sigma; float left = Numerator / denominator; float right = MathF.Exp(exponentNumerator / exponentDenominator); return left * right; } /// protected override void OnFrameApply(ImageFrame source) { foreach (Memory memory in source.GetPixelMemoryGroup()) { 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); } } /// /// Performs a vertical 1D complex convolution with the specified parameters. /// /// 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, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; HlslGaussianBlurProcessor.VerticalConvolutionProcessor shader = new( width, maxY: height - 1, maxX: width - 1, kernel.Length, source, target, kernel); Gpu.Default.For(width, height, in shader); } /// /// Performs an horizontal 1D complex convolution with the specified parameters. /// /// 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, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; HlslGaussianBlurProcessor.HorizontalConvolutionProcessor shader = new( width, maxY: height - 1, maxX: width - 1, kernel.Length, source, target, kernel); Gpu.Default.For(width, height, in shader); } } }