From 1dd0b703f3057a8556d3671d4ce9972d445c3d4e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 31 Aug 2019 00:36:28 +0200 Subject: [PATCH] Upgraded ImageSharp build, code refactoring --- .../ComputeSharp.BokehBlur.csproj | 2 +- .../Processor/HlslBokehBlurProcessor.cs | 8 ++- .../HlslBokehBlurProcessor{TPixel}.cs | 67 +++++++++++-------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj b/samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj index 3e629c91..2f980729 100644 --- a/samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj +++ b/samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj @@ -16,7 +16,7 @@ - + diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs index 2f1899c1..858e7a79 100644 --- a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs @@ -1,5 +1,7 @@ -using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Primitives; namespace ComputeSharp.BokehBlur.Processor { @@ -57,9 +59,9 @@ namespace ComputeSharp.BokehBlur.Processor public float Gamma { get; } /// - public IImageProcessor CreatePixelSpecificProcessor() where TPixel : struct, IPixel + public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel { - return new HlslBokehBlurProcessor(this); + return new HlslBokehBlurProcessor(this, source, sourceRectangle); } } } diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs index 82ca2d3c..394a00b2 100644 --- a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs @@ -62,16 +62,24 @@ namespace ComputeSharp.BokehBlur.Processor /// 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 - public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition) + /// The defining the processor parameters + /// The source instance to modify + /// The source that indicates the area to edit + public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Image source, Rectangle 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); @@ -251,10 +259,10 @@ namespace ComputeSharp.BokehBlur.Processor } /// - public void Apply(Image source, Rectangle sourceRectangle) + public void Apply() { // Preliminary gamma highlight pass - using IMemoryOwner source4 = GetExposedVector4Buffer(source); + using IMemoryOwner source4 = GetExposedVector4Buffer(); using ReadOnlyBuffer sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span); using ReadWriteBuffer processingBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size); @@ -266,17 +274,17 @@ namespace ComputeSharp.BokehBlur.Processor { // Compute the resulting complex buffer for the current component ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernels[i]); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer, source.Width, source.Height); - ApplyHorizontalConvolution(firstPassBuffer, secondPassBuffer, kernelBuffer, source.Width, source.Height); + ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); + ApplyHorizontalConvolution(firstPassBuffer, secondPassBuffer, kernelBuffer); // Add the results of the convolution with the current kernel Vector4 parameters = KernelParameters[i]; - SumProcessingPartials(secondPassBuffer, processingBuffer, parameters.Z, parameters.W, source.Width, source.Height); + SumProcessingPartials(secondPassBuffer, processingBuffer, parameters.Z, parameters.W); } // Apply the inverse gamma exposure pass, and write the final pixel data processingBuffer.GetData(source4.Memory.Span); - ApplyInverseGammaExposure(source4, source); + ApplyInverseGammaExposure(source4); } /// @@ -285,14 +293,13 @@ namespace ComputeSharp.BokehBlur.Processor /// The source to read data from /// The target to write the results to /// The with the values for the current complex kernel - /// The width of the image being processed - /// The height of the image being processed private void ApplyVerticalConvolution( ReadOnlyBuffer source, ReadWriteBuffer target, - ReadOnlyBuffer kernel, - int width, int height) + ReadOnlyBuffer kernel) { + int height = Source.Height; + int width = Source.Width; int maxY = height - 1; int maxX = width - 1; int kernelLength = kernel.Size; @@ -327,14 +334,13 @@ namespace ComputeSharp.BokehBlur.Processor /// The source to read data from /// The target to write the results to /// The with the values for the current complex kernel - /// The width of the image being processed - /// The height of the image being processed private void ApplyHorizontalConvolution( ReadWriteBuffer source, ReadWriteBuffer target, - ReadOnlyBuffer kernel, - int width, int height) + ReadOnlyBuffer kernel) { + int height = Source.Height; + int width = Source.Width; int maxY = height - 1; int maxX = width - 1; int kernelLength = kernel.Size; @@ -369,17 +375,16 @@ namespace ComputeSharp.BokehBlur.Processor /// /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. /// - /// The source to read [Pure] - private IMemoryOwner GetExposedVector4Buffer(Image sourceImage) + private IMemoryOwner GetExposedVector4Buffer() { - IMemoryOwner source4 = sourceImage.GetConfiguration().MemoryAllocator.Allocate(sourceImage.Width * sourceImage.Height); + IMemoryOwner source4 = Source.GetConfiguration().MemoryAllocator.Allocate(Source.Width * Source.Height); float exp = Gamma; - int width = sourceImage.Width; + int width = Source.Width; - Parallel.For(0, sourceImage.Height, y => + Parallel.For(0, Source.Height, y => { - ref TPixel rPixel = ref sourceImage.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++) @@ -399,18 +404,17 @@ namespace ComputeSharp.BokehBlur.Processor /// Applies the inverse gamma exposure pass to compute the final pixel values for a target image /// /// The source buffer to read from - /// The target instance to write to - private void ApplyInverseGammaExposure(IMemoryOwner sourceValues, Image targetImage) + private void ApplyInverseGammaExposure(IMemoryOwner sourceValues) { - int width = targetImage.Width; + int width = Source.Width; float expGamma = 1 / Gamma; - Parallel.For(0, targetImage.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 targetImage.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++) @@ -439,9 +443,11 @@ namespace ComputeSharp.BokehBlur.Processor ReadWriteBuffer source, ReadWriteBuffer target, float z, - float w, - int width, int height) + float w) { + int height = Source.Height; + int width = Source.Width; + Gpu.Default.For(width, height, id => { int offsetXY = id.Y * width * 2 + id.X * 2; @@ -451,5 +457,8 @@ namespace ComputeSharp.BokehBlur.Processor target[id.Y * width + id.X] += real * z + imaginary * w; }); } + + /// + public void Dispose() { } } }