diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index 30807dac..ee01f2a1 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -18,24 +18,19 @@ namespace ComputeSharp.Sample array[i] = i + 1; } - // Print the initial matrix - Console.WriteLine("===================== BEFORE ====================="); - PrintMatrix(array, width, height); - Console.WriteLine("=================================================="); - Console.WriteLine(); - using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader Gpu.Default.For(100, new MainKernel(gpuBuffer)); + // Print the initial matrix + PrintMatrix(array, width, height, "BEFORE"); + // Get the data back gpuBuffer.CopyTo(array); // Print the updated matrix - Console.WriteLine("===================== AFTER ======================"); - PrintMatrix(array, width, height); - Console.WriteLine("=================================================="); + PrintMatrix(array, width, height, "AFTER"); } /// @@ -59,8 +54,14 @@ namespace ComputeSharp.Sample /// The input array representing the matrix to print. /// The width of the array to print. /// The height of the array to print. - private static void PrintMatrix(float[] array, int width, int height) + /// The name of the matrix to print. + private static void PrintMatrix(float[] array, int width, int height, string name) { + int pad = 48 - name.Length; + string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; + + Console.WriteLine(title); + int length = width * height; int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; ref float r = ref array[0]; @@ -73,6 +74,8 @@ namespace ComputeSharp.Sample if (i < length - 1) Console.Write(", "); if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); } + + Console.WriteLine(new string('=', 50)); } } } diff --git a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs index aab7f3f1..e1271908 100644 --- a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs @@ -21,10 +21,12 @@ namespace ComputeSharp.Core.Extensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this HRESULT result) { +#if DEBUG + DeviceHelper.FlushAllID3D12InfoQueueMessages(); +#endif + 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 5d1cff7d..f756e281 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs @@ -1,77 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; -using ComputeSharp.Core.Extensions; +using System.Collections.Generic; using TerraFX.Interop; +#if DEBUG +using ComputeSharp.Core.Extensions; +using FX = TerraFX.Interop.Windows; +#endif 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 { @@ -80,7 +15,12 @@ namespace ComputeSharp.Graphics.Helpers /// internal static readonly Dictionary DevicesCache = new(); - internal static readonly Dictionary> D3D12InfoQueueMap = new(); +#if DEBUG + /// + /// The local map of instances for the existing devices. + /// + private static readonly Dictionary> D3D12InfoQueueMap = new(); +#endif /// /// Retrieves a instance for an object. @@ -100,11 +40,13 @@ namespace ComputeSharp.Graphics.Helpers DevicesCache.Add(luid, device); +#if DEBUG ComPtr d3D12InfoQueue = default; - int res = d3D12Device.Get()->QueryInterface(Windows.__uuidof(), d3D12InfoQueue.GetVoidAddressOf()); + int res = d3D12Device.Get()->QueryInterface(FX.__uuidof(), d3D12InfoQueue.GetVoidAddressOf()); D3D12InfoQueueMap.Add(luid, d3D12InfoQueue); +#endif } return device; @@ -120,9 +62,12 @@ namespace ComputeSharp.Graphics.Helpers lock (DevicesCache) { DevicesCache.Remove(device.Luid); - D3D12InfoQueueMap.Remove(device.Luid, out var queue); + +#if DEBUG + D3D12InfoQueueMap.Remove(device.Luid, out ComPtr queue); queue.Dispose(); +#endif } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs new file mode 100644 index 00000000..8b82767e --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -0,0 +1,86 @@ +#if DEBUG + +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + internal static partial class DeviceHelper + { + /// + /// Flushes all the pending debug messages for all existing instances to the console/debugger. + /// + public static unsafe void FlushAllID3D12InfoQueueMessages() + { + lock (DevicesCache) + { + int j = 0; + StringBuilder builder = new(1024); + + foreach (var pair in D3D12InfoQueueMap) + { + var device = DevicesCache[pair.Key]; + var queue = pair.Value; + + ulong messages = queue.Get()->GetNumStoredMessages(); + + if (messages > 0) + { + 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("=================================================="); + + 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($"[Description]: \"{new string(message->pDescription)}\""); + builder.Append("=================================================="); + + Marshal.FreeHGlobal((IntPtr)message); + } + + string text = builder.ToString(); + + if (Debugger.IsAttached) + { + Debug.WriteLine(text); + } + else + { + Console.WriteLine(text); + } + + queue.Get()->ClearStoredMessages(); + } + + j++; + } + } + } + } +} + +#endif diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index faaf41fd..036f771a 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; using Microsoft.Toolkit.Diagnostics; @@ -138,11 +139,17 @@ namespace ComputeSharp.Graphics.Helpers /// /// The creation flags for instances. /// - private const uint IDXGIFactoryCreationFlags = FX.DXGI_CREATE_FACTORY_DEBUG; + private const uint IDXGIFactoryCreationFlags = +#if DEBUG + FX.DXGI_CREATE_FACTORY_DEBUG; +#else + 0; +#endif /// /// Enables the debug layer for DirectX APIs. /// + [Conditional("DEBUG")] private static unsafe void EnableDebugMode() { using ComPtr d3D12Debug = default;