Switch to cached devices in ComputeSharp tests

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:13 +02:00
1 親 37823f366c
コミット 748f14d7fd
3ファイル変更
+78 -3

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

-1
ファイルの表示
@@ -37,7 +37,6 @@
<Compile Include="..\ComputeSharp.Tests\Attributes\DataAttribute.cs" Link="Attributes\DataAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\DeviceAttribute.cs" Link="Attributes\DeviceAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Attributes\ResourceAttribute.cs" Link="Attributes\ResourceAttribute.cs" />
<Compile Include="..\ComputeSharp.Tests\Extensions\DeviceExtensions.cs" Link="Extensions\DeviceExtensions.cs" />
<Compile Include="..\ComputeSharp.Tests\Extensions\GraphicsDeviceExtensions.cs" Link="Extensions\GraphicsDeviceExtensions.cs" />
</ItemGroup>
+57
ファイルの表示
@@ -0,0 +1,57 @@
using System.Linq;
using CommunityToolkit.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Extensions;
/// <summary>
/// A helper class for testing <see cref="GraphicsDevice"/> APIs.
/// </summary>
public static class DeviceExtensions
{
/// <summary>
/// Gets a <see cref="GraphicsDevice"/> instance matching a specified type.
/// </summary>
/// <param name="type">The device to retrieve.</param>
/// <returns>The device of the requested type.</returns>
/// <exception cref="AssertInconclusiveException">Thrown when the requested device is not present.</exception>
public static GraphicsDevice Get(this Device type)
{
GraphicsDevice? device = type switch
{
Device.Discrete => GraphicsDevice.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
Device.Warp => GraphicsDevice.QueryDevices(info => !info.IsHardwareAccelerated).FirstOrDefault(),
_ => ThrowHelper.ThrowArgumentException<GraphicsDevice>("Invalid device.")
};
if (device is null)
{
// If the device is null, fail only if the device was WARP (as it should always be available).
// If a dedicated device was requested instead, just report the test as inconclusive (no GPU).
switch (type)
{
case Device.Discrete: Assert.Inconclusive(); break;
case Device.Warp: Assert.Fail(); break;
}
}
return device!;
}
}
/// <summary>
/// Indicates a specific compute device.
/// </summary>
public enum Device
{
/// <summary>
/// A discrete device.
/// </summary>
Discrete,
/// <summary>
/// A WARP device (software fallback).
/// </summary>
Warp
}
+21 -2
ファイルの表示
@@ -9,6 +9,25 @@ namespace ComputeSharp.Tests.Extensions;
/// </summary>
public static class DeviceExtensions
{
/// <summary>
/// The <see cref="GraphicsDevice"/> instance for the discrete device.
/// </summary>
private static readonly GraphicsDevice? DiscreteDevice;
/// <summary>
/// The <see cref="GraphicsDevice"/> instance for the WARP device.
/// </summary>
private static readonly GraphicsDevice? WarpDevice;
/// <summary>
/// Initializes <see cref="DiscreteDevice"/> and <see cref="WarpDevice"/>.
/// </summary>
static DeviceExtensions()
{
DiscreteDevice = GraphicsDevice.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault();
WarpDevice = GraphicsDevice.QueryDevices(info => !info.IsHardwareAccelerated).FirstOrDefault();
}
/// <summary>
/// Gets a <see cref="GraphicsDevice"/> instance matching a specified type.
/// </summary>
@@ -19,8 +38,8 @@ public static class DeviceExtensions
{
GraphicsDevice? device = type switch
{
Device.Discrete => GraphicsDevice.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(),
Device.Warp => GraphicsDevice.QueryDevices(info => !info.IsHardwareAccelerated).FirstOrDefault(),
Device.Discrete => DiscreteDevice,
Device.Warp => WarpDevice,
_ => ThrowHelper.ThrowArgumentException<GraphicsDevice>("Invalid device.")
};