From 8a798a6f72f3aa55c4a4bbc9eacf16158ff837e2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Nov 2020 12:56:26 +0100 Subject: [PATCH] Added Gpu.QueryDevices API --- .../Helpers/DeviceHelper.Enumerator.cs | 164 ++++++++++++++++++ .../Helpers/DeviceHelper.cs | 2 +- src/ComputeSharp.Graphics/Luid.cs | 4 + src/ComputeSharp/Gpu.cs | 13 ++ 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs new file mode 100644 index 00000000..97ded9ce --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -0,0 +1,164 @@ +using ComputeSharp.Core.Interop; +using System; +using System.Collections; +using System.Collections.Generic; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; +using HRESULT = System.Int32; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + internal static partial class DeviceHelper + { + /// + /// A custom query type that iterates through devices matching a given predicate. + /// + public sealed unsafe class DeviceQuery : IEnumerable + { + /// + /// The instance to use to select devices to create. + /// + private readonly Predicate predicate; + + /// + /// Creates a new instance for the given predicate. + /// + /// The input predicate to select devices to create. + public DeviceQuery(Predicate predicate) + { + this.predicate = predicate; + } + + /// + public IEnumerator GetEnumerator() + { + return new Enumerator(this.predicate); + } + + /// + IEnumerator IEnumerable.GetEnumerator() + { + return new Enumerator(this.predicate); + } + + /// + /// The enumerator type for a instance. + /// + private sealed unsafe class Enumerator : NativeObject, IEnumerator + { + /// + /// The instance to use to select devices to create. + /// + private readonly Predicate predicate; + + /// + /// The instance used to enumerate devices. + /// + private ComPtr dxgiFactory4; + + /// + /// Indicates whether or not the enumerator has already been initialized and is set. + /// + private bool isInitialized; + + /// + /// The current adapter index to enumerate adapters from . + /// + private uint index; + + /// + /// The current instance to return. + /// + private GraphicsDevice? graphicsDevice; + + /// + /// Creates a new instance for the given predicate. + /// + /// The input predicate to select devices to create. + public Enumerator(Predicate predicate) + { + this.predicate = predicate; + } + + /// + public GraphicsDevice Current => this.graphicsDevice!; + + /// + object IEnumerator.Current => this.graphicsDevice!; + + /// + public bool MoveNext() + { + if (!this.isInitialized) + { + this.isInitialized = true; + + Guid dxgiFactory4Guid = FX.IID_IDXGIFactory4; + + fixed (IDXGIFactory4** dxgiFactory4 = this.dxgiFactory4) + { + FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, &dxgiFactory4Guid, (void**)dxgiFactory4).Assert(); + } + } + + while (true) + { + using ComPtr dxgiAdapter1 = default; + + HRESULT enumAdapters1Result = dxgiFactory4.Get()->EnumAdapters1(this.index, dxgiAdapter1.GetAddressOf()); + + if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND) + { + return false; + } + + enumAdapters1Result.Assert(); + + this.index++; + + DXGI_ADAPTER_DESC1 dxgiDescription1; + + dxgiAdapter1.Get()->GetDesc1(&dxgiDescription1).Assert(); + + if (dxgiDescription1.DedicatedVideoMemory == 0) continue; + + Guid d3d12DeviceGuid = FX.IID_ID3D12Device; + + HRESULT createDeviceResult = FX.D3D12CreateDevice( + dxgiAdapter1.Upcast().Get(), + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + &d3d12DeviceGuid, + null); + + if (FX.SUCCEEDED(createDeviceResult) && + this.predicate(new GraphicsDeviceInfo(&dxgiDescription1))) + { + using ComPtr d3d12device = default; + + FX.D3D12CreateDevice( + dxgiAdapter1.Upcast().Get(), + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + &d3d12DeviceGuid, + d3d12device.GetVoidAddressOf()).Assert(); + + this.graphicsDevice = new GraphicsDevice(d3d12device.Move(), &dxgiDescription1); + } + } + } + + /// + public void Reset() + { + throw new NotImplementedException(); + } + + /// + protected override void OnDispose() + { + this.dxgiFactory4.Dispose(); + } + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index bf8ca58f..b10e2249 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Graphics.Helpers /// /// A with methods to inspect the available devices on the current machine. /// - internal static class DeviceHelper + internal static partial class DeviceHelper { /// /// Gets whether or not there is a default device available, without creating it. diff --git a/src/ComputeSharp.Graphics/Luid.cs b/src/ComputeSharp.Graphics/Luid.cs index 568884b9..275982ed 100644 --- a/src/ComputeSharp.Graphics/Luid.cs +++ b/src/ComputeSharp.Graphics/Luid.cs @@ -9,9 +9,13 @@ namespace ComputeSharp.Graphics /// public readonly struct Luid : IEquatable { + #pragma warning disable CS0649 + private readonly uint lowPart; private readonly int highPart; + #pragma warning restore + /// [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ComputeSharp/Gpu.cs b/src/ComputeSharp/Gpu.cs index debe671c..220a0916 100644 --- a/src/ComputeSharp/Gpu.cs +++ b/src/ComputeSharp/Gpu.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Helpers; @@ -26,5 +28,16 @@ namespace ComputeSharp /// Thrown when a default device is not available. /// Make sure to check before accessing this property. public static GraphicsDevice Default => DefaultFactory.Value; + + /// + /// Executes a query on the currently available DX12.0 devices matching a given predicate. + /// + /// The predicate to use to select the devices to create. + /// A sequence of instances matching . + [Pure] + public static IEnumerable QueryDevices(Predicate predicate) + { + return new DeviceHelper.DeviceQuery(predicate); + } } }