Added Gpu.QueryDevices API

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-18 12:56:26 +01:00
1 親 c0db9ca81b
コミット 8a798a6f72
4ファイル変更
+182 -1

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

+164
ファイルの表示
@@ -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
{
/// <inheritdoc/>
internal static partial class DeviceHelper
{
/// <summary>
/// A custom query type that iterates through devices matching a given predicate.
/// </summary>
public sealed unsafe class DeviceQuery : IEnumerable<GraphicsDevice>
{
/// <summary>
/// The <see cref="Predicate{T}"/> instance to use to select devices to create.
/// </summary>
private readonly Predicate<GraphicsDeviceInfo> predicate;
/// <summary>
/// Creates a new <see cref="Enumerator"/> instance for the given predicate.
/// </summary>
/// <param name="predicate">The input predicate to select devices to create.</param>
public DeviceQuery(Predicate<GraphicsDeviceInfo> predicate)
{
this.predicate = predicate;
}
/// <inheritdoc/>
public IEnumerator<GraphicsDevice> GetEnumerator()
{
return new Enumerator(this.predicate);
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this.predicate);
}
/// <summary>
/// The enumerator type for a <see cref="DeviceQuery"/> instance.
/// </summary>
private sealed unsafe class Enumerator : NativeObject, IEnumerator<GraphicsDevice>
{
/// <summary>
/// The <see cref="Predicate{T}"/> instance to use to select devices to create.
/// </summary>
private readonly Predicate<GraphicsDeviceInfo> predicate;
/// <summary>
/// The <see cref="IDXGIFactory4"/> instance used to enumerate devices.
/// </summary>
private ComPtr<IDXGIFactory4> dxgiFactory4;
/// <summary>
/// Indicates whether or not the enumerator has already been initialized and <see cref="dxgiFactory4"/> is set.
/// </summary>
private bool isInitialized;
/// <summary>
/// The current adapter index to enumerate adapters from <see cref="dxgiFactory4"/>.
/// </summary>
private uint index;
/// <summary>
/// The current <see cref="GraphicsDevice"/> instance to return.
/// </summary>
private GraphicsDevice? graphicsDevice;
/// <summary>
/// Creates a new <see cref="Enumerator"/> instance for the given predicate.
/// </summary>
/// <param name="predicate">The input predicate to select devices to create.</param>
public Enumerator(Predicate<GraphicsDeviceInfo> predicate)
{
this.predicate = predicate;
}
/// <inheritdoc/>
public GraphicsDevice Current => this.graphicsDevice!;
/// <inheritdoc/>
object IEnumerator.Current => this.graphicsDevice!;
/// <inheritdoc/>
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<IDXGIAdapter1> 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<IDXGIAdapter1, IUnknown>().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0,
&d3d12DeviceGuid,
null);
if (FX.SUCCEEDED(createDeviceResult) &&
this.predicate(new GraphicsDeviceInfo(&dxgiDescription1)))
{
using ComPtr<ID3D12Device> d3d12device = default;
FX.D3D12CreateDevice(
dxgiAdapter1.Upcast<IDXGIAdapter1, IUnknown>().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0,
&d3d12DeviceGuid,
d3d12device.GetVoidAddressOf()).Assert();
this.graphicsDevice = new GraphicsDevice(d3d12device.Move(), &dxgiDescription1);
}
}
}
/// <inheritdoc/>
public void Reset()
{
throw new NotImplementedException();
}
/// <inheritdoc/>
protected override void OnDispose()
{
this.dxgiFactory4.Dispose();
}
}
}
}
}
+1 -1
ファイルの表示
@@ -10,7 +10,7 @@ namespace ComputeSharp.Graphics.Helpers
/// <summary>
/// A <see langword="class"/> with methods to inspect the available devices on the current machine.
/// </summary>
internal static class DeviceHelper
internal static partial class DeviceHelper
{
/// <summary>
/// Gets whether or not there is a default device available, without creating it.
+4
ファイルの表示
@@ -9,9 +9,13 @@ namespace ComputeSharp.Graphics
/// </summary>
public readonly struct Luid : IEquatable<Luid>
{
#pragma warning disable CS0649
private readonly uint lowPart;
private readonly int highPart;
#pragma warning restore
/// <inheritdoc/>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
+13
ファイルの表示
@@ -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
/// <exception cref="NotSupportedException">Thrown when a default device is not available.</exception>
/// <remarks>Make sure to check <see cref="IsSupported"/> before accessing this property.</remarks>
public static GraphicsDevice Default => DefaultFactory.Value;
/// <summary>
/// Executes a query on the currently available DX12.0 devices matching a given predicate.
/// </summary>
/// <param name="predicate">The predicate to use to select the devices to create.</param>
/// <returns>A sequence of <see cref="GraphicsDevice"/> instances matching <paramref name="predicate"/>.</returns>
[Pure]
public static IEnumerable<GraphicsDevice> QueryDevices(Predicate<GraphicsDeviceInfo> predicate)
{
return new DeviceHelper.DeviceQuery(predicate);
}
}
}