using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Core.Helpers;
using TerraFX.Interop;
using FX = TerraFX.Interop.Windows;
using HRESULT = System.Int32;
namespace ComputeSharp.Graphics.Helpers
{
///
/// A with methods to inspect the available devices on the current machine.
///
internal static partial class DeviceHelper
{
///
/// Gets whether or not there is a default device available, without creating it.
///
/// Whether or not there is a device supporting at least DX12.0.
[Pure]
public static unsafe bool IsDefaultDeviceAvailable()
{
DXGI_ADAPTER_DESC1 dxgiDescription1;
return TryGetDefaultDevice(null, &dxgiDescription1);
}
///
/// Gets the default instance.
///
/// The default instance supporting at least DX12.0.
/// Thrown when a default device is not available.
public static unsafe GraphicsDevice GetDefaultDevice()
{
using ComPtr d3d12device = default;
DXGI_ADAPTER_DESC1 dxgiDescription1;
if (TryGetDefaultDevice(d3d12device.GetAddressOf(), &dxgiDescription1))
{
return new GraphicsDevice(d3d12device.Move(), &dxgiDescription1);
}
return ThrowHelper.ThrowNotSupportedException("There isn't a supported GPU device on the current machine");
}
///
/// Tries to check or create a default object.
///
/// A pointer to the object to create, or .
/// A pointer to the value for the device found.
/// Whether a default device was found with the requested feature level.
public static unsafe bool TryGetDefaultDevice(ID3D12Device** d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
using ComPtr dxgiFactory4 = default;
Guid dxgiFactory4Guid = FX.IID_IDXGIFactory4;
EnableDebugMode();
FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, &dxgiFactory4Guid, dxgiFactory4.GetVoidAddressOf()).Assert();
uint i = 0;
while (true)
{
using ComPtr dxgiAdapter1 = default;
HRESULT enumAdapters1Result = dxgiFactory4.Get()->EnumAdapters1(i++, dxgiAdapter1.GetAddressOf());
if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND)
{
return false;
}
enumAdapters1Result.Assert();
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,
(void**)d3d12device);
if (FX.SUCCEEDED(createDeviceResult))
{
return true;
}
}
}
///
/// The creation flags for instances.
///
private const uint IDXGIFactoryCreationFlags =
#if DEBUG
FX.DXGI_CREATE_FACTORY_DEBUG;
#else
0;
#endif
///
/// Enables the debug layer for DirectX APIs.
///
[Conditional("DEBUG")]
private static unsafe void EnableDebugMode()
{
using ComPtr d3D12Debug = default;
using ComPtr d3D12Debug1 = default;
Guid d3D12DebugGuid = FX.IID_ID3D12Debug;
FX.D3D12GetDebugInterface(&d3D12DebugGuid, d3D12Debug.GetVoidAddressOf()).Assert();
d3D12Debug.Get()->EnableDebugLayer();
Guid d3D12Debug1Guid = FX.IID_ID3D12Debug1;
if (FX.SUCCEEDED(d3D12Debug.CopyTo(d3D12Debug1.GetAddressOf())))
{
d3D12Debug1.Get()->SetEnableGPUBasedValidation(FX.TRUE);
d3D12Debug1.Get()->SetEnableSynchronizedCommandQueueValidation(FX.TRUE);
}
}
}
}