using System.Linq;
using CommunityToolkit.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Extensions;
///
/// A helper class for testing APIs.
///
public static class DeviceExtensions
{
///
/// Gets a instance matching a specified type.
///
/// The device to retrieve.
/// The device of the requested type.
/// Thrown when the requested device is not present.
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("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!;
}
}
///
/// Indicates a specific compute device.
///
public enum Device
{
///
/// A discrete device.
///
Discrete,
///
/// A WARP device (software fallback).
///
Warp
}