diff --git a/.gitignore b/.gitignore index 109cd325..23c39fe0 100644 --- a/.gitignore +++ b/.gitignore @@ -331,3 +331,4 @@ ASALocalRun/ # Sample bokeh blur image **/*_bokeh.jpg +**/*_gaussian.jpg diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 0f5c5fb0..4f97f672 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29201.188 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp", "src\ComputeSharp\ComputeSharp.csproj", "{314EB353-E7E3-4146-8111-685CC83EC1DE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Core", "src\ComputeSharp.Core\ComputeSharp.Core.csproj", "{9F9581FB-6EF7-418E-BFA5-0EFED874E980}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Core", "src\ComputeSharp.Core\ComputeSharp.Core.csproj", "{9F9581FB-6EF7-418E-BFA5-0EFED874E980}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Graphics", "src\ComputeSharp.Graphics\ComputeSharp.Graphics.csproj", "{2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}" EndProject @@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Benchmark", "samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj", "{34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.BokehBlur", "samples\ComputeSharp.BokehBlur\ComputeSharp.BokehBlur.csproj", "{524CC5DD-1DEE-4A30-9515-AB90965E5207}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.ImageProcessing", "samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj", "{524CC5DD-1DEE-4A30-9515-AB90965E5207}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Sample", "samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj", "{7A48A85D-8866-4DED-A404-653ED09BF396}" EndProject @@ -40,6 +40,22 @@ Global Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.ActiveCfg = Debug|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.Build.0 = Debug|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.Build.0 = Release|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.ActiveCfg = Release|Any CPU + {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.Build.0 = Release|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.ActiveCfg = Debug|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.Build.0 = Debug|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.Build.0 = Release|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.ActiveCfg = Release|Any CPU + {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.Build.0 = Release|Any CPU {2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|Any CPU.Build.0 = Debug|Any CPU {2499CC74-BC9A-4B30-A3B6-05F9C7BF9B34}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -56,14 +72,22 @@ Global {23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|Any CPU.Build.0 = Release|Any CPU {23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|x64.ActiveCfg = Release|Any CPU {23AE6EED-79CD-4ADD-9A85-0148BD957D19}.Release|x64.Build.0 = Release|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.ActiveCfg = Debug|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Debug|x64.Build.0 = Debug|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|Any CPU.Build.0 = Release|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.ActiveCfg = Release|Any CPU - {314EB353-E7E3-4146-8111-685CC83EC1DE}.Release|x64.Build.0 = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.Build.0 = Debug|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.ActiveCfg = Debug|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.Build.0 = Debug|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.ActiveCfg = Release|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.Build.0 = Release|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.ActiveCfg = Release|Any CPU + {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.Build.0 = Release|Any CPU {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|Any CPU.Build.0 = Debug|Any CPU {7A48A85D-8866-4DED-A404-653ED09BF396}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -88,40 +112,16 @@ Global {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.ActiveCfg = Debug|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Debug|x64.Build.0 = Debug|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|Any CPU.Build.0 = Release|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.ActiveCfg = Release|Any CPU - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF}.Release|x64.Build.0 = Release|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|Any CPU.Build.0 = Debug|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.ActiveCfg = Debug|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Debug|x64.Build.0 = Debug|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.ActiveCfg = Release|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|Any CPU.Build.0 = Release|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.ActiveCfg = Release|Any CPU - {524CC5DD-1DEE-4A30-9515-AB90965E5207}.Release|x64.Build.0 = Release|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.ActiveCfg = Debug|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Debug|x64.Build.0 = Debug|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|Any CPU.Build.0 = Release|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.ActiveCfg = Release|Any CPU - {9F9581FB-6EF7-418E-BFA5-0EFED874E980}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution + {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {524CC5DD-1DEE-4A30-9515-AB90965E5207} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} - {34AFE6E6-9A4C-4C3F-AE69-DD3693086DAF} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} - {524CC5DD-1DEE-4A30-9515-AB90965E5207} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.BokehBlur/Program.cs b/samples/ComputeSharp.BokehBlur/Program.cs deleted file mode 100644 index 4c970cb4..00000000 --- a/samples/ComputeSharp.BokehBlur/Program.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.IO; -using System.Reflection; -using ComputeSharp.BokehBlur.Processor; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; -using SixLabors.ImageSharp.Processing; - -namespace ComputeSharp.BokehBlur -{ - class Program - { - static void Main() - { - Console.WriteLine(">> Loading image"); - string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg"); - using Image image = Image.Load(path); - - Console.WriteLine(">> Applying blur"); - image.Mutate(c => c.ApplyProcessor(new HlslBokehBlurProcessor(80, 2, 3))); - - Console.WriteLine(">> Saving to disk"); - string targetPath = Path.Combine( - Path.GetRelativePath(Path.GetDirectoryName(path), @"..\..\..\"), - $"{Path.GetFileNameWithoutExtension(path)}_bokeh{Path.GetExtension(path)}"); - image.Save(targetPath); - } - } -} diff --git a/samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj similarity index 100% rename from samples/ComputeSharp.BokehBlur/ComputeSharp.BokehBlur.csproj rename to samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs similarity index 98% rename from samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs rename to samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index 858e7a79..c62cd173 100644 --- a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -3,7 +3,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.Primitives; -namespace ComputeSharp.BokehBlur.Processor +namespace ComputeSharp.BokehBlur.Processors { /// /// Applies bokeh blur processing to the image diff --git a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs similarity index 99% rename from samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs rename to samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 948a5bab..0ae26d58 100644 --- a/samples/ComputeSharp.BokehBlur/Processor/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -13,7 +13,7 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using SixLabors.Primitives; -namespace ComputeSharp.BokehBlur.Processor +namespace ComputeSharp.BokehBlur.Processors { /// /// Applies bokeh blur processing to the image diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs new file mode 100644 index 00000000..c32bc696 --- /dev/null +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -0,0 +1,56 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Primitives; + +namespace ComputeSharp.BokehBlur.Processors +{ + /// + /// Defines Gaussian blur by a (Sigma, Radius) pair + /// + public sealed class HlslGaussianBlurProcessor : IImageProcessor + { + /// + /// The default value for . + /// + public const float DefaultSigma = 3f; + + /// + /// Initializes a new instance of the class + /// + public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { } + + /// + /// Initializes a new instance of the class + /// + /// The radius value representing the size of the area to sample + public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { } + + /// + /// Initializes a new instance of the class + /// + /// The sigma value representing the weight of the blur + /// The radius value representing the size of the area to sample (this should be at least twice the sigma value) + public HlslGaussianBlurProcessor(float sigma, int radius) + { + Sigma = sigma; + Radius = radius; + } + + /// + /// Gets the sigma value representing the weight of the blur + /// + public float Sigma { get; } + + /// + /// Gets the radius defining the size of the area to sample + /// + public int Radius { get; } + + /// + public IImageProcessor CreatePixelSpecificProcessor(Image source, Rectangle sourceRectangle) where TPixel : struct, IPixel + { + return new HlslGaussianBlurProcessor(this, source, sourceRectangle); + } + } +} diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs new file mode 100644 index 00000000..274f3228 --- /dev/null +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -0,0 +1,207 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Primitives; + +namespace ComputeSharp.BokehBlur.Processors +{ + /// + /// Applies Gaussian blur processing to an image. + /// + /// The pixel format. + internal class HlslGaussianBlurProcessor : IImageProcessor where TPixel : struct, IPixel + { + /// + /// The 1D kernel to apply + /// + private readonly float[] Kernel; + + /// + /// The source instance to modify + /// + private readonly Image Source; + + /// + /// Initializes a new instance of the class + /// + /// The defining the processor parameters + /// The source for the current processor instance + /// The source area to process for the current processor instance + public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Image source, Rectangle sourceRectangle) + { + int kernelSize = definition.Radius * 2 + 1; + Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); + Source = source; + } + + /// + /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function + /// + private static float[] CreateGaussianBlurKernel(int size, float weight) + { + float[] kernel = new float[size]; + ref float rKernel = ref kernel[0]; + + float sum = 0F; + float midpoint = (size - 1) / 2F; + + for (int i = 0; i < size; i++) + { + float x = i - midpoint; + float gx = Gaussian(x, weight); + sum += gx; + Unsafe.Add(ref rKernel, i) = gx; + } + + // Normalize kernel so that the sum of all weights equals 1 + for (int i = 0; i < size; i++) + { + Unsafe.Add(ref rKernel, i) /= sum; + } + + return kernel; + } + + /// + /// Implementation of 1D Gaussian G(x) function + /// + /// The x provided to G(x) + /// The spread of the blur + /// The Gaussian G(x) + private static float Gaussian(float x, float sigma) + { + const float Numerator = 1.0f; + float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; + + float exponentNumerator = -x * x; + float exponentDenominator = 2 * sigma * sigma; + + float left = Numerator / denominator; + float right = MathF.Exp(exponentNumerator / exponentDenominator); + + return left * right; + } + + /// + public void Apply() + { + using Image source = Source.CloneAs(); + Span sourceSpan = MemoryMarshal.Cast(source.GetPixelSpan()); + + using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceSpan); + using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Size); + using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); + + ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); + ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); + + sourceBuffer.GetData(sourceSpan); + WriteResultData(source); + } + + /// + /// 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 + private void ApplyVerticalConvolution( + ReadWriteBuffer source, + ReadWriteBuffer target, + ReadOnlyBuffer kernel) + { + int height = Source.Height; + int width = Source.Width; + int maxY = height - 1; + int maxX = width - 1; + int kernelLength = kernel.Size; + + Gpu.Default.For(width, height, id => + { + Vector4 result = Vector4.Zero; + int radiusY = kernelLength >> 1; + int sourceOffsetColumnBase = id.X; + + for (int i = 0; i < kernelLength; i++) + { + int offsetY = Hlsl.Clamp(id.Y + i - radiusY, 0, maxY); + int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); + Vector4 color = source[offsetY * width + offsetX]; + + result += kernel[i] * color; + } + + int offsetXY = id.Y * width + id.X; + target[offsetXY] = 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 with the values for the current complex kernel + private void ApplyHorizontalConvolution( + ReadWriteBuffer source, + ReadWriteBuffer target, + ReadOnlyBuffer kernel) + { + int height = Source.Height; + int width = Source.Width; + int maxY = height - 1; + int maxX = width - 1; + int kernelLength = kernel.Size; + + Gpu.Default.For(width, height, id => + { + Vector4 result = Vector4.Zero; + 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 + offsetX; + Vector4 color = source[offsetXY]; + + result += kernel[i] * color; + } + + offsetXY = id.Y * width + id.X; + target[offsetXY] = result; + }); + } + + /// + /// Writes the final data to the source image + /// + /// The source to read from + private void WriteResultData(Image image) + { + int width = Source.Width; + + Parallel.For(0, Source.Height, y => + { + ref TPixel rPixel = ref Source.GetPixelRowSpan(y).GetPinnableReference(); + ref Vector4 r4 = ref MemoryMarshal.Cast(image.GetPixelRowSpan(y)).GetPinnableReference(); + + for (int x = 0; x < width; x++) + { + Unsafe.Add(ref rPixel, x).FromVector4(Unsafe.Add(ref r4, x)); + } + }); + } + + /// + public void Dispose() { } + } +} diff --git a/samples/ComputeSharp.ImageProcessing/Program.cs b/samples/ComputeSharp.ImageProcessing/Program.cs new file mode 100644 index 00000000..9aaac4e1 --- /dev/null +++ b/samples/ComputeSharp.ImageProcessing/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using System.Reflection; +using ComputeSharp.BokehBlur.Processors; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors; + +namespace ComputeSharp.ImageProcessing +{ + class Program + { + static void Main() + { + Console.WriteLine(">> Loading image"); + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg"); + using Image image = Image.Load(path); + + // Apply a series of processors and save the results + foreach (var effect in new (string Name, IImageProcessor Processor)[] + { + ("bokeh", new HlslBokehBlurProcessor(80, 2, 3)), + ("gaussian", new HlslGaussianBlurProcessor(80)) + }) + { + Console.WriteLine($">> Applying {effect.Name}"); + using Image copy = image.Clone(); + copy.Mutate(c => c.ApplyProcessor(effect.Processor)); + + Console.WriteLine($">> Saving {effect.Name} to disk"); + string targetPath = Path.Combine( + Path.GetRelativePath(Path.GetDirectoryName(path), @"..\..\..\"), + $"{Path.GetFileNameWithoutExtension(path)}_{effect.Name}{Path.GetExtension(path)}"); + copy.Save(targetPath); + } + } + } +} diff --git a/samples/ComputeSharp.BokehBlur/city.jpg b/samples/ComputeSharp.ImageProcessing/city.jpg similarity index 100% rename from samples/ComputeSharp.BokehBlur/city.jpg rename to samples/ComputeSharp.ImageProcessing/city.jpg