From df2301b78268e4be519fa05a06994831047e4f1f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 5 Jan 2021 19:34:01 +0100 Subject: [PATCH] Switched bokeh blur effect to textures --- .../ComputeSharp.Benchmark.csproj | 1 - .../Primitives/ComplexVector4.cs | 23 ---- .../Processors/HlslBokehBlurProcessor.cs | 85 +------------ .../HlslBokehBlurProcessor{TPixel}.cs | 120 +++++++++++++++--- 4 files changed, 103 insertions(+), 126 deletions(-) delete mode 100644 samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index 4a6eded4..ccadcf15 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -7,7 +7,6 @@ - diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs deleted file mode 100644 index 1c24a7dd..00000000 --- a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; - -namespace SixLabors.ImageSharp -{ - /// - /// A vector with a pair of representing a complex color. - /// - internal struct ComplexVector4 - { - /// - /// The real part of the complex vector. - /// - public Vector4 Real; - - /// - /// The imaginary part of the complex number. - /// - public Vector4 Imaginary; - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index e9bd5cfe..dd4ecb24 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -1,5 +1,4 @@ -using System.Numerics; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; @@ -65,87 +64,5 @@ namespace ComputeSharp.BokehBlur.Processors return Unsafe.As>(processor); } - - /// - /// 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 f35defb8..a9937447 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -257,9 +257,10 @@ namespace ComputeSharp.BokehBlur.Processors // Preliminary gamma highlight pass using IMemoryOwner source4 = GetExposedVector4Buffer(memory); - using ReadOnlyBuffer sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span); - using ReadWriteBuffer processingBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); - using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); + using ReadOnlyTexture2D sourceTexture = Gpu.Default.AllocateReadOnlyTexture2D(source4.Memory.Span, source.Width, source.Height); + using ReadWriteTexture2D processingTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D realTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D imaginaryTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); ref Vector4 param0 = ref KernelParameters[0]; // Avoid bounds check to access the kernel parameters @@ -270,12 +271,12 @@ namespace ComputeSharp.BokehBlur.Processors kernelBuffer.SetData(Kernels[j]); Vector4 parameters = Unsafe.Add(ref param0, j); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); + ApplyVerticalConvolution(sourceTexture, realTexture, imaginaryTexture, kernelBuffer); + ApplyHorizontalConvolutionAndAccumulatePartials(realTexture, imaginaryTexture, processingTexture, kernelBuffer, parameters.Z, parameters.W); } // Apply the inverse gamma exposure pass - processingBuffer.GetData(source4.Memory.Span); + processingTexture.GetData(source4.Memory.Span); // Write the final pixel data ApplyInverseGammaExposure(source4.Memory, memory); @@ -285,42 +286,84 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Performs a vertical 1D complex convolution with the specified parameters. /// - /// The source to read data from. - /// The target to write the results to. + /// The source to read data from. + /// The target to write the real results to. + /// The target to write the imaginary results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadOnlyBuffer source, - ReadWriteBuffer target, + ReadOnlyTexture2D source, + ReadWriteTexture2D reals, + ReadWriteTexture2D imaginaries, ReadOnlyBuffer kernel) { int height = Source.Height; int width = Source.Width; VerticalConvolutionProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernelLength: kernel.Length, source, - target, + reals, + imaginaries, kernel); Gpu.Default.For(width, height, in shader); } + /// + /// Kernel for the vertical convolution pass. + /// + [AutoConstructor] + internal partial struct VerticalConvolutionProcessor : IComputeShader + { + public int maxY; + public int maxX; + public int kernelLength; + + public ReadOnlyTexture2D source; + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; + 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[offsetX, offsetY]; + Complex64 factors = kernel[i]; + + real += factors.Real * color; + imaginary += factors.Imaginary * color; + } + + reals[ids.XY] = real; + imaginaries[ids.XY] = imaginary; + } + } + /// /// Performs an horizontal 1D complex convolution with the specified parameters. /// - /// The source to read data from. - /// The target to write the results to. + /// The source to read the real results from. + /// The source to read the imaginary results from. /// 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, + ReadWriteTexture2D reals, + ReadWriteTexture2D imaginaries, + ReadWriteTexture2D target, ReadOnlyBuffer kernel, float z, float w) @@ -329,19 +372,60 @@ namespace ComputeSharp.BokehBlur.Processors int width = Source.Width; HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernelLength: kernel.Length, z, w, - source, + reals, + imaginaries, target, kernel); Gpu.Default.For(width, height, in shader); } + /// + /// Kernel for the horizontal convolution pass. + /// + [AutoConstructor] + internal partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader + { + public int maxY; + public int maxX; + public int kernelLength; + public float z; + public float w; + + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; + public ReadWriteTexture2D 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); + Vector4 sourceReal = reals[offsetX, offsetY]; + Vector4 sourceImaginary = imaginaries[offsetX, offsetY]; + Complex64 factors = kernel[i]; + + real += factors.Real * sourceReal - factors.Imaginary * sourceImaginary; + imaginary += factors.Real * sourceImaginary + factors.Imaginary * sourceReal; + } + + target[ids.XY] += real * z + imaginary * w; + } + } + /// /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. ///