From 717fd71d8c0d12e79567998078a061903f75a09a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 13 Apr 2020 13:34:03 +0200 Subject: [PATCH] Updated ImageSharp to nightly version --- .../ComputeSharp.ImageProcessing.csproj | 2 +- .../Processors/HlslBokehBlurProcessor.cs | 6 +- .../HlslBokehBlurProcessor{TPixel}.cs | 47 ++++++------ .../Processors/HlslGaussianBlurProcessor.cs | 6 +- .../HlslGaussianBlurProcessor{TPixel}.cs | 72 ++++++++----------- 5 files changed, 57 insertions(+), 76 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj index 6bd66e66..b422d295 100644 --- a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj +++ b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj @@ -16,7 +16,7 @@ - + diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index c62cd173..2ae2eef1 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -1,7 +1,6 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; -using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processors { @@ -59,9 +58,10 @@ namespace ComputeSharp.BokehBlur.Processors public float Gamma { get; } /// - public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel + public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) + where TPixel : unmanaged, IPixel { - return new HlslBokehBlurProcessor(this, source, sourceRectangle); + return new HlslBokehBlurProcessor(this, configuration, source, sourceRectangle); } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index cc432f8b..00930fd5 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -11,7 +11,6 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; -using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processors { @@ -20,7 +19,8 @@ namespace ComputeSharp.BokehBlur.Processors /// /// The pixel format /// This processor is based on the code from Mike Pound, see github.com/mikepound/convolve - public sealed class HlslBokehBlurProcessor : IImageProcessor where TPixel : struct, IPixel + public sealed class HlslBokehBlurProcessor : ImageProcessor + where TPixel : unmanaged, IPixel { /// /// The kernel radius @@ -62,24 +62,20 @@ namespace ComputeSharp.BokehBlur.Processors /// private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Vector2[][] Kernels)> Cache = new ConcurrentDictionary<(int, int), (Vector4[], float, Vector2[][])>(); - /// - /// The source instance to modify - /// - private readonly Image Source; - /// /// 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 instance to modify /// The source that indicates the area to edit - public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Image source, Rectangle sourceRectangle) + public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) + : base(configuration, source, sourceRectangle) { Radius = definition.Radius; KernelSize = Radius * 2 + 1; ComponentsCount = definition.Components; Gamma = definition.Gamma; - Source = source; // Reuse the initialized values from the cache, if possible var parameters = (Radius, ComponentsCount); @@ -259,12 +255,12 @@ namespace ComputeSharp.BokehBlur.Processors } /// - public void Apply() + protected override void OnFrameApply(ImageFrame source) { // Preliminary gamma highlight pass - using IMemoryOwner source4 = GetExposedVector4Buffer(); + using IMemoryOwner source4 = GetExposedVector4Buffer(source); - using ReadOnlyBuffer sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span); + using ReadOnlyBuffer sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span); using ReadWriteBuffer processingBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size); using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size * 2); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); @@ -281,9 +277,11 @@ namespace ComputeSharp.BokehBlur.Processors ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); } - // Apply the inverse gamma exposure pass, and write the final pixel data + // Apply the inverse gamma exposure pass processingBuffer.GetData(source4.Memory.Span); - ApplyInverseGammaExposure(source4); + + // Write the final pixel data + ApplyInverseGammaExposure(source4, source); } /// @@ -429,16 +427,17 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. /// + /// The source image [Pure] - private IMemoryOwner GetExposedVector4Buffer() + private IMemoryOwner GetExposedVector4Buffer(ImageFrame source) { - IMemoryOwner source4 = Source.GetConfiguration().MemoryAllocator.Allocate(Source.Width * Source.Height); + IMemoryOwner source4 = Configuration.MemoryAllocator.Allocate(source.Width * source.Height); float exp = Gamma; int width = Source.Width; - Parallel.For(0, Source.Height, y => + Parallel.For(0, source.Height, y => { - ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference(); + ref TPixel rPixel = ref source.GetPixelRowSpan(y).GetPinnableReference(); ref Vector4 r4 = ref source4.Memory.Span.Slice(y * width).GetPinnableReference(); for (int x = 0; x < width; x++) @@ -458,17 +457,18 @@ namespace ComputeSharp.BokehBlur.Processors /// Applies the inverse gamma exposure pass to compute the final pixel values for a target image /// /// The source buffer to read from - private void ApplyInverseGammaExposure(IMemoryOwner sourceValues) + /// The source image + private void ApplyInverseGammaExposure(IMemoryOwner sourceValues, ImageFrame source) { - int width = Source.Width; + int width = source.Width; float expGamma = 1 / Gamma; - Parallel.For(0, Source.Height, y => + Parallel.For(0, source.Height, y => { Vector4 low = Vector4.Zero; var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference(); + ref TPixel rPixel = ref source.GetPixelRowSpan(y).GetPinnableReference(); ref Vector4 r4 = ref sourceValues.Memory.Span.Slice(y * width).GetPinnableReference(); for (int x = 0; x < width; x++) @@ -483,8 +483,5 @@ namespace ComputeSharp.BokehBlur.Processors } }); } - - /// - public void Dispose() { } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index c32bc696..d628edad 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -1,7 +1,6 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; -using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processors { @@ -48,9 +47,10 @@ namespace ComputeSharp.BokehBlur.Processors public int Radius { get; } /// - public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel + public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) + where TPixel : unmanaged, IPixel { - return new HlslGaussianBlurProcessor(this, source, sourceRectangle); + return new HlslGaussianBlurProcessor(this, configuration, source, sourceRectangle); } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 6201af7e..864e221b 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -1,13 +1,12 @@ using System; +using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading.Tasks; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; -using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processors { @@ -15,29 +14,26 @@ namespace ComputeSharp.BokehBlur.Processors /// Applies Gaussian blur processing to an image. /// /// The pixel format. - internal class HlslGaussianBlurProcessor : IImageProcessor where TPixel : struct, IPixel + internal class HlslGaussianBlurProcessor : ImageProcessor + where TPixel : unmanaged, IPixel { /// /// The 1D kernel to apply /// private readonly float[] Kernel; - /// - /// The source instance to modify - /// - private readonly Image Source; - /// /// 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, Image source, Rectangle sourceRectangle) + 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); - Source = source; } /// @@ -89,20 +85,31 @@ namespace ComputeSharp.BokehBlur.Processors } /// - public void Apply() + protected override void OnFrameApply(ImageFrame source) { - using Image source = Source.CloneAs(); - Span sourceSpan = MemoryMarshal.Cast(source.GetPixelSpan()); + IMemoryGroup group = source.GetPixelMemoryGroup(); - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan); - using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size); - using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); + for (int i = group.Count - 1; i >= 0; i--) + { + Span spanTPixel = group[i].Span; - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); + using IMemoryOwner group4 = Configuration.MemoryAllocator.Allocate(spanTPixel.Length); - sourceBuffer.GetData(sourceSpan); - WriteResultData(source); + 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.Size); + using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); + + ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); + ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); + + sourceBuffer.GetData(spanVector4); + + PixelOperations.Instance.FromVector4Destructive(Configuration, spanVector4, spanTPixel); + } } /// @@ -230,28 +237,5 @@ namespace ComputeSharp.BokehBlur.Processors target[offsetXY] = result; } } - - /// - /// Writes the final data to the source image - /// - /// The source to read from - private void WriteResultData(Image image) - { - int width = Source.Width; - - Parallel.For(0, Source.Height, y => - { - ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference(); - ref Vector4 r4 = ref MemoryMarshal.Cast(image.GetPixelRowSpan(y)).GetPinnableReference(); - - for (int x = 0; x < width; x++) - { - Unsafe.Add(ref rPixel, x).FromVector4(Unsafe.Add(ref r4, x)); - } - }); - } - - /// - public void Dispose() { } } }