From 4b79f826b4c418bb3aa8eb4340c69b4542b7f660 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 13 Apr 2020 13:57:29 +0200 Subject: [PATCH] More code refactorings to image processors --- .../HlslBokehBlurProcessor{TPixel}.cs | 178 +++++++++++++----- .../ComputeSharp.ImageProcessing/Program.cs | 6 +- 2 files changed, 131 insertions(+), 53 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 00930fd5..ae125ced 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -6,9 +6,9 @@ using System.Diagnostics.Contracts; 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; @@ -257,31 +257,36 @@ namespace ComputeSharp.BokehBlur.Processors /// protected override void OnFrameApply(ImageFrame source) { - // Preliminary gamma highlight pass - using IMemoryOwner source4 = GetExposedVector4Buffer(source); + IMemoryGroup group = source.GetPixelMemoryGroup(); - 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); - - ref Vector4 param0 = ref KernelParameters[0]; // Avoid bounds check to access the kernel parameters - - // Perform two 1D convolutions for each component in the current instance - for (int i = 0; i < Kernels.Length; i++) + for (int i = group.Count - 1; i >= 0; i--) { - kernelBuffer.SetData(Kernels[i]); - Vector4 parameters = Unsafe.Add(ref param0, i); + // Preliminary gamma highlight pass + using IMemoryOwner source4 = GetExposedVector4Buffer(group[i]); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); + 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); + + ref Vector4 param0 = ref KernelParameters[0]; // Avoid bounds check to access the kernel parameters + + // Perform two 1D convolutions for each component in the current instance + for (int j = 0; j < Kernels.Length; j++) + { + kernelBuffer.SetData(Kernels[j]); + Vector4 parameters = Unsafe.Add(ref param0, j); + + ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); + ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); + } + + // Apply the inverse gamma exposure pass + processingBuffer.GetData(source4.Memory.Span); + + // Write the final pixel data + ApplyInverseGammaExposure(source4.Memory, group[i]); } - - // Apply the inverse gamma exposure pass - processingBuffer.GetData(source4.Memory.Span); - - // Write the final pixel data - ApplyInverseGammaExposure(source4, source); } /// @@ -429,59 +434,134 @@ namespace ComputeSharp.BokehBlur.Processors /// /// The source image [Pure] - private IMemoryOwner GetExposedVector4Buffer(ImageFrame source) + private IMemoryOwner GetExposedVector4Buffer(Memory source) { - IMemoryOwner source4 = Configuration.MemoryAllocator.Allocate(source.Width * source.Height); + IMemoryOwner target = Configuration.MemoryAllocator.Allocate(source.Length); + float exp = Gamma; int width = Source.Width; + int height = source.Length / width; - Parallel.For(0, source.Height, y => + ParallelRowIterator.IterateRows( + Configuration, + new Rectangle(0, 0, width, height), + new GetExposedVector4BufferProcessor(width, exp, source, target.Memory, Configuration)); + + return target; + } + + /// + /// The processor for + /// + private readonly struct GetExposedVector4BufferProcessor : IRowOperation + { + private readonly int width; + private readonly float exp; + + private readonly Memory source; + private readonly Memory target; + private readonly Configuration configuration; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public GetExposedVector4BufferProcessor( + int width, + float exp, + Memory source, + Memory target, + Configuration configuration) { - ref TPixel rPixel = ref source.GetPixelRowSpan(y).GetPinnableReference(); - ref Vector4 r4 = ref source4.Memory.Span.Slice(y * width).GetPinnableReference(); + this.width = width; + this.exp = exp; + this.source = source; + this.target = target; + this.configuration = configuration; + } - for (int x = 0; x < width; x++) + /// + public void Invoke(int y) + { + int offset = y * width; + + Span rowTPixel = source.Span.Slice(offset, width); + Span rowVector4 = target.Span.Slice(offset, width); + + PixelOperations.Instance.ToVector4(configuration, rowTPixel, rowVector4); + + foreach (ref Vector4 v in rowVector4) { - ref Vector4 v = ref Unsafe.Add(ref r4, x); - v = Unsafe.Add(ref rPixel, x).ToVector4(); v.X = MathF.Pow(v.X, exp); v.Y = MathF.Pow(v.Y, exp); v.Z = MathF.Pow(v.Z, exp); } - }); - - return source4; + } } /// /// Applies the inverse gamma exposure pass to compute the final pixel values for a target image /// - /// The source buffer to read from - /// The source image - private void ApplyInverseGammaExposure(IMemoryOwner sourceValues, ImageFrame source) + /// The source buffer to read from + /// The target image + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyInverseGammaExposure(Memory source, Memory target) { - int width = source.Width; + int width = Source.Width; + int height = source.Length / width; float expGamma = 1 / Gamma; - Parallel.For(0, source.Height, y => + ParallelRowIterator.IterateRows( + Configuration, + new Rectangle(0, 0, width, height), + new ApplyInverseGammaExposureProcessor(width, expGamma, source, target, Configuration)); + } + + /// + /// The processor for + /// + private readonly struct ApplyInverseGammaExposureProcessor : IRowOperation + { + private readonly int width; + private readonly float exp; + + private readonly Memory source; + private readonly Memory target; + private readonly Configuration configuration; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ApplyInverseGammaExposureProcessor( + int width, + float exp, + Memory source, + Memory target, + Configuration configuration) { + this.width = width; + this.exp = exp; + this.source = source; + this.target = target; + this.configuration = configuration; + } + + /// + public void Invoke(int y) + { + int offset = y * width; + + Span rowVector4 = source.Span.Slice(offset, width); + Span rowTPixel = target.Span.Slice(offset, width); + 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 Vector4 r4 = ref sourceValues.Memory.Span.Slice(y * width).GetPinnableReference(); - - for (int x = 0; x < width; x++) + foreach (ref Vector4 v in rowVector4) { - Vector4 v = Unsafe.Add(ref r4, x); var clamp = Vector4.Clamp(v, low, high); - v.X = MathF.Pow(clamp.X, expGamma); - v.Y = MathF.Pow(clamp.Y, expGamma); - v.Z = MathF.Pow(clamp.Z, expGamma); - - Unsafe.Add(ref rPixel, x).FromVector4(v); + v.X = MathF.Pow(clamp.X, exp); + v.Y = MathF.Pow(clamp.Y, exp); + v.Z = MathF.Pow(clamp.Z, exp); } - }); + + PixelOperations.Instance.FromVector4Destructive(configuration, rowVector4, rowTPixel); + } } } } diff --git a/samples/ComputeSharp.ImageProcessing/Program.cs b/samples/ComputeSharp.ImageProcessing/Program.cs index 9aaac4e1..91f52ca5 100644 --- a/samples/ComputeSharp.ImageProcessing/Program.cs +++ b/samples/ComputeSharp.ImageProcessing/Program.cs @@ -3,7 +3,6 @@ using System.IO; using System.Reflection; using ComputeSharp.BokehBlur.Processors; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; @@ -15,7 +14,7 @@ namespace ComputeSharp.ImageProcessing { Console.WriteLine(">> Loading image"); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg"); - using Image image = Image.Load(path); + using Image image = Image.Load(path); // Apply a series of processors and save the results foreach (var effect in new (string Name, IImageProcessor Processor)[] @@ -25,8 +24,7 @@ namespace ComputeSharp.ImageProcessing }) { Console.WriteLine($">> Applying {effect.Name}"); - using Image copy = image.Clone(); - copy.Mutate(c => c.ApplyProcessor(effect.Processor)); + using Image copy = image.Clone(c => c.ApplyProcessor(effect.Processor)); Console.WriteLine($">> Saving {effect.Name} to disk"); string targetPath = Path.Combine(