More bug fixes, added TransferTexture2D<T> tests

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-06 14:28:37 +01:00
1 親 53012a5667
コミット 5f6efb0811
5ファイル変更
+184 -15

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

+17 -11
ファイルの表示
@@ -345,11 +345,13 @@ namespace ComputeSharp.Resources
/// Writes the contents of a given <see cref="UploadTexture2D{T}"/> instance to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="UploadTexture2D{T}"/> instance to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="sourceX">The horizontal offset within <paramref name="source"/>.</param>
/// <param name="sourceY">The vertical offset within <paramref name="source"/>.</param>
/// <param name="destinationX">The horizontal offset in the destination texture.</param>
/// <param name="destinationY">The vertical offset in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
internal void CopyFrom(UploadTexture2D<T> source, int x, int y, int width, int height)
internal void CopyFrom(UploadTexture2D<T> source, int sourceX, int sourceY, int destinationX, int destinationY, int width, int height)
{
GraphicsDevice.ThrowIfDisposed();
@@ -358,14 +360,18 @@ namespace ComputeSharp.Resources
source.ThrowIfDeviceMismatch(GraphicsDevice);
source.ThrowIfDisposed();
Guard.IsInRange(x, 0, Width, nameof(x));
Guard.IsInRange(y, 0, Height, nameof(y));
Guard.IsInRange(sourceX, 0, source.Width, nameof(sourceX));
Guard.IsInRange(sourceY, 0, source.Width, nameof(sourceY));
Guard.IsInRange(destinationX, 0, Width, nameof(destinationX));
Guard.IsInRange(destinationY, 0, Height, nameof(destinationY));
Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width));
Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height));
Guard.IsBetweenOrEqualTo(width, 1, source.Width, nameof(width));
Guard.IsBetweenOrEqualTo(height, 1, source.Height, nameof(height));
Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
Guard.IsLessThanOrEqualTo(sourceX + width, source.Width, nameof(sourceX));
Guard.IsLessThanOrEqualTo(sourceY + height, source.Height, nameof(sourceY));
Guard.IsLessThanOrEqualTo(destinationX + width, Width, nameof(destinationX));
Guard.IsLessThanOrEqualTo(destinationY + height, Height, nameof(destinationY));
using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
@@ -378,13 +384,13 @@ namespace ComputeSharp.Resources
{
copyCommandList.D3D12GraphicsCommandList->CopyTextureRegion(
d3D12ResourceDestination: D3D12Resource,
destinationX: (uint)x,
destinationY: (uint)y,
(uint)destinationX,
(uint)destinationY,
destinationZ: 0,
d3D12ResourceSource: source.D3D12Resource,
d3D12PlacedSubresourceFootprintSource,
sourceX: 0,
sourceY: 0,
(uint)sourceX,
(uint)sourceY,
sourceZ: 0,
(uint)width,
(uint)height,
+1 -1
ファイルの表示
@@ -102,7 +102,7 @@ namespace ComputeSharp.Resources
{
ThrowIfDisposed();
return new(this.mappedData, Height, Width, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
}
}
+1 -1
ファイルの表示
@@ -110,7 +110,7 @@ namespace ComputeSharp.Resources
{
ThrowIfDisposed();
return new(this.mappedData, Height, Width, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
return new(this.mappedData, Width, Height, Depth, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
}
}
+2 -2
ファイルの表示
@@ -16,7 +16,7 @@ namespace ComputeSharp
public static void CopyTo<T>(this UploadTexture2D<T> texture, Texture2D<T> destination)
where T : unmanaged
{
destination.CopyFrom(texture, 0, 0, texture.Width, texture.Height);
destination.CopyFrom(texture, 0, 0, 0, 0, texture.Width, texture.Height);
}
/// <summary>
@@ -32,7 +32,7 @@ namespace ComputeSharp
public static void CopyTo<T>(this UploadTexture2D<T> texture, Texture2D<T> destination, int x, int y, int width, int height)
where T : unmanaged
{
destination.CopyFrom(texture, x, y, width, height);
destination.CopyFrom(texture, x, y, 0, 0, width, height);
}
}
}
+163
ファイルの表示
@@ -0,0 +1,163 @@
using System;
using System.Linq;
using ComputeSharp.Resources;
using ComputeSharp.Tests.Extensions;
using Microsoft.Toolkit.HighPerformance.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("TransferTexture2D")]
public partial class TransferTexture2DTests
{
[TestMethod]
[DataRow(typeof(UploadTexture2D<>))]
[DataRow(typeof(ReadBackTexture2D<>))]
public unsafe void Allocate_Uninitialized_Ok(Type bufferType)
{
using TransferTexture2D<float> buffer = Gpu.Default.AllocateTransferTexture2D<float>(bufferType, 128, 256);
Assert.IsNotNull(buffer);
Assert.AreEqual(buffer.Width, 128);
Assert.AreEqual(buffer.Height, 256);
Assert.AreEqual(buffer.View.Width, 128);
Assert.AreEqual(buffer.View.Height, 256);
Assert.AreSame(buffer.GraphicsDevice, Gpu.Default);
}
[TestMethod]
[DataRow(typeof(UploadTexture2D<>), 128, -14253)]
[DataRow(typeof(UploadTexture2D<>), 128, -1)]
[DataRow(typeof(UploadTexture2D<>), 0, -4314)]
[DataRow(typeof(UploadTexture2D<>), -14, -53)]
[DataRow(typeof(ReadBackTexture2D<>), 128, -14253)]
[DataRow(typeof(ReadBackTexture2D<>), 128, -1)]
[DataRow(typeof(ReadBackTexture2D<>), 0, -4314)]
[DataRow(typeof(ReadBackTexture2D<>), -14, -53)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Allocate_Uninitialized_Fail(Type bufferType, int width, int height)
{
using TransferTexture2D<float> buffer = Gpu.Default.AllocateTransferTexture2D<float>(bufferType, width, height);
}
[TestMethod]
[DataRow(typeof(UploadTexture2D<>))]
[DataRow(typeof(ReadBackTexture2D<>))]
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Type bufferType)
{
using TransferTexture2D<float> buffer = Gpu.Default.AllocateTransferTexture2D<float>(bufferType, 32, 32);
buffer.Dispose();
_ = buffer.View;
}
[TestMethod]
public void Allocate_UploadTexture2D_Copy_Full()
{
using UploadTexture2D<int> uploadTexture2D = Gpu.Default.AllocateUploadTexture2D<int>(256, 256);
for (int i = 0; i < uploadTexture2D.Height; i++)
{
new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes());
}
using ReadOnlyTexture2D<int> readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D<int>(uploadTexture2D.Width, uploadTexture2D.Height);
uploadTexture2D.CopyTo(readOnlyTexture2D);
int[,] result = readOnlyTexture2D.ToArray();
Assert.AreEqual(uploadTexture2D.Width, result.GetLength(1));
Assert.AreEqual(uploadTexture2D.Height, result.GetLength(0));
for (int i = 0; i < uploadTexture2D.Height; i++)
{
Assert.IsTrue(uploadTexture2D.View.GetRowSpan(i).SequenceEqual(result.GetRowSpan(i)));
}
}
[TestMethod]
[DataRow(0, 0, 2048, 2048)]
[DataRow(128, 0, 1813, 2048)]
[DataRow(0, 128, 2000, 944)]
[DataRow(97, 33, 512, 794)]
public void Allocate_UploadTexture2D_Copy_Range(int x, int y, int width, int height)
{
using UploadTexture2D<int> uploadTexture2D = Gpu.Default.AllocateUploadTexture2D<int>(2048, 2048);
for (int i = 0; i < uploadTexture2D.Height; i++)
{
new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes());
}
using ReadOnlyTexture2D<int> readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D<int>(uploadTexture2D.Width, uploadTexture2D.Height);
uploadTexture2D.CopyTo(readOnlyTexture2D, x, y, width, height);
int[,] result = readOnlyTexture2D.ToArray();
for (int i = 0; i < height; i++)
{
Span<int> sourceRow = uploadTexture2D.View.GetRowSpan(i + y).Slice(x, width);
Span<int> destinationRow = result.GetRowSpan(i).Slice(0, width);
Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
}
}
[TestMethod]
public void Allocate_ReadBackTexture2D_Copy_Full()
{
int[,] source = new int[256, 256];
for (int i = 0; i < 256; i++)
{
new Random(42).NextBytes(source.GetRowSpan(i).AsBytes());
}
using ReadOnlyTexture2D<int> readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source);
using ReadBackTexture2D<int> readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D<int>(256, 256);
readOnlyTexture2D.CopyTo(readBackTexture2D);
Assert.AreEqual(readBackTexture2D.Width, 256);
Assert.AreEqual(readBackTexture2D.Height, 256);
for (int i = 0; i < readBackTexture2D.Height; i++)
{
Assert.IsTrue(readBackTexture2D.View.GetRowSpan(i).SequenceEqual(source.GetRowSpan(i)));
}
}
[TestMethod]
[DataRow(0, 0, 2048, 2048)]
[DataRow(128, 0, 1813, 2048)]
[DataRow(0, 128, 2000, 944)]
[DataRow(97, 33, 512, 794)]
public void Allocate_ReadBackTexture2D_Copy_Range(int x, int y, int width, int height)
{
int[,] source = new int[2048, 2048];
for (int i = 0; i < 2048; i++)
{
new Random(42).NextBytes(source.GetRowSpan(i).AsBytes());
}
using ReadOnlyTexture2D<int> readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source);
using ReadBackTexture2D<int> readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D<int>(2048, 2048);
readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height);
for (int i = 0; i < height; i++)
{
Span<int> sourceRow = source.GetRowSpan(i + y).Slice(x, width);
Span<int> destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width);
Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
}
}
}
}