From 400a2f4ab36b00432ca484f7b685fd4e36be90a8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 29 Dec 2020 00:33:23 +0100 Subject: [PATCH] Fixed image processing sample --- .../Processors/HlslBokehBlurProcessor.cs | 87 +++++++++++++- .../HlslBokehBlurProcessor{TPixel}.cs | 86 +------------- .../Processors/HlslGaussianBlurProcessor.cs | 79 ++++++++++++- .../HlslGaussianBlurProcessor{TPixel}.cs | 110 +++--------------- 4 files changed, 183 insertions(+), 179 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index c756217e..84e05a55 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -1,4 +1,5 @@ -using SixLabors.ImageSharp; +using System.Numerics; +using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; @@ -7,7 +8,7 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Applies bokeh blur processing to the image. /// - public sealed class HlslBokehBlurProcessor : IImageProcessor + public sealed partial class HlslBokehBlurProcessor : IImageProcessor { /// /// The default radius used by the parameterless constructor. @@ -63,5 +64,87 @@ namespace ComputeSharp.BokehBlur.Processors { 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; + } + } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index f22b9abd..bbc41abb 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -301,7 +301,7 @@ namespace ComputeSharp.BokehBlur.Processors int height = Source.Height; int width = Source.Width; - VerticalConvolutionProcessor shader = new( + HlslBokehBlurProcessor.VerticalConvolutionProcessor shader = new( width, maxY: height - 1, maxX: width - 1, @@ -313,47 +313,6 @@ namespace ComputeSharp.BokehBlur.Processors Gpu.Default.For(width, height, in shader); } - /// - /// Kernel for . - /// - [AutoConstructor] - private 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; - } - } - /// /// Performs an horizontal 1D complex convolution with the specified parameters. /// @@ -373,7 +332,7 @@ namespace ComputeSharp.BokehBlur.Processors int height = Source.Height; int width = Source.Width; - HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( + HlslBokehBlurProcessor.HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( width, maxY: height - 1, maxX: width - 1, @@ -387,47 +346,6 @@ namespace ComputeSharp.BokehBlur.Processors Gpu.Default.For(width, height, in shader); } - /// - /// Kernel for . - /// - [AutoConstructor] - private 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; - } - } - /// /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. /// diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index f7aca904..b0d22c81 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -1,4 +1,5 @@ -using SixLabors.ImageSharp; +using System.Numerics; +using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; @@ -7,7 +8,7 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Defines Gaussian blur by a (Sigma, Radius) pair. /// - public sealed class HlslGaussianBlurProcessor : IImageProcessor + public sealed partial class HlslGaussianBlurProcessor : IImageProcessor { /// /// The default value for . @@ -52,5 +53,79 @@ namespace ComputeSharp.BokehBlur.Processors { 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; + } + } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 31ca80cc..4ea8671c 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.BokehBlur.Processors /// Applies Gaussian blur processing to an image. /// /// The pixel format. - internal class HlslGaussianBlurProcessor : ImageProcessor + internal sealed class HlslGaussianBlurProcessor : ImageProcessor where TPixel : unmanaged, IPixel { /// @@ -124,51 +124,16 @@ namespace ComputeSharp.BokehBlur.Processors int height = Source.Height; int width = Source.Width; - Gpu.Default.For(width, height, new VerticalConvolutionProcessor - { - width = width, - maxY = height - 1, - maxX = width - 1, - kernelLength = kernel.Length, - source = source, - target = target, - kernel = kernel - }); - } + HlslGaussianBlurProcessor.VerticalConvolutionProcessor shader = new( + width, + maxY: height - 1, + maxX: width - 1, + kernel.Length, + source, + target, + kernel); - /// - /// Kernel for . - /// - private struct VerticalConvolutionProcessor : IComputeShader - { - public int width; - public int maxY; - public int maxX; - public int kernelLength; - - public ReadWriteBuffer source; - public ReadWriteBuffer target; - public 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; - } + Gpu.Default.For(width, height, in shader); } /// @@ -186,53 +151,16 @@ namespace ComputeSharp.BokehBlur.Processors int height = Source.Height; int width = Source.Width; - Gpu.Default.For(width, height, new HorizontalConvolutionProcessor - { - width = width, - maxY = height - 1, - maxX = width - 1, - kernelLength = kernel.Length, - source = source, - target = target, - kernel = kernel - }); - } + HlslGaussianBlurProcessor.HorizontalConvolutionProcessor shader = new( + width, + maxY: height - 1, + maxX: width - 1, + kernel.Length, + source, + target, + kernel); - /// - /// Kernel for . - /// - private struct HorizontalConvolutionProcessor : IComputeShader - { - public int width; - public int maxY; - public int maxX; - public int kernelLength; - - public ReadWriteBuffer source; - public ReadWriteBuffer target; - public 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; - } + Gpu.Default.For(width, height, in shader); } } }