diff --git a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs
index e9ee10de..e38b317d 100644
--- a/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Texture3DExtensions.cs
@@ -33,6 +33,35 @@ public static class Texture3DExtensions
return data;
}
+ ///
+ /// Reads the contents of the current instance and returns an array.
+ ///
+ /// The type of items stored on the texture.
+ /// 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.
+ /// A array with the contents of the current texture.
+ public static unsafe T[,,] ToArray(this Texture3D source, int x, int y, int z, int width, int height, int depth)
+ where T : unmanaged
+ {
+ Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width));
+ Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height));
+ Guard.IsGreaterThanOrEqualTo(depth, 0, nameof(depth));
+
+ T[,,] data = new T[depth, height, width];
+
+ fixed (T* p = data)
+ {
+ source.CopyTo(new Span(p, width * height * depth), x, y, z, width, height, depth);
+ }
+
+ return data;
+ }
+
///
/// Reads the contents of the current instance and writes them into a target array.
///
diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs
index fb2934d3..728369b9 100644
--- a/tests/ComputeSharp.Tests/Texture2DTests.cs
+++ b/tests/ComputeSharp.Tests/Texture2DTests.cs
@@ -146,8 +146,8 @@ public partial class Texture2DTests
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]
[Resource(typeof(ReadWriteTexture2D<>))]
- [Data(0, -1, 50, 50)]
[Data(-1, 0, 50, 50)]
+ [Data(0, -1, 50, 50)]
[Data(12, 0, -1, 50)]
[Data(12, 0, 20, -1)]
[Data(12, 20, 20, 50)]
diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs
index bab480cb..2797a423 100644
--- a/tests/ComputeSharp.Tests/Texture3DTests.cs
+++ b/tests/ComputeSharp.Tests/Texture3DTests.cs
@@ -131,6 +131,64 @@ public partial class Texture3DTests
texture.Dispose();
}
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture3D<>))]
+ [Resource(typeof(ReadWriteTexture3D<>))]
+ [Data(0, 0, 0, 64, 64, 8)]
+ [Data(0, 14, 0, 64, 50, 1)]
+ [Data(14, 0, 0, 50, 64, 1)]
+ [Data(0, 0, 3, 50, 64, 1)]
+ [Data(10, 10, 1, 54, 54, 4)]
+ [Data(20, 20, 3, 32, 27, 3)]
+ [Data(40, 2, 4, 4, 62, 4)]
+ [Data(0, 60, 1, 64, 4, 6)]
+ [Data(63, 2, 2, 1, 60, 5)]
+ [Data(2, 63, 7, 60, 1, 1)]
+ public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int y, int z, int width, int height, int depth)
+ {
+ float[] array = Enumerable.Range(0, 64 * 64 * 8).Select(static i => (float)i).ToArray();
+
+ using Texture3D texture = device.Get().AllocateTexture3D(textureType, array, 64, 64, 8);
+
+ float[,,] result = texture.ToArray(x, y, z, width, height, depth);
+
+ Assert.AreEqual(depth, result.GetLength(0));
+ Assert.AreEqual(height, result.GetLength(1));
+ Assert.AreEqual(width, result.GetLength(2));
+
+ for (int k = 0; k < depth; k++)
+ {
+ Span2D expected = new Span2D(array, 64 * 64 * (k + z), 64, 64, 0).Slice(y, x, height, width);
+ Span2D data = new(result, k);
+
+ CollectionAssert.AreEqual(expected.ToArray(), data.ToArray());
+ }
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture3D<>))]
+ [Resource(typeof(ReadWriteTexture3D<>))]
+ [Data(-1, 0, 0, 50, 50, 1)]
+ [Data(0, -1, 0, 50, 50, 1)]
+ [Data(0, 0, -1, 50, 50, 1)]
+ [Data(12, 0, 0, -1, 50, 1)]
+ [Data(12, 0, 0, 20, -1, 0)]
+ [Data(12, 0, 0, 20, 2, -1)]
+ [Data(12, 20, 0, 20, 50, 2)]
+ [Data(12, 20, 0, 60, 20, 3)]
+ [Data(12, 10, 6, 20, 20, 10)]
+ [Data(80, 20, 0, 40, 20, 1)]
+ [Data(0, 80, 0, 40, 20, 1)]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int y, int z, int width, int height, int depth)
+ {
+ using Texture3D texture = device.Get().AllocateTexture3D(textureType, 64, 64, 8);
+
+ _ = texture.ToArray(x, y, z, width, height, depth);
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture3D<>))]