Fixed image processing sample

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-29 23:54:05 +01:00
1 親 f7d998ef30
コミット 400a2f4ab3
4ファイル変更
+183 -179

検索条件に一致するファイルはありません

+85 -2
ファイルの表示
@@ -1,4 +1,5 @@
using SixLabors.ImageSharp; using System.Numerics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors; using SixLabors.ImageSharp.Processing.Processors;
@@ -7,7 +8,7 @@ namespace ComputeSharp.BokehBlur.Processors
/// <summary> /// <summary>
/// Applies bokeh blur processing to the image. /// Applies bokeh blur processing to the image.
/// </summary> /// </summary>
public sealed class HlslBokehBlurProcessor : IImageProcessor public sealed partial class HlslBokehBlurProcessor : IImageProcessor
{ {
/// <summary> /// <summary>
/// The default radius used by the parameterless constructor. /// The default radius used by the parameterless constructor.
@@ -63,5 +64,87 @@ namespace ComputeSharp.BokehBlur.Processors
{ {
return new HlslBokehBlurProcessor<TPixel>(this, configuration, source, sourceRectangle); return new HlslBokehBlurProcessor<TPixel>(this, configuration, source, sourceRectangle);
} }
/// <summary>
/// Kernel for the vertical convolution pass.
/// </summary>
[AutoConstructor]
internal partial struct VerticalConvolutionProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public ReadOnlyBuffer<Vector4> source;
public ReadWriteBuffer<ComplexVector4> target;
public ReadOnlyBuffer<Complex64> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 real = Vector4.Zero;
Vector4 imaginary = Vector4.Zero;
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);
Vector4 color = source[offsetY * width + offsetX];
Complex64 factors = kernel[i];
real += factors.Real * color;
imaginary += factors.Imaginary * color;
}
int offsetXY = ids.Y * width + ids.X;
target[offsetXY].Real = real;
target[offsetXY].Imaginary = imaginary;
}
}
/// <summary>
/// Kernel for the horizontal convolution pass.
/// </summary>
[AutoConstructor]
internal partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public float z;
public float w;
public ReadWriteBuffer<ComplexVector4> source;
public ReadWriteBuffer<Vector4> target;
public ReadOnlyBuffer<Complex64> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 real = Vector4.Zero;
Vector4 imaginary = Vector4.Zero;
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);
var offsetXY = offsetY * width + offsetX;
ComplexVector4 source4 = source[offsetXY];
Complex64 factors = kernel[i];
real += factors.Real * source4.Real - factors.Imaginary * source4.Imaginary;
imaginary += factors.Real * source4.Imaginary + factors.Imaginary * source4.Real;
}
target[ids.Y * width + ids.X] += real * z + imaginary * w;
}
}
} }
} }
+2 -84
ファイルの表示
@@ -301,7 +301,7 @@ namespace ComputeSharp.BokehBlur.Processors
int height = Source.Height; int height = Source.Height;
int width = Source.Width; int width = Source.Width;
VerticalConvolutionProcessor shader = new( HlslBokehBlurProcessor.VerticalConvolutionProcessor shader = new(
width, width,
maxY: height - 1, maxY: height - 1,
maxX: width - 1, maxX: width - 1,
@@ -313,47 +313,6 @@ namespace ComputeSharp.BokehBlur.Processors
Gpu.Default.For(width, height, in shader); Gpu.Default.For(width, height, in shader);
} }
/// <summary>
/// Kernel for <see cref="ApplyVerticalConvolution"/>.
/// </summary>
[AutoConstructor]
private partial struct VerticalConvolutionProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public ReadOnlyBuffer<Vector4> source;
public ReadWriteBuffer<ComplexVector4> target;
public ReadOnlyBuffer<Complex64> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 real = Vector4.Zero;
Vector4 imaginary = Vector4.Zero;
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);
Vector4 color = source[offsetY * width + offsetX];
Complex64 factors = kernel[i];
real += factors.Real * color;
imaginary += factors.Imaginary * color;
}
int offsetXY = ids.Y * width + ids.X;
target[offsetXY].Real = real;
target[offsetXY].Imaginary = imaginary;
}
}
/// <summary> /// <summary>
/// Performs an horizontal 1D complex convolution with the specified parameters. /// Performs an horizontal 1D complex convolution with the specified parameters.
/// </summary> /// </summary>
@@ -373,7 +332,7 @@ namespace ComputeSharp.BokehBlur.Processors
int height = Source.Height; int height = Source.Height;
int width = Source.Width; int width = Source.Width;
HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( HlslBokehBlurProcessor.HorizontalConvolutionAndAccumulatePartialsProcessor shader = new(
width, width,
maxY: height - 1, maxY: height - 1,
maxX: width - 1, maxX: width - 1,
@@ -387,47 +346,6 @@ namespace ComputeSharp.BokehBlur.Processors
Gpu.Default.For(width, height, in shader); Gpu.Default.For(width, height, in shader);
} }
/// <summary>
/// Kernel for <see cref="ApplyHorizontalConvolutionAndAccumulatePartials"/>.
/// </summary>
[AutoConstructor]
private partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public float z;
public float w;
public ReadWriteBuffer<ComplexVector4> source;
public ReadWriteBuffer<Vector4> target;
public ReadOnlyBuffer<Complex64> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 real = Vector4.Zero;
Vector4 imaginary = Vector4.Zero;
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);
var offsetXY = offsetY * width + offsetX;
ComplexVector4 source4 = source[offsetXY];
Complex64 factors = kernel[i];
real += factors.Real * source4.Real - factors.Imaginary * source4.Imaginary;
imaginary += factors.Real * source4.Imaginary + factors.Imaginary * source4.Real;
}
target[ids.Y * width + ids.X] += real * z + imaginary * w;
}
}
/// <summary> /// <summary>
/// Applies the gamma correction/highlight to the input pixel buffer and returns an <see cref="IMemoryOwner{T}"/> instance with <see cref="Vector4"/> values. /// Applies the gamma correction/highlight to the input pixel buffer and returns an <see cref="IMemoryOwner{T}"/> instance with <see cref="Vector4"/> values.
/// </summary> /// </summary>
+77 -2
ファイルの表示
@@ -1,4 +1,5 @@
using SixLabors.ImageSharp; using System.Numerics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors; using SixLabors.ImageSharp.Processing.Processors;
@@ -7,7 +8,7 @@ namespace ComputeSharp.BokehBlur.Processors
/// <summary> /// <summary>
/// Defines Gaussian blur by a (Sigma, Radius) pair. /// Defines Gaussian blur by a (Sigma, Radius) pair.
/// </summary> /// </summary>
public sealed class HlslGaussianBlurProcessor : IImageProcessor public sealed partial class HlslGaussianBlurProcessor : IImageProcessor
{ {
/// <summary> /// <summary>
/// The default value for <see cref="Sigma"/>. /// The default value for <see cref="Sigma"/>.
@@ -52,5 +53,79 @@ namespace ComputeSharp.BokehBlur.Processors
{ {
return new HlslGaussianBlurProcessor<TPixel>(this, configuration, source, sourceRectangle); return new HlslGaussianBlurProcessor<TPixel>(this, configuration, source, sourceRectangle);
} }
/// <summary>
/// Kernel for the vertical convolution pass.
/// </summary>
[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<Vector4> source;
public readonly ReadWriteBuffer<Vector4> target;
public readonly ReadOnlyBuffer<float> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 result = Vector4.Zero;
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);
Vector4 color = source[offsetY * width + offsetX];
result += kernel[i] * color;
}
int offsetXY = ids.Y * width + ids.X;
target[offsetXY] = result;
}
}
/// <summary>
/// Kernel for the horizontal convolution pass.
/// </summary>
[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<Vector4> source;
public readonly ReadWriteBuffer<Vector4> target;
public readonly ReadOnlyBuffer<float> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 result = Vector4.Zero;
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];
result += kernel[i] * color;
}
offsetXY = ids.Y * width + ids.X;
target[offsetXY] = result;
}
}
} }
} }
+19 -91
ファイルの表示
@@ -13,7 +13,7 @@ namespace ComputeSharp.BokehBlur.Processors
/// Applies Gaussian blur processing to an image. /// Applies Gaussian blur processing to an image.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
internal class HlslGaussianBlurProcessor<TPixel> : ImageProcessor<TPixel> internal sealed class HlslGaussianBlurProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <summary> /// <summary>
@@ -124,51 +124,16 @@ namespace ComputeSharp.BokehBlur.Processors
int height = Source.Height; int height = Source.Height;
int width = Source.Width; int width = Source.Width;
Gpu.Default.For(width, height, new VerticalConvolutionProcessor HlslGaussianBlurProcessor.VerticalConvolutionProcessor shader = new(
{ width,
width = width, maxY: height - 1,
maxY = height - 1, maxX: width - 1,
maxX = width - 1, kernel.Length,
kernelLength = kernel.Length, source,
source = source, target,
target = target, kernel);
kernel = kernel
});
}
/// <summary> Gpu.Default.For(width, height, in shader);
/// Kernel for <see cref="ApplyVerticalConvolution"/>.
/// </summary>
private struct VerticalConvolutionProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public ReadWriteBuffer<Vector4> source;
public ReadWriteBuffer<Vector4> target;
public ReadOnlyBuffer<float> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 result = Vector4.Zero;
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);
Vector4 color = source[offsetY * width + offsetX];
result += kernel[i] * color;
}
int offsetXY = ids.Y * width + ids.X;
target[offsetXY] = result;
}
} }
/// <summary> /// <summary>
@@ -186,53 +151,16 @@ namespace ComputeSharp.BokehBlur.Processors
int height = Source.Height; int height = Source.Height;
int width = Source.Width; int width = Source.Width;
Gpu.Default.For(width, height, new HorizontalConvolutionProcessor HlslGaussianBlurProcessor.HorizontalConvolutionProcessor shader = new(
{ width,
width = width, maxY: height - 1,
maxY = height - 1, maxX: width - 1,
maxX = width - 1, kernel.Length,
kernelLength = kernel.Length, source,
source = source, target,
target = target, kernel);
kernel = kernel
});
}
/// <summary> Gpu.Default.For(width, height, in shader);
/// Kernel for <see cref="ApplyHorizontalConvolution"/>.
/// </summary>
private struct HorizontalConvolutionProcessor : IComputeShader
{
public int width;
public int maxY;
public int maxX;
public int kernelLength;
public ReadWriteBuffer<Vector4> source;
public ReadWriteBuffer<Vector4> target;
public ReadOnlyBuffer<float> kernel;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
Vector4 result = Vector4.Zero;
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];
result += kernel[i] * color;
}
offsetXY = ids.Y * width + ids.X;
target[offsetXY] = result;
}
} }
} }
} }