diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
index 4f935baa..2169427b 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -345,11 +345,13 @@ namespace ComputeSharp.Resources
/// Writes the contents of a given instance to a specified area of the current instance.
///
/// The input instance to read data from.
- /// The horizontal offset in the destination texture.
- /// The vertical offset in the destination texture.
+ /// The horizontal offset within .
+ /// The vertical offset within .
+ /// The horizontal offset in the destination texture.
+ /// The vertical offset in the destination texture.
/// The width of the memory area to write to.
/// The height of the memory area to write to.
- internal void CopyFrom(UploadTexture2D source, int x, int y, int width, int height)
+ internal void CopyFrom(UploadTexture2D 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,
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
index f6669ebd..8d825c03 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs
@@ -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);
}
}
diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
index a0c7237e..746d6ad2 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs
@@ -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);
}
}
diff --git a/src/ComputeSharp/Extensions/Resources/UploadTexture2DExtensions.cs b/src/ComputeSharp/Extensions/Resources/UploadTexture2DExtensions.cs
index a111e454..5b95c66b 100644
--- a/src/ComputeSharp/Extensions/Resources/UploadTexture2DExtensions.cs
+++ b/src/ComputeSharp/Extensions/Resources/UploadTexture2DExtensions.cs
@@ -16,7 +16,7 @@ namespace ComputeSharp
public static void CopyTo(this UploadTexture2D texture, Texture2D destination)
where T : unmanaged
{
- destination.CopyFrom(texture, 0, 0, texture.Width, texture.Height);
+ destination.CopyFrom(texture, 0, 0, 0, 0, texture.Width, texture.Height);
}
///
@@ -32,7 +32,7 @@ namespace ComputeSharp
public static void CopyTo(this UploadTexture2D texture, Texture2D 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);
}
}
}
diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs
new file mode 100644
index 00000000..6160ea8f
--- /dev/null
+++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs
@@ -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 buffer = Gpu.Default.AllocateTransferTexture2D(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 buffer = Gpu.Default.AllocateTransferTexture2D(bufferType, width, height);
+ }
+
+ [TestMethod]
+ [DataRow(typeof(UploadTexture2D<>))]
+ [DataRow(typeof(ReadBackTexture2D<>))]
+ [ExpectedException(typeof(ObjectDisposedException))]
+ public void UsageAfterDispose(Type bufferType)
+ {
+ using TransferTexture2D buffer = Gpu.Default.AllocateTransferTexture2D(bufferType, 32, 32);
+
+ buffer.Dispose();
+
+ _ = buffer.View;
+ }
+
+ [TestMethod]
+ public void Allocate_UploadTexture2D_Copy_Full()
+ {
+ using UploadTexture2D uploadTexture2D = Gpu.Default.AllocateUploadTexture2D(256, 256);
+
+ for (int i = 0; i < uploadTexture2D.Height; i++)
+ {
+ new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes());
+ }
+
+ using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(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 uploadTexture2D = Gpu.Default.AllocateUploadTexture2D(2048, 2048);
+
+ for (int i = 0; i < uploadTexture2D.Height; i++)
+ {
+ new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes());
+ }
+
+ using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(uploadTexture2D.Width, uploadTexture2D.Height);
+
+ uploadTexture2D.CopyTo(readOnlyTexture2D, x, y, width, height);
+
+ int[,] result = readOnlyTexture2D.ToArray();
+
+ for (int i = 0; i < height; i++)
+ {
+ Span sourceRow = uploadTexture2D.View.GetRowSpan(i + y).Slice(x, width);
+ Span 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 readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source);
+ using ReadBackTexture2D readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D(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 readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source);
+ using ReadBackTexture2D readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D(2048, 2048);
+
+ readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height);
+
+ for (int i = 0; i < height; i++)
+ {
+ Span sourceRow = source.GetRowSpan(i + y).Slice(x, width);
+ Span destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width);
+
+ Assert.IsTrue(sourceRow.SequenceEqual(destinationRow));
+ }
+ }
+ }
+}