Browse Source

Update benchmarks and simplify color numeric bulk colormatrix

pull/2371/head
James Jackson-South 3 years ago
parent
commit
32d08ba566
  1. 7
      src/ImageSharp/Common/Helpers/ColorNumerics.cs
  2. 31
      tests/ImageSharp.Benchmarks/Bulk/ColorMatrixTransforms.cs
  3. 4
      tests/ImageSharp.Benchmarks/Bulk/FromRgba32Bytes.cs
  4. 2
      tests/ImageSharp.Benchmarks/Bulk/FromVector4.cs
  5. 2
      tests/ImageSharp.Benchmarks/Bulk/FromVector4_Rgb24.cs
  6. 2
      tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs
  7. 29
      tests/ImageSharp.Benchmarks/Bulk/PremultiplyVector4.cs
  8. 2
      tests/ImageSharp.Benchmarks/Bulk/Rgb24Bytes.cs
  9. 2
      tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs
  10. 2
      tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs
  11. 2
      tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs
  12. 2
      tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs
  13. 2
      tests/ImageSharp.Benchmarks/Bulk/ToRgba32Bytes.cs
  14. 2
      tests/ImageSharp.Benchmarks/Bulk/ToVector4.cs
  15. 2
      tests/ImageSharp.Benchmarks/Bulk/ToVector4_Bgra32.cs
  16. 2
      tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgb24.cs
  17. 2
      tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs
  18. 29
      tests/ImageSharp.Benchmarks/Bulk/UnPremultiplyVector4.cs
  19. 34
      tests/ImageSharp.Benchmarks/Bulk/Vector4Factory.cs
  20. 4
      tests/ImageSharp.Benchmarks/General/GetSetPixel.cs
  21. 34
      tests/ImageSharp.Benchmarks/General/Vectorization/ColorNumerics.cs
  22. 10
      tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj
  23. 2
      tests/ImageSharp.Benchmarks/Processing/BokehBlur.cs

7
src/ImageSharp/Common/Helpers/ColorNumerics.cs

@ -3,7 +3,6 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp;
@ -17,7 +16,7 @@ internal static class ColorNumerics
/// Vector for converting pixel to gray value as specified by
/// ITU-R Recommendation BT.709.
/// </summary>
private static readonly Vector4 Bt709 = new Vector4(.2126f, .7152f, .0722f, 0.0f);
private static readonly Vector4 Bt709 = new(.2126f, .7152f, .0722f, 0.0f);
/// <summary>
/// Convert a pixel value to grayscale using ITU-R Recommendation BT.709.
@ -167,11 +166,9 @@ internal static class ColorNumerics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Transform(Span<Vector4> vectors, ref ColorMatrix matrix)
{
ref Vector4 baseRef = ref MemoryMarshal.GetReference(vectors);
for (int i = 0; i < vectors.Length; i++)
{
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
ref Vector4 v = ref vectors[i];
Transform(ref v, ref matrix);
}
}

31
tests/ImageSharp.Benchmarks/Bulk/ColorMatrixTransforms.cs

@ -0,0 +1,31 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Processing;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public class ColorMatrixTransforms
{
private static readonly Vector4[] Vectors = Vector4Factory.CreateVectors();
[Benchmark(Baseline = true)]
public void Transform()
{
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
for (int i = 0; i < Vectors.Length; i++)
{
ref Vector4 input = ref Vectors[i];
ColorNumerics.Transform(ref input, ref matrix);
}
}
[Benchmark]
public void Transform_Span()
{
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
ColorNumerics.Transform(Vectors.AsSpan(), ref matrix);
}
}

4
tests/ImageSharp.Benchmarks/Color/Bulk/FromRgba32Bytes.cs → tests/ImageSharp.Benchmarks/Bulk/FromRgba32Bytes.cs

@ -8,7 +8,7 @@ using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public abstract class FromRgba32Bytes<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
@ -64,7 +64,7 @@ public abstract class FromRgba32Bytes<TPixel>
[Benchmark]
public void OptimizedBulk()
{
PixelOperations<TPixel>.Instance.FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count);
PixelOperations<TPixel>.Instance.FromRgba32Bytes(this.configuration, this.source.GetSpan(), this.destination.GetSpan(), this.Count);
}
}

2
tests/ImageSharp.Benchmarks/Color/Bulk/FromVector4.cs → tests/ImageSharp.Benchmarks/Bulk/FromVector4.cs

@ -12,7 +12,7 @@ using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.ShortCore31))]
public abstract class FromVector4<TPixel>

2
tests/ImageSharp.Benchmarks/Color/Bulk/FromVector4_Rgb24.cs → tests/ImageSharp.Benchmarks/Bulk/FromVector4_Rgb24.cs

@ -4,7 +4,7 @@
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.ShortMultiFramework))]
public class FromVector4_Rgb24 : FromVector4<Rgb24>

