From 17ea1a0f71198fdb8e795197b47a2ab91540f58c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 12:13:59 +0200 Subject: [PATCH 1/7] 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; + } } } From ac5ed0ed6085cb906af47213c6abdb56b44d3b05 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 12:42:03 +0200 Subject: [PATCH 2/7] Added new APIs to the GPU class --- src/ComputeSharp.Graphics/GraphicsDevice.cs | 7 +-- src/ComputeSharp/Gpu.cs | 52 +++++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) 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; } From 46e8fd6b153128f1bf7f9a06c4ec1f9ccba3d639 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 12:44:53 +0200 Subject: [PATCH 3/7] Code refactoring --- .../Helpers/DeviceHelper.cs | 40 +++++++++++++++++++ src/ComputeSharp/Gpu.cs | 31 +------------- 2 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs new file mode 100644 index 00000000..c7052482 --- /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 QueryAllSupportedDevices() + { + using Factory factory = new Factory1(); + List devices = new List(); + + foreach (Adapter adapter in factory.Adapters) + { + if (adapter.Description.DedicatedVideoMemory == 0) continue; + try + { + Device device = new Device(adapter, GraphicsDevice.FeatureLevel); + devices.Add(device); + } + catch + { + // Unsupported device + } + } + + return devices; + } + } +} diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index 960b115c..ad3e247f 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using ComputeSharp.Graphics; -using SharpDX.DXGI; +using ComputeSharp.Graphics.Helpers; using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp @@ -16,7 +16,7 @@ namespace ComputeSharp /// /// 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(); + public static bool IsSupported { get; } = (_Devices = DeviceHelper.QueryAllSupportedDevices()).Any(); private static GraphicsDevice _Default; @@ -42,32 +42,5 @@ namespace ComputeSharp // 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(); - - foreach (Adapter adapter in factory.Adapters) - { - if (adapter.Description.DedicatedVideoMemory == 0) continue; - try - { - Device device = new Device(adapter, GraphicsDevice.FeatureLevel); - devices.Add(device); - } - catch - { - // Unsupported device - } - } - - return devices; - } } } From f6f73b9e3f514ccf29063a86a742d2a584cc7f95 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 12:50:06 +0200 Subject: [PATCH 4/7] Added device description to the GraphicsDevice class --- src/ComputeSharp.Graphics/GraphicsDevice.cs | 25 ++++--------------- .../Helpers/DeviceHelper.cs | 6 ++--- src/ComputeSharp/Gpu.cs | 7 +++--- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 99a86fb7..edb771bd 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -15,12 +15,14 @@ namespace ComputeSharp.Graphics /// Creates a new instance for the input /// /// The to use for the new instance - public GraphicsDevice(Device device) + /// The available info for the new instance + public GraphicsDevice(Device device, AdapterDescription description) { #if DEBUG DebugInterface.Get().EnableDebugLayer(); #endif NativeDevice = device; + Description = description; WavefrontSize = NativeDevice.D3D12Options1.WaveLaneCountMin; NativeComputeCommandQueue = NativeDevice.CreateCommandQueue(new CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Compute)); @@ -48,26 +50,9 @@ namespace ComputeSharp.Graphics } /// - /// Tries to create a new instance from the input object + /// Gets the available info for the current instance /// - /// 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; - } - } + 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 index c7052482..25547e65 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -15,10 +15,10 @@ namespace ComputeSharp.Graphics.Helpers /// /// The collection of objects with support for DX12.1 [Pure] - public static IReadOnlyList QueryAllSupportedDevices() + public static IReadOnlyList<(Device, AdapterDescription)> QueryAllSupportedDevices() { using Factory factory = new Factory1(); - List devices = new List(); + List<(Device, AdapterDescription)> devices = new List<(Device, AdapterDescription)>(); foreach (Adapter adapter in factory.Adapters) { @@ -26,7 +26,7 @@ namespace ComputeSharp.Graphics.Helpers try { Device device = new Device(adapter, GraphicsDevice.FeatureLevel); - devices.Add(device); + devices.Add((device, adapter.Description)); } catch { diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index ad3e247f..0bfe9b60 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Linq; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Helpers; +using SharpDX.DXGI; using Device = SharpDX.Direct3D12.Device; namespace ComputeSharp @@ -29,7 +30,7 @@ namespace ComputeSharp { if (!IsSupported) throw new NotSupportedException("There isn't a supported GPU device on the current machine"); - return _Default ??= new GraphicsDevice(_Devices.First()); + return _Default ??= new GraphicsDevice(_Devices[0].Device, _Devices[0].Description); } } @@ -38,9 +39,9 @@ namespace ComputeSharp /// /// A sequence of supported objects that can be used to run compute shaders [Pure] - public static IEnumerable EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device)); + public static IEnumerable EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device.Device, device.Description)); // The loaded collection of supported devices - private static IReadOnlyList _Devices; + private static IReadOnlyList<(Device Device, AdapterDescription Description)> _Devices; } } From 279d7c372f378b664ba42123782404b07a7f7e4b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 13:10:57 +0200 Subject: [PATCH 5/7] Added GPU initialization tests --- tests/ComputeSharp.NetCore.Tests/MiscTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/ComputeSharp.NetCore.Tests/MiscTests.cs diff --git a/tests/ComputeSharp.NetCore.Tests/MiscTests.cs b/tests/ComputeSharp.NetCore.Tests/MiscTests.cs new file mode 100644 index 00000000..76ba23c8 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/MiscTests.cs @@ -0,0 +1,17 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.NetCore.Tests +{ + [TestClass] + [TestCategory("Misc")] + public class MiscTests + { + [TestMethod] + public void CheckIsSupportedAndGetDefaultDevice() + { + if (Gpu.IsSupported) Assert.IsNotNull(Gpu.Default); + else Assert.ThrowsException(() => Gpu.Default); + } + } +} From 666f7e67ded2e6c65346c649c66ed5972eb1f06a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 13:21:03 +0200 Subject: [PATCH 6/7] Fixed a crash when loading a specific GPU device --- src/ComputeSharp.Graphics/GraphicsDevice.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index edb771bd..0a6adde2 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -18,9 +18,6 @@ namespace ComputeSharp.Graphics /// The available info for the new instance public GraphicsDevice(Device device, AdapterDescription description) { -#if DEBUG - DebugInterface.Get().EnableDebugLayer(); -#endif NativeDevice = device; Description = description; WavefrontSize = NativeDevice.D3D12Options1.WaveLaneCountMin; From 131bc5fdce71507ec5434eea49c75a5e4c685165 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 13:21:14 +0200 Subject: [PATCH 7/7] Added test for multi GPU --- .../InitializationTests.cs | 42 +++++++++++++++++++ tests/ComputeSharp.NetCore.Tests/MiscTests.cs | 17 -------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 tests/ComputeSharp.NetCore.Tests/InitializationTests.cs delete mode 100644 tests/ComputeSharp.NetCore.Tests/MiscTests.cs 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(); + } + } + } +} diff --git a/tests/ComputeSharp.NetCore.Tests/MiscTests.cs b/tests/ComputeSharp.NetCore.Tests/MiscTests.cs deleted file mode 100644 index 76ba23c8..00000000 --- a/tests/ComputeSharp.NetCore.Tests/MiscTests.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.NetCore.Tests -{ - [TestClass] - [TestCategory("Misc")] - public class MiscTests - { - [TestMethod] - public void CheckIsSupportedAndGetDefaultDevice() - { - if (Gpu.IsSupported) Assert.IsNotNull(Gpu.Default); - else Assert.ThrowsException(() => Gpu.Default); - } - } -}