Add Texture3D<T>.ToArray overload

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-12 17:46:08 +01:00
1 親 3570b4d65e
コミット da90dd9157
3ファイル変更
+88 -1

検索条件に一致するファイルはありません

+29
ファイルの表示
@@ -33,6 +33,35 @@ public static class Texture3DExtensions
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture3D{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
/// <param name="source">The input <see cref="Texture3D{T}"/> instance to read data from.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="z">The depthwise offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
/// <param name="depth">The depth of the memory area to copy.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the current texture.</returns>
public static unsafe T[,,] ToArray<T>(this Texture3D<T> 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<T>(p, width * height * depth), x, y, z, width, height, depth);
}
return data;
}
/// <summary>
/// Reads the contents of the current <see cref="Texture3D{T}"/> instance and writes them into a target array.
/// </summary>
+1 -1
ファイルの表示
@@ -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)]
+58
ファイルの表示
@@ -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<float> 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<float> expected = new Span2D<float>(array, 64 * 64 * (k + z), 64, 64, 0).Slice(y, x, height, width);
Span2D<float> 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<float> texture = device.Get().AllocateTexture3D<float>(textureType, 64, 64, 8);
_ = texture.ToArray(x, y, z, width, height, depth);
}
[CombinatorialTestMethod]
[AllDevices]
[Resource(typeof(ReadOnlyTexture3D<>))]