Added derived Texture2D<T> transfer types

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-01 20:25:28 +01:00
1 親 a09ca8a653
コミット 26d8b83747
4ファイル変更
+214

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

+117
ファイルの表示
@@ -194,6 +194,66 @@ namespace ComputeSharp.Resources
}
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture2D{T}"/> instance and writes them into a target <see cref="ReadBackTexture2D{T}"/> instance.
/// </summary>
/// <param name="destination">The target <see cref="ReadBackTexture2D{T}"/> instance to write data to.</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="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
internal void CopyTo(ReadBackTexture2D<T> 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<T>(), (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();
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
@@ -261,6 +321,63 @@ namespace ComputeSharp.Resources
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <summary>
/// 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="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)
{
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<T>(), (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();
}
/// <inheritdoc/>
protected override bool OnDispose()
{
+35
ファイルの表示
@@ -0,0 +1,35 @@
using System.Diagnostics;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Views;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> representing a typed 2D texture stored on CPU memory, that can be used to transfer back from the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(TransferTexture2DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ReadBackTexture2D<T> : TransferTexture2D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="ReadBackTexture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal ReadBackTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode)
: base(device, width, height, ResourceType.ReadBack, allocationMode)
{
}
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.ReadBackTexture2D<{typeof(T)}>[{Width}, {Height}]";
}
}
}
+35
ファイルの表示
@@ -0,0 +1,35 @@
using System.Diagnostics;
using ComputeSharp.Graphics.Resources.Enums;
using ComputeSharp.Resources;
using ComputeSharp.Resources.Views;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> representing a typed 2D texture stored on CPU memory, that can be used to transfer data to the GPU.
/// </summary>
/// <typeparam name="T">The type of items stored on the texture.</typeparam>
[DebuggerTypeProxy(typeof(TransferTexture2DDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class UploadTexture2D<T> : TransferTexture2D<T>
where T : unmanaged
{
/// <summary>
/// Creates a new <see cref="UploadTexture2D{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
/// <param name="width">The width of the texture.</param>
/// <param name="height">The height of the texture.</param>
/// <param name="allocationMode">The allocation mode to use for the new resource.</param>
internal UploadTexture2D(GraphicsDevice device, int width, int height, AllocationMode allocationMode)
: base(device, width, height, ResourceType.Upload, allocationMode)
{
}
/// <inheritdoc/>
public override string ToString()
{
return $"ComputeSharp.UploadTexture2D<{typeof(T)}>[{Width}, {Height}]";
}
}
}
+27
ファイルの表示
@@ -0,0 +1,27 @@
using System.Diagnostics;
namespace ComputeSharp.Resources.Views
{
/// <summary>
/// A debug proxy used to display items in a <see cref="TransferTexture2D{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items to display.</typeparam>
internal sealed class TransferTexture2DDebugView<T>
where T : unmanaged
{
/// <summary>
/// Initializes a new instance of the <see cref="TransferTexture2DDebugView{T}"/> class with the specified parameters.
/// </summary>
/// <param name="texture">The input <see cref="TransferTexture2D{T}"/> instance with the items to display.</param>
public TransferTexture2DDebugView(TransferTexture2D<T>? texture)
{
Items = texture?.Span.ToArray();
}
/// <summary>
/// Gets the items to display for the current instance.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public T[,]? Items { get; }
}
}