From bae73b562c10ce553e7c630bf667eb94944c78da Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 13 Apr 2020 02:44:11 +0200 Subject: [PATCH] Updated ComputeSharp sample --- .../HlslBokehBlurProcessor{TPixel}.cs | 86 +++++++++++++++---- .../HlslGaussianBlurProcessor{TPixel}.cs | 82 ++++++++++++++---- 2 files changed, 136 insertions(+), 32 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index ea2b0df7..cc432f8b 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -292,6 +292,7 @@ namespace ComputeSharp.BokehBlur.Processors /// 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( ReadOnlyBuffer source, ReadWriteBuffer target, @@ -299,20 +300,44 @@ namespace ComputeSharp.BokehBlur.Processors { int height = Source.Height; int width = Source.Width; - int maxY = height - 1; - int maxX = width - 1; - int kernelLength = kernel.Size; - Gpu.Default.For(width, height, id => + Gpu.Default.For(width, height, new VerticalConvolutionProcessor + { + width = width, + maxY = height - 1, + maxX = width - 1, + kernelLength = kernel.Size, + source = source, + target = target, + kernel = kernel + }); + } + + /// + /// Kernel for + /// + private 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 = id.X; + int sourceOffsetColumnBase = ids.X; for (int i = 0; i < kernelLength; i++) { - int offsetY = Hlsl.Clamp(id.Y + i - radiusY, 0, maxY); + int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); Vector4 color = source[offsetY * width + offsetX]; Vector2 factors = kernel[i]; @@ -321,10 +346,10 @@ namespace ComputeSharp.BokehBlur.Processors imaginary += factors.Y * color; } - int offsetXY = id.Y * width * 2 + id.X * 2; + int offsetXY = ids.Y * width * 2 + ids.X * 2; target[offsetXY] = real; target[offsetXY + 1] = imaginary; - }); + } } /// @@ -335,6 +360,7 @@ namespace ComputeSharp.BokehBlur.Processors /// The with the values for the current complex kernel /// The weight factor for the real component of the complex pixel values /// The weight factor for the imaginary component of the complex pixel values + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyHorizontalConvolutionAndAccumulatePartials( ReadWriteBuffer source, ReadWriteBuffer target, @@ -344,17 +370,45 @@ namespace ComputeSharp.BokehBlur.Processors { int height = Source.Height; int width = Source.Width; - int maxY = height - 1; - int maxX = width - 1; - int kernelLength = kernel.Size; - Gpu.Default.For(width, height, id => + Gpu.Default.For(width, height, new HorizontalConvolutionAndAccumulatePartialsProcessor + { + width = width, + maxY = height - 1, + maxX = width - 1, + kernelLength = kernel.Size, + z = z, + w = w, + source = source, + target = target, + kernel = kernel + }); + } + + /// + /// Kernel for + /// + private 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 = id.X; - int offsetY = Hlsl.Clamp(id.Y, 0, maxY); + int sourceOffsetColumnBase = ids.X; + int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); for (int i = 0; i < kernelLength; i++) { @@ -368,8 +422,8 @@ namespace ComputeSharp.BokehBlur.Processors imaginary += factors.X * sourceImaginary + factors.Y * sourceReal; } - target[id.Y * width + id.X] += real * z + imaginary * w; - }); + target[ids.Y * width + ids.X] += real * z + imaginary * w; + } } /// diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 274f3228..6201af7e 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -111,6 +111,7 @@ namespace ComputeSharp.BokehBlur.Processors /// 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, @@ -118,28 +119,52 @@ namespace ComputeSharp.BokehBlur.Processors { int height = Source.Height; int width = Source.Width; - int maxY = height - 1; - int maxX = width - 1; - int kernelLength = kernel.Size; - Gpu.Default.For(width, height, id => + Gpu.Default.For(width, height, new VerticalConvolutionProcessor + { + width = width, + maxY = height - 1, + maxX = width - 1, + kernelLength = kernel.Size, + source = source, + target = target, + kernel = 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 = id.X; + int sourceOffsetColumnBase = ids.X; for (int i = 0; i < kernelLength; i++) { - int offsetY = Hlsl.Clamp(id.Y + i - radiusY, 0, maxY); + 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 = id.Y * width + id.X; + int offsetXY = ids.Y * width + ids.X; target[offsetXY] = result; - }); + } } /// @@ -148,6 +173,7 @@ namespace ComputeSharp.BokehBlur.Processors /// 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, @@ -155,16 +181,40 @@ namespace ComputeSharp.BokehBlur.Processors { int height = Source.Height; int width = Source.Width; - int maxY = height - 1; - int maxX = width - 1; - int kernelLength = kernel.Size; - Gpu.Default.For(width, height, id => + Gpu.Default.For(width, height, new HorizontalConvolutionProcessor + { + width = width, + maxY = height - 1, + maxX = width - 1, + kernelLength = kernel.Size, + source = source, + target = target, + kernel = 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 = id.X; - int offsetY = Hlsl.Clamp(id.Y, 0, maxY); + int sourceOffsetColumnBase = ids.X; + int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); int offsetXY; for (int i = 0; i < kernelLength; i++) @@ -176,9 +226,9 @@ namespace ComputeSharp.BokehBlur.Processors result += kernel[i] * color; } - offsetXY = id.Y * width + id.X; + offsetXY = ids.Y * width + ids.X; target[offsetXY] = result; - }); + } } ///