diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index b1c69ab2..3ea01570 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -43,7 +43,7 @@ public ref struct ComputeContext /// The number of iterations to run on the Y axis. /// The number of iterations to run on the Z axis. /// The input instance representing the compute shader to run. - internal unsafe void Run(int x, int y, int z, ref T shader) + internal readonly unsafe void Run(int x, int y, int z, ref T shader) where T : struct, IComputeShader { bool xIs1 = x == 1; @@ -99,7 +99,7 @@ public ref struct ComputeContext /// The number of threads in each thread group for the Y axis. /// The number of threads in each thread group for the Z axis. /// The input instance representing the compute shader to run. - internal unsafe void Run( + internal readonly unsafe void Run( int x, int y, int z, @@ -127,7 +127,7 @@ public ref struct ComputeContext PipelineData pipelineData = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); - ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState); + ref CommandList commandList = ref GetCommandList(in this, this.device, pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); @@ -145,7 +145,7 @@ public ref struct ComputeContext /// The type of pixel to work on. /// The target texture to invoke the pixel shader upon. /// The input instance representing the pixel shader to run. - internal unsafe void Run(IReadWriteTexture2D texture, ref T shader) + internal readonly unsafe void Run(IReadWriteTexture2D texture, ref T shader) where T : struct, IPixelShader where TPixel : unmanaged { @@ -162,7 +162,7 @@ public ref struct ComputeContext PipelineData pipelineData = ShaderRunner.GetPipelineData(this.device, threadsX, threadsY, threadsZ, ref shader); - ref CommandList commandList = ref GetCommandList(ref this, this.device, pipelineData.D3D12PipelineState); + ref CommandList commandList = ref GetCommandList(in this, this.device, pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); @@ -197,17 +197,23 @@ public ref struct ComputeContext /// The input to load. /// A reference to the instance to use. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe ref CommandList GetCommandList(ref ComputeContext @this, GraphicsDevice device, ID3D12PipelineState* pipelineState) + private static unsafe ref CommandList GetCommandList(in ComputeContext @this, GraphicsDevice device, ID3D12PipelineState* pipelineState) { - if (@this.commandList.IsAllocated) + // 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. This delegate + // cast is necessary to work around the fact that ref structs cannot currently be used as type arguments. + ref ComputeContext context = ref ((delegate*)(delegate*)&Unsafe.AsRef)(in @this); + + if (context.commandList.IsAllocated) { - @this.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); } else { - @this.commandList = new CommandList(device, pipelineState); + context.commandList = new CommandList(device, pipelineState); } - return ref @this.commandList; + return ref context.commandList; } }