From 0e1a5bcbb95779fd01201100b20458788f07a6cf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 3 Dec 2020 13:58:40 +0100 Subject: [PATCH] Fixed Texture2D memory copy to/from buffers --- .../Buffers/Abstract/Texture2D{T}.cs | 20 ++-- .../Commands/CommandList.cs | 94 ++++++++++++++----- 2 files changed, 81 insertions(+), 33 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 52647fa8..f45c51f0 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -225,15 +225,13 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.ReadBack, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height); + nint byteSize = (nint)width * height * Unsafe.SizeOf(); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY)) { - copyCommandList.CopyTextureRegion(D3D12Resource, x, y, width, height, d3D12Resource.Get(), 0u, 0u); + copyCommandList.CopyTextureToBufferRegion(D3D12Resource, (uint)x, (uint)y, (uint)width, (uint)height, (uint)Unsafe.SizeOf(), d3D12Resource.Get(), DXGIFormatHelper.GetForType()); copyCommandList.ExecuteAndWaitForCompletion(); } @@ -359,11 +357,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource( - ResourceType.Upload, - DXGIFormatHelper.GetForType(), - (uint)width, - (uint)height); + nint byteSize = (nint)width * height * Unsafe.SizeOf(); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { @@ -372,7 +368,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); - copyCommandList.CopyTextureRegion(d3D12Resource.Get(), 0, 0, width, height, D3D12Resource, (uint)x, (uint)y); + copyCommandList.CopyBufferToTextureRegion(d3D12Resource.Get(), (uint)x, (uint)y, (uint)width, (uint)height, (uint)Unsafe.SizeOf(), D3D12Resource, DXGIFormatHelper.GetForType()); copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 1a323c7f..6649062e 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -5,6 +5,7 @@ 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 { @@ -92,32 +93,83 @@ namespace ComputeSharp.Graphics.Commands } /// - /// Copies a texture memory region from one resource to another. + /// Copies a texture memory region from one resource (a buffer) to another (a texture). /// - /// The source to read from. - /// The horizontal offset within . - /// The vertical offset within . - /// The width of the memory region to copy. - /// The height of the memory region to copy. - /// The destination to write to. - /// The horizontal offset within . - /// The vertical offset within . - public readonly void CopyTextureRegion( + /// The source (a buffer) to read from. + /// 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 texture) to write to. + /// The parameter curreently in use by the texture. + public readonly void CopyBufferToTextureRegion( ID3D12Resource* d3D12ResourceSource, - int sourceX, - int sourceY, - int sourceWidth, - int sourceHeight, + uint x, + uint y, + uint width, + uint height, + uint elementSizeInBytes, ID3D12Resource* d3d12ResourceDestination, - uint destinationX, - uint destinationY) + DXGI_FORMAT dxgiFormat) { - D3D12_TEXTURE_COPY_LOCATION - d3D12TextureCopyLocationSource = new(d3D12ResourceSource), - d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination); - D3D12_BOX d3D12Box = new(sourceX, sourceY, sourceX + sourceWidth, sourceY + sourceHeight); + 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)); - this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(&d3D12TextureCopyLocationDestination, destinationX, destinationY, 0, &d3D12TextureCopyLocationSource, &d3D12Box); + 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); + } + + /// + /// Copies a texture memory region from one resource (a texture) to another (a buffer). + /// + /// The source (a texture) to read from. + /// 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( + ID3D12Resource* d3D12ResourceSource, + uint x, + uint y, + uint width, + uint height, + uint elementSizeInBytes, + ID3D12Resource* d3d12ResourceDestination, + DXGI_FORMAT dxgiFormat) + { + 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); } ///