using System;
using System.Collections;
using System.Collections.Generic;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Interop;
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;
fixed (IDXGIFactory4** dxgiFactory4 = this.dxgiFactory4)
{
EnableDebugMode();
FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), (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;
HRESULT createDeviceResult = FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof(),
null);
if (FX.SUCCEEDED(createDeviceResult) &&
this.predicate(new GraphicsDeviceInfo(&dxgiDescription1)))
{
using ComPtr d3D12Device = default;
FX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
FX.__uuidof(),
d3D12Device.GetVoidAddressOf()).Assert();
this.graphicsDevice = GetOrCreateDevice(d3D12Device.Move(), &dxgiDescription1);
return true;
}
}
}
///
public void Reset()
{
throw new NotImplementedException();
}
///
protected override bool OnDispose()
{
this.dxgiFactory4.Dispose();
return true;
}
}
}
}
}