Added compute/pixel texture sampling test
このコミットが含まれているのは:
1 親
3578f00eb9
コミット
828aa8cc01
6ファイル変更
+136
-2
検索条件に一致するファイルはありません
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 1.3 MiB |
@@ -40,6 +40,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\samples\ComputeSharp.ImageProcessing\city.jpg" Link="Imaging\city.jpg" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Update="Assets/CityAfter1024x1024Sampling.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\ColorfulInfinity.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
@@ -8,7 +8,7 @@ using ComputeSharp.Resources;
|
||||
namespace ComputeSharp.Tests.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper class for testing <see cref="Buffer{T}"/> APIs.
|
||||
/// A helper class for testing resource types.
|
||||
/// </summary>
|
||||
public static class GraphicsDeviceExtensions
|
||||
{
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SixLabors.ImageSharp;
|
||||
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
|
||||
|
||||
namespace ComputeSharp.Tests.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper class for loading images into resources.
|
||||
/// </summary>
|
||||
public static class ImagingExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified texture data.
|
||||
/// </summary>
|
||||
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
|
||||
/// <param name="path">The path of the texture to load.</param>
|
||||
/// <returns>A texture with the data from the image at the specified path.</returns>
|
||||
[Pure]
|
||||
public static ReadOnlyTexture2D<Rgba32, Float4> LoadTexture(this GraphicsDevice device, string path)
|
||||
{
|
||||
using Image<ImageSharpRgba32> image = Image.Load<ImageSharpRgba32>(path);
|
||||
|
||||
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span));
|
||||
|
||||
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
|
||||
|
||||
return device.AllocateReadOnlyTexture2D<Rgba32, Float4>(pixels, image.Width, image.Height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Image{TPixel}"/> instance with the specified texture data.
|
||||
/// </summary>
|
||||
/// <param name="texture">The source <see cref="ReadWriteTexture2D{T, TPixel}"/> instance to read data from.</param>
|
||||
/// <returns>An image with the data from the input texture.</returns>
|
||||
[Pure]
|
||||
public static Image<ImageSharpRgba32> ToImage(this ReadWriteTexture2D<Rgba32, Float4> texture)
|
||||
{
|
||||
Image<ImageSharpRgba32> image = new(texture.Width, texture.Height);
|
||||
|
||||
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span));
|
||||
|
||||
Span<Rgba32> pixels = MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span);
|
||||
|
||||
texture.CopyTo(pixels);
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,11 +75,22 @@ namespace ComputeSharp.Tests
|
||||
/// <param name="expectedPath">The path to the reference image.</param>
|
||||
/// <param name="actualPath">The path to the expected image.</param>
|
||||
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
|
||||
public static void AssertEqual(string expectedPath, string actualPath, float threshold)
|
||||
public static void AssertEqual(string expectedPath, string actualPath, float threshold)
|
||||
{
|
||||
using var expected = Image.Load<ImageSharpRgba32>(expectedPath);
|
||||
using var actual = Image.Load<ImageSharpRgba32>(actualPath);
|
||||
|
||||
AssertEqual(expected, actual, threshold);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that two images are equal.
|
||||
/// </summary>
|
||||
/// <param name="expected">The reference image.</param>
|
||||
/// <param name="actual">The expected image.</param>
|
||||
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
|
||||
public static void AssertEqual(Image<ImageSharpRgba32> expected, Image<ImageSharpRgba32> actual, float threshold)
|
||||
{
|
||||
if (expected.Size() != actual.Size())
|
||||
{
|
||||
Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.__Internals;
|
||||
@@ -6,6 +7,8 @@ using ComputeSharp.Resources;
|
||||
using ComputeSharp.Tests.Attributes;
|
||||
using ComputeSharp.Tests.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SixLabors.ImageSharp;
|
||||
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -61,5 +64,70 @@ namespace ComputeSharp.Tests
|
||||
throw e.InnerException;
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SampleFromSourceTexture_ComputeShader(Device device)
|
||||
{
|
||||
_ = device.Get();
|
||||
|
||||
string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets");
|
||||
|
||||
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"));
|
||||
|
||||
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg"));
|
||||
using ReadWriteTexture2D<Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(sampled.Width, sampled.Height);
|
||||
|
||||
device.Get().For(sampled.Width, sampled.Height, new SamplingComputeShader(source, destination));
|
||||
|
||||
using var processed = destination.ToImage();
|
||||
|
||||
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public readonly partial struct SamplingComputeShader : IComputeShader
|
||||
{
|
||||
public readonly IReadOnlyTexture2D<Float4> source;
|
||||
public readonly IReadWriteTexture2D<Float4> destination;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
destination[ThreadIds.XY] = source[ThreadIds.Normalized.XY];
|
||||
}
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void SampleFromSourceTexture_PixelShader(Device device)
|
||||
{
|
||||
_ = device.Get();
|
||||
|
||||
string imagingPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging");
|
||||
string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets");
|
||||
|
||||
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"));
|
||||
|
||||
using ReadOnlyTexture2D<Rgba32, Float4> source = device.Get().LoadTexture(Path.Combine(imagingPath, "city.jpg"));
|
||||
using ReadWriteTexture2D<Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(sampled.Width, sampled.Height);
|
||||
|
||||
device.Get().ForEach(destination, new SamplingPixelShader(source));
|
||||
|
||||
using var processed = destination.ToImage();
|
||||
|
||||
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.00000086f);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public readonly partial struct SamplingPixelShader : IPixelShader<Float4>
|
||||
{
|
||||
public readonly IReadOnlyTexture2D<Float4> texture;
|
||||
|
||||
public Float4 Execute()
|
||||
{
|
||||
return texture[ThreadIds.Normalized.XY];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする