From 3570b4d65e69efac53b0fe7d043a78a302b02054 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Nov 2021 17:27:52 +0100 Subject: [PATCH] Add new APIs to copy between Texture3D-s --- .../Extensions/Texture3DExtensions.cs | 102 +++++++ .../Resources/Abstract/Texture2D{T}.cs | 2 +- .../Resources/Abstract/Texture3D{T}.cs | 77 +++++ tests/ComputeSharp.Tests/Texture3DTests.cs | 283 ++++++++++++++++++ 4 files changed, 463 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs index c0622aeb..e9ee10de 100644 --- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs @@ -150,6 +150,57 @@ public static class Texture3DExtensions source.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, sourceOffsetX, sourceOffsetY, sourceOffsetZ, width, height, depth); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + public static void CopyTo(this Texture3D source, Texture3D destination) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The input instance to read data from. + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyTo(this Texture3D source, Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } + /// /// Reads the contents of a instance and writes them into a target instance. /// @@ -351,6 +402,57 @@ public static class Texture3DExtensions destination.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); } + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + public static void CopyFrom(this Texture3D destination, Texture3D source) + where T : unmanaged + { + source.CopyTo(destination, 0, 0, 0, 0, 0, 0, source.Width, source.Height, source.Depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, 0, 0, 0, width, height, depth); + } + + /// + /// Reads the contents of a instance and writes them into a target instance. + /// + /// The type of items stored on the texture. + /// The target instance to write data to. + /// The input instance to read data from. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offset in the destination texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + public static void CopyFrom(this Texture3D destination, Texture3D source, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + where T : unmanaged + { + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, width, height, depth); + } + /// /// Reads the contents of an instance and writes them into a target instance. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4c2f723e..efd1761e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -226,7 +226,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Reads the contents of the specified range from the current instance and writes them into a target instance. /// - /// The target instance to write data to. + /// The target instance to write data to. /// The horizontal offset within . /// The vertical offset within . /// The horizontal offset in the source texture. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 6e569b89..b2a26d23 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -240,6 +240,83 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + /// Reads the contents of the specified range from the current instance and writes them into a instance. + /// + /// The target instance to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The horizontal offset within . + /// The vertical offset within . + /// The depthwise offset within . + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + internal void CopyTo(Texture3D destination, int sourceOffsetX, int sourceOffsetY, int sourceOffsetZ, int destinationOffsetX, int destinationOffsetY, int destinationOffsetZ, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsInRange(sourceOffsetX, 0, Width, nameof(sourceOffsetX)); + Guard.IsInRange(sourceOffsetY, 0, Height, nameof(sourceOffsetY)); + Guard.IsInRange(sourceOffsetZ, 0, Depth, nameof(sourceOffsetZ)); + Guard.IsInRange(destinationOffsetX, 0, destination.Width, nameof(destinationOffsetX)); + Guard.IsInRange(destinationOffsetY, 0, destination.Height, nameof(destinationOffsetY)); + Guard.IsInRange(destinationOffsetZ, 0, destination.Depth, nameof(destinationOffsetZ)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(destinationOffsetX + width, 1, destination.Width, nameof(destinationOffsetX)); + Guard.IsBetweenOrEqualTo(destinationOffsetY + height, 1, destination.Height, nameof(destinationOffsetY)); + Guard.IsBetweenOrEqualTo(destinationOffsetZ + depth, 1, destination.Depth, nameof(destinationOffsetZ)); + Guard.IsLessThanOrEqualTo(sourceOffsetX + width, Width, nameof(sourceOffsetX)); + Guard.IsLessThanOrEqualTo(sourceOffsetY + height, Height, nameof(sourceOffsetY)); + Guard.IsLessThanOrEqualTo(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); + + D3D12_COMMAND_LIST_TYPE d3D12CommandListType = + this.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE || + destination.d3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE + ? D3D12_COMMAND_LIST_TYPE_COMPUTE + : D3D12_COMMAND_LIST_TYPE_COPY; + + using CommandList copyCommandList = new(GraphicsDevice, d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, destination.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion( + d3D12ResourceDestination: destination.D3D12Resource, + (uint)destinationOffsetX, + (uint)destinationOffsetY, + (ushort)destinationOffsetZ, + d3D12ResourceSource: D3D12Resource, + (uint)sourceOffsetX, + (uint)sourceOffsetY, + (ushort)sourceOffsetZ, + (uint)width, + (uint)height, + (ushort)depth); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + copyCommandList.D3D12GraphicsCommandList->ResourceBarrier(destination.D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, destination.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// /// Reads the contents of the specified range from the current instance and writes them into a instance. /// diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 2591ce8c..bab480cb 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -194,6 +194,289 @@ public partial class Texture3DTests texture.CopyTo(result, x, y, z, width, height, depth); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 3)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 1, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 1, 512, 512, 2)] + // [Data(512, 512, 4, 512, 512, 4, 0, 0, 1, 0, 0, 2, 512, 512, 2)] + // This test fails, only on WARP devices, and only if not run on its own. It seems + // to be possibly caused by a bug in ID3D12GraphicsCommandList::CopyTextureRegion. + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(512, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(1024, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 2)] + public void CopyTo_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth * sourceDepth).Select(static i => (float)i).ToArray(); + + using Texture3D source = device.Get().AllocateTexture3D(sourceType, array, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + + float[,,] stack = destination.ToArray(); + + for (int k = 0; k < destinationDepth; k++) + { + ReadOnlySpan2D result = new(stack, k); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (k >= destinationOffsetZ && + k < destinationOffsetZ + copyDepth && + i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceZ = k - destinationOffsetZ + sourceOffsetZ; + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + ReadOnlySpan2D expected = new(array, sourceZ * sourceHeight * sourceWidth, sourceHeight, sourceWidth, 0); + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 2055, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, -1, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, int.MaxValue, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 1025, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, -1, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, int.MaxValue, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, 6)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, -1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, int.MaxValue)] + [Data(512, 512, 3, 512, 512, 3, 450, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 450, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 5, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 450, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 450, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 4, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, -1, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, -1, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, -1, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, -1, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, -1, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, -1, 128, 128, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyTo_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + using Texture3D source = device.Get().AllocateTexture3D(sourceType, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + source.CopyTo(destination, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 3)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 1, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 1, 512, 512, 2)] + // [Data(512, 512, 4, 512, 512, 4, 0, 0, 1, 0, 0, 2, 512, 512, 2)] + // TODO: see explanation above in CopyTo_TextureToVoid_Ok + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(512, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(512, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 1)] + [Data(1024, 1024, 3, 512, 512, 3, 127, 256, 1, 128, 128, 1, 128, 256, 2)] + [Data(1024, 1024, 3, 512, 512, 3, 122, 329, 1, 127, 256, 2, 125, 200, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 1)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 0, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 0, 512, 0, 512, 512, 2)] + [Data(512, 512, 2, 1024, 1024, 3, 0, 0, 0, 512, 512, 0, 512, 512, 2)] + public void CopyFrom_TextureToVoid_Ok( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + float[] array = Enumerable.Range(0, sourceHeight * sourceWidth * sourceDepth).Select(static i => (float)i).ToArray(); + + using Texture3D source = device.Get().AllocateTexture3D(sourceType, array, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + + float[,,] stack = destination.ToArray(); + + for (int k = 0; k < destinationDepth; k++) + { + ReadOnlySpan2D result = new(stack, k); + + for (int i = 0; i < destinationHeight; i++) + { + for (int j = 0; j < destinationWidth; j++) + { + if (k >= destinationOffsetZ && + k < destinationOffsetZ + copyDepth && + i >= destinationOffsetY && + i < destinationOffsetY + copyHeight && + j >= destinationOffsetX && + j < destinationOffsetX + copyWidth) + { + int sourceZ = k - destinationOffsetZ + sourceOffsetZ; + int sourceY = i - destinationOffsetY + sourceOffsetY; + int sourceX = j - destinationOffsetX + sourceOffsetX; + + ReadOnlySpan2D expected = new(array, sourceZ * sourceHeight * sourceWidth, sourceHeight, sourceWidth, 0); + + Assert.AreEqual(expected[sourceY, sourceX], result[i, j]); + } + else + { + Assert.AreEqual(0, result[i, j]); + } + } + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [AdditionalResource(typeof(ReadOnlyTexture3D<>))] + [AdditionalResource(typeof(ReadWriteTexture3D<>))] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 2055, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, -1, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, int.MaxValue, 1024, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 1025, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, -1, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, int.MaxValue, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, 6)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, -1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 0, 256, 128, int.MaxValue)] + [Data(512, 512, 3, 512, 512, 3, 450, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 450, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 5, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 450, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 450, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, 4, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, -1, 0, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, -1, 0, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, -1, 0, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, -1, 0, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, -1, 0, 128, 128, 1)] + [Data(512, 512, 3, 512, 512, 3, 0, 0, 0, 0, 0, -1, 128, 128, 1)] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void CopyFrom_TextureToVoid_Fail( + Device device, + Type sourceType, + Type destinationType, + int sourceWidth, + int sourceHeight, + int sourceDepth, + int destinationWidth, + int destinationHeight, + int destinationDepth, + int sourceOffsetX, + int sourceOffsetY, + int sourceOffsetZ, + int destinationOffsetX, + int destinationOffsetY, + int destinationOffsetZ, + int copyWidth, + int copyHeight, + int copyDepth) + { + using Texture3D source = device.Get().AllocateTexture3D(sourceType, sourceWidth, sourceHeight, sourceDepth); + using Texture3D destination = device.Get().AllocateTexture3D(destinationType, destinationWidth, destinationHeight, destinationDepth, AllocationMode.Clear); + + destination.CopyFrom(source, sourceOffsetX, sourceOffsetY, sourceOffsetZ, destinationOffsetX, destinationOffsetY, destinationOffsetZ, copyWidth, copyHeight, copyDepth); + } + [CombinatorialTestMethod] [AllDevices] public void Dispatch_ReadOnlyTexture3D(Device device)