From f43305ec4f2f6c679f2a4188d11d099739e55866 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Feb 2021 13:04:19 +0100 Subject: [PATCH] Added exception on warning/error manual detection --- .../Extensions/HResultExtensions.cs | 14 ++++- .../Helpers/DeviceHelper.ID3D12InfoQueue.cs | 58 +++++++++---------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs index e1271908..d3a5b855 100644 --- a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs @@ -22,13 +22,23 @@ namespace ComputeSharp.Core.Extensions public static void Assert(this HRESULT result) { #if DEBUG - DeviceHelper.FlushAllID3D12InfoQueueMessages(); -#endif + bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); if (result < 0) { ThrowHelper.ThrowWin32Exception(result); } + + if (hasErrorsOrWarnings) + { + ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue"); + } +#else + if (result < 0) + { + ThrowHelper.ThrowWin32Exception(result); + } +#endif } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs index c4d5e284..ad37f005 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using TerraFX.Interop; +using static TerraFX.Interop.D3D12_MESSAGE_SEVERITY; namespace ComputeSharp.Graphics.Helpers { @@ -13,9 +14,13 @@ namespace ComputeSharp.Graphics.Helpers { /// /// Flushes all the pending debug messages for all existing instances to the console/debugger. + /// It also checks whether or not there are any error messages being logged that didn't result in an actual crash yet. /// - public static unsafe void FlushAllID3D12InfoQueueMessages() + /// Whether or not there are any logged errors or warnings. + public static unsafe bool FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings() { + bool hasErrorsOrWarnings = false; + lock (DevicesCache) { int j = 0; @@ -28,38 +33,27 @@ namespace ComputeSharp.Graphics.Helpers ulong messages = queue.Get()->GetNumStoredMessagesAllowedByRetrievalFilter(); - if (messages > 0) + for (ulong i = 0; i < messages; i++) { + nuint length; + queue.Get()->GetMessage(i, null, &length); + + D3D12_MESSAGE* message = (D3D12_MESSAGE*)Marshal.AllocHGlobal((nint)length); + + queue.Get()->GetMessage(i, message, &length); + builder.Clear(); - builder.AppendLine("================ ID3D12InfoQueue ================="); - builder.AppendLine($"[ID3D12InfoQueue messages for device #{j}"); - builder.AppendLine($"[Luid]: {device.Luid}"); - builder.AppendLine($"[Name]: {device.Name}"); - builder.AppendLine($"[DedicatedMemorySize]: {device.DedicatedMemorySize}"); - builder.AppendLine($"[IsHardwareAccelerated]: {device.IsHardwareAccelerated}"); - builder.AppendLine($"[IsCacheCoherentUMA]: {device.IsCacheCoherentUMA}"); - builder.AppendLine($"[ComputeUnits]: {device.ComputeUnits}"); - builder.AppendLine($"[WavefrontSize]: {device.WavefrontSize}"); - builder.Append("=================================================="); + builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]"); + builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}"); + builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}"); + builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}"); + builder.Append($"[Description]: \"{new string(message->pDescription)}\""); - for (ulong i = 0; i < messages; i++) + Marshal.FreeHGlobal((IntPtr)message); + + if (message->Severity is D3D12_MESSAGE_SEVERITY_ERROR or D3D12_MESSAGE_SEVERITY_CORRUPTION or D3D12_MESSAGE_SEVERITY_WARNING) { - nuint length; - queue.Get()->GetMessage(i, null, &length); - - D3D12_MESSAGE* message = (D3D12_MESSAGE*)Marshal.AllocHGlobal((nint)length); - - queue.Get()->GetMessage(i, message, &length); - - builder.AppendLine(); - builder.AppendLine($"[D3D12 debug message #{i}]"); - builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}"); - builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}"); - builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}"); - builder.AppendLine($"[Description]: \"{new string(message->pDescription)}\""); - builder.Append("=================================================="); - - Marshal.FreeHGlobal((IntPtr)message); + hasErrorsOrWarnings = true; } string text = builder.ToString(); @@ -72,13 +66,15 @@ namespace ComputeSharp.Graphics.Helpers { Console.WriteLine(text); } - - queue.Get()->ClearStoredMessages(); } + queue.Get()->ClearStoredMessages(); + j++; } } + + return hasErrorsOrWarnings; } } }