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()
{