Merge pull request #12 from Sergio0694/feature_devices-query

Devices query
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-13 13:22:14 +02:00
コミット 348cadd622
4ファイル変更
+129 -7

検索条件に一致するファイルはありません

+15 -5
ファイルの表示
@@ -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)
/// <summary>
/// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="Device"/>
/// </summary>
/// <param name="device">The <see cref="Device"/> to use for the new <see cref="GraphicsDevice"/> instance</param>
/// <param name="description">The available info for the new <see cref="GraphicsDevice"/> instance</param>
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();
}
/// <summary>
/// Gets the available info for the current <see cref="GraphicsDevice"/> instance
/// </summary>
public AdapterDescription Description { get; }
/// <summary>
/// Gets the number of lanes in a SIMD wave on the current device (also known as "wavefront size" or "warp width")
/// </summary>
+40
ファイルの表示
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using SharpDX.DXGI;
using Device = SharpDX.Direct3D12.Device;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A <see langword="class"/> with methods to inspect the available devices on the current machine
/// </summary>
public static class DeviceHelper
{
/// <summary>
/// Gets a collection of all the supported devices on the current machine
/// </summary>
/// <returns>The collection of <see cref="Device"/> objects with support for DX12.1</returns>
[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;
}
}
}
+32 -2
ファイルの表示
@@ -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
/// </summary>
public static class Gpu
{
/// <summary>
/// Gets whether or not the <see cref="Gpu"/> APIs can be used on the current machine (ie. if there is at least a supported GPU device)
/// </summary>
public static bool IsSupported { get; } = (_Devices = DeviceHelper.QueryAllSupportedDevices()).Any();
private static GraphicsDevice _Default;
/// <summary>
/// Gets the default <see cref="GraphicsDevice"/> instance for the current machine
/// </summary>
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);
}
}
/// <summary>
/// Enumerates all the available <see cref="GraphicsDevice"/> objects on the current machine
/// </summary>
/// <returns>A sequence of supported <see cref="GraphicsDevice"/> objects that can be used to run compute shaders</returns>
[Pure]
public static IEnumerable<GraphicsDevice> EnumerateDevices() => _Devices.Select(device => new GraphicsDevice(device.Device, device.Description));
// The loaded collection of supported devices
private static IReadOnlyList<(Device Device, AdapterDescription Description)> _Devices;
}
}
+42
ファイルの表示
@@ -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<NotSupportedException>(() => Gpu.Default);
}
[TestMethod]
public void RunShaderOnAllDevices()
{
foreach (GraphicsDevice gpu in Gpu.EnumerateDevices())
{
using (ReadWriteBuffer<float> buffer = gpu.AllocateReadWriteBuffer<float>(100))
{
Action<ThreadIds> 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();
}
}
}
}