Fixed Texture2D<T> memory copy APIs

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-04 17:20:07 +01:00
1 親 7fabac09ae
コミット 8851720749
2ファイル変更
+88 -9

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

+13 -6
ファイルの表示
@@ -12,6 +12,7 @@ using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE;
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
@@ -237,9 +238,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
Guard.HasSizeGreaterThanOrEqualTo(destination, width * height, nameof(destination));
nint byteSize = (nint)width * height * Unsafe.SizeOf<T>();
ulong
rowByteSize = (ulong)((nint)width * Unsafe.SizeOf<T>()),
rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)),
resourceByteSize = rowPitchSize * (uint)height;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, (ulong)byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, resourceByteSize);
using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE))
{
@@ -251,7 +255,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
MemoryHelper.Copy(resource.Pointer, 0, destination);
MemoryHelper.Copy(resource.Pointer, width, height, rowByteSize, rowPitchSize, destination);
}
/// <summary>
@@ -371,13 +375,16 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y));
Guard.HasSizeGreaterThanOrEqualTo(source, width * height, nameof(source));
nint byteSize = (nint)width * height * Unsafe.SizeOf<T>();
ulong
rowByteSize = (ulong)((nint)width * Unsafe.SizeOf<T>()),
rowPitchSize = (rowByteSize + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((ulong)(FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)),
resourceByteSize = rowPitchSize * (uint)height;
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, (ulong)byteSize);
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, resourceByteSize);
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
{
MemoryHelper.Copy(source, resource.Pointer, 0);
MemoryHelper.Copy(source, resource.Pointer, width, height, rowByteSize, rowPitchSize);
}
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE);
+75 -3
ファイルの表示
@@ -9,7 +9,7 @@ namespace ComputeSharp.Graphics.Helpers
internal static class MemoryHelper
{
/// <summary>
/// Copies the content of a <see cref="ReadOnlySpan{T}"/> to the area pointed by an input <see cref="IntPtr"/> value.
/// Copies the content of a <see cref="ReadOnlySpan{T}"/> to the area pointed by an input pointer.
/// </summary>
/// <typeparam name="T">The type of values in the input <see cref="ReadOnlySpan{T}"/>.</typeparam>
/// <param name="source">The source <see cref="ReadOnlySpan{T}"/> to read.</param>
@@ -23,10 +23,10 @@ namespace ComputeSharp.Graphics.Helpers
}
/// <summary>
/// Copies a memory area pointed by an <see cref="IntPtr"/> value to a target <see cref="Span{T}"/>.
/// Copies a memory area pointed by a pointer to a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The <see cref="IntPtr"/> that indicates the memory area to read from.</param>
/// <param name="source">The pointer that indicates the memory area to read from.</param>
/// <param name="sourceOffset">The source offset to start reading data from.</param>
/// <param name="destination">The destination <see cref="Span{T}"/> to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -35,5 +35,77 @@ namespace ComputeSharp.Graphics.Helpers
{
new Span<T>((T*)source + sourceOffset, destination.Length).CopyTo(destination);
}
/// <summary>
/// Copies the content of a <see cref="ReadOnlySpan{T}"/> to the 2D area pointed by an input pointer.
/// </summary>
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The destination <see cref="Span{T}"/> to read from.</param>
/// <param name="destination">The pointer that indicates the 2D memory area to write to.</param>
/// <param name="width">The width of the 2D memory area to read.</param>
/// <param name="height">The height of the 2D memory area to read.</param>
/// <param name="widthInBytes">The width of the memory area in bytes.</param>
/// <param name="pitchInBytes">The pitch (padded width) of the memory area in bytes.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(
ReadOnlySpan<T> source,
void* destination,
int width,
int height,
ulong widthInBytes,
ulong pitchInBytes)
where T : unmanaged
{
if (widthInBytes == pitchInBytes)
{
source.CopyTo(new Span<T>(destination, width * height));
}
else
{
for (int i = 0; i < height; i++)
{
ReadOnlySpan<T> sourceRow = source.Slice(i * width, width);
Span<T> destinationRow = new((byte*)destination + (uint)i * pitchInBytes, width);
sourceRow.CopyTo(destinationRow);
}
}
}
/// <summary>
/// Copies a 2D memory area pointed by a pointer value to a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The pointer that indicates the memory area to read from.</param>
/// <param name="width">The width of the 2D memory area to read.</param>
/// <param name="height">The height of the 2D memory area to read.</param>
/// <param name="widthInBytes">The width of the memory area in bytes.</param>
/// <param name="pitchInBytes">The pitch (padded width) of the memory area in bytes.</param>
/// <param name="destination">The destination <see cref="Span{T}"/> to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(
void* source,
int width,
int height,
ulong widthInBytes,
ulong pitchInBytes,
Span<T> destination)
where T : unmanaged
{
if (widthInBytes == pitchInBytes)
{
new Span<T>(source, width * height).CopyTo(destination);
}
else
{
for (int i = 0; i < height; i++)
{
ReadOnlySpan<T> sourceRow = new((byte*)source + (uint)i * pitchInBytes, width);
Span<T> destinationRow = destination.Slice(i * width, width);
sourceRow.CopyTo(destinationRow);
}
}
}
}
}