From e35bfd830a4e7a8066dbc2609deeee4a7cb75e14 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Aug 2022 16:27:09 +0200 Subject: [PATCH] Add [UnscopedRef] to ComputeContext.GetCommandList APIs --- .../ComputeContextExtensions.cs | 26 +++++++-------- src/ComputeSharp/Shaders/ComputeContext.cs | 32 +++++++++---------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/ComputeSharp.Pix/ComputeContextExtensions.cs b/src/ComputeSharp.Pix/ComputeContextExtensions.cs index 16de0c2a..73a4a69b 100644 --- a/src/ComputeSharp.Pix/ComputeContextExtensions.cs +++ b/src/ComputeSharp.Pix/ComputeContextExtensions.cs @@ -20,7 +20,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEvent(this in ComputeContext context, Color color, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); int argb = color.ToArgb(); uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); @@ -37,7 +37,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEvent(this in ComputeContext context, byte index, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); uint pixColor = Pix.PIX_COLOR_INDEX(index); @@ -52,7 +52,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEvent(this in ComputeContext context, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->BeginEvent(0, message); } @@ -78,7 +78,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEventUnsafe(this in ComputeContext context, Color color, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); int argb = color.ToArgb(); uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); @@ -107,7 +107,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEventUnsafe(this in ComputeContext context, byte index, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); uint pixColor = Pix.PIX_COLOR_INDEX(index); @@ -134,7 +134,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void BeginEventUnsafe(this in ComputeContext context, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->BeginEventUnsafe(0, message); } @@ -146,7 +146,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void EndEvent(this in ComputeContext context) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); Pix.PIXEndEventOnCommandList(commandList.D3D12GraphicsCommandList); } @@ -160,7 +160,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void Log(this in ComputeContext context, Color color, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); int argb = color.ToArgb(); uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); @@ -177,7 +177,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void Log(this in ComputeContext context, byte index, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); uint pixColor = Pix.PIX_COLOR_INDEX(index); @@ -192,7 +192,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void Log(this in ComputeContext context, string message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->SetPixMarker(0, message); } @@ -218,7 +218,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void LogUnsafe(this in ComputeContext context, Color color, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); int argb = color.ToArgb(); uint pixColor = Pix.PIX_COLOR((byte)((argb & 0x00FF0000) >> 16), (byte)((argb & 0x0000FF00) >> 8), (byte)(argb & 0x000000FF)); @@ -247,7 +247,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void LogUnsafe(this in ComputeContext context, byte index, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); uint pixColor = Pix.PIX_COLOR_INDEX(index); @@ -274,7 +274,7 @@ public static class ComputeContextExtensions [Conditional("USE_PIX")] public static unsafe void LogUnsafe(this in ComputeContext context, ReadOnlySpan message) { - ref CommandList commandList = ref ComputeContext.GetCommandList(in context, pipelineState: null); + ref CommandList commandList = ref context.GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->SetPixMarkerUnsafe(0, message); } diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index ef62fe51..951b348d 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -75,7 +75,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this); + ref CommandList commandList = ref GetCommandList(); commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); } @@ -95,7 +95,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); + ref CommandList commandList = ref GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized); } @@ -115,7 +115,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); + ref CommandList commandList = ref GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value); } @@ -200,7 +200,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); - ref CommandList commandList = ref GetCommandList(in this, pipelineData.D3D12PipelineState); + ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); @@ -237,7 +237,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable PipelineData pipelineData = PipelineDataLoader.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); - ref CommandList commandList = ref GetCommandList(in this, pipelineData.D3D12PipelineState); + ref CommandList commandList = ref GetCommandList(pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); @@ -266,7 +266,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this, pipelineState: null); + ref CommandList commandList = ref GetCommandList(pipelineState: null); commandList.D3D12GraphicsCommandList->TransitionBarrier(d3D12Resource, d3D12ResourceStatesBefore, d3D12ResourceStatesAfter); } @@ -332,16 +332,16 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// /// Gets the current instance. /// - /// The current instance. /// A reference to the instance to use. /// Thrown if the has not been initialized yet. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe ref CommandList GetCommandList(in ComputeContext @this) + [UnscopedRef] + private readonly unsafe ref CommandList GetCommandList() { // This method has to take the context by readonly reference to allow callers to be marked as readonly. // This is needed to skip the hidden copies done by Roslyn, which would break the dispatching, as the // original context would not see the changes done by the following queued dispatches. - ref CommandList commandList = ref Unsafe.AsRef(in @this).commandList; + ref CommandList commandList = ref Unsafe.AsRef(in this.commandList); if (!commandList.IsAllocated) { @@ -354,29 +354,29 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// /// Gets the current instance, and initializes it as needed. /// - /// The current instance. /// The input to load. /// A reference to the instance to use. [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static unsafe ref CommandList GetCommandList(in ComputeContext @this, ID3D12PipelineState* pipelineState) + [UnscopedRef] + internal readonly unsafe ref CommandList GetCommandList(ID3D12PipelineState* pipelineState) { - ref ComputeContext context = ref Unsafe.AsRef(in @this); + ref CommandList commandList = ref Unsafe.AsRef(in this.commandList); - if (context.commandList.IsAllocated) + if (commandList.IsAllocated) { // Skip setting the pipeline state if the new state is null. This is the case when the upcoming // operation is not a shader dispatch, but just a resource clear. In this case there is no state. if (pipelineState is not null) { - context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); } } else { - context.commandList = new CommandList(context.device!, pipelineState); + commandList = new CommandList(this.device!, pipelineState); } - return ref context.commandList; + return ref commandList; } ///