From ea0f792ae6c520d0f4362c5fe8b7c0323f80ee27 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Jul 2022 14:24:57 +0200 Subject: [PATCH] Implement GraphicsDevice.GetDefault() method --- .../Graphics/GraphicsDevice.GetDevice.cs | 2 +- .../Graphics/Helpers/DeviceHelper.Factory.cs | 39 +++++++ .../Graphics/Helpers/DeviceHelper.cs | 108 ++++++++++-------- 3 files changed, 99 insertions(+), 50 deletions(-) diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs index 41dbd338..68e678c8 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.GetDevice.cs @@ -53,7 +53,7 @@ partial class GraphicsDevice /// Thrown if no adapter could be created (this should never be the case). public static GraphicsDevice GetDefault() { - throw new NotImplementedException(); + return DeviceHelper.GetDefaultDeviceFromCacheOrCreateInstance(); } /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs index 8445a801..78bfce29 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Factory.cs @@ -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 /// private static readonly Dictionary> D3D12InfoQueueMap = new(); + /// + /// The cached default instance, if any. + /// + internal static GraphicsDevice? defaultDevice; + + /// + /// Gets the cached instance or creates and caches a new one. + /// + /// The default instance. + 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); + } + } + /// /// Retrieves a instance for an object. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index d31e3065..ab635502 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -30,7 +30,7 @@ internal static partial class DeviceHelper /// /// The instance used to produce the default instance. /// - public static readonly Lazy DefaultFactory = new(GetDefaultDevice); + public static readonly Lazy DefaultFactory = new(GetOrCreateDefaultDevice); /// /// Gets the of the default device. @@ -56,7 +56,7 @@ internal static partial class DeviceHelper /// /// The default instance supporting and . /// Thrown when a default device is not available. - private static unsafe GraphicsDevice GetDefaultDevice() + private static unsafe GraphicsDevice GetOrCreateDefaultDevice() { using ComPtr d3D12Device = default; using ComPtr dxgiAdapter = default; @@ -79,6 +79,7 @@ internal static partial class DeviceHelper /// A pointer to the object used to create , or . /// A pointer to the value for the device found. /// Whether a default device was found with the requested feature level. + /// Thrown if the target device was lost and incorrectly disposed (see remarks). private static unsafe bool TryGetDefaultDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { using ComPtr 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 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(), + 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(), - null); - - if (Windows.SUCCEEDED(createDeviceResult)) - { - using ComPtr d3D12DeviceCandidate = default; - - createDeviceResult = DirectX.D3D12CreateDevice( - dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL_11_0, - Windows.__uuidof(), - 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 d3D12DeviceCandidate = default; + d3D12DeviceCandidate.CopyTo(d3D12Device); + dxgiAdapter1.CopyTo(dxgiAdapter); - HRESULT createDeviceResult = DirectX.D3D12CreateDevice( - dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL_11_0, - Windows.__uuidof(), - 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 /// A pointer to the object used to create , or . /// A pointer to the value for the device found. /// Whether a warp device was created successfully. + /// Thrown if the target device was lost and incorrectly disposed (see remarks). private static unsafe bool TryGetWarpDevice(ID3D12Device** d3D12Device, IDXGIAdapter** dxgiAdapter, DXGI_ADAPTER_DESC1* dxgiDescription1) { using ComPtr dxgiFactory6 = default; @@ -183,14 +173,34 @@ internal static partial class DeviceHelper dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert(); + using ComPtr d3D12DeviceCandidate = default; + HRESULT createDeviceResult = DirectX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), D3D_FEATURE_LEVEL_11_0, Windows.__uuidof(), - (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; } }