diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
index 9ffb2d25..ff9ba47e 100644
--- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
+++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs
@@ -52,6 +52,28 @@ namespace ComputeSharp.Tests.Extensions
};
}
+ ///
+ /// Allocates a new instance of the specified type.
+ ///
+ /// The type of items in the texture.
+ /// The target instance to allocate the texture for.
+ /// The type of texture to allocate.
+ /// The width of the texture to create.
+ /// The height of the texture to create.
+ /// The depth of the texture to create.
+ /// A instance of the requested size.
+ [Pure]
+ public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth)
+ where T : unmanaged
+ {
+ return type switch
+ {
+ _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(width, height, depth),
+ _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(width, height, depth),
+ _ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
+ };
+ }
+
///
/// Allocates a new instance of the specified type.
///
@@ -95,6 +117,48 @@ namespace ComputeSharp.Tests.Extensions
};
}
+ ///
+ /// Allocates a new instance of the specified type.
+ ///
+ /// The type of items in the texture.
+ /// The target instance to allocate the texture for.
+ /// The type of buffer to allocate.
+ /// The data to load on the texture.
+ /// The width of the texture to create.
+ /// The height of the texture to create.
+ /// The depth of the texture to create.
+ /// A instance of the requested type.
+ [Pure]
+ public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[] data, int width, int height, int depth)
+ where T : unmanaged
+ {
+ return type switch
+ {
+ _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data, width, height, depth),
+ _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data, width, height, depth),
+ _ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
+ };
+ }
+
+ ///
+ /// Allocates a new instance of the specified type.
+ ///
+ /// The type of items in the texture.
+ /// The target instance to allocate the texture for.
+ /// The type of texture to allocate.
+ /// A instance of the requested size.
+ [Pure]
+ public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, T[,,] data)
+ where T : unmanaged
+ {
+ return type switch
+ {
+ _ when type == typeof(ReadOnlyTexture3D<>) => device.AllocateReadOnlyTexture3D(data),
+ _ when type == typeof(ReadWriteTexture3D<>) => device.AllocateReadWriteTexture3D(data),
+ _ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
+ };
+ }
+
///
/// Allocates a new instance of the specified type.
///
diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs
new file mode 100644
index 00000000..a6764388
--- /dev/null
+++ b/tests/ComputeSharp.Tests/Texture3DTests.cs
@@ -0,0 +1,175 @@
+using System;
+using System.Linq;
+using ComputeSharp.Graphics.Buffers.Abstract;
+using ComputeSharp.Tests.Extensions;
+using Microsoft.Toolkit.HighPerformance.Extensions;
+using Microsoft.Toolkit.HighPerformance.Memory;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.Tests
+{
+ [TestClass]
+ [TestCategory("Texture3D")]
+ public class Texture3DTests
+ {
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>))]
+ [DataRow(typeof(ReadWriteTexture3D<>))]
+ public void Allocate_Uninitialized_Ok(Type textureType)
+ {
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 64, 64, 12);
+
+ Assert.IsNotNull(texture);
+ Assert.AreEqual(texture.Width, 64);
+ Assert.AreEqual(texture.Height, 64);
+ Assert.AreEqual(texture.Depth, 12);
+ Assert.AreSame(texture.GraphicsDevice, Gpu.Default);
+ }
+
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 128, -14253, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 128, -1, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, -4314, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), -14, -53, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, -1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 2, 4, ushort.MaxValue + 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 128, -14253, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 128, -1, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, -4314, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), -14, -53, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, -1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 2, 4, ushort.MaxValue + 1)]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void Allocate_Uninitialized_Fail(Type textureType, int width, int height, int depth)
+ {
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, width, height, depth);
+ }
+
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 2, 12, 4)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 64, 64, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 64, 27, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 2, 12, 4)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 64, 64, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 64, 27, 3)]
+ public void Allocate_FromArray(Type textureType, int width, int height, int depth)
+ {
+ float[] data = Enumerable.Range(0, width * height * depth).Select(static i => (float)i).ToArray();
+
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, data, width, height, depth);
+
+ Assert.IsNotNull(texture);
+ Assert.AreEqual(texture.Width, width);
+ Assert.AreEqual(texture.Height, height);
+ Assert.AreEqual(texture.Depth, depth);
+ Assert.AreSame(texture.GraphicsDevice, Gpu.Default);
+
+ float[,,] result = texture.GetData();
+
+ Assert.IsNotNull(result);
+ Assert.AreEqual(result.GetLength(0), depth);
+ Assert.AreEqual(result.GetLength(1), height);
+ Assert.AreEqual(result.GetLength(2), width);
+ Assert.AreEqual(data.Length, result.Length);
+ Assert.IsTrue(data.SequenceEqual(result.Cast()));
+
+ Array.Clear(result, 0, result.Length);
+
+ float[] flat = new float[result.Length];
+
+ texture.GetData(flat);
+
+ CollectionAssert.AreEqual(flat, data);
+ }
+
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>))]
+ [DataRow(typeof(ReadWriteTexture3D<>))]
+ [ExpectedException(typeof(ObjectDisposedException))]
+ public void UsageAfterDispose(Type textureType)
+ {
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 10, 10, 4);
+
+ texture.Dispose();
+
+ _ = texture.GetData();
+ }
+
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, 0, 0, 64, 64, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 0, 64, 50, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 1, 64, 50, 2)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 14, 0, 1, 50, 64, 2)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 10, 10, 2, 54, 54, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 20, 20, 0, 32, 27, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 60, 0, 0, 4, 64, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, 60, 2, 64, 4, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 63, 2, 0, 1, 60, 2)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 2, 63, 1, 60, 1, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, 0, 0, 64, 64, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 0, 64, 50, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 1, 64, 50, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 14, 0, 1, 50, 64, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 10, 10, 2, 54, 54, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 20, 20, 0, 32, 27, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 60, 0, 0, 4, 64, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, 60, 2, 64, 4, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 63, 2, 0, 1, 60, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 2, 63, 1, 60, 1, 2)]
+ public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int z, int width, int height, int depth)
+ {
+ int[,,] array = new int[3, 64, 64];
+
+ foreach (var item in array.AsSpan().Enumerate())
+ item.Value = item.Index;
+
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array);
+
+ int[,,] result = new int[depth, height, width];
+
+ texture.GetData(result.AsSpan(), x, y, z, width, height, depth);
+
+ for (int k = 0; k < depth; k++)
+ {
+ Span2D
+ source = array.AsSpan2D(z + k).Slice(y, x, height, width),
+ destination = result.AsSpan2D(k);
+
+ CollectionAssert.AreEqual(source.ToArray(), destination.ToArray());
+ }
+ }
+
+ [TestMethod]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, -1, 0, 50, 50, 2)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), -1, 0, 0, 50, 50, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, -1, 50, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, 20, 50, 0)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 1, 20, -1, 2)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 2, 20, 50, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 0, 60, 20, 3)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 80, 20, 2, 40, 20, 1)]
+ [DataRow(typeof(ReadOnlyTexture3D<>), 0, 80, 2, 40, 20, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, -1, 0, 50, 50, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), -1, 0, 0, 50, 50, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, -1, 50, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, 20, 50, 0)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 1, 20, -1, 2)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 2, 20, 50, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 0, 60, 20, 3)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 80, 20, 2, 40, 20, 1)]
+ [DataRow(typeof(ReadWriteTexture3D<>), 0, 80, 2, 40, 20, 1)]
+ [ExpectedException(typeof(ArgumentOutOfRangeException))]
+ public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int z, int width, int height, int depth)
+ {
+ float[] array = Enumerable.Range(0, 64 * 64 * 3).Select(static i => (float)i).ToArray();
+
+ using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array, 64, 64, 3);
+
+ float[] result = new float[array.Length];
+
+ texture.GetData(result, x, y, z, width, height, depth);
+ }
+ }
+}