using System.Numerics; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; namespace ComputeSharp.BokehBlur.Processors { /// /// Defines Gaussian blur by a (Sigma, Radius) pair. /// public sealed partial class HlslGaussianBlurProcessor : IImageProcessor { /// /// The default value for . /// public const float DefaultSigma = 3f; /// /// Initializes a new instance of the class. /// public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { } /// /// Initializes a new instance of the class. /// /// The radius value representing the size of the area to sample. public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { } /// /// Initializes a new instance of the class. /// /// The sigma value representing the weight of the blur. /// The radius value representing the size of the area to sample (this should be at least twice the sigma value) public HlslGaussianBlurProcessor(float sigma, int radius) { Sigma = sigma; Radius = radius; } /// /// Gets the sigma value representing the weight of the blur. /// public float Sigma { get; } /// /// Gets the radius defining the size of the area to sample. /// public int Radius { get; } /// public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel { return new HlslGaussianBlurProcessor(this, configuration, source, sourceRectangle); } /// /// Kernel for the vertical convolution pass. /// [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 ReadOnlyBuffer kernel; /// public void Execute(ThreadIds ids) { Vector4 result = Vector4.Zero; int radiusY = kernelLength >> 1; int sourceOffsetColumnBase = ids.X; for (int i = 0; i < kernelLength; i++) { int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); Vector4 color = source[offsetY * width + offsetX]; result += kernel[i] * color; } int offsetXY = ids.Y * width + ids.X; target[offsetXY] = result; } } /// /// Kernel for the horizontal convolution pass. /// [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 ReadOnlyBuffer kernel; /// public void Execute(ThreadIds ids) { Vector4 result = Vector4.Zero; int radiusX = kernelLength >> 1; int sourceOffsetColumnBase = ids.X; int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); int offsetXY; for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); offsetXY = offsetY * width + offsetX; Vector4 color = source[offsetXY]; result += kernel[i] * color; } offsetXY = ids.Y * width + ids.X; target[offsetXY] = result; } } } }