Add unit tests for ComputeContext.Transition API

Also test ReadWriteTexture[2D|3D]<T, TPixel>.AsReadOnly()
このコミットが含まれているのは:
Sergio Pedri committed 2022-01-17 19:52:43 +01:00
1 親 9801b500bb
コミット 40d3cd0182
3ファイル変更
+169 -12

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

+43
ファイルの表示
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
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;
@@ -883,6 +888,32 @@ public partial class ComputeContextTests
await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => Task.Run(() => TestAsync(device))));
}
[CombinatorialTestMethod]
[AllDevices]
public void Transition_ReadWriteTexture2D(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 ReadWriteTexture2D<Rgba32, float4> source = device.Get().LoadReadWriteTexture2D<Rgba32, float4>(Path.Combine(imagingPath, "city.jpg"));
using ReadWriteTexture2D<Rgba32, float4> destination = device.Get().AllocateReadWriteTexture2D<Rgba32, float4>(sampled.Width, sampled.Height);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(source, ResourceState.ReadOnly);
context.ForEach(destination, new LinearSamplingPixelShader(source.AsReadOnly()));
context.Transition(source, ResourceState.ReadWrite);
}
using var processed = destination.ToImage<Rgba32, ImageSharpRgba32>();
ImagingTests.TolerantImageComparer.AssertEqual(sampled, processed, 0.0000017f);
}
[CombinatorialTestMethod]
[AllDevices]
[ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)]
@@ -1037,4 +1068,16 @@ public partial class ComputeContextTests
return color;
}
}
[AutoConstructor]
internal readonly partial struct LinearSamplingPixelShader : IPixelShader<float4>
{
public readonly IReadOnlyTexture2D<float4> source;
/// <inheritdoc/>
public float4 Execute()
{
return source[ThreadIds.Normalized.XY];
}
}
}
+65 -8
ファイルの表示
@@ -4,6 +4,7 @@ using System.Reflection;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using ComputeSharp.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
@@ -46,14 +47,7 @@ public partial class Texture2DTests
}
}
try
{
new Action<Device, Type>(Test<Rgba32, float4>).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType });
}
catch (TargetInvocationException e) when (e.InnerException is not null)
{
throw e.InnerException;
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device, textureType);
}
[CombinatorialTestMethod]
@@ -120,4 +114,67 @@ public partial class Texture2DTests
return texture[ThreadIds.Normalized.XY];
}
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
public void AsReadOnly_MultipleCalls_SameInstance(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
using ReadWriteTexture2D<T, TPixel> texture = device.Get().AllocateReadWriteTexture2D<T, TPixel>(64, 64);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(texture, ResourceState.ReadOnly);
IReadOnlyTexture2D<TPixel> wrapper1 = texture.AsReadOnly();
Assert.IsNotNull(wrapper1);
IReadOnlyTexture2D<TPixel> wrapper2 = texture.AsReadOnly();
Assert.IsNotNull(wrapper2);
Assert.AreSame(wrapper1, wrapper2);
context.Transition(texture, ResourceState.ReadWrite);
}
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
[ExpectedException(typeof(InvalidOperationException))]
public void AsReadOnly_InvalidState_ThrowInvalidOperationException(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
using ReadWriteTexture2D<T, TPixel> texture = device.Get().AllocateReadWriteTexture2D<T, TPixel>(64, 64);
_ = texture.AsReadOnly();
Assert.Fail();
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
}
+61 -4
ファイルの表示
@@ -3,6 +3,7 @@ using System.Reflection;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using ComputeSharp.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests;
@@ -44,13 +45,69 @@ public partial class Texture3DTests
}
}
try
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device, textureType);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
public void AsReadOnly_MultipleCalls_SameInstance(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
new Action<Device, Type>(Test<Rgba32, float4>).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType });
using ReadWriteTexture3D<T, TPixel> texture = device.Get().AllocateReadWriteTexture3D<T, TPixel>(32, 32, 2);
using (var context = device.Get().CreateComputeContext())
{
context.Transition(texture, ResourceState.ReadOnly);
IReadOnlyTexture3D<TPixel> wrapper1 = texture.AsReadOnly();
Assert.IsNotNull(wrapper1);
IReadOnlyTexture3D<TPixel> wrapper2 = texture.AsReadOnly();
Assert.IsNotNull(wrapper2);
Assert.AreSame(wrapper1, wrapper2);
context.Transition(texture, ResourceState.ReadWrite);
}
}
catch (TargetInvocationException e) when (e.InnerException is not null)
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(Bgra32), typeof(float4))]
[Data(typeof(R16), typeof(float))]
[Data(typeof(R8), typeof(float))]
[Data(typeof(Rg16), typeof(float2))]
[Data(typeof(Rg32), typeof(float2))]
[Data(typeof(Rgba32), typeof(float4))]
[Data(typeof(Rgba64), typeof(float4))]
[ExpectedException(typeof(InvalidOperationException))]
public void AsReadOnly_InvalidState_ThrowInvalidOperationException(Device device, Type t, Type tPixel)
{
static void Test<T, TPixel>(Device device)
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
throw e.InnerException;
using ReadWriteTexture3D<T, TPixel> texture = device.Get().AllocateReadWriteTexture3D<T, TPixel>(32, 32, 2);
_ = texture.AsReadOnly();
Assert.Fail();
}
TestHelper.Run(Test<Rgba32, float4>, t, tPixel, device);
}
}