Implemented Texture2D<T> memory copy

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-03 00:12:00 +01:00
1 親 f0b9f97199
コミット 10636d169b
2ファイル変更
+86 -2

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

+57 -2
ファイルの表示
@@ -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
/// <param name="height">The height of the memory area to copy.</param>
public void GetData(Span<T> 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<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
ResourceType.ReadBack,
DXGIFormatHelper.GetForType<T>(),
(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);
}
/// <summary>
@@ -318,7 +347,33 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="height">The height of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> 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<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(
ResourceType.Upload,
DXGIFormatHelper.GetForType<T>(),
(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();
}
/// <inheritdoc/>
+29
ファイルの表示
@@ -91,6 +91,35 @@ namespace ComputeSharp.Graphics.Commands
this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, destinationOffset, d3D12ResourceSource, sourceOffset, numBytes);
}
/// <summary>
/// Copies a texture memory region from one resource to another.
/// </summary>
/// <param name="d3D12ResourceSource">The source <see cref="ID3D12Resource"/> to read from.</param>
/// <param name="sourceX">The horizontal offset within <paramref name="d3D12ResourceSource"/>.</param>
/// <param name="sourceY">The vertical offset within <paramref name="d3D12ResourceSource"/>.</param>
/// <param name="sourceWidth">The width of the memory region to copy.</param>
/// <param name="sourceHeight">The height of the memory region to copy.</param>
/// <param name="d3d12ResourceDestination">The destination <see cref="ID3D12Resource"/> to write to.</param>
/// <param name="destinationX">The horizontal offset within <paramref name="d3d12ResourceDestination"/>.</param>
/// <param name="destinationY">The vertical offset within <paramref name="d3d12ResourceDestination"/>.</param>
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);
}
/// <summary>
/// Binds an input <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to a specified root parameter.
/// </summary>