diff --git a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
index d0a12488..fe671afa 100644
--- a/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
+++ b/src/ComputeSharp/Graphics/Extensions/Texture2DExtensions.cs
@@ -29,6 +29,33 @@ public static class Texture2DExtensions
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 width of the memory area to copy.
+ /// The height of the memory area to copy.
+ /// A array with the contents of the current texture.
+ [Pure]
+ public static unsafe T[,] ToArray(this Texture2D source, int x, int y, int width, int height)
+ where T : unmanaged
+ {
+ Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width));
+ Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height));
+
+ T[,] data = new T[height, width];
+
+ fixed (T* p = data)
+ {
+ source.CopyTo(new Span(p, width * height), x, y, width, height);
+ }
+
+ 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 04c018aa..fb2934d3 100644
--- a/tests/ComputeSharp.Tests/Texture2DTests.cs
+++ b/tests/ComputeSharp.Tests/Texture2DTests.cs
@@ -112,6 +112,56 @@ public partial class Texture2DTests
texture.Dispose();
}
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture2D<>))]
+ [Resource(typeof(ReadWriteTexture2D<>))]
+ [Data(0, 0, 64, 64)]
+ [Data(0, 14, 64, 50)]
+ [Data(14, 0, 50, 64)]
+ [Data(10, 10, 54, 54)]
+ [Data(20, 20, 32, 27)]
+ [Data(60, 0, 4, 64)]
+ [Data(0, 60, 64, 4)]
+ [Data(63, 2, 1, 60)]
+ [Data(2, 63, 60, 1)]
+ public void GetData_RangeToArray_Ok(Device device, Type textureType, int x, int y, int width, int height)
+ {
+ float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
+
+ using Texture2D texture = device.Get().AllocateTexture2D(textureType, array, 64, 64);
+
+ float[,] result = texture.ToArray(x, y, width, height);
+
+ Assert.AreEqual(height, result.GetLength(0));
+ Assert.AreEqual(width, result.GetLength(1));
+
+ Span2D expected = new Span2D(array, 64, 64).Slice(y, x, height, width);
+ Span2D data = new(result);
+
+ CollectionAssert.AreEqual(expected.ToArray(), data.ToArray());
+ }
+
+ [CombinatorialTestMethod]
+ [AllDevices]
+ [Resource(typeof(ReadOnlyTexture2D<>))]
+ [Resource(typeof(ReadWriteTexture2D<>))]
+ [Data(0, -1, 50, 50)]
+ [Data(-1, 0, 50, 50)]
+ [Data(12, 0, -1, 50)]
+ [Data(12, 0, 20, -1)]
+ [Data(12, 20, 20, 50)]
+ [Data(12, 20, 60, 20)]
+ [Data(80, 20, 40, 20)]
+ [Data(0, 80, 40, 20)]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetData_RangeToArray_Fail(Device device, Type textureType, int x, int y, int width, int height)
+ {
+ using Texture2D texture = device.Get().AllocateTexture2D(textureType, 64, 64);
+
+ _ = texture.ToArray(x, y, width, height);
+ }
+
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture2D<>))]