2
tests/ImageSharp.Benchmarks/Color/Bulk/Pad3Shuffle4Channel.cs → tests/ImageSharp.Benchmarks/Bulk/Pad3Shuffle4Channel.cs

@ -3,7 +3,7 @@
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
public class Pad3Shuffle4Channel

29
tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs → tests/ImageSharp.Benchmarks/Bulk/PremultiplyVector4.cs

@ -6,11 +6,11 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public class PremultiplyVector4
{
private static readonly Vector4[] Vectors = CreateVectors();
private static readonly Vector4[] Vectors = Vector4Factory.CreateVectors();
[Benchmark(Baseline = true)]
public void PremultiplyBaseline()
@ -46,29 +46,4 @@ public class PremultiplyVector4
source *= w;
source.W = w;
}
private static Vector4[] CreateVectors()
{
Random rnd = new(42);
return GenerateRandomVectorArray(rnd, 2048, 0, 1);
}
private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal)
{
Vector4[] values = new Vector4[length];
for (int i = 0; i < length; i++)
{
ref Vector4 v = ref values[i];
v.X = GetRandomFloat(rnd, minVal, maxVal);
v.Y = GetRandomFloat(rnd, minVal, maxVal);
v.Z = GetRandomFloat(rnd, minVal, maxVal);
v.W = GetRandomFloat(rnd, minVal, maxVal);
}
return values;
}
private static float GetRandomFloat(Random rnd, float minVal, float maxVal)
=> ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal;
}

2
tests/ImageSharp.Benchmarks/Color/Bulk/Rgb24Bytes.cs → tests/ImageSharp.Benchmarks/Bulk/Rgb24Bytes.cs

@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public abstract class Rgb24Bytes<TPixel>
where TPixel : unmanaged, IPixel<TPixel>

2
tests/ImageSharp.Benchmarks/Color/Bulk/Shuffle3Channel.cs → tests/ImageSharp.Benchmarks/Bulk/Shuffle3Channel.cs

@ -3,7 +3,7 @@
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
public class Shuffle3Channel

2
tests/ImageSharp.Benchmarks/Color/Bulk/Shuffle4Slice3Channel.cs → tests/ImageSharp.Benchmarks/Bulk/Shuffle4Slice3Channel.cs

@ -3,7 +3,7 @@
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
public class Shuffle4Slice3Channel

2
tests/ImageSharp.Benchmarks/Color/Bulk/ShuffleByte4Channel.cs → tests/ImageSharp.Benchmarks/Bulk/ShuffleByte4Channel.cs

@ -3,7 +3,7 @@
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
public class ShuffleByte4Channel

2
tests/ImageSharp.Benchmarks/Color/Bulk/ShuffleFloat4Channel.cs → tests/ImageSharp.Benchmarks/Bulk/ShuffleFloat4Channel.cs

@ -4,7 +4,7 @@
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Tests;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.HwIntrinsics_SSE_AVX))]
public class ShuffleFloat4Channel

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToRgba32Bytes.cs → tests/ImageSharp.Benchmarks/Bulk/ToRgba32Bytes.cs

@ -8,7 +8,7 @@ using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public abstract class ToRgba32Bytes<TPixel>
where TPixel : unmanaged, IPixel<TPixel>

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs → tests/ImageSharp.Benchmarks/Bulk/ToVector4.cs

@ -9,7 +9,7 @@ using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public abstract class ToVector4<TPixel>
where TPixel : unmanaged, IPixel<TPixel>

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4_Bgra32.cs → tests/ImageSharp.Benchmarks/Bulk/ToVector4_Bgra32.cs

@ -6,7 +6,7 @@ using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.ShortMultiFramework))]
public class ToVector4_Bgra32 : ToVector4<Bgra32>

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4_Rgb24.cs → tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgb24.cs

@ -6,7 +6,7 @@ using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.ShortMultiFramework))]
public class ToVector4_Rgb24 : ToVector4<Rgb24>

2
tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4_Rgba32.cs → tests/ImageSharp.Benchmarks/Bulk/ToVector4_Rgba32.cs

@ -9,7 +9,7 @@ using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
[Config(typeof(Config.ShortCore31))]
public class ToVector4_Rgba32 : ToVector4<Rgba32>

29
tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs → tests/ImageSharp.Benchmarks/Bulk/UnPremultiplyVector4.cs

