From 7e4d5dff53c7a590b471bf452b37f626e71f2be6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 23:25:04 +0200 Subject: [PATCH] Code refactoring --- .../Buffers/ReadOnlyBuffer{T}.cs | 2 +- .../Buffers/ReadWriteBuffer{T}.cs | 6 +- .../Commands/CommandList.cs | 7 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 159 ++++++++---------- src/ComputeSharp.Shaders/ShaderRunner.cs | 2 +- 5 files changed, 82 insertions(+), 94 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 678f245e..89ebb6dd 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -120,7 +120,7 @@ namespace ComputeSharp.Graphics.Buffers using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes); - copyCommandList.Flush(true); + copyCommandList.Flush(); } else SetDataWithCpuBuffer(buffer); } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index 165d73b0..e93159d4 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -39,7 +39,7 @@ namespace ComputeSharp.Graphics.Buffers using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(this, 0, readbackBaffer, 0, SizeInBytes); - copyCommandList.Flush(true); + copyCommandList.Flush(); readbackBaffer.Map(0); MemoryHelper.Copy(readbackBaffer.MappedResource, span); @@ -58,7 +58,7 @@ namespace ComputeSharp.Graphics.Buffers using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes); - copyCommandList.Flush(true); + copyCommandList.Flush(); } /// @@ -70,7 +70,7 @@ namespace ComputeSharp.Graphics.Buffers using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes); - copyCommandList.Flush(true); + copyCommandList.Flush(); } else SetDataWithCpuBuffer(buffer); } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index fe5a0d9c..175f3717 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -87,13 +87,12 @@ namespace ComputeSharp.Graphics.Commands } /// - /// Executes the pending operations and optionally waits for them to be completed + /// Executes the pending operations and waits for them to be completed /// - /// Indicates whether or not to wait for the completion of the scheduled operations - public void Flush(bool wait = false) + public void Flush() { Close(); - GraphicsDevice.ExecuteCommandLists(wait, this); + GraphicsDevice.ExecuteCommandList(this); } /// diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 330ec92e..dc23ecf3 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -103,6 +103,80 @@ namespace ComputeSharp.Graphics return NativeDevice.CreateRootSignature(rootSignatureDescription.Serialize()); } + /// + /// Executes a and waits for the GPU to finish processing it + /// + /// The input to execute + internal void ExecuteCommandList(CommandList commandList) + { + Fence fence = commandList.CommandListType switch + { + CommandListType.Direct => NativeDirectFence, + CommandListType.Compute => NativeComputeFence, + CommandListType.Copy => NativeCopyFence, + _ => throw new NotSupportedException("This command list type is not supported.") + }; + + long fenceValue = GetFenceValueForCommandList(commandList); + + if (fenceValue <= fence.CompletedValue) return; + + lock (fence) + { + fence.SetEventOnCompletion(fenceValue, fenceEvent.SafeWaitHandle.DangerousGetHandle()); + fenceEvent.WaitOne(); + } + } + + /// + /// Executes a and, signals and retrieves the following fence value + /// + /// The input to execute + /// The value of the new fence to wait for + private long GetFenceValueForCommandList(CommandList commandList) + { + CommandAllocatorPool commandAllocatorPool; + CommandQueue commandQueue; + Fence fence; + long fenceValue; + + switch (commandList.CommandListType) + { + case CommandListType.Compute: + commandAllocatorPool = ComputeAllocatorPool; + commandQueue = NativeComputeCommandQueue; + fence = NativeComputeFence; + fenceValue = NextComputeFenceValue; + + NextDirectFenceValue++; + break; + case CommandListType.Copy: + commandAllocatorPool = CopyAllocatorPool; + commandQueue = NativeCopyCommandQueue; + fence = NativeCopyFence; + fenceValue = NextCopyFenceValue; + + NextCopyFenceValue++; + break; + case CommandListType.Direct: + commandAllocatorPool = DirectAllocatorPool; + commandQueue = NativeDirectCommandQueue; + fence = NativeDirectFence; + fenceValue = NextDirectFenceValue; + + NextDirectFenceValue++; + break; + default: throw new NotSupportedException($"Unsupported command list of type {commandList.CommandListType}"); + } + + commandAllocatorPool.Enqueue(commandList.CommandAllocator, fenceValue); + commandQueue.ExecuteCommandLists(commandList.NativeCommandList); + commandQueue.Signal(fence, fenceValue); + + return fenceValue; + } + + /// public void Dispose() { NativeDirectCommandQueue.Signal(NativeDirectFence, NextDirectFenceValue); @@ -130,90 +204,5 @@ namespace ComputeSharp.Graphics NativeDevice.Dispose(); } - - internal void ExecuteCommandLists(bool wait, params CommandList[] commandLists) - { - Fence fence = commandLists[0].CommandListType switch - { - CommandListType.Direct => NativeDirectFence, - CommandListType.Compute => NativeComputeFence, - CommandListType.Copy => NativeCopyFence, - _ => throw new NotSupportedException("This command list type is not supported.") - }; - - long fenceValue = ExecuteCommandLists(commandLists); - - if (wait) - { - WaitForFence(fence, fenceValue); - } - } - - internal long ExecuteCommandLists(params CommandList[] commandLists) - { - CommandAllocatorPool commandAllocatorPool; - CommandQueue commandQueue; - Fence fence; - long fenceValue; - - switch (commandLists[0].CommandListType) - { - case CommandListType.Compute: - commandAllocatorPool = ComputeAllocatorPool; - commandQueue = NativeComputeCommandQueue; - - fence = NativeComputeFence; - fenceValue = NextComputeFenceValue; - NextDirectFenceValue++; - break; - case CommandListType.Copy: - commandAllocatorPool = CopyAllocatorPool; - commandQueue = NativeCopyCommandQueue; - - fence = NativeCopyFence; - fenceValue = NextCopyFenceValue; - NextCopyFenceValue++; - break; - case CommandListType.Direct: - commandAllocatorPool = DirectAllocatorPool; - commandQueue = NativeDirectCommandQueue; - - fence = NativeDirectFence; - fenceValue = NextDirectFenceValue; - NextDirectFenceValue++; - break; - default: - throw new NotSupportedException("This command list type is not supported."); - } - - SharpDX.Direct3D12.CommandList[] nativeCommandLists = new SharpDX.Direct3D12.CommandList[commandLists.Length]; - - for (int i = 0; i < commandLists.Length; i++) - { - nativeCommandLists[i] = commandLists[i].NativeCommandList; - commandAllocatorPool.Enqueue(commandLists[i].CommandAllocator, fenceValue); - } - - commandQueue.ExecuteCommandLists(nativeCommandLists); - commandQueue.Signal(fence, fenceValue); - - return fenceValue; - } - - internal bool IsFenceComplete(Fence fence, long fenceValue) - { - return fenceValue <= fence.CompletedValue; - } - - internal void WaitForFence(Fence fence, long fenceValue) - { - if (IsFenceComplete(fence, fenceValue)) return; - - lock (fence) - { - fence.SetEventOnCompletion(fenceValue, fenceEvent.SafeWaitHandle.DangerousGetHandle()); - fenceEvent.WaitOne(); - } - } } } diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index e916a35d..b32694e2 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -72,7 +72,7 @@ namespace ComputeSharp.Shaders // Dispatch and wait for completion commandList.Dispatch(groupsX, groupsY, groupsZ); - commandList.Flush(true); + commandList.Flush(); } } }