diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index ef34aa7e..99a86fb7 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -11,16 +11,11 @@ namespace ComputeSharp.Graphics { private readonly AutoResetEvent fenceEvent = new AutoResetEvent(false); - /// - /// Creates a new instance for the default GPU on the current machine - /// - public GraphicsDevice() : this(new Device(null, FeatureLevel)) { } - /// /// Creates a new instance for the input /// /// The to use for the new instance - private GraphicsDevice(Device device) + public GraphicsDevice(Device device) { #if DEBUG DebugInterface.Get().EnableDebugLayer(); diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 4603fb0a..960b115c 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,7 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.Linq; using ComputeSharp.Graphics; using SharpDX.DXGI; +using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp { @@ -10,26 +13,59 @@ namespace ComputeSharp /// public static class Gpu { + /// + /// Gets whether or not the APIs can be used on the current machine (ie. if there is at least a supported GPU device) + /// + public static bool IsSupported { get; } = (_Devices = QueryAllSupportedDevices()).Any(); + private static GraphicsDevice _Default; /// /// Gets the default instance for the current machine /// - public static GraphicsDevice Default => _Default ??= new GraphicsDevice(); + public static GraphicsDevice Default + { + get + { + if (!IsSupported) throw new NotSupportedException("There isn't a supported GPU device on the current machine"); + + return _Default ??= new GraphicsDevice(_Devices.First()); + } + } /// - /// Gets a collection of all the supported graphics devices on the current machine + /// Enumerates all the available objects on the current machine /// - /// The collection of objects with support for DX12.1 + /// A sequence of supported objects that can be used to run compute shaders [Pure] - public static IReadOnlyList QueryAll() + public static IEnumerable EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device)); + + // The loaded collection of supported devices + private static IReadOnlyList _Devices; + + /// + /// Gets a collection of all the supported devices on the current machine + /// + /// The collection of objects with support for DX12.1 + [Pure] + private static IReadOnlyList QueryAllSupportedDevices() { using Factory factory = new Factory1(); - List devices = new List(); + List devices = new List(); foreach (Adapter adapter in factory.Adapters) - if (GraphicsDevice.TryGetFromAdapter(adapter, out GraphicsDevice? device)) - devices.Add(device!); + { + if (adapter.Description.DedicatedVideoMemory == 0) continue; + try + { + Device device = new Device(adapter, GraphicsDevice.FeatureLevel); + devices.Add(device); + } + catch + { + // Unsupported device + } + } return devices; }