From 2a381438019d7d063d2ef38476cb43e90074d7e4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 3 Dec 2020 14:25:29 +0100 Subject: [PATCH] Minor code refactoring --- .../Buffers/Abstract/StructuredBuffer{T}.cs | 6 +- .../Buffers/Abstract/Texture2D{T}.cs | 4 +- .../Buffers/ConstantBuffer{T}.cs | 2 +- .../Commands/CommandList.cs | 76 +++++----------- .../ID3D12GraphicsCommandListExtensions.cs | 91 +++++++++++++++++++ 5 files changed, 119 insertions(+), 60 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 3c89e089..2d448ca2 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -47,7 +47,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0, (ulong)byteSize); + copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset,(ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -79,7 +79,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyBufferRegion(d3D12Resource.Get(), 0, D3D12Resource, (ulong)byteOffset, (ulong)byteSize); + copyCommandList.CopyBufferRegion(D3D12Resource, (ulong)byteOffset, d3D12Resource.Get(), 0,(ulong)byteSize); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -98,7 +98,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract // Directly copy the input buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyBufferRegion(source.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes); + copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); copyCommandList.ExecuteAndWaitForCompletion(); } else SetDataWithCpuBuffer(source); diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index f45c51f0..7b1645d7 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -231,7 +231,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.CopyTextureToBufferRegion(D3D12Resource, (uint)x, (uint)y, (uint)width, (uint)height, (uint)Unsafe.SizeOf(), d3D12Resource.Get(), DXGIFormatHelper.GetForType()); + copyCommandList.CopyTextureRegion(d3D12Resource.Get(), (uint)Unsafe.SizeOf(), D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, (uint)width, (uint)height); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -368,7 +368,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyBufferToTextureRegion(d3D12Resource.Get(), (uint)x, (uint)y, (uint)width, (uint)height, (uint)Unsafe.SizeOf(), D3D12Resource, DXGIFormatHelper.GetForType()); + copyCommandList.CopyTextureRegion(D3D12Resource, DXGIFormatHelper.GetForType(), (uint)x, (uint)y, d3D12Resource.Get(), (uint)width, (uint)height, (uint)Unsafe.SizeOf()); copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 2811f136..6a525432 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -130,7 +130,7 @@ namespace ComputeSharp // Directly copy the input buffer, if possible using CommandList copyCommandList = new CommandList(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyBufferRegion(source.D3D12Resource, 0, D3D12Resource, 0, (ulong)SizeInBytes); + copyCommandList.CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); copyCommandList.ExecuteAndWaitForCompletion(); } else SetDataWithCpuBuffer(source); diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 6649062e..f41999e1 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -5,7 +5,6 @@ using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; -using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Commands { @@ -82,12 +81,12 @@ namespace ComputeSharp.Graphics.Commands /// /// Copies a memory region from one resource to another. /// - /// The source to read from. - /// The starting offset to read the source resource from. /// The destination to write to. /// The starting offset to write the destination resource from. + /// The source to read from. + /// The starting offset to read the source resource from. /// The total number of bytes to copy from one resource to another. - public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ulong numBytes) + public readonly void CopyBufferRegion(ID3D12Resource* d3d12ResourceDestination, ulong destinationOffset, ID3D12Resource* d3D12ResourceSource, ulong sourceOffset, ulong numBytes) { this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes); } @@ -95,81 +94,50 @@ namespace ComputeSharp.Graphics.Commands /// /// Copies a texture memory region from one resource (a buffer) to another (a texture). /// - /// The source (a buffer) to read from. + /// The destination (a texture) to write to. + /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. + /// The source (a buffer) to read from. /// The width of the memory area to write to. /// The height of the memory area to write to. /// The size of each element to copy. - /// The destination (a texture) to write to. - /// The parameter curreently in use by the texture. - public readonly void CopyBufferToTextureRegion( - ID3D12Resource* d3D12ResourceSource, + + public readonly void CopyTextureRegion( + ID3D12Resource* d3d12ResourceDestination, + DXGI_FORMAT dxgiFormat, uint x, uint y, + ID3D12Resource* d3D12ResourceSource, uint width, uint height, - uint elementSizeInBytes, - ID3D12Resource* d3d12ResourceDestination, - DXGI_FORMAT dxgiFormat) + uint elementSizeInBytes) { - D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( - dxgiFormat, - width, - height, - 1, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); - - D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; - d3D12PlacedSubresourceFootprint.Offset = 0; - d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; - - D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, 0), - d3D12TextureCopyLocationSource = new(d3D12ResourceSource, d3D12PlacedSubresourceFootprint); - - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, 0, &d3D12TextureCopyLocationSource, null); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, dxgiFormat, x, y, d3D12ResourceSource, width, height, elementSizeInBytes); } /// /// Copies a texture memory region from one resource (a texture) to another (a buffer). /// + /// The destination (a buffer) to write to. + /// The size of each element to copy. /// The source (a texture) to read from. + /// The parameter curreently in use by the texture. /// The horizontal offset in the destination texture. /// The vertical offset in the destination texture. /// The width of the memory area to write to. /// The height of the memory area to write to. - /// The size of each element to copy. - /// The destination (a buffer) to write to. - /// The parameter curreently in use by the texture. - public readonly void CopyTextureToBufferRegion( + public readonly void CopyTextureRegion( + ID3D12Resource* d3d12ResourceDestination, + uint elementSizeInBytes, ID3D12Resource* d3D12ResourceSource, + DXGI_FORMAT dxgiFormat, uint x, uint y, uint width, - uint height, - uint elementSizeInBytes, - ID3D12Resource* d3d12ResourceDestination, - DXGI_FORMAT dxgiFormat) + uint height) { - D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( - dxgiFormat, - width, - height, - 1, - (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); - - D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; - d3D12PlacedSubresourceFootprint.Offset = 0; - d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; - - D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, d3D12PlacedSubresourceFootprint), - d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); - - D3D12_BOX d3D12Box = new((int)x, (int)y, (int)(x + width), (int)(y + height)); - - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(&d3D12TextureCopyLocationDestination, 0, 0, 0, &d3D12TextureCopyLocationSource, &d3D12Box); + this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(d3d12ResourceDestination, elementSizeInBytes, d3D12ResourceSource, dxgiFormat, x, y, width, height); } /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs new file mode 100644 index 00000000..84e71da4 --- /dev/null +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -0,0 +1,91 @@ +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Extensions +{ + /// + /// A with extensions for the type. + /// + internal static unsafe class ID3D12GraphicsCommandListExtensions + { + /// + /// Copies a texture memory region from one resource (a buffer) to another (a texture). + /// + /// The instance in use. + /// The destination (a texture) to write to. + /// The parameter curreently in use by the texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The source (a buffer) to read from. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The size of each element to copy. + + public static void CopyTextureRegion( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3d12ResourceDestination, + DXGI_FORMAT dxgiFormat, + uint x, + uint y, + ID3D12Resource* d3D12ResourceSource, + uint width, + uint height, + uint elementSizeInBytes) + { + D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( + dxgiFormat, + width, + height, + 1, + (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + d3D12PlacedSubresourceFootprint.Offset = 0; + d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; + D3D12_TEXTURE_COPY_LOCATION + d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, 0), + d3D12TextureCopyLocationSource = new(d3D12ResourceSource, d3D12PlacedSubresourceFootprint); + + d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, x, y, 0, &d3D12TextureCopyLocationSource, null); + } + + /// + /// Copies a texture memory region from one resource (a texture) to another (a buffer). + /// + /// The instance in use. + /// The destination (a buffer) to write to. + /// The size of each element to copy. + /// The source (a texture) to read from. + /// The parameter curreently in use by the texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + public static void CopyTextureRegion( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3d12ResourceDestination, + uint elementSizeInBytes, + ID3D12Resource* d3D12ResourceSource, + DXGI_FORMAT dxgiFormat, + uint x, + uint y, + uint width, + uint height) + { + D3D12_SUBRESOURCE_FOOTPRINT d3D12SubresourceFootprint = new( + dxgiFormat, + width, + height, + 1, + (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + d3D12PlacedSubresourceFootprint.Offset = 0; + d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; + D3D12_TEXTURE_COPY_LOCATION + d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination, d3D12PlacedSubresourceFootprint), + d3D12TextureCopyLocationSource = new(d3D12ResourceSource, 0); + D3D12_BOX d3D12Box = new((int)x, (int)y, (int)(x + width), (int)(y + height)); + + d3D12GraphicsCommandList.CopyTextureRegion(&d3D12TextureCopyLocationDestination, 0, 0, 0, &d3D12TextureCopyLocationSource, &d3D12Box); + } + } +}