diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 616a888a..457c1eb6 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -1,5 +1,7 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Threading; using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Commands.Interop; #if NET6_0_OR_GREATER @@ -14,6 +16,7 @@ using static TerraFX.Interop.DirectX.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.DirectX.D3D12_FEATURE; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; using static TerraFX.Interop.DirectX.DXGI_ADAPTER_FLAG; + namespace ComputeSharp; /// @@ -84,6 +87,30 @@ public sealed unsafe partial class GraphicsDevice : NativeObject private ComPtr pool; #endif + /// + /// The reason the device was removed, if any. + /// + private HRESULT deviceRemovedReason; + + /// + /// Raised whenever the device is lost. + /// + /// + /// + /// "Device lost" refers to a situation where the GPU graphics device becomes unusable for further operations. This can occur + /// due to GPU hardware malfunction, driver bugs, driver software updates, or switching the app from one GPU to another. + /// + /// + /// A lost device can no longer be used, and any attempt to do so will throw an exception. To recover from + /// this situation, the app must create a new device and then recreate all its graphics resources. + /// + /// + /// This event will only be raised at most once for a given instance. Additionally, + /// the event is raised asynchronously with respect to the device being lost, and on a thread pool thread. + /// + /// + public event EventHandler? DeviceLost; + /// /// Creates a new instance for the input . /// @@ -346,6 +373,56 @@ public sealed unsafe partial class GraphicsDevice : NativeObject d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap); } + /// + /// Raises the event if needed. + /// + internal void RaiseDeviceLostEventIfNeeded() + { + if (IsDisposed) + { + return; + } + + // If the current device removed reason is not S_OK, it means the event + // has already been raised (but the device will keep reporting that reason). + // In that case, do nothing to avoid raising it again while the device is alive. + if (Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)) != S.S_OK) + { + return; + } + + HRESULT result = this.d3D12Device.Get()->GetDeviceRemovedReason(); + + // Only raise the event once, and store the device removed reason to track it + if (Interlocked.CompareExchange( + ref Unsafe.As(ref this.deviceRemovedReason), + (int)result, + S.S_OK) == S.S_OK) + { + ThreadPool.QueueUserWorkItem(static state => ((GraphicsDevice)state!).RaiseDeviceLostEvent(), this); + } + } + + /// + /// Raises the event. + /// + private void RaiseDeviceLostEvent() + { + HRESULT result = Volatile.Read(ref Unsafe.As(ref this.deviceRemovedReason)); + + DeviceLostReason reason = (int)result switch + { + DXGI.DXGI_ERROR_DEVICE_HUNG => DeviceLostReason.DeviceHung, + DXGI.DXGI_ERROR_DEVICE_REMOVED => DeviceLostReason.DeviceRemoved, + DXGI.DXGI_ERROR_DEVICE_RESET => DeviceLostReason.DeviceReset, + DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR => DeviceLostReason.DriverInternalError, + DXGI.DXGI_ERROR_INVALID_CALL => DeviceLostReason.InvalidCall, + _ => DeviceLostReason.UnspecifiedError + }; + + DeviceLost?.Invoke(this, reason); + } + /// protected override void OnDispose() {