diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs index 48845c10..c75ef8c2 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -1,10 +1,14 @@ #if DEBUG +#if NET6_0_OR_GREATER using System; +#endif using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; using static TerraFX.Interop.DirectX.D3D12_MESSAGE_SEVERITY; #if !NET6_0_OR_GREATER using Enum = ComputeSharp.NetStandard.System.Enum; @@ -26,26 +30,26 @@ internal static partial class DeviceHelper lock (DevicesCache) { - int j = 0; StringBuilder builder = new(1024); foreach (var pair in D3D12InfoQueueMap) { - var device = DevicesCache[pair.Key]; - var queue = pair.Value; + GraphicsDevice device = DevicesCache[pair.Key]; + ID3D12InfoQueue* queue = pair.Value.Get(); - ulong messages = queue.Get()->GetNumStoredMessagesAllowedByRetrievalFilter(); + ulong messages = queue->GetNumStoredMessagesAllowedByRetrievalFilter(); for (ulong i = 0; i < messages; i++) { nuint length; - queue.Get()->GetMessage(i, null, &length); + + queue->GetMessage(i, null, &length); D3D12_MESSAGE* message = (D3D12_MESSAGE*)NativeMemory.Alloc(length); try { - queue.Get()->GetMessage(i, message, &length); + queue->GetMessage(i, message, &length); builder.Clear(); builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]"); @@ -72,13 +76,43 @@ internal static partial class DeviceHelper } else { - Console.WriteLine(text); + Trace.WriteLine(text); } } - queue.Get()->ClearStoredMessages(); + queue->ClearStoredMessages(); - j++; + HRESULT result = device.D3D12Device->GetDeviceRemovedReason(); + + if (result != S.S_OK) + { + string message = (int)result switch + { + DXGI.DXGI_ERROR_DEVICE_HUNG => nameof(DXGI.DXGI_ERROR_DEVICE_HUNG), + DXGI.DXGI_ERROR_DEVICE_REMOVED => nameof(DXGI.DXGI_ERROR_DEVICE_REMOVED), + DXGI.DXGI_ERROR_DEVICE_RESET => nameof(DXGI.DXGI_ERROR_DEVICE_RESET), + DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR => nameof(DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR), + DXGI.DXGI_ERROR_INVALID_CALL => nameof(DXGI.DXGI_ERROR_INVALID_CALL), + _ => ThrowHelper.ThrowArgumentOutOfRangeException("Invalid GetDeviceRemovedReason HRESULT.") + }; + + builder.Clear(); + builder.AppendLine($"[D3D12 device remove \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]"); + builder.AppendLine($"[Reason]: {message}"); + + hasErrorsOrWarnings = true; + + string text = builder.ToString(); + + if (Debugger.IsAttached) + { + Debug.WriteLine(text); + } + else + { + Trace.WriteLine(text); + } + } } }