Implement GraphicsDevice.GetDefault() method

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:11 +02:00
1 親 ca96337b8a
コミット ea0f792ae6
3ファイル変更
+99 -50

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

+1 -1
ファイルの表示
@@ -53,7 +53,7 @@ partial class GraphicsDevice
/// <exception cref="NotSupportedException">Thrown if no adapter could be created (this should never be the case).</exception>
public static GraphicsDevice GetDefault()
{
throw new NotImplementedException();
return DeviceHelper.GetDefaultDeviceFromCacheOrCreateInstance();
}
/// <summary>
+39
ファイルの表示
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -18,6 +19,44 @@ partial class DeviceHelper
/// </summary>
private static readonly Dictionary<Luid, ComPtr<ID3D12InfoQueue>> D3D12InfoQueueMap = new();
/// <summary>
/// The cached default <see cref="GraphicsDevice"/> instance, if any.
/// </summary>
internal static GraphicsDevice? defaultDevice;
/// <summary>
/// Gets the cached <see cref="GraphicsDevice"/> instance or creates and caches a new one.
/// </summary>
/// <returns>The default <see cref="GraphicsDevice"/> instance.</returns>
public static unsafe GraphicsDevice GetDefaultDeviceFromCacheOrCreateInstance()
{
lock (DevicesCache)
{
do
{
GraphicsDevice? defaultDevice = DeviceHelper.defaultDevice;
// Fast path retrieving the cached instance and returning it
if (defaultDevice is not null)
{
return defaultDevice;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void InitializeAndAssignDefaultDevice()
{
DeviceHelper.defaultDevice = GetOrCreateDefaultDevice();
}
// Non inlined method to load the default device and assign it to the
// cached field. If this method returns, then the default device has
// correctly been initialized. Otherwise, the method will just throw.
InitializeAndAssignDefaultDevice();
}
while (true);
}
}
/// <summary>
/// Retrieves a <see cref="GraphicsDevice"/> instance for an <see cref="ID3D12Device"/> object.
/// </summary>
+59 -49
ファイルの表示
@@ -30,7 +30,7 @@ internal static partial class DeviceHelper
/// <summary>
/// The <see cref="Lazy{T}"/> instance used to produce the default <see cref="GraphicsDevice"/> instance.
/// </summary>
public static readonly Lazy<GraphicsDevice> DefaultFactory = new(GetDefaultDevice);
public static readonly Lazy<GraphicsDevice> DefaultFactory = new(GetOrCreateDefaultDevice);
/// <summary>
/// Gets the <see cref="Luid"/> of the default device.
@@ -56,7 +56,7 @@ internal static partial class DeviceHelper
/// </summary>
/// <returns>The default <see cref="GraphicsDevice"/> instance supporting <see cref="D3D_FEATURE_LEVEL_11_0"/> and <see cref="D3D_SHADER_MODEL_6_0"/>.</returns>
/// <exception cref="NotSupportedException">Thrown when a default device is not available.</exception>
private static unsafe GraphicsDevice GetDefaultDevice()
private static unsafe GraphicsDevice GetOrCreateDefaultDevice()
{
using ComPtr<ID3D12Device> d3D12Device = default;
using ComPtr<IDXGIAdapter> dxgiAdapter = default;
@@ -79,6 +79,7 @@ internal static partial class DeviceHelper
/// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
/// <returns>Whether a default device was found with the requested feature level.</returns>
/// <exception cref="InvalidOperationException">Thrown if the target device was lost and incorrectly disposed (see remarks).</exception>
private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
using ComPtr<IDXGIFactory6> dxgiFactory6 = default;
@@ -106,60 +107,48 @@ internal static partial class DeviceHelper
dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert();
// Skip the WARP adapter
if (dxgiDescription1->VendorId == MicrosoftVendorId &&
dxgiDescription1->DeviceId == WarpDeviceId)
{
continue;
}
// Explicit paths for when a device is being retrieved or not, with special handling
// for the additional check that is required for the SM6 level. This can't be checked
// without creating a device first, so the path for when the target device pointer is
// null is useful to do an initial filtering using D3D12CreateDevice to avoid creating
// a device for adapters that would've failed at the FL11 check already.
if (d3D12Device == null)
using ComPtr<ID3D12Device> d3D12DeviceCandidate = default;
// Try to create a device for the current adapter. There are 3 possible results here:
// 1) The call succeeds. In this case the first device retrieved is the default one.
// 2) The call fails for a device lost reason. This indicates a default device was
// previously retrieved but not disposed correctly. In this case, the code throws.
// 3) The call fails for other reasons. In this case the adapter is simply skipped.
// This might be the case if an adapter doesn't support the requested feature level.
HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL_11_0,
Windows.__uuidof<ID3D12Device>(),
d3D12DeviceCandidate.GetVoidAddressOf());
// Check and throw if the device is a device lost state and not disposed properly
if ((int)createDeviceResult is
DXGI.DXGI_ERROR_DEVICE_HUNG or
DXGI.DXGI_ERROR_DEVICE_REMOVED or
DXGI.DXGI_ERROR_DEVICE_RESET or
DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR or
DXGI.DXGI_ERROR_INVALID_CALL or
DXGI.DXGI_ERROR_ACCESS_DENIED)
{
HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL_11_0,
Windows.__uuidof<ID3D12Device>(),
null);
if (Windows.SUCCEEDED(createDeviceResult))
{
using ComPtr<ID3D12Device> d3D12DeviceCandidate = default;
createDeviceResult = DirectX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL_11_0,
Windows.__uuidof<ID3D12Device>(),
d3D12DeviceCandidate.GetVoidAddressOf());
if (Windows.SUCCEEDED(createDeviceResult) &&
d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
{
return true;
}
}
ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly.");
}
else
// Check for success, and then also filter out devices that support SM6.0. This can
// only be checked on a concrete ID3D12Device instance, so it can't be done earlier.
if (Windows.SUCCEEDED(createDeviceResult) &&
d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
{
using ComPtr<ID3D12Device> d3D12DeviceCandidate = default;
d3D12DeviceCandidate.CopyTo(d3D12Device);
dxgiAdapter1.CopyTo(dxgiAdapter);
HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL_11_0,
Windows.__uuidof<ID3D12Device>(),
d3D12DeviceCandidate.GetVoidAddressOf());
if (Windows.SUCCEEDED(createDeviceResult) &&
d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
{
d3D12DeviceCandidate.CopyTo(d3D12Device);
dxgiAdapter1.CopyTo(dxgiAdapter);
return true;
}
return true;
}
}
}
@@ -171,6 +160,7 @@ internal static partial class DeviceHelper
/// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
/// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
/// <returns>Whether a warp device was created successfully.</returns>
/// <exception cref="InvalidOperationException">Thrown if the target device was lost and incorrectly disposed (see remarks).</exception>
private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
using ComPtr<IDXGIFactory6> dxgiFactory6 = default;
@@ -183,14 +173,34 @@ internal static partial class DeviceHelper
dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert();
using ComPtr<ID3D12Device> d3D12DeviceCandidate = default;
HRESULT createDeviceResult = DirectX.D3D12CreateDevice(
dxgiAdapter1.AsIUnknown().Get(),
D3D_FEATURE_LEVEL_11_0,
Windows.__uuidof<ID3D12Device>(),
(void**)d3D12Device);
d3D12DeviceCandidate.GetVoidAddressOf());
dxgiAdapter1.CopyTo(dxgiAdapter);
if ((int)createDeviceResult is
DXGI.DXGI_ERROR_DEVICE_HUNG or
DXGI.DXGI_ERROR_DEVICE_REMOVED or
DXGI.DXGI_ERROR_DEVICE_RESET or
DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR or
DXGI.DXGI_ERROR_INVALID_CALL or
DXGI.DXGI_ERROR_ACCESS_DENIED)
{
ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly.");
}
return Windows.SUCCEEDED(createDeviceResult);
// There is no need to check for SM6.0, as it's guaranteed to be supported by Windows
if (Windows.SUCCEEDED(createDeviceResult))
{
d3D12DeviceCandidate.CopyTo(d3D12Device);
dxgiAdapter1.CopyTo(dxgiAdapter);
return true;
}
return false;
}
}