diff --git a/src/ComputeSharp.Pix/ComputeContextExtensions.cs b/src/ComputeSharp.Pix/ComputeContextExtensions.cs index 20b5efbf..bafaa338 100644 --- a/src/ComputeSharp.Pix/ComputeContextExtensions.cs +++ b/src/ComputeSharp.Pix/ComputeContextExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System.Drawing; using ComputeSharp.Graphics.Commands; using ComputeSharp.Interop; @@ -9,6 +9,37 @@ namespace ComputeSharp; /// public static class ComputeContextExtensions { + /// + /// Starts a PIX event on a target object. + /// + /// The to use to start the PIX event. + /// The color to use for the event (the alpha channel will be ignored). + /// The message to use for the event. + public static unsafe void BeginEvent(this in ComputeContext context, Color color, string message) + { + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + int argb = color.ToArgb(); + uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); + + commandList.D3D12GraphicsCommandList->BeginEvent(pixColor, message); + } + + /// + /// Starts a PIX event on a target object. + /// + /// The to use to start the PIX event. + /// The index to identify the group this event belongs to (PIX will choose a color to represent all entries in the group). + /// The message to use for the event. + public static unsafe void BeginEvent(this in ComputeContext context, byte index, string message) + { + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + uint pixColor = Pix.PIX_COLOR_INDEX(index); + + commandList.D3D12GraphicsCommandList->BeginEvent(pixColor, message); + } + /// /// Starts a PIX event on a target object. /// @@ -16,7 +47,9 @@ public static class ComputeContextExtensions /// The message to use for the event. public static unsafe void BeginEvent(this in ComputeContext context, string message) { - throw new NotImplementedException(); + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + commandList.D3D12GraphicsCommandList->BeginEvent(0, message); } /// @@ -25,7 +58,40 @@ public static class ComputeContextExtensions /// The to use to end the PIX event. public static unsafe void EndEvent(this in ComputeContext context) { - throw new NotImplementedException(); + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + Pix.PIXEndEventOnCommandList(commandList.D3D12GraphicsCommandList); + } + + /// + /// Sets a PIX marker on a target object. + /// + /// The to use to insert the PIX marker. + /// The color to use for the log (the alpha channel will be ignored). + /// The message to use for the marker. + public static unsafe void Log(this in ComputeContext context, Color color, string message) + { + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + int argb = color.ToArgb(); + uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); + + commandList.D3D12GraphicsCommandList->SetPixMarker(pixColor, message); + } + + /// + /// Sets a PIX marker on a target object. + /// + /// The to use to insert the PIX marker. + /// The index to identify the group this log belongs to (PIX will choose a color to represent all entries in the group). + /// The message to use for the marker. + public static unsafe void Log(this in ComputeContext context, byte index, string message) + { + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + uint pixColor = Pix.PIX_COLOR_INDEX(index); + + commandList.D3D12GraphicsCommandList->SetPixMarker(pixColor, message); } /// @@ -37,6 +103,6 @@ public static class ComputeContextExtensions { ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); - commandList.D3D12GraphicsCommandList->SetPixMarker(message); + commandList.D3D12GraphicsCommandList->SetPixMarker(0, message); } } diff --git a/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs index a423bd61..971bca2a 100644 --- a/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -11,11 +11,12 @@ namespace ComputeSharp.Interop; internal static class ID3D12GraphicsCommandListExtensions { /// - /// Sets a PIX marker on a target object. + /// Begins a PIX event on a target object. /// /// The target object. - /// The message to use for the marker. - public static unsafe void SetPixMarker(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, string message) + /// The color to use for the event. + /// The message to use for the event. + public static unsafe void BeginEvent(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, uint color, string message) { // Rent a byte buffer with space for the null-terminator too int maxByteLength = Encoding.ASCII.GetMaxByteCount(message.Length) + 1; @@ -34,7 +35,39 @@ internal static class ID3D12GraphicsCommandListExtensions buffer[bytesWritten] = 0; // Write the marker with the null-terminated ANSI message - Pix.PIXSetMarkerOnCommandList((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref d3D12GraphicsCommandList), 0, (sbyte*)bufferPtr); + Pix.PIXBeginEventOnCommandList((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref d3D12GraphicsCommandList), color, (sbyte*)bufferPtr); + } + + // Return the buffer (if an exception is thrown, it's fine to not do so) + ArrayPool.Shared.Return(buffer); + } + + /// + /// Sets a PIX marker on a target object. + /// + /// The target object. + /// The color to use for the marker. + /// The message to use for the marker. + public static unsafe void SetPixMarker(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, uint color, string message) + { + // Rent a byte buffer with space for the null-terminator too + int maxByteLength = Encoding.ASCII.GetMaxByteCount(message.Length) + 1; + byte[] buffer = ArrayPool.Shared.Rent(maxByteLength); + int bytesWritten; + + fixed (byte* bufferPtr = buffer) + { + // Transcode from unicode to ASCII + fixed (char* messagePtr = message) + { + bytesWritten = Encoding.ASCII.GetBytes(messagePtr, message.Length, bufferPtr, buffer.Length); + } + + // Add the null terminator + buffer[bytesWritten] = 0; + + // Write the marker with the null-terminated ANSI message + Pix.PIXSetMarkerOnCommandList((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref d3D12GraphicsCommandList), color, (sbyte*)bufferPtr); } // Return the buffer (if an exception is thrown, it's fine to not do so)