From 17ea1a0f71198fdb8e795197b47a2ab91540f58c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 12:13:59 +0200 Subject: [PATCH] Added Gpu.QueryAll method to list all available GPUs --- src/ComputeSharp.Graphics/GraphicsDevice.cs | 39 +++++++++++++++++++-- src/ComputeSharp/Gpu.cs | 24 +++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 8aeb3983..ef34aa7e 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -2,6 +2,8 @@ using System.Threading; using SharpDX.Direct3D; using SharpDX.Direct3D12; +using SharpDX.DXGI; +using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp.Graphics { @@ -9,12 +11,21 @@ namespace ComputeSharp.Graphics { private readonly AutoResetEvent fenceEvent = new AutoResetEvent(false); - public GraphicsDevice(bool enableDebugLayer = 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) { #if DEBUG - if (enableDebugLayer) DebugInterface.Get().EnableDebugLayer(); + DebugInterface.Get().EnableDebugLayer(); #endif - NativeDevice = new Device(null, FeatureLevel); + NativeDevice = device; WavefrontSize = NativeDevice.D3D12Options1.WaveLaneCountMin; NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Compute)); @@ -41,6 +52,28 @@ namespace ComputeSharp.Graphics CopyCommandList.Close(); } + /// + /// Tries to create a new instance from the input object + /// + /// The input for the current GPU being targeted + /// The resulting value, in case of success + /// A value indicating whether or not the resulting device was created correctly + public static bool TryGetFromAdapter(Adapter adapter, out GraphicsDevice? result) + { + try + { + Device device = new Device(adapter, FeatureLevel); + result = new GraphicsDevice(device); + + return true; + } + catch + { + result = null; + return false; + } + } + /// /// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width") /// diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 7af82c16..4603fb0a 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,4 +1,7 @@ -using ComputeSharp.Graphics; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using ComputeSharp.Graphics; +using SharpDX.DXGI; namespace ComputeSharp { @@ -12,6 +15,23 @@ namespace ComputeSharp /// /// Gets the default instance for the current machine /// - public static GraphicsDevice Default => _Default ??= new GraphicsDevice(true); + public static GraphicsDevice Default => _Default ??= new GraphicsDevice(); + + /// + /// Gets a collection of all the supported graphics devices on the current machine + /// + /// The collection of objects with support for DX12.1 + [Pure] + public static IReadOnlyList QueryAll() + { + using Factory factory = new Factory1(); + List devices = new List(); + + foreach (Adapter adapter in factory.Adapters) + if (GraphicsDevice.TryGetFromAdapter(adapter, out GraphicsDevice? device)) + devices.Add(device!); + + return devices; + } } }