From 35ffb4ed43417221ba63b7eaed268e04b69aab29 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Feb 2021 22:26:03 +0100 Subject: [PATCH] Added prototype for ID3D12InfoQueue --- .../Extensions/HResultExtensions.cs | 3 + .../Helpers/DeviceHelper.Factory.cs | 84 ++++++++++++++++++- .../Helpers/DeviceHelper.cs | 9 +- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs index f085dccc..aab7f3f1 100644 --- a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using HRESULT = System.Int32; @@ -22,6 +23,8 @@ namespace ComputeSharp.Core.Extensions { if (result < 0) { + DeviceDebugHelper.FlushMessages(); + ThrowHelper.ThrowWin32Exception(result); } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs index d841433d..5d1cff7d 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs @@ -1,15 +1,86 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using ComputeSharp.Core.Extensions; using TerraFX.Interop; namespace ComputeSharp.Graphics.Helpers { + internal static class DeviceDebugHelper + { + public static unsafe void FlushMessages() + { + int j = 0; + + foreach (var pair in DeviceHelper.D3D12InfoQueueMap) + { + var device = DeviceHelper.DevicesCache[pair.Key]; + var queue = pair.Value; + + ulong messages = queue.Get()->GetNumStoredMessages(); + + if (messages > 0) + { + var builder = new StringBuilder(); + builder.AppendLine("====================[ID3D12InfoQueue]===================="); + builder.AppendLine($"[ID3D12InfoQueue messages for device #{j}"); + builder.AppendLine($"[LUID]: {device.Luid}"); + builder.AppendLine($"[NAME]: {device.Name}"); + builder.AppendLine($"[DEDICATED MEMORY]: {device.DedicatedMemorySize}"); + builder.AppendLine($"[IS HARDWARE]: {device.IsHardwareAccelerated}"); + builder.AppendLine($"[IS UMA]: {device.IsCacheCoherentUMA}"); + builder.AppendLine($"[COMPUTE UNITS]: {device.ComputeUnits}"); + builder.AppendLine($"[WAVE SIZE]: {device.WavefrontSize}"); + builder.Append("========================================================="); + + 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.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($"[INFO]: \"{new string(message->pDescription)}\""); + builder.Append("========================================================="); + + Marshal.FreeHGlobal((IntPtr)message); + } + + string text = builder.ToString(); + + Console.WriteLine(text); + + if (Debugger.IsAttached) + { + Debug.WriteLine(text); + } + + queue.Get()->ClearStoredMessages(); + } + + j++; + } + } + } + /// internal static partial class DeviceHelper { /// /// The local cache of instances that are currently usable. /// - private static readonly Dictionary DevicesCache = new(); + internal static readonly Dictionary DevicesCache = new(); + + internal static readonly Dictionary> D3D12InfoQueueMap = new(); /// /// Retrieves a instance for an object. @@ -28,6 +99,12 @@ namespace ComputeSharp.Graphics.Helpers device = new GraphicsDevice(d3D12Device, dxgiDescription1); DevicesCache.Add(luid, device); + + ComPtr d3D12InfoQueue = default; + + int res = d3D12Device.Get()->QueryInterface(Windows.__uuidof(), d3D12InfoQueue.GetVoidAddressOf()); + + D3D12InfoQueueMap.Add(luid, d3D12InfoQueue); } return device; @@ -43,6 +120,9 @@ namespace ComputeSharp.Graphics.Helpers lock (DevicesCache) { DevicesCache.Remove(device.Luid); + D3D12InfoQueueMap.Remove(device.Luid, out var queue); + + queue.Dispose(); } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 036f771a..faaf41fd 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; using Microsoft.Toolkit.Diagnostics; @@ -139,17 +138,11 @@ namespace ComputeSharp.Graphics.Helpers /// /// The creation flags for instances. /// - private const uint IDXGIFactoryCreationFlags = -#if DEBUG - FX.DXGI_CREATE_FACTORY_DEBUG; -#else - 0; -#endif + private const uint IDXGIFactoryCreationFlags = FX.DXGI_CREATE_FACTORY_DEBUG; /// /// Enables the debug layer for DirectX APIs. /// - [Conditional("DEBUG")] private static unsafe void EnableDebugMode() { using ComPtr d3D12Debug = default;