From bead19fc87083966ec5e10f708ba525841b4f3a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 4 Jan 2021 17:48:50 +0100 Subject: [PATCH] Updated sample to use normalized textures --- .gitignore | 5 +- .../ComputeSharp.ShareX.csproj | 1 - .../Gpu/HlslConvolutionManager.cs.cs | 62 +++++++------------ samples/ComputeSharp.ShareX/Program.cs | 4 +- 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 23c39fe0..fd8f3e38 100644 --- a/.gitignore +++ b/.gitignore @@ -329,6 +329,5 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ -# Sample bokeh blur image -**/*_bokeh.jpg -**/*_gaussian.jpg +# Sample images +**/city_*.jpg diff --git a/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj b/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj index 19f36cbb..00562633 100644 --- a/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj +++ b/samples/ComputeSharp.ShareX/ComputeSharp.ShareX.csproj @@ -12,7 +12,6 @@ - diff --git a/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs b/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs index c3cf8bfa..a02df046 100644 --- a/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs +++ b/samples/ComputeSharp.ShareX/Gpu/HlslConvolutionManager.cs.cs @@ -3,9 +3,6 @@ using System.Drawing; using System.Drawing.Imaging; using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using Rectangle = System.Drawing.Rectangle; namespace ComputeSharp.ShareX @@ -93,30 +90,23 @@ namespace ComputeSharp.ShareX BitmapData sourceData = source.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb), resultData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - Span - sourceSpan = new((Rgba32*)sourceData.Scan0.ToPointer(), bounds.Width * bounds.Height), - resultSpan = new((Rgba32*)resultData.Scan0.ToPointer(), sourceSpan.Length); - Span vector4Span = new((void*)Marshal.AllocHGlobal(sizeof(Vector4) * sourceSpan.Length), sourceSpan.Length); + Span + sourceSpan = new((Bgra32*)sourceData.Scan0.ToPointer(), bounds.Width * bounds.Height), + resultSpan = new((Bgra32*)resultData.Scan0.ToPointer(), sourceSpan.Length); try { - PixelOperations.Instance.ToVector4(Configuration.Default, sourceSpan, vector4Span); - - using ReadWriteBuffer leftBuffer = Gpu.Default.AllocateReadWriteBuffer(vector4Span); - using ReadWriteBuffer rightBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan.Length); + using ReadWriteTexture2D leftBuffer = Gpu.Default.AllocateReadWriteTexture2D(sourceSpan, source.Width, source.Height); + using ReadWriteTexture2D rightBuffer = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(this.kernel); ApplyVerticalConvolution(leftBuffer, rightBuffer, kernelBuffer, source.Height, source.Width); ApplyHorizontalConvolution(rightBuffer, leftBuffer, kernelBuffer, source.Height, source.Width); - leftBuffer.GetData(vector4Span); - - PixelOperations.Instance.FromVector4Destructive(Configuration.Default, vector4Span, resultSpan); + leftBuffer.GetData(resultSpan); } finally { - Marshal.FreeHGlobal((IntPtr)Unsafe.AsPointer(ref vector4Span.GetPinnableReference())); - source.UnlockBits(sourceData); result.UnlockBits(resultData); } @@ -127,21 +117,20 @@ namespace ComputeSharp.ShareX /// /// 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 results to. /// The with the values for the current complex kernel. /// The height of the image to process. /// The width of the image to process. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, + ReadWriteTexture2D source, + ReadWriteTexture2D target, ReadOnlyBuffer kernel, int height, int width) { VerticalConvolutionProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernel.Length, @@ -158,13 +147,12 @@ namespace ComputeSharp.ShareX [AutoConstructor] internal readonly partial struct VerticalConvolutionProcessor : IComputeShader { - public readonly int width; public readonly int maxY; public readonly int maxX; public readonly int kernelLength; - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// @@ -178,34 +166,32 @@ namespace ComputeSharp.ShareX { int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); - Vector4 color = source[offsetY * width + offsetX]; + Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color; } - int offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; + target[ids.XY] = result; } } /// /// 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 data from. + /// The target to write the results to. /// The with the values for the current complex kernel. /// The height of the image to process. /// The width of the image to process. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyHorizontalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, + ReadWriteTexture2D source, + ReadWriteTexture2D target, ReadOnlyBuffer kernel, int height, int width) { HorizontalConvolutionProcessor shader = new( - width, maxY: height - 1, maxX: width - 1, kernel.Length, @@ -222,13 +208,12 @@ namespace ComputeSharp.ShareX [AutoConstructor] internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader { - public readonly int width; public readonly int maxY; public readonly int maxX; public readonly int kernelLength; - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// @@ -238,19 +223,16 @@ namespace ComputeSharp.ShareX int radiusX = kernelLength >> 1; int sourceOffsetColumnBase = ids.X; int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); - int offsetXY; for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - offsetXY = offsetY * width + offsetX; - Vector4 color = source[offsetXY]; + Vector4 color = source[offsetX, offsetY]; result += kernel[i] * color; } - offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; + target[ids.XY] = result; } } } diff --git a/samples/ComputeSharp.ShareX/Program.cs b/samples/ComputeSharp.ShareX/Program.cs index 54abd493..90241bc2 100644 --- a/samples/ComputeSharp.ShareX/Program.cs +++ b/samples/ComputeSharp.ShareX/Program.cs @@ -18,7 +18,7 @@ namespace ComputeSharp.ShareX Console.WriteLine(">> Blurring on CPU"); - using Bitmap resultBitmapCpu = ConvolutionMatrixManager.GaussianBlur(60, 60, 10).Apply(sourceBitmap); + using Bitmap resultBitmapCpu = ConvolutionMatrixManager.GaussianBlur(61, 61, 10).Apply(sourceBitmap); string resultPathCpu = Path.Combine( Path.GetRelativePath(Path.GetDirectoryName(path)!, @"..\..\..\"), @@ -30,7 +30,7 @@ namespace ComputeSharp.ShareX Console.WriteLine(">> Blurring on GPU"); - using Bitmap resultBitmapGpu = HlslConvolutionManager.Create(60, 10).Apply(sourceBitmap); + using Bitmap resultBitmapGpu = HlslConvolutionManager.Create(61, 10).Apply(sourceBitmap); string resultPathGpu = Path.Combine( Path.GetRelativePath(Path.GetDirectoryName(path)!, @"..\..\..\"),