diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index a9937447..0f175def 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -1,14 +1,12 @@ using System; -using System.Buffers; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; @@ -252,35 +250,39 @@ namespace ComputeSharp.BokehBlur.Processors /// protected override void OnFrameApply(ImageFrame source) { - foreach (Memory memory in source.GetPixelMemoryGroup()) + if (!source.TryGetSinglePixelSpan(out Span pixelSpan)) { - // Preliminary gamma highlight pass - using IMemoryOwner source4 = GetExposedVector4Buffer(memory); - - 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 - - // 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(sourceTexture, realTexture, imaginaryTexture, kernelBuffer); - ApplyHorizontalConvolutionAndAccumulatePartials(realTexture, imaginaryTexture, processingTexture, kernelBuffer, parameters.Z, parameters.W); - } - - // Apply the inverse gamma exposure pass - processingTexture.GetData(source4.Memory.Span); - - // Write the final pixel data - ApplyInverseGammaExposure(source4.Memory, memory); + ThrowHelper.ThrowInvalidOperationException("Cannot process image frames wrapping discontiguous memory"); } + + Span span = MemoryMarshal.Cast(pixelSpan); + + using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(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); + + // Preliminary gamma highlight pass + ApplyGammaHighlight(sourceTexture); + + ref Vector4 param0 = ref MemoryMarshal.GetArrayDataReference(KernelParameters); + + // 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(sourceTexture, realTexture, imaginaryTexture, kernelBuffer); + ApplyHorizontalConvolutionAndAccumulatePartials(realTexture, imaginaryTexture, processingTexture, kernelBuffer, parameters.Z, parameters.W); + } + + // Apply the inverse gamma exposure pass + ApplyInverseGammaExposure(processingTexture, sourceTexture); + + // Write the final pixel data + sourceTexture.GetData(span); } /// @@ -292,7 +294,7 @@ namespace ComputeSharp.BokehBlur.Processors /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadOnlyTexture2D source, + ReadWriteTexture2D source, ReadWriteTexture2D reals, ReadWriteTexture2D imaginaries, ReadOnlyBuffer kernel) @@ -322,7 +324,7 @@ namespace ComputeSharp.BokehBlur.Processors public int maxX; public int kernelLength; - public ReadOnlyTexture2D source; + public ReadWriteTexture2D source; public ReadWriteTexture2D reals; public ReadWriteTexture2D imaginaries; public ReadOnlyBuffer kernel; @@ -427,112 +429,75 @@ namespace ComputeSharp.BokehBlur.Processors } /// - /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. + /// Applies the gamma highlight pass to compute the final pixel values for a target image. /// - /// The source image. + /// The source instance to modify. [Pure] - private IMemoryOwner GetExposedVector4Buffer(Memory source) + private void ApplyGammaHighlight(ReadWriteTexture2D source) { - IMemoryOwner target = Configuration.MemoryAllocator.Allocate(source.Length); - - int width = Source.Width; - int height = source.Length / width; - - ParallelRowIterator.IterateRows( - Configuration, - new Rectangle(0, 0, width, height), - new GetExposedVector4BufferProcessor(width, source, target.Memory, Configuration)); - - return target; + Gpu.Default.For(source.Height, new GammaHighlightProcessor(source.Width, source)); } /// - /// The processor for . + /// Kernel for the gamma highlight pass. /// [AutoConstructor] - private readonly partial struct GetExposedVector4BufferProcessor : IRowOperation + internal readonly partial struct GammaHighlightProcessor : IComputeShader { - private readonly int width; + public readonly int width; - private readonly Memory source; - private readonly Memory target; - private readonly Configuration configuration; + public readonly ReadWriteTexture2D source; /// - public void Invoke(int y) + public void Execute(ThreadIds ids) { - 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) + for (int i = 0; i < width; i++) { - Vector4 pixel = v; - float alpha = pixel.W; + Float4 v = source[i, ids.X]; - pixel = pixel * pixel * pixel; - pixel.W = alpha; + v.XYZ = v.XYZ * v.XYZ * v.XYZ; - v = pixel; + source[i, ids.X] = v; } } } /// - /// Applies the inverse gamma exposure pass to compute the final pixel values for a target image. + /// Applies the inverse gamma highlight pass to compute the final pixel values for a target image. /// - /// The source buffer to read from. - /// The target image. + /// The source instance to read from. + /// The target instance to write to. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyInverseGammaExposure(Memory source, Memory target) + private void ApplyInverseGammaExposure( + ReadWriteTexture2D source, + ReadWriteTexture2D target) { - int width = Source.Width; - int height = source.Length / width; - - ParallelRowIterator.IterateRows( - Configuration, - new Rectangle(0, 0, width, height), - new ApplyInverseGammaExposureProcessor(width, source, target, Configuration)); + Gpu.Default.For(source.Height, new InverseGammaHighlightProcessor(source.Width, source, target)); } /// - /// The processor for . + /// Kernel for the inverse gamma highlight pass. /// [AutoConstructor] - private readonly partial struct ApplyInverseGammaExposureProcessor : IRowOperation + internal readonly partial struct InverseGammaHighlightProcessor : IComputeShader { - private readonly int width; + public readonly int width; - private readonly Memory source; - private readonly Memory target; - private readonly Configuration configuration; + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; /// - public void Invoke(int y) + public void Execute(ThreadIds ids) { - 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); - - foreach (ref Vector4 v in rowVector4) + for (int i = 0; i < width; i++) { - var clamp = Vector4.Clamp(v, low, high); + Float4 v = source[i, ids.X]; - clamp.X = MathF.Pow(clamp.X, 1 / 3f); - clamp.Y = MathF.Pow(clamp.Y, 1 / 3f); - clamp.Z = MathF.Pow(clamp.Z, 1 / 3f); + v.XYZ = Hlsl.Clamp(v.XYZ, 0, float.MaxValue); + v.XYZ = Hlsl.Pow(v.XYZ, 0.333333343f); - v = clamp; + target[i, ids.X] = v; } - - PixelOperations.Instance.FromVector4Destructive(configuration, rowVector4, rowTPixel); } } }