diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
index 18903173..ca562175 100644
--- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs
@@ -194,6 +194,66 @@ namespace ComputeSharp.Resources
}
}
+ ///
+ /// Reads the contents of the specified range from the current instance and writes them into a target instance.
+ ///
+ /// The target instance to write data to.
+ /// 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.
+ internal void CopyTo(ReadBackTexture2D destination, int x, int y, int width, int height)
+ {
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ destination.ThrowIfDeviceMismatch(GraphicsDevice);
+ destination.ThrowIfDisposed();
+
+ Guard.IsInRange(x, 0, Width, nameof(x));
+ Guard.IsInRange(y, 0, Height, nameof(y));
+ Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width));
+ Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height));
+ Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width));
+ Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height));
+ Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
+ Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
+
+ D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)destination.Width, (uint)destination.Height);
+
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ &d3D12ResourceDescription,
+ out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint,
+ out ulong rowSizeInBytes,
+ out ulong totalSizeInBytes);
+
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE);
+ }
+
+ copyCommandList.CopyTextureRegion(
+ destination.D3D12Resource,
+ &d3D12PlacedSubresourceFootprint,
+ D3D12Resource,
+ (uint)x,
+ (uint)y,
+ 0,
+ (uint)width,
+ (uint)height,
+ 1);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+
///
/// Writes the contents of a given memory area to a specified area of the current instance.
///
@@ -261,6 +321,63 @@ namespace ComputeSharp.Resources
copyCommandList.ExecuteAndWaitForCompletion();
}
+ ///
+ /// 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 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)
+ {
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ source.ThrowIfDeviceMismatch(GraphicsDevice);
+ source.ThrowIfDisposed();
+
+ Guard.IsInRange(x, 0, Width, nameof(x));
+ Guard.IsInRange(y, 0, Height, nameof(y));
+ 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));
+
+ D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)source.Width, (uint)source.Height);
+
+ GraphicsDevice.D3D12Device->GetCopyableFootprint(
+ &d3D12ResourceDescription,
+ out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint,
+ out ulong rowSizeInBytes,
+ out ulong totalSizeInBytes);
+
+ using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ }
+
+ copyCommandList.CopyTextureRegion(
+ D3D12Resource,
+ (uint)x,
+ (uint)y,
+ 0,
+ source.D3D12Resource,
+ &d3D12PlacedSubresourceFootprint);
+
+ if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE)
+ {
+ copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState);
+ }
+
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+
///
protected override bool OnDispose()
{
diff --git a/src/ComputeSharp.Graphics/Resources/ReadBackTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadBackTexture2D{T}.cs
new file mode 100644
index 00000000..0c53dda1
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Resources/ReadBackTexture2D{T}.cs
@@ -0,0 +1,35 @@
+using System.Diagnostics;
+using ComputeSharp.Graphics.Resources.Enums;
+using ComputeSharp.Resources;
+using ComputeSharp.Resources.Views;
+
+namespace ComputeSharp
+{
+ ///
+ /// A representing a typed 2D texture stored on CPU memory, that can be used to transfer back from the GPU.
+ ///
+ /// The type of items stored on the texture.
+ [DebuggerTypeProxy(typeof(TransferTexture2DDebugView<>))]
+ [DebuggerDisplay("{ToString(),raw}")]
+ public sealed class ReadBackTexture2D : TransferTexture2D
+ where T : unmanaged
+ {
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The associated with the current instance.
+ /// The width of the texture.
+ /// The height of the texture.
+ /// The allocation mode to use for the new resource.
+ internal ReadBackTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode)
+ : base(device, width, height, ResourceType.ReadBack, allocationMode)
+ {
+ }
+
+ ///
+ public override string ToString()
+ {
+ return $"ComputeSharp.ReadBackTexture2D<{typeof(T)}>[{Width}, {Height}]";
+ }
+ }
+}
diff --git a/src/ComputeSharp.Graphics/Resources/UploadTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/UploadTexture2D{T}.cs
new file mode 100644
index 00000000..f2f3018e
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Resources/UploadTexture2D{T}.cs
@@ -0,0 +1,35 @@
+using System.Diagnostics;
+using ComputeSharp.Graphics.Resources.Enums;
+using ComputeSharp.Resources;
+using ComputeSharp.Resources.Views;
+
+namespace ComputeSharp
+{
+ ///
+ /// A representing a typed 2D texture stored on CPU memory, that can be used to transfer data to the GPU.
+ ///
+ /// The type of items stored on the texture.
+ [DebuggerTypeProxy(typeof(TransferTexture2DDebugView<>))]
+ [DebuggerDisplay("{ToString(),raw}")]
+ public sealed class UploadTexture2D : TransferTexture2D
+ where T : unmanaged
+ {
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The associated with the current instance.
+ /// The width of the texture.
+ /// The height of the texture.
+ /// The allocation mode to use for the new resource.
+ internal UploadTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode)
+ : base(device, width, height, ResourceType.Upload, allocationMode)
+ {
+ }
+
+ ///
+ public override string ToString()
+ {
+ return $"ComputeSharp.UploadTexture2D<{typeof(T)}>[{Width}, {Height}]";
+ }
+ }
+}
diff --git a/src/ComputeSharp.Graphics/Resources/Views/TransferTexture2DDebugView{T}.cs b/src/ComputeSharp.Graphics/Resources/Views/TransferTexture2DDebugView{T}.cs
new file mode 100644
index 00000000..8806bdca
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Resources/Views/TransferTexture2DDebugView{T}.cs
@@ -0,0 +1,27 @@
+using System.Diagnostics;
+
+namespace ComputeSharp.Resources.Views
+{
+ ///
+ /// A debug proxy used to display items in a instance.
+ ///
+ /// The type of items to display.
+ internal sealed class TransferTexture2DDebugView
+ where T : unmanaged
+ {
+ ///
+ /// Initializes a new instance of the class with the specified parameters.
+ ///
+ /// The input instance with the items to display.
+ public TransferTexture2DDebugView(TransferTexture2D? texture)
+ {
+ Items = texture?.Span.ToArray();
+ }
+
+ ///
+ /// Gets the items to display for the current instance.
+ ///
+ [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
+ public T[,]? Items { get; }
+ }
+}