using System.Numerics; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; namespace ComputeSharp.BokehBlur.Processors { /// /// Applies bokeh blur processing to the image. /// public sealed partial class HlslBokehBlurProcessor : IImageProcessor { /// /// The default radius used by the parameterless constructor. /// public const int DefaultRadius = 32; /// /// The default component count used by the parameterless constructor. /// public const int DefaultComponents = 2; /// /// The default gamma used by the parameterless constructor. /// public const float DefaultGamma = 3f; /// /// Initializes a new instance of the class. /// public HlslBokehBlurProcessor() : this(DefaultRadius, DefaultComponents, DefaultGamma) { } /// /// Initializes a new instance of the class. /// /// The size of the area to sample. /// The number of components to use to approximate the original 2D bokeh blur convolution kernel. /// The gamma highlight factor to use to further process the image. public HlslBokehBlurProcessor(int radius, int components, float gamma) { Radius = radius; Components = components; Gamma = gamma; } /// /// Gets the radius. /// public int Radius { get; } /// /// Gets the number of components. /// public int Components { get; } /// /// Gets the gamma highlight factor to use when applying the effect. /// public float Gamma { get; } /// public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel { return new HlslBokehBlurProcessor(this, configuration, source, sourceRectangle); } /// /// Kernel for the vertical convolution pass. /// [AutoConstructor] internal partial struct VerticalConvolutionProcessor : IComputeShader { public int width; public int maxY; public int maxX; public int kernelLength; public ReadOnlyBuffer source; public ReadWriteBuffer target; public ReadOnlyBuffer kernel; /// public void Execute(ThreadIds ids) { Vector4 real = Vector4.Zero; Vector4 imaginary = 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]; Complex64 factors = kernel[i]; real += factors.Real * color; imaginary += factors.Imaginary * color; } int offsetXY = ids.Y * width + ids.X; target[offsetXY].Real = real; target[offsetXY].Imaginary = imaginary; } } /// /// Kernel for the horizontal convolution pass. /// [AutoConstructor] internal partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader { public int width; public int maxY; public int maxX; public int kernelLength; public float z; public float w; public ReadWriteBuffer source; public ReadWriteBuffer target; public ReadOnlyBuffer kernel; /// public void Execute(ThreadIds ids) { Vector4 real = Vector4.Zero; Vector4 imaginary = Vector4.Zero; int radiusX = kernelLength >> 1; int sourceOffsetColumnBase = ids.X; int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); var offsetXY = offsetY * width + offsetX; ComplexVector4 source4 = source[offsetXY]; Complex64 factors = kernel[i]; real += factors.Real * source4.Real - factors.Imaginary * source4.Imaginary; imaginary += factors.Real * source4.Imaginary + factors.Imaginary * source4.Real; } target[ids.Y * width + ids.X] += real * z + imaginary * w; } } } }