diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 0ae26d58..ea2b0df7 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -267,19 +267,18 @@ namespace ComputeSharp.BokehBlur.Processors 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 ReadWriteBuffer secondPassBuffer = 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++) { - // Compute the resulting complex buffer for the current component - ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernels[i]); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolution(firstPassBuffer, secondPassBuffer, kernelBuffer); + kernelBuffer.SetData(Kernels[i]); + Vector4 parameters = Unsafe.Add(ref param0, i); - // Add the results of the convolution with the current kernel - Vector4 parameters = KernelParameters[i]; - SumProcessingPartials(secondPassBuffer, processingBuffer, parameters.Z, parameters.W); + ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); + ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); } // Apply the inverse gamma exposure pass, and write the final pixel data @@ -334,10 +333,14 @@ namespace ComputeSharp.BokehBlur.Processors /// The source to read data from /// The target to write the results to /// The with the values for the current complex kernel - private void ApplyHorizontalConvolution( + /// The weight factor for the real component of the complex pixel values + /// The weight factor for the imaginary component of the complex pixel values + private void ApplyHorizontalConvolutionAndAccumulatePartials( ReadWriteBuffer source, ReadWriteBuffer target, - ReadOnlyBuffer kernel) + ReadOnlyBuffer kernel, + float z, + float w) { int height = Source.Height; int width = Source.Width; @@ -352,12 +355,11 @@ namespace ComputeSharp.BokehBlur.Processors int radiusX = kernelLength >> 1; int sourceOffsetColumnBase = id.X; int offsetY = Hlsl.Clamp(id.Y, 0, maxY); - int offsetXY; for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - offsetXY = offsetY * width * 2 + offsetX * 2; + var offsetXY = offsetY * width * 2 + offsetX * 2; Vector4 sourceReal = source[offsetXY]; Vector4 sourceImaginary = source[offsetXY + 1]; Vector2 factors = kernel[i]; @@ -366,9 +368,7 @@ namespace ComputeSharp.BokehBlur.Processors imaginary += factors.X * sourceImaginary + factors.Y * sourceReal; } - offsetXY = id.Y * width * 2 + id.X * 2; - target[offsetXY] = real; - target[offsetXY + 1] = imaginary; + target[id.Y * width + id.X] += real * z + imaginary * w; }); } @@ -430,32 +430,6 @@ namespace ComputeSharp.BokehBlur.Processors }); } - /// - /// Sums the partial results for a complex convolution pass over a single kernel component - /// - /// The source to read data from - /// The target to write the results to - /// The weight factor for the real component of the complex pixel values - /// The weight factor for the imaginary component of the complex pixel values - private void SumProcessingPartials( - ReadWriteBuffer source, - ReadWriteBuffer target, - float z, - 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; - Vector4 real = source[offsetXY]; - Vector4 imaginary = source[offsetXY + 1]; - - target[id.Y * width + id.X] += real * z + imaginary * w; - }); - } - /// public void Dispose() { } }