diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs
index 8aeb3983..0a6adde2 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,15 @@ namespace ComputeSharp.Graphics
{
private readonly AutoResetEvent fenceEvent = new AutoResetEvent(false);
- public GraphicsDevice(bool enableDebugLayer = false)
+ ///
+ /// Creates a new instance for the input
+ ///
+ /// The to use for the new instance
+ /// The available info for the new instance
+ public GraphicsDevice(Device device, AdapterDescription description)
{
-#if DEBUG
- if (enableDebugLayer) DebugInterface.Get().EnableDebugLayer();
-#endif
- NativeDevice = new Device(null, FeatureLevel);
+ NativeDevice = device;
+ Description = description;
WavefrontSize = NativeDevice.D3D12Options1.WaveLaneCountMin;
NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Compute));
@@ -41,6 +46,11 @@ namespace ComputeSharp.Graphics
CopyCommandList.Close();
}
+ ///
+ /// Gets the available info for the current instance
+ ///
+ public AdapterDescription Description { get; }
+
///
/// 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.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs
new file mode 100644
index 00000000..25547e65
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs
@@ -0,0 +1,40 @@
+using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using SharpDX.DXGI;
+using Device = SharpDX.Direct3D12.Device;
+
+namespace ComputeSharp.Graphics.Helpers
+{
+ ///
+ /// A with methods to inspect the available devices on the current machine
+ ///
+ public static class DeviceHelper
+ {
+ ///
+ /// Gets a collection of all the supported devices on the current machine
+ ///
+ /// The collection of objects with support for DX12.1
+ [Pure]
+ public static IReadOnlyList<(Device, AdapterDescription)> QueryAllSupportedDevices()
+ {
+ using Factory factory = new Factory1();
+ List<(Device, AdapterDescription)> devices = new List<(Device, AdapterDescription)>();
+
+ foreach (Adapter adapter in factory.Adapters)
+ {
+ if (adapter.Description.DedicatedVideoMemory == 0) continue;
+ try
+ {
+ Device device = new Device(adapter, GraphicsDevice.FeatureLevel);
+ devices.Add((device, adapter.Description));
+ }
+ catch
+ {
+ // Unsupported device
+ }
+ }
+
+ return devices;
+ }
+ }
+}
diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs
index 7af82c16..0bfe9b60 100644
--- a/src/ComputeSharp/Gpu.cs
+++ b/src/ComputeSharp/Gpu.cs
@@ -1,4 +1,11 @@
-using ComputeSharp.Graphics;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.Linq;
+using ComputeSharp.Graphics;
+using ComputeSharp.Graphics.Helpers;
+using SharpDX.DXGI;
+using Device = SharpDX.Direct3D12.Device;
namespace ComputeSharp
{
@@ -7,11 +14,34 @@ 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 = DeviceHelper.QueryAllSupportedDevices()).Any();
+
private static GraphicsDevice _Default;
///
/// Gets the default instance for the current machine
///
- public static GraphicsDevice Default => _Default ??= new GraphicsDevice(true);
+ 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[0].Device, _Devices[0].Description);
+ }
+ }
+
+ ///
+ /// Enumerates all the available objects on the current machine
+ ///
+ /// A sequence of supported objects that can be used to run compute shaders
+ [Pure]
+ public static IEnumerable EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device.Device, device.Description));
+
+ // The loaded collection of supported devices
+ private static IReadOnlyList<(Device Device, AdapterDescription Description)> _Devices;
}
}
diff --git a/tests/ComputeSharp.NetCore.Tests/InitializationTests.cs b/tests/ComputeSharp.NetCore.Tests/InitializationTests.cs
new file mode 100644
index 00000000..4e3b4c0f
--- /dev/null
+++ b/tests/ComputeSharp.NetCore.Tests/InitializationTests.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Linq;
+using ComputeSharp.Graphics;
+using ComputeSharp.Graphics.Buffers;
+using ComputeSharp.NetCore.Tests.Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.NetCore.Tests
+{
+ [TestClass]
+ [TestCategory("Initialization")]
+ public class InitializationTests
+ {
+ [TestMethod]
+ public void CheckIsSupportedAndGetDefaultDevice()
+ {
+ if (Gpu.IsSupported) Assert.IsNotNull(Gpu.Default);
+ else Assert.ThrowsException(() => Gpu.Default);
+ }
+
+ [TestMethod]
+ public void RunShaderOnAllDevices()
+ {
+ foreach (GraphicsDevice gpu in Gpu.EnumerateDevices())
+ {
+ using (ReadWriteBuffer buffer = gpu.AllocateReadWriteBuffer(100))
+ {
+ Action action = id => buffer[id.X] = id.X;
+
+ gpu.For(100, action);
+
+ float[] array = buffer.GetData();
+ float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
+
+ Assert.IsTrue(array.AsSpan().ContentEquals(expected));
+ }
+
+ gpu.Dispose();
+ }
+ }
+ }
+}