From 748f14d7fdabb1c9f780812ee777de3e6dd021bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 29 Jul 2022 01:51:06 +0200 Subject: [PATCH] Switch to cached devices in ComputeSharp tests --- .../ComputeSharp.Tests.DeviceLost.csproj | 1 - .../Extensions/DeviceExtensions.cs | 57 +++++++++++++++++++ .../Extensions/DeviceExtensions.cs | 23 +++++++- 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 tests/ComputeSharp.Tests.DeviceLost/Extensions/DeviceExtensions.cs diff --git a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj index ebc7641b..24bb2044 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj +++ b/tests/ComputeSharp.Tests.DeviceLost/ComputeSharp.Tests.DeviceLost.csproj @@ -37,7 +37,6 @@ - diff --git a/tests/ComputeSharp.Tests.DeviceLost/Extensions/DeviceExtensions.cs b/tests/ComputeSharp.Tests.DeviceLost/Extensions/DeviceExtensions.cs new file mode 100644 index 00000000..983083bb --- /dev/null +++ b/tests/ComputeSharp.Tests.DeviceLost/Extensions/DeviceExtensions.cs @@ -0,0 +1,57 @@ +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 +} diff --git a/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs index 983083bb..07a3b3ac 100644 --- a/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs @@ -9,6 +9,25 @@ namespace ComputeSharp.Tests.Extensions; /// public static class DeviceExtensions { + /// + /// The instance for the discrete device. + /// + private static readonly GraphicsDevice? DiscreteDevice; + + /// + /// The instance for the WARP device. + /// + private static readonly GraphicsDevice? WarpDevice; + + /// + /// Initializes and . + /// + static DeviceExtensions() + { + DiscreteDevice = GraphicsDevice.QueryDevices(info => info.IsHardwareAccelerated).FirstOrDefault(); + WarpDevice = GraphicsDevice.QueryDevices(info => !info.IsHardwareAccelerated).FirstOrDefault(); + } + /// /// Gets a instance matching a specified type. /// @@ -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("Invalid device.") };