From d935bc1a09b8c62c1b8107f02316869073473e9e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 31 Jul 2021 13:39:07 +0200 Subject: [PATCH] Reduced thread contention with parallel dispatches --- .../Commands/Interop/ID3D12CommandListPool.cs | 21 ++++++++++++++----- .../Extensions/Interop/HResultExtensions.cs | 2 ++ .../Helpers/DeviceHelper.IDXGIFactory6.cs | 3 ++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs index 565b3fc8..b8d3e991 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs @@ -45,15 +45,26 @@ namespace ComputeSharp.Graphics.Commands.Interop { if (this.d3D12CommandListBundleQueue.TryDequeue(out D3D12CommandListBundle d3D12CommandListBundle)) { - d3D12CommandListBundle.D3D12CommandAllocator->Reset().Assert(); - d3D12CommandListBundle.D3D12CommandList->Reset(d3D12CommandListBundle.D3D12CommandAllocator, d3D12PipelineState).Assert(); - d3D12CommandList = d3D12CommandListBundle.D3D12CommandList; d3D12CommandAllocator = d3D12CommandListBundle.D3D12CommandAllocator; - - return; } + else + { + d3D12CommandAllocator = null; + d3D12CommandList = null; + } + } + // Reset the command allocator and command list outside of the lock, or create a new pair if one to be reused + // wasn't available. These operations are relatively expensive, so doing so here reduces thread contention + // when multiple shader executions are being dispatched in parallel on the same device. + if (d3D12CommandAllocator is not null) + { + d3D12CommandAllocator->Reset().Assert(); + d3D12CommandList->Reset(d3D12CommandAllocator, d3D12PipelineState).Assert(); + } + else + { CreateCommandListAndAllocator(d3D12Device, d3D12PipelineState, out d3D12CommandList, out d3D12CommandAllocator); } } diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs index d3a5b855..ad12813a 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs @@ -1,7 +1,9 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; +#if DEBUG using ComputeSharp.Graphics.Helpers; +#endif using Microsoft.Toolkit.Diagnostics; using HRESULT = System.Int32; diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs index 1bd56c18..3e74affd 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; +using HRESULT = System.Int32; namespace ComputeSharp.Graphics.Helpers { @@ -33,7 +34,7 @@ namespace ComputeSharp.Graphics.Helpers FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof(), dxgiFactory4.GetVoidAddressOf()).Assert(); - int result = dxgiFactory4.CopyTo(dxgiFactory6); + HRESULT result = dxgiFactory4.CopyTo(dxgiFactory6); if (result == FX.S_OK) {