From d5556daa3669db56f300dc6408b0a3a7b62c9fdf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 18 Jun 2022 03:09:43 +0200 Subject: [PATCH] Add PIX set marker extension for ComputeContext --- .../ComputeContextExtensions.cs | 42 ++++++++++++++++++ .../ID3D12GraphicsCommandListExtensions.cs | 43 ++++++++++++++++++ src/ComputeSharp.Pix/Interop/Pix.cs | 44 +++++++++++++++++++ src/ComputeSharp/Properties/AssemblyInfo.cs | 1 + src/ComputeSharp/Shaders/ComputeContext.cs | 8 ++-- 5 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.Pix/ComputeContextExtensions.cs create mode 100644 src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs create mode 100644 src/ComputeSharp.Pix/Interop/Pix.cs diff --git a/src/ComputeSharp.Pix/ComputeContextExtensions.cs b/src/ComputeSharp.Pix/ComputeContextExtensions.cs new file mode 100644 index 00000000..20b5efbf --- /dev/null +++ b/src/ComputeSharp.Pix/ComputeContextExtensions.cs @@ -0,0 +1,42 @@ +using System; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Interop; + +namespace ComputeSharp; + +/// +/// A that contains extension methods using PIX APIs for the type. +/// +public static class ComputeContextExtensions +{ + /// + /// Starts a PIX event on a target object. + /// + /// The to use to start the PIX event. + /// The message to use for the event. + public static unsafe void BeginEvent(this in ComputeContext context, string message) + { + throw new NotImplementedException(); + } + + /// + /// Ends a PIX event on a target object. + /// + /// The to use to end the PIX event. + public static unsafe void EndEvent(this in ComputeContext context) + { + throw new NotImplementedException(); + } + + /// + /// Sets a PIX marker on a target object. + /// + /// The to use to insert the PIX marker. + /// The message to use for the marker. + public static unsafe void Log(this in ComputeContext context, string message) + { + ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + + commandList.D3D12GraphicsCommandList->SetPixMarker(message); + } +} diff --git a/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs new file mode 100644 index 00000000..a423bd61 --- /dev/null +++ b/src/ComputeSharp.Pix/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -0,0 +1,43 @@ +using System.Buffers; +using System.Runtime.CompilerServices; +using System.Text; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Interop; + +/// +/// A with extensions for the type. +/// +internal static class ID3D12GraphicsCommandListExtensions +{ + /// + /// Sets a PIX marker 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) + { + // 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), 0, (sbyte*)bufferPtr); + } + + // Return the buffer (if an exception is thrown, it's fine to not do so) + ArrayPool.Shared.Return(buffer); + } +} diff --git a/src/ComputeSharp.Pix/Interop/Pix.cs b/src/ComputeSharp.Pix/Interop/Pix.cs new file mode 100644 index 00000000..ca2bb7fc --- /dev/null +++ b/src/ComputeSharp.Pix/Interop/Pix.cs @@ -0,0 +1,44 @@ +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Interop; + +/// +/// A collection of native bindings for PIX APIs (see ). +/// +internal static unsafe class Pix +{ + /// + /// Starts a PIX event on a target object. + /// + /// C export: typedef void(WINAPI* BeginEventOnCommandList)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString). + /// + /// + /// The target object. + /// The color to use for the event. + /// The format string to use for the event (in ANSI format, -terminated). + [DllImport("WinPixEventRuntime", ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + public static extern void PIXBeginEventOnCommandList(ID3D12GraphicsCommandList* commandList, ulong color, sbyte* formatString); + + /// + /// Ends a PIX event on a target object. + /// + /// C export: typedef void(WINAPI* EndEventOnCommandList)(ID3D12GraphicsCommandList* commandList). + /// + /// + /// The target object. + [DllImport("WinPixEventRuntime", ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + public static extern void PIXEndEventOnCommandList(ID3D12GraphicsCommandList* commandList); + + /// + /// Sets a PIX marker on a target object. + /// + /// C export: typedef void(WINAPI* SetMarkerOnCommandList)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString). + /// + /// + /// The target object. + /// The color to use for the marker. + /// The format string to use for the marker (in ANSI format, -terminated). + [DllImport("WinPixEventRuntime", ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] + public static extern void PIXSetMarkerOnCommandList(ID3D12GraphicsCommandList* commandList, ulong color, sbyte* formatString); +} diff --git a/src/ComputeSharp/Properties/AssemblyInfo.cs b/src/ComputeSharp/Properties/AssemblyInfo.cs index 80e60132..2ef6928c 100644 --- a/src/ComputeSharp/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp/Properties/AssemblyInfo.cs @@ -3,6 +3,7 @@ [module: SkipLocalsInit] [assembly: InternalsVisibleTo("ComputeSharp.Dynamic")] +[assembly: InternalsVisibleTo("ComputeSharp.Pix")] [assembly: InternalsVisibleTo("ComputeSharp.Uwp")] [assembly: InternalsVisibleTo("ComputeSharp.WinUI")] diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index fc3b0a40..46c03c31 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -81,7 +81,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, null); + ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized); } @@ -101,7 +101,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, null); + ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value); } @@ -275,7 +275,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, null); + ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); commandList.D3D12GraphicsCommandList->TransitionBarrier(d3D12Resource, d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); } @@ -345,7 +345,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// The input to load. /// A reference to the instance to use. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe ref CommandList GetCommandList(in ComputeContext @this, ID3D12PipelineState* pipelineState) + internal static unsafe ref CommandList GetCommandList(in ComputeContext @this, ID3D12PipelineState* pipelineState) { ref ComputeContext context = ref Unsafe.AsRef(in @this);