@ -6,11 +6,11 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
public class UnPremultiplyVector4
{
private static readonly Vector4[] Vectors = CreateVectors();
private static readonly Vector4[] Vectors = Vector4Factory.CreateVectors();
[Benchmark(Baseline = true)]
public void UnPremultiplyBaseline()
@ -47,29 +47,4 @@ public class UnPremultiplyVector4
source /= w;
source.W = w;
}
private static Vector4[] CreateVectors()
{
Random rnd = new(42);
return GenerateRandomVectorArray(rnd, 2048, 0, 1);
}
private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal)
{
Vector4[] values = new Vector4[length];
for (int i = 0; i < length; i++)
{
ref Vector4 v = ref values[i];
v.X = GetRandomFloat(rnd, minVal, maxVal);
v.Y = GetRandomFloat(rnd, minVal, maxVal);
v.Z = GetRandomFloat(rnd, minVal, maxVal);
v.W = GetRandomFloat(rnd, minVal, maxVal);
}
return values;
}
private static float GetRandomFloat(Random rnd, float minVal, float maxVal)
=> ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal;
}

34
tests/ImageSharp.Benchmarks/Bulk/Vector4Factory.cs

@ -0,0 +1,34 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.Benchmarks.Bulk;
internal static class Vector4Factory
{
public static Vector4[] CreateVectors(int length = 2048, int min = 0, int max = 1)
{
Random rnd = new(42);
return GenerateRandomVectorArray(rnd, length, min, max);
}
private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal)
{
Vector4[] values = new Vector4[length];
for (int i = 0; i < length; i++)
{
ref Vector4 v = ref values[i];
v.X = GetRandomFloat(rnd, minVal, maxVal);
v.Y = GetRandomFloat(rnd, minVal, maxVal);
v.Z = GetRandomFloat(rnd, minVal, maxVal);
v.W = GetRandomFloat(rnd, minVal, maxVal);
}
return values;
}
private static float GetRandomFloat(Random rnd, float minVal, float maxVal)
=> (float)rnd.NextDouble() * (maxVal - minVal) + minVal;
}

4
tests/ImageSharp.Benchmarks/General/GetSetPixel.cs

@ -12,7 +12,7 @@ public class GetSetPixel
[Benchmark(Baseline = true, Description = "System.Drawing GetSet pixel")]
public System.Drawing.Color GetSetSystemDrawing()
{
using var source = new Bitmap(400, 400);
using Bitmap source = new(400, 400);
source.SetPixel(200, 200, System.Drawing.Color.White);
return source.GetPixel(200, 200);
}
@ -20,7 +20,7 @@ public class GetSetPixel
[Benchmark(Description = "ImageSharp GetSet pixel")]
public Rgba32 GetSetImageSharp()
{
using var image = new Image<Rgba32>(400, 400);
using Image<Rgba32> image = new(400, 400);
image[200, 200] = Color.White;
return image[200, 200];
}

34
tests/ImageSharp.Benchmarks/General/Vectorization/ColorNumerics.cs

@ -1,34 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Processing;
namespace SixLabors.ImageSharp.Benchmarks.General.Vectorization;
[Config(typeof(Config.MultiFramework))]
public class ColorNumerics
{
private static Vector4[] input =
{
new(.5F), new(.5F), new(.5F), new(.5F), new(.5F), new(.5F), new(.5F),
};
[Benchmark]
public Vector4 Transform()
{
Vector4 input = new(.5F);
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
ImageSharp.ColorNumerics.Transform(ref input, ref matrix);
return input;
}
[Benchmark]
public void Transform_Span()
{
ColorMatrix matrix = KnownFilterMatrices.CreateHueFilter(45F);
ImageSharp.ColorNumerics.Transform(input.AsSpan(), ref matrix);
}
}

10
tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj

@ -11,7 +11,13 @@
<IsTestProject>false</IsTestProject>
<Configurations>Debug;Release</Configurations>
<!-- Uncomment this to run benchmarks depending on Colorful or Pfim (colorspaces and TGA): -->
<!--<SignAssembly>false</SignAssembly>-->
<!--<SignAssembly>false</SignAssembly>-->
</PropertyGroup>
<PropertyGroup>
<!--BenchmarkDotNet cannot run static benchmarks-->
<!--Mark members as static-->
<NoWarn>CA1822</NoWarn>
</PropertyGroup>
<Choose>
@ -53,7 +59,7 @@
<Compile Remove="Codecs\Jpeg\BlockOperations\**" />
<Compile Remove="Codecs\Jpeg\YCbCrColorConversion.cs" />
<Compile Remove="Codecs\Jpeg\DecodeJpegParseStreamOnly.cs" />
<Compile Remove="Color\Bulk\**" />
<Compile Remove="Bulk\**" />
<Compile Remove="Color\RgbToYCbCr.cs" />
<Compile Remove="Color\YCbCrToRgb.cs" />
<Compile Remove="General\Vectorization\**" />

2
tests/ImageSharp.Benchmarks/Processing/BokehBlur.cs

@ -13,7 +13,7 @@ public class BokehBlur
[Benchmark]
public void Blur()
{
using var image = new Image<Rgba32>(Configuration.Default, 400, 400, Color.White);
using Image<Rgba32> image = new(Configuration.Default, 400, 400, Color.White);
image.Mutate(c => c.BokehBlur());
}
}

Loading…
Cancel
Save