diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
index ebd10fa5..52647fa8 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs
@@ -4,10 +4,13 @@ using System.Runtime.CompilerServices;
using ComputeSharp.Core.Interop;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Buffers.Enums;
+using ComputeSharp.Graphics.Buffers.Interop;
+using ComputeSharp.Graphics.Commands;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
+using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
@@ -210,7 +213,33 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// The height of the memory area to copy.
public void GetData(Span destination, int x, int y, int width, int height)
{
- // TODO
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ Guard.IsInRange(x, 0, Width, nameof(x));
+ Guard.IsInRange(y, 0, Height, nameof(y));
+ Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width));
+ Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height));
+ Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
+ Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
+ Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination));
+
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.ReadBack,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height);
+
+ using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY))
+ {
+ copyCommandList.CopyTextureRegion(D3D12Resource, x, y, width, height, d3D12Resource.Get(), 0u, 0u);
+ copyCommandList.ExecuteAndWaitForCompletion();
+ }
+
+ using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
+
+ MemoryHelper.Copy(resource.Pointer, 0, destination);
}
///
@@ -318,7 +347,33 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// The height of the memory area to write to.
public void SetData(ReadOnlySpan source, int x, int y, int width, int height)
{
- // TODO
+ GraphicsDevice.ThrowIfDisposed();
+
+ ThrowIfDisposed();
+
+ Guard.IsInRange(x, 0, Width, nameof(x));
+ Guard.IsInRange(y, 0, Height, nameof(y));
+ Guard.IsBetweenOrEqualTo(width, 0, Width, nameof(width));
+ Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height));
+ Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x));
+ Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
+ Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source));
+
+ using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
+ ResourceType.Upload,
+ DXGIFormatHelper.GetForType(),
+ (uint)width,
+ (uint)height);
+
+ using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
+ {
+ MemoryHelper.Copy(source, resource.Pointer, 0);
+ }
+
+ using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
+
+ copyCommandList.CopyTextureRegion(d3D12Resource.Get(), 0, 0, width, height, D3D12Resource, (uint)x, (uint)y);
+ copyCommandList.ExecuteAndWaitForCompletion();
}
///
diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs
index cebe74c8..1a323c7f 100644
--- a/src/ComputeSharp.Graphics/Commands/CommandList.cs
+++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs
@@ -91,6 +91,35 @@ namespace ComputeSharp.Graphics.Commands
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes);
}
+ ///
+ /// Copies a texture memory region from one resource to another.
+ ///
+ /// The source to read from.
+ /// The horizontal offset within .
+ /// The vertical offset within .
+ /// The width of the memory region to copy.
+ /// The height of the memory region to copy.
+ /// The destination to write to.
+ /// The horizontal offset within .
+ /// The vertical offset within .
+ public readonly void CopyTextureRegion(
+ ID3D12Resource* d3D12ResourceSource,
+ int sourceX,
+ int sourceY,
+ int sourceWidth,
+ int sourceHeight,
+ ID3D12Resource* d3d12ResourceDestination,
+ uint destinationX,
+ uint destinationY)
+ {
+ D3D12_TEXTURE_COPY_LOCATION
+ d3D12TextureCopyLocationSource = new(d3D12ResourceSource),
+ d3D12TextureCopyLocationDestination = new(d3d12ResourceDestination);
+ D3D12_BOX d3D12Box = new(sourceX, sourceY, sourceX + sourceWidth, sourceY + sourceHeight);
+
+ this.d3D12GraphicsCommandList.Get()->CopyTextureRegion(&d3D12TextureCopyLocationDestination, destinationX, destinationY, 0, &d3D12TextureCopyLocationSource, &d3D12Box);
+ }
+
///
/// Binds an input value to a specified root parameter.
///