diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 5d32eb74..c909e598 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -12,6 +12,7 @@ using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_STATES; +using FX = TerraFX.Interop.Windows; namespace ComputeSharp.Graphics.Buffers.Abstract { @@ -237,9 +238,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination)); - nint byteSize = (nint)width * height * Unsafe.SizeOf(); + ulong + rowByteSize = (ulong)((nint)width * Unsafe.SizeOf()), + rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)), + resourceByteSize = rowPitchSize * (uint)height; - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, resourceByteSize); using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) { @@ -251,7 +255,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); - MemoryHelper.Copy(resource.Pointer, 0, destination); + MemoryHelper.Copy(resource.Pointer, width, height, rowByteSize, rowPitchSize, destination); } /// @@ -371,13 +375,16 @@ namespace ComputeSharp.Graphics.Buffers.Abstract Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source)); - nint byteSize = (nint)width * height * Unsafe.SizeOf(); + ulong + rowByteSize = (ulong)((nint)width * Unsafe.SizeOf()), + rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)), + resourceByteSize = rowPitchSize * (uint)height; - using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize); + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, resourceByteSize); using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) { - MemoryHelper.Copy(source, resource.Pointer, 0); + MemoryHelper.Copy(source, resource.Pointer, width, height, rowByteSize, rowPitchSize); } using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 41945ac2..9115977c 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -9,7 +9,7 @@ namespace ComputeSharp.Graphics.Helpers internal static class MemoryHelper { /// - /// Copies the content of a to the area pointed by an input value. + /// Copies the content of a to the area pointed by an input pointer. /// /// The type of values in the input . /// The source to read. @@ -23,10 +23,10 @@ namespace ComputeSharp.Graphics.Helpers } /// - /// Copies a memory area pointed by an value to a target . + /// Copies a memory area pointed by a pointer to a target . /// /// The type of values to read. - /// The that indicates the memory area to read from. + /// The pointer that indicates the memory area to read from. /// The source offset to start reading data from. /// The destination to write. [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -35,5 +35,77 @@ namespace ComputeSharp.Graphics.Helpers { new Span((T*)source + sourceOffset, destination.Length).CopyTo(destination); } + + /// + /// Copies the content of a to the 2D area pointed by an input pointer. + /// + /// The type of values to read. + /// The destination to read from. + /// The pointer that indicates the 2D memory area to write to. + /// The width of the 2D memory area to read. + /// The height of the 2D memory area to read. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + ReadOnlySpan source, + void* destination, + int width, + int height, + ulong widthInBytes, + ulong pitchInBytes) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + source.CopyTo(new Span(destination, width * height)); + } + else + { + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = source.Slice(i * width, width); + Span destinationRow = new((byte*)destination + (uint)i * pitchInBytes, width); + + sourceRow.CopyTo(destinationRow); + } + } + } + + /// + /// Copies a 2D memory area pointed by a pointer value to a target . + /// + /// The type of values to read. + /// The pointer that indicates the memory area to read from. + /// The width of the 2D memory area to read. + /// The height of the 2D memory area to read. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + /// The destination to write. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + void* source, + int width, + int height, + ulong widthInBytes, + ulong pitchInBytes, + Span destination) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + new Span(source, width * height).CopyTo(destination); + } + else + { + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = new((byte*)source + (uint)i * pitchInBytes, width); + Span destinationRow = destination.Slice(i * width, width); + + sourceRow.CopyTo(destinationRow); + } + } + } } }