using System.Linq;
using Microsoft.Toolkit.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Extensions
{
///
/// A helper class for testing APIs.
///
public static class DeviceExtensions
{
///
/// The instance for the default discrete device.
///
private static readonly GraphicsDevice? DiscreteDevice;
///
/// The instance for the WARP device.
///
private static readonly GraphicsDevice WarpDevice;
///
/// Initializes and .
///
static DeviceExtensions()
{
GraphicsDevice defaultDevice = Gpu.Default;
if (defaultDevice.IsHardwareAccelerated)
{
DiscreteDevice = defaultDevice;
WarpDevice = Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First();
}
else
{
DiscreteDevice = null;
WarpDevice = defaultDevice;
}
}
///
/// 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 => DiscreteDevice,
Device.Warp => WarpDevice,
_ => ThrowHelper.ThrowArgumentException("Invalid device")
};
if (device is null)
{
Assert.Inconclusive();
}
return device!;
}
}
///
/// Indicates a specific compute device.
///
public enum Device
{
///
/// A discrete device.
///
Discrete,
///
/// A WARP device (software fallback).
///
Warp
}
}