From 808d4489beb07abb81532debe1883757a11ea91c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Nov 2020 01:17:06 +0100 Subject: [PATCH] Minor code refactoring --- .../Buffers/Buffer{T}.cs | 29 +------------ .../Extensions/ID3D12DeviceExtensions.cs | 42 ++++++++++++------- 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs index 872c4d58..a189aff4 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs @@ -3,9 +3,6 @@ using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop; -using static TerraFX.Interop.D3D12_HEAP_TYPE; -using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; -using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; using FX = TerraFX.Interop.Windows; @@ -63,7 +60,7 @@ namespace ComputeSharp.Graphics.Buffers /// The buffer type for the current buffer internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) { - this.d3D12Resource = CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(bufferType, sizeInBytes); Size = size; SizeInBytes = sizeInBytes; @@ -106,30 +103,6 @@ namespace ComputeSharp.Graphics.Buffers /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Creates a committed resource for a given buffer type. - /// - /// The instance in use. - /// The buffer type currently in use. - /// The size in bytes of the current buffer. - /// An reference for the current buffer. - private static ComPtr CreateCommittedResource(ID3D12Device* d3D12Device, BufferType bufferType, int sizeInBytes) - { - (D3D12_HEAP_TYPE d3D12HeapType, - D3D12_RESOURCE_FLAGS d3D12ResourceFlags, - D3D12_RESOURCE_STATES d3D12ResourceStates) = bufferType switch - { - BufferType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), - BufferType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), - BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON), - BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST), - BufferType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), - _ => throw null! - }; - - return d3D12Device->CreateCommittedResource(d3D12HeapType, (uint)sizeInBytes, d3D12ResourceFlags, d3D12ResourceStates); - } - /// /// Creates a view for a constant buffer. /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 792adf09..38cb3e7c 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -1,6 +1,7 @@ -using ComputeSharp.Graphics.Helpers; -using System; +using System; using System.Diagnostics.Contracts; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Helpers; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY; @@ -9,6 +10,9 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; +using static TerraFX.Interop.D3D12_HEAP_TYPE; +using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; +using static TerraFX.Interop.D3D12_RESOURCE_STATES; using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Extensions @@ -92,19 +96,29 @@ namespace ComputeSharp.Graphics.Extensions } /// - /// Creates a new for a given device. + /// Creates a committed resource for a given buffer type. /// - /// The target to use to create the descriptor heap. - /// The number of descriptors to allocate. - /// A pointer to the newly allocated instance. - /// Thrown when the creation of the command queue fails. + /// The instance in use. + /// The buffer type currently in use. + /// The size in bytes of the current buffer. + /// An reference for the current buffer. public static ComPtr CreateCommittedResource( - this ref ID3D12Device d3d12device, - D3D12_HEAP_TYPE d3D12HeapType, - ulong width, - D3D12_RESOURCE_FLAGS d3D12ResourceFlags, - D3D12_RESOURCE_STATES d3D12ResourceStates) + this ref ID3D12Device d3D12Device, + BufferType bufferType, + int sizeInBytes) { + (D3D12_HEAP_TYPE d3D12HeapType, + D3D12_RESOURCE_FLAGS d3D12ResourceFlags, + D3D12_RESOURCE_STATES d3D12ResourceStates) = bufferType switch + { + BufferType.Constant => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), + BufferType.ReadOnly => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), + BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON), + BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST), + BufferType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ), + _ => throw null! + }; + using ComPtr d3D12Resource = default; D3D12_HEAP_PROPERTIES d3D12HeapProperties; @@ -113,10 +127,10 @@ namespace ComputeSharp.Graphics.Extensions d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; d3D12HeapProperties.CreationNodeMask = 1; d3D12HeapProperties.VisibleNodeMask = 1; - D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(width, d3D12ResourceFlags); + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer((uint)sizeInBytes, d3D12ResourceFlags); Guid d3D12ResourceGuid = FX.IID_ID3D12Resource; - d3d12device.CreateCommittedResource( + d3D12Device.CreateCommittedResource( &d3D12HeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &d3D12ResourceDescription,