From 265a5d53b2fc7b6d2981f03d0b8b3bd04a1b2208 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 14 Nov 2020 16:28:30 +0100 Subject: [PATCH] Minor code refactoring --- .../DescriptorAllocator2.cs | 4 +-- .../ID3D12DeviceExtensions.cs} | 27 ++++++++++++------- src/ComputeSharp.Graphics/GraphicsDevice2.cs | 12 ++++----- 3 files changed, 25 insertions(+), 18 deletions(-) rename src/ComputeSharp.Graphics/{Helpers/D3D12Helper.cs => Extensions/ID3D12DeviceExtensions.cs} (76%) diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs index ecd83f64..1bf93610 100644 --- a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs @@ -1,4 +1,4 @@ -using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Extensions; using System; using TerraFX.Interop; @@ -50,7 +50,7 @@ namespace ComputeSharp.Graphics /// The instance to use public DescriptorAllocator2(ID3D12Device* device) { - D3D12DescriptorHeap = D3D12Helper.CreateDescriptorHeap(device, DescriptorsPerHeap); + D3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap); this.allocationLock = new object(); this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); diff --git a/src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs similarity index 76% rename from src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs rename to src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 0e384c7f..c852d8d1 100644 --- a/src/ComputeSharp.Graphics/Helpers/D3D12Helper.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -4,10 +4,10 @@ using System.Runtime.InteropServices; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; -namespace ComputeSharp.Graphics.Helpers +namespace ComputeSharp.Graphics.Extensions { /// - /// A with helper methods to perform some D3D12 operations. + /// A with extensions for the type. /// internal static unsafe class D3D12Helper { @@ -18,7 +18,7 @@ namespace ComputeSharp.Graphics.Helpers /// The type of command queue to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12CommandQueue* CreateCommandQueue(ID3D12Device* d3d12device, D3D12_COMMAND_LIST_TYPE type) + public static ID3D12CommandQueue* CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type) { D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc; d3d12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE; @@ -28,7 +28,7 @@ namespace ComputeSharp.Graphics.Helpers Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue; ID3D12CommandQueue* commandQueue; - int result = d3d12device->CreateCommandQueue( + int result = d3d12device.CreateCommandQueue( &d3d12CommandQueueDesc, &d3d12CommandQueueDescGuid, (void**)&commandQueue); @@ -44,12 +44,12 @@ namespace ComputeSharp.Graphics.Helpers /// The target to use to create the fence. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12Fence* CreateFence(ID3D12Device* d3d12device) + public static ID3D12Fence* CreateFence(this ref ID3D12Device d3d12device) { Guid d3D12FenceGuid = FX.IID_ID3D12Fence; ID3D12Fence* d3d12Fence; - int result = d3d12device->CreateFence( + int result = d3d12device.CreateFence( 0, D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE, &d3D12FenceGuid, @@ -67,7 +67,7 @@ namespace ComputeSharp.Graphics.Helpers /// The number of descriptors to allocate. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12DescriptorHeap* CreateDescriptorHeap(ID3D12Device* d3d12device, uint descriptorsCount) + public static ID3D12DescriptorHeap* CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount) { D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; @@ -77,7 +77,7 @@ namespace ComputeSharp.Graphics.Helpers Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; ID3D12DescriptorHeap* d3d12DescriptorHeap; - int result = d3d12device->CreateDescriptorHeap( + int result = d3d12device.CreateDescriptorHeap( &d3d12DescriptorHeapDesc, &d3d12DescriptorHeapDescGuid, (void**)&d3d12DescriptorHeap); @@ -87,13 +87,20 @@ namespace ComputeSharp.Graphics.Helpers return d3d12DescriptorHeap; } + /// + /// Checks the feature support of a given type for a given device. + /// + /// The type of feature support data to retrieve. + /// The target to use to check features for. + /// The type of features to check. + /// A value with the features data. [Pure] - public static unsafe TFeature CheckFeatureSupport(ID3D12Device* d3d12device, D3D12_FEATURE d3d12feature) + public static unsafe TFeature CheckFeatureSupport(this ref ID3D12Device d3d12device, D3D12_FEATURE d3d12feature) where TFeature : unmanaged { TFeature feature; - int result = d3d12device->CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)); + int result = d3d12device.CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)); if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs index 9f82ccd2..87107db4 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -1,6 +1,6 @@ using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Commands; -using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Extensions; using TerraFX.Interop; namespace ComputeSharp.Graphics @@ -68,10 +68,10 @@ namespace ComputeSharp.Graphics internal GraphicsDevice2(ID3D12Device* d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1) { D3D12Device = d3d12device; - D3D12ComputeCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE); - D3D12CopyCommandQueue = D3D12Helper.CreateCommandQueue(d3d12device, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY); - D3D12ComputeFence = D3D12Helper.CreateFence(d3d12device); - D3D12CopyFence = D3D12Helper.CreateFence(d3d12device); + D3D12ComputeCommandQueue = d3d12device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE); + D3D12CopyCommandQueue = d3d12device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY); + D3D12ComputeFence = d3d12device->CreateFence(); + D3D12CopyFence = d3d12device->CreateFence(); ComputeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COMPUTE); CopyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_COPY); ShaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device); @@ -79,7 +79,7 @@ namespace ComputeSharp.Graphics Name = new string((char*)dxgiDescription1->Description); MemorySize = dxgiDescription1->DedicatedVideoMemory; - var d3D12Options1Data = D3D12Helper.CheckFeatureSupport(d3d12device, D3D12_FEATURE.D3D12_FEATURE_D3D12_OPTIONS1); + var d3D12Options1Data = d3d12device->CheckFeatureSupport(D3D12_FEATURE.D3D12_FEATURE_D3D12_OPTIONS1); ComputeUnits = d3D12Options1Data.TotalLaneCount; WavefrontSize = d3D12Options1Data.WaveLaneCountMin;