diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs b/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs
new file mode 100644
index 00000000..54003f2d
--- /dev/null
+++ b/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs
@@ -0,0 +1,46 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System.Runtime.CompilerServices;
+
+namespace SixLabors.ImageSharp
+{
+ ///
+ /// Represents a complex number, where the real and imaginary parts are stored as values.
+ ///
+ ///
+ /// This is a more efficient version of the type.
+ ///
+ internal readonly struct Complex64
+ {
+ ///
+ /// The real part of the complex number
+ ///
+ public readonly float Real;
+
+ ///
+ /// The imaginary part of the complex number
+ ///
+ public readonly float Imaginary;
+
+ ///
+ /// Initializes a new instance of the struct.
+ ///
+ /// The real part in the complex number.
+ /// The imaginary part in the complex number.
+ public Complex64(float real, float imaginary)
+ {
+ this.Real = real;
+ this.Imaginary = imaginary;
+ }
+
+ ///
+ /// Performs the multiplication operation between a instance and a scalar.
+ ///
+ /// The value to multiply.
+ /// The scalar to use to multiply the value.
+ /// The result
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static Complex64 operator *(Complex64 value, float scalar) => new Complex64(value.Real * scalar, value.Imaginary * scalar);
+ }
+}
\ No newline at end of file
diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs
new file mode 100644
index 00000000..af2e5902
--- /dev/null
+++ b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs
@@ -0,0 +1,23 @@
+// 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{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
index 5627cc8b..f1d13b89 100644
--- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
+++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs
@@ -6,6 +6,7 @@ using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+using ComputeSharp.ImageProcessing.Primitives;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
@@ -49,7 +50,7 @@ namespace ComputeSharp.BokehBlur.Processors
///
/// The kernel components for the current instance
///
- private readonly Vector2[][] Kernels;
+ private readonly Complex64[][] Kernels;
///
/// The scaling factor for kernel values
@@ -59,7 +60,7 @@ namespace ComputeSharp.BokehBlur.Processors
///
/// The mapping of initialized complex kernels and parameters, to speed up the initialization of new instances
///
- private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Vector2[][] Kernels)> Cache = new ConcurrentDictionary<(int, int), (Vector4[], float, Vector2[][])>();
+ private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Complex64[][] Kernels)> Cache = new();
///
/// Initializes a new instance of the class
@@ -168,9 +169,9 @@ namespace ComputeSharp.BokehBlur.Processors
///
/// Creates the collection of complex 1D kernels with the specified parameters
///
- private Vector2[][] CreateComplexKernels()
+ private Complex64[][] CreateComplexKernels()
{
- var kernels = new Vector2[KernelParameters.Length][];
+ var kernels = new Complex64[KernelParameters.Length][];
ref Vector4 baseRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan());
for (int i = 0; i < KernelParameters.Length; i++)
{
@@ -186,10 +187,10 @@ namespace ComputeSharp.BokehBlur.Processors
///
/// The exponential parameter for each complex component
/// The angle component for each complex component
- private Vector2[] CreateComplex1DKernel(float a, float b)
+ private Complex64[] CreateComplex1DKernel(float a, float b)
{
- var kernel = new Vector2[KernelSize];
- ref Vector2 baseRef = ref MemoryMarshal.GetReference(kernel.AsSpan());
+ var kernel = new Complex64[KernelSize];
+ ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel.AsSpan());
int r = Radius, n = -r;
for (int i = 0; i < KernelSize; i++, n++)
@@ -199,7 +200,7 @@ namespace ComputeSharp.BokehBlur.Processors
value *= value;
// Fill in the complex kernel values
- Unsafe.Add(ref baseRef, i) = new Vector2(
+ Unsafe.Add(ref baseRef, i) = new Complex64(
MathF.Exp(-a * value) * MathF.Cos(b * value),
MathF.Exp(-a * value) * MathF.Sin(b * value));
}
@@ -214,26 +215,26 @@ namespace ComputeSharp.BokehBlur.Processors
{
// Calculate the complex weighted sum
float total = 0;
- Span kernelsSpan = Kernels.AsSpan();
- ref Vector2[] baseKernelsRef = ref MemoryMarshal.GetReference(kernelsSpan);
+ Span kernelsSpan = Kernels.AsSpan();
+ ref Complex64[] baseKernelsRef = ref MemoryMarshal.GetReference(kernelsSpan);
ref Vector4 baseParamsRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan());
for (int i = 0; i < KernelParameters.Length; i++)
{
- ref Vector2[] kernelRef = ref Unsafe.Add(ref baseKernelsRef, i);
+ ref Complex64[] kernelRef = ref Unsafe.Add(ref baseKernelsRef, i);
int length = kernelRef.Length;
- ref Vector2 valueRef = ref kernelRef[0];
+ ref Complex64 valueRef = ref kernelRef[0];
ref Vector4 paramsRef = ref Unsafe.Add(ref baseParamsRef, i);
for (int j = 0; j < length; j++)
{
for (int k = 0; k < length; k++)
{
- ref Vector2 jRef = ref Unsafe.Add(ref valueRef, j);
- ref Vector2 kRef = ref Unsafe.Add(ref valueRef, k);
+ ref Complex64 jRef = ref Unsafe.Add(ref valueRef, j);
+ ref Complex64 kRef = ref Unsafe.Add(ref valueRef, k);
total +=
- paramsRef.Z * (jRef.X * kRef.X - jRef.Y * kRef.Y)
- + paramsRef.W * (jRef.X * kRef.Y + jRef.Y * kRef.X);
+ paramsRef.Z * (jRef.Real * kRef.Real - jRef.Imaginary * kRef.Imaginary)
+ + paramsRef.W * (jRef.Real * kRef.Imaginary + jRef.Imaginary * kRef.Real);
}
}
}
@@ -242,9 +243,9 @@ namespace ComputeSharp.BokehBlur.Processors
float scalar = 1f / MathF.Sqrt(total);
for (int i = 0; i < kernelsSpan.Length; i++)
{
- ref Vector2[] kernelsRef = ref Unsafe.Add(ref baseKernelsRef, i);
+ ref Complex64[] kernelsRef = ref Unsafe.Add(ref baseKernelsRef, i);
int length = kernelsRef.Length;
- ref Vector2 valueRef = ref kernelsRef[0];
+ ref Complex64 valueRef = ref kernelsRef[0];
for (int j = 0; j < length; j++)
{
@@ -263,8 +264,8 @@ namespace ComputeSharp.BokehBlur.Processors
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 * 2);
- using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(KernelSize);
+ using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length);
+ using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(KernelSize);
ref Vector4 param0 = ref KernelParameters[0]; // Avoid bounds check to access the kernel parameters
@@ -295,8 +296,8 @@ namespace ComputeSharp.BokehBlur.Processors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ApplyVerticalConvolution(
ReadOnlyBuffer source,
- ReadWriteBuffer target,
- ReadOnlyBuffer kernel)
+ ReadWriteBuffer target,
+ ReadOnlyBuffer kernel)
{
int height = Source.Height;
int width = Source.Width;
@@ -325,8 +326,8 @@ namespace ComputeSharp.BokehBlur.Processors
public int kernelLength;
public ReadOnlyBuffer source;
- public ReadWriteBuffer target;
- public ReadOnlyBuffer kernel;
+ public ReadWriteBuffer target;
+ public ReadOnlyBuffer kernel;
///
public void Execute(ThreadIds ids)
@@ -341,15 +342,16 @@ namespace ComputeSharp.BokehBlur.Processors
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];
+ Complex64 factors = kernel[i];
- real += factors.X * color;
- imaginary += factors.Y * color;
+ real += factors.Real * color;
+ imaginary += factors.Imaginary * color;
}
- int offsetXY = ids.Y * width * 2 + ids.X * 2;
- target[offsetXY] = real;
- target[offsetXY + 1] = imaginary;
+ int offsetXY = ids.Y * width + ids.X;
+
+ target[offsetXY].Real = real;
+ target[offsetXY].Imaginary = imaginary;
}
}
@@ -363,9 +365,9 @@ namespace ComputeSharp.BokehBlur.Processors
/// The weight factor for the imaginary component of the complex pixel values
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ApplyHorizontalConvolutionAndAccumulatePartials(
- ReadWriteBuffer source,
+ ReadWriteBuffer source,
ReadWriteBuffer target,
- ReadOnlyBuffer kernel,
+ ReadOnlyBuffer kernel,
float z,
float w)
{
@@ -399,9 +401,9 @@ namespace ComputeSharp.BokehBlur.Processors
public float z;
public float w;
- public ReadWriteBuffer source;
+ public ReadWriteBuffer source;
public ReadWriteBuffer target;
- public ReadOnlyBuffer kernel;
+ public ReadOnlyBuffer kernel;
///
public void Execute(ThreadIds ids)
@@ -415,13 +417,12 @@ namespace ComputeSharp.BokehBlur.Processors
for (int i = 0; i < kernelLength; i++)
{
int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX);
- var offsetXY = offsetY * width * 2 + offsetX * 2;
- Vector4 sourceReal = source[offsetXY];
- Vector4 sourceImaginary = source[offsetXY + 1];
- Vector2 factors = kernel[i];
+ var offsetXY = offsetY * width + offsetX;
+ ComplexVector4 source4 = source[offsetXY];
+ Complex64 factors = kernel[i];
- real += factors.X * sourceReal - factors.Y * sourceImaginary;
- imaginary += factors.X * sourceImaginary + factors.Y * sourceReal;
+ 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;