diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 7fb37dd0..5ac36156 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics.Contracts; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -257,61 +256,38 @@ namespace ComputeSharp.BokehBlur.Processors 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); + using ReadWriteTexture2D texture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D temporary = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D reals = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D imaginaries = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadOnlyBuffer kernel = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); // Preliminary gamma highlight pass - ApplyGammaHighlight(sourceTexture); - - ref Vector4 param0 = ref MemoryMarshal.GetArrayDataReference(KernelParameters); + Gpu.Default.For(source.Height, new(texture)); // 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); + Vector4 parameters = KernelParameters[j]; - ApplyVerticalConvolution(sourceTexture, realTexture, imaginaryTexture, kernelBuffer); - ApplyHorizontalConvolutionAndAccumulatePartials(realTexture, imaginaryTexture, processingTexture, kernelBuffer, parameters.Z, parameters.W); + kernel.SetData(Kernels[j]); + + Gpu.Default.For( + source.Width, + source.Height, + new(texture, reals, imaginaries, kernel)); + + Gpu.Default.For( + source.Width, + source.Height, + new(parameters.Z, parameters.W, reals, imaginaries, temporary, kernel)); } // Apply the inverse gamma exposure pass - ApplyInverseGammaExposure(processingTexture, sourceTexture); + Gpu.Default.For(source.Height, new(temporary, texture)); // Write the final pixel data - sourceTexture.GetData(span); - } - - /// - /// Performs a vertical 1D complex convolution with the specified parameters. - /// - /// 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( - IReadWriteTexture2D source, - ReadWriteTexture2D reals, - ReadWriteTexture2D imaginaries, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; - - VerticalConvolutionProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernelLength: kernel.Length, - source, - reals, - imaginaries, - kernel); - - Gpu.Default.For(width, height, in shader); + texture.GetData(span); } /// @@ -320,10 +296,6 @@ namespace ComputeSharp.BokehBlur.Processors [AutoConstructor] internal partial struct VerticalConvolutionProcessor : IComputeShader { - public int maxY; - public int maxX; - public int kernelLength; - public IReadWriteTexture2D source; public ReadWriteTexture2D reals; public ReadWriteTexture2D imaginaries; @@ -334,13 +306,15 @@ namespace ComputeSharp.BokehBlur.Processors { Vector4 real = Vector4.Zero; Vector4 imaginary = Vector4.Zero; + int maxY = source.Height; + int maxX = source.Width; + int kernelLength = kernel.Length; 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); + int offsetX = Hlsl.Clamp(ids.X, 0, maxX); Vector4 color = source[offsetX, offsetY]; Complex64 factors = kernel[i]; @@ -353,49 +327,12 @@ namespace ComputeSharp.BokehBlur.Processors } } - /// - /// Performs an horizontal 1D complex convolution with the specified parameters. - /// - /// 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( - ReadWriteTexture2D reals, - ReadWriteTexture2D imaginaries, - ReadWriteTexture2D target, - ReadOnlyBuffer kernel, - float z, - float w) - { - int height = Source.Height; - int width = Source.Width; - - HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernelLength: kernel.Length, - z, - w, - 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; @@ -409,13 +346,15 @@ namespace ComputeSharp.BokehBlur.Processors { Vector4 real = Vector4.Zero; Vector4 imaginary = Vector4.Zero; + int maxY = target.Height; + int maxX = target.Width; + int kernelLength = kernel.Length; 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); + int offsetX = Hlsl.Clamp(ids.X + i - radiusX, 0, maxX); Vector4 sourceReal = reals[offsetX, offsetY]; Vector4 sourceImaginary = imaginaries[offsetX, offsetY]; Complex64 factors = kernel[i]; @@ -428,29 +367,19 @@ namespace ComputeSharp.BokehBlur.Processors } } - /// - /// Applies the gamma highlight pass to compute the final pixel values for a target image. - /// - /// The source instance to modify. - [Pure] - private void ApplyGammaHighlight(IReadWriteTexture2D source) - { - Gpu.Default.For(source.Height, new GammaHighlightProcessor(source.Width, source)); - } - /// /// Kernel for the gamma highlight pass. /// [AutoConstructor] internal readonly partial struct GammaHighlightProcessor : IComputeShader { - public readonly int width; - public readonly IReadWriteTexture2D source; /// public void Execute(ThreadIds ids) { + int width = source.Width; + for (int i = 0; i < width; i++) { Float4 v = source[i, ids.X]; @@ -462,33 +391,20 @@ namespace ComputeSharp.BokehBlur.Processors } } - /// - /// Applies the inverse gamma highlight pass to compute the final pixel values for a target image. - /// - /// The source instance to read from. - /// The target instance to write to. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyInverseGammaExposure( - ReadWriteTexture2D source, - IReadWriteTexture2D target) - { - Gpu.Default.For(source.Height, new InverseGammaHighlightProcessor(source.Width, source, target)); - } - /// /// Kernel for the inverse gamma highlight pass. /// [AutoConstructor] internal readonly partial struct InverseGammaHighlightProcessor : IComputeShader { - public readonly int width; - public readonly ReadWriteTexture2D source; public readonly IReadWriteTexture2D target; /// public void Execute(ThreadIds ids) { + int width = source.Width; + for (int i = 0; i < width; i++) { Float4 v = source[i, ids.X]; diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index bc72a6a8..0aad185e 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -98,48 +98,18 @@ namespace ComputeSharp.BokehBlur.Processors using ReadWriteTexture2D firstPassTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); - ApplyVerticalConvolution(sourceTexture, firstPassTexture, kernelBuffer); - ApplyHorizontalConvolution(firstPassTexture, sourceTexture, kernelBuffer); + Gpu.Default.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer)); + Gpu.Default.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer)); sourceTexture.GetData(span); } - /// - /// Performs a vertical 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyVerticalConvolution( - IReadWriteTexture2D source, - ReadWriteTexture2D target, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; - - VerticalConvolutionProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - /// /// Kernel for the vertical convolution pass. /// [AutoConstructor] internal readonly partial struct VerticalConvolutionProcessor : IComputeShader { - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; - public readonly IReadWriteTexture2D source; public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; @@ -148,13 +118,15 @@ namespace ComputeSharp.BokehBlur.Processors public void Execute(ThreadIds ids) { Vector4 result = Vector4.Zero; + int maxY = source.Height - 1; + int maxX = source.Width - 1; + int kernelLength = kernel.Length; 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); + int offsetX = Hlsl.Clamp(ids.X, 0, maxX); Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color; @@ -164,42 +136,12 @@ namespace ComputeSharp.BokehBlur.Processors } } - /// - /// Performs an horizontal 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyHorizontalConvolution( - ReadWriteTexture2D source, - IReadWriteTexture2D target, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; - - HorizontalConvolutionProcessor shader = new( - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - /// /// Kernel for the horizontal convolution pass. /// [AutoConstructor] internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader { - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; - public readonly ReadWriteTexture2D source; public readonly IReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; @@ -208,13 +150,15 @@ namespace ComputeSharp.BokehBlur.Processors public void Execute(ThreadIds ids) { Vector4 result = Vector4.Zero; + int maxY = source.Height - 1; + int maxX = source.Width - 1; + int kernelLength = kernel.Length; 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); + int offsetX = Hlsl.Clamp(ids.X + i - radiusX, 0, maxX); Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color;