From 9b0118bc5bfdd7e41e295dcdcea01681612708b9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 14 Nov 2020 23:07:07 +0100 Subject: [PATCH] Minor code refactoring --- .../Commands/CommandAllocatorPool2.cs | 16 +++---------- .../Extensions/ID3D12DeviceExtensions.cs | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs index 13243008..e0bed7fe 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs @@ -1,4 +1,5 @@ -using System; +using ComputeSharp.Graphics.Extensions; +using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; @@ -69,18 +70,7 @@ namespace ComputeSharp.Graphics.Commands } } - // Create a new command allocator - ID3D12CommandAllocator* d3D12CommandAllocator; - Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator; - - int result = d3D12Device->CreateCommandAllocator( - this.d3d12CommandListType, - &d3D12CommandAllocatorGuid, - (void**)&d3D12CommandAllocator); - - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); - - return d3D12CommandAllocator; + return d3D12Device->CreateCommandAllocator(this.d3d12CommandListType); } } diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 6fa698c7..a36ce53c 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -132,6 +132,30 @@ namespace ComputeSharp.Graphics.Extensions return d3D12Resource; } + /// + /// Creates a new for a given device. + /// + /// The target to use to create the command allocator. + /// The type of command list to create. + /// A pointer to the newly allocated instance. + /// Thrown when the creation of the command allocator fails. + public static ID3D12CommandAllocator* CreateCommandAllocator( + this ref ID3D12Device d3d12device, + D3D12_COMMAND_LIST_TYPE d3d12CommandListType) + { + ID3D12CommandAllocator* d3D12CommandAllocator; + Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator; + + int result = d3d12device.CreateCommandAllocator( + d3d12CommandListType, + &d3D12CommandAllocatorGuid, + (void**)&d3D12CommandAllocator); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3D12CommandAllocator; + } + /// /// Checks the feature support of a given type for a given device. ///