Add unit test for 3D readonly wrapper sampling

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-17 19:52:43 +01:00
1 親 6779bb5284
コミット 1b5a96aaf5
4ファイル変更
+88 -4

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

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 2.4 MiB

+59 -3
ファイルの表示
@@ -9,7 +9,6 @@ using ComputeSharp.Tests.Helpers;
using Microsoft.Toolkit.HighPerformance;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
namespace ComputeSharp.Tests;
@@ -905,7 +904,7 @@ public partial class ComputeContextTests
using (var context = device.Get().CreateComputeContext())
{
context.Transition(source, ResourceState.ReadOnly);
context.ForEach(destination, new LinearSamplingPixelShader(source.AsReadOnly()));
context.ForEach(destination, new LinearSampling2DPixelShader(source.AsReadOnly()));
context.Transition(source, ResourceState.ReadWrite);
}
@@ -914,6 +913,50 @@ public partial class ComputeContextTests
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}
[CombinatorialTestMethod]
[AllDevices]
public void Transition_ReadWriteTexture3D(Device device)
{
_ = device.Get();
string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets");
string originalPath = Path.Combine(assetsPath, "CityWith1920x1280Resizing.png");
string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png");
var imageInfo = Image.Identify(originalPath);
using var sampled = Image.Load<ImageSharpRgba32>(Path.Combine(assetsPath, sampledPath));
using ReadWriteTexture3D<Rgba32, float4> source = device.Get().AllocateReadWriteTexture3D<Rgba32, float4>(imageInfo.Width, imageInfo.Height, 3);
using (UploadTexture2D<Rgba32> upload2D = device.Get().LoadUploadTexture2D<Rgba32>(originalPath))
using (UploadTexture3D<Rgba32> upload3D = device.Get().AllocateUploadTexture3D<Rgba32>(upload2D.Width, upload2D.Height, 2))
{
for (int z = 0; z < upload3D.Depth; z++)
{
upload2D.View.CopyTo(upload3D.View.GetDepthView(z));
}
upload3D.CopyTo(source);
}
using ReadWriteTexture3D<Rgba32, float4> destination = device.Get().AllocateReadWriteTexture3D<Rgba32, float4>(sampled.Width, sampled.Height, 2);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(source, ResourceState.ReadOnly);
context.For(destination.Width, destination.Height, destination.Depth, new LinearSampling3DComputeShader(source.AsReadOnly(), destination));
context.Transition(source, ResourceState.ReadWrite);
}
for (int z = 0; z < destination.Depth; z++)
{
using var processed = destination.ToImage<Rgba32, ImageSharpRgba32>(depth: z);
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.000012f);
}
}
[CombinatorialTestMethod]
[AllDevices]
[ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)]
@@ -1070,7 +1113,7 @@ public partial class ComputeContextTests
}
[AutoConstructor]
internal readonly partial struct LinearSamplingPixelShader : IPixelShader<float4>
internal readonly partial struct LinearSampling2DPixelShader : IPixelShader<float4>
{
public readonly IReadOnlyTexture2D<float4> source;
@@ -1080,4 +1123,17 @@ public partial class ComputeContextTests
return source[ThreadIds.Normalized.XY];
}
}
[AutoConstructor]
internal readonly partial struct LinearSampling3DComputeShader : IComputeShader
{
public readonly IReadOnlyTexture3D<float4> source;
public readonly IReadWriteTexture3D<float4> destination;
/// <inheritdoc/>
public void Execute()
{
destination[ThreadIds.XYZ] = source[ThreadIds.Normalized.XYZ];
}
}
}
+4 -1
ファイルの表示
@@ -48,7 +48,10 @@
<ItemGroup>
<None Include="..\..\samples\ComputeSharp.ImageProcessing\city.jpg" Link="Imaging\city.jpg" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\..\samples\ComputeSharp.SwapChain\Textures\RustyMetal.png" Link="Shaders\Textures\RustyMetal.png" CopyToOutputDirectory="PreserveNewest" />
<None Update="Assets/CityAfter1024x1024Sampling.png">
<None Update="Assets\CityAfter1024x1024Sampling.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\CityWith1920x1280Resizing.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\HelloWorld.png">
+25
ファイルの表示
@@ -36,4 +36,29 @@ public static class ImagingExtensions
return image;
}
/// <summary>
/// Creates a new <see cref="Image{TPixel}"/> instance with the specified texture data.
/// </summary>
/// <typeparam name="TFrom">The input pixel format used in the texture.</typeparam>
/// <typeparam name="TTo">The target pixel format for the returned image.</typeparam>
/// <param name="texture">The source <see cref="Texture3D{T}"/> instance to read data from.</param>
/// <param name="depth">The depth layer to read the image from.</param>
/// <returns>An image with the data from the input texture at a specified depth layer.</returns>
public static unsafe Image<TTo> ToImage<TFrom, TTo>(this Texture3D<TFrom> texture, int depth)
where TFrom : unmanaged
where TTo : unmanaged, IPixel<TTo>
{
Guard.IsEqualTo(sizeof(TTo), sizeof(TFrom), nameof(TTo));
Image<TTo> image = new(texture.Width, texture.Height);
Assert.IsTrue(image.TryGetSinglePixelSpan(out Span<TTo> span));
Span<TFrom> pixels = MemoryMarshal.Cast<TTo, TFrom>(span);
texture.CopyTo(pixels, 0, 0, depth, texture.Width, texture.Height, 1);
return image;
}
}