Refactor Buffer<T> copy extensions
このコミットが含まれているのは:
1 親
c735347386
コミット
5261df4c0e
5ファイル変更
+213
-77
検索条件に一致するファイルはありません
@@ -99,6 +99,33 @@ public static class BufferExtensions
|
||||
buffer.CopyTo(ref MemoryMarshal.GetReference(destination), destination.Length, offset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified <see cref="Buffer{T}"/> instance and copies them to the target <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffers.</typeparam>
|
||||
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
|
||||
/// <param name="destination">The target <see cref="Buffer{T}"/> instance to write data to.</param>
|
||||
public static void CopyTo<T>(this Buffer<T> buffer, Buffer<T> destination)
|
||||
where T : unmanaged
|
||||
{
|
||||
buffer.CopyTo(destination, 0, 0, buffer.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified range from a <see cref="Buffer{T}"/> instance and copies them to the target <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffers.</typeparam>
|
||||
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
|
||||
/// <param name="destination">The target <see cref="Buffer{T}"/> instance to write data to.</param>
|
||||
/// <param name="sourceOffset">The offset to start reading data from.</param>
|
||||
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
public static void CopyTo<T>(this Buffer<T> buffer, Buffer<T> destination, int sourceOffset, int destinationOffset, int count)
|
||||
where T : unmanaged
|
||||
{
|
||||
buffer.CopyTo(destination, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to a <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
@@ -152,4 +179,31 @@ public static class BufferExtensions
|
||||
{
|
||||
buffer.CopyFrom(ref MemoryMarshal.GetReference(source), source.Length, offset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of the specified <see cref="Buffer{T}"/> instance to the curreent <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffers.</typeparam>
|
||||
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to write data data to.</param>
|
||||
/// <param name="source">The source <see cref="Buffer{T}"/> instance to read data from.</param>
|
||||
public static void CopyFrom<T>(this Buffer<T> buffer, Buffer<T> source)
|
||||
where T : unmanaged
|
||||
{
|
||||
source.CopyTo(buffer, 0, 0, source.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of the specified range from a <see cref="Buffer{T}"/> instance and copies them to the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items stored on the buffers.</typeparam>
|
||||
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to write data to.</param>
|
||||
/// <param name="source">The source <see cref="Buffer{T}"/> instance to read data from.</param>
|
||||
/// <param name="sourceOffset">The offset to start reading data from.</param>
|
||||
/// <param name="destinationOffset">The starting offset within <paramref name="buffer"/> to write data to.</param>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
public static void CopyFrom<T>(this Buffer<T> buffer, Buffer<T> source, int sourceOffset, int destinationOffset, int count)
|
||||
where T : unmanaged
|
||||
{
|
||||
source.CopyTo(buffer, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
}
|
||||
@@ -131,34 +131,32 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
internal abstract void CopyTo(ref T destination, int length, int offset);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given memory area to a specified area of the current <see cref="Buffer{T}"/> instance.
|
||||
/// Writes the contents of a given range from the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="source">The input memory area to read data from.</param>
|
||||
/// <param name="length">The length of the input memory area to read data from.</param>
|
||||
/// <param name="offset">The offset to start writing data to.</param>
|
||||
internal abstract void CopyFrom(ref T source, int length, int offset);
|
||||
/// <param name="destination">The target <see cref="Buffer{T}"/> instance to write data to.</param>
|
||||
/// <param name="sourceOffset">The offset to start reading data from.</param>
|
||||
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
|
||||
/// <param name="length">The number of items to read.</param>
|
||||
internal abstract void CopyTo(Buffer<T> destination, int sourceOffset, int destinationOffset, int length);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Buffer{T}"/> to the current <see cref="Buffer{T}"/> instance.
|
||||
/// Writes the contents of a given range from the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Buffer{T}"/> instance, using a temporary CPU buffer.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <see cref="Buffer{T}"/> to read data from.</param>
|
||||
public abstract void CopyFrom(Buffer<T> source);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Buffer{T}"/> to the current <see cref="Buffer{T}"/> instance, using a temporary CPU buffer.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <see cref="Buffer{T}"/> to read data from.</param>
|
||||
protected void CopyFromWithCpuBuffer(Buffer<T> source)
|
||||
/// <param name="destination">The target <see cref="Buffer{T}"/> instance to write data to.</param>
|
||||
/// <param name="sourceOffset">The offset to start reading data from.</param>
|
||||
/// <param name="destinationOffset">The starting offset within <paramref name="destination"/> to write data to.</param>
|
||||
/// <param name="length">The number of items to read.</param>
|
||||
protected void CopyToWithCpuBuffer(Buffer<T> destination, int sourceOffset, int destinationOffset, int length)
|
||||
{
|
||||
T[] array = ArrayPool<T>.Shared.Rent(source.Length);
|
||||
T[] array = ArrayPool<T>.Shared.Rent(length);
|
||||
|
||||
try
|
||||
{
|
||||
ref T r0 = ref MemoryMarshal.GetArrayDataReference(array);
|
||||
|
||||
source.CopyTo(ref r0, source.Length, 0);
|
||||
CopyTo(ref r0, length, sourceOffset);
|
||||
|
||||
CopyFrom(ref r0, source.Length, 0);
|
||||
destination.CopyFrom(ref r0, length, destinationOffset);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -166,6 +164,14 @@ public unsafe abstract class Buffer<T> : NativeObject
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given memory area to a specified area of the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="source">The input memory area to read data from.</param>
|
||||
/// <param name="length">The length of the input memory area to read data from.</param>
|
||||
/// <param name="offset">The offset to start writing data to.</param>
|
||||
internal abstract void CopyFrom(ref T source, int length, int offset);
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
|
||||
@@ -36,8 +36,9 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
@@ -84,6 +85,44 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(Buffer<T> destination, int sourceOffset, int destinationOffset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length));
|
||||
Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (!destination.IsPaddingPresent)
|
||||
{
|
||||
nint sourceByteOffset = (nint)sourceOffset * sizeof(T);
|
||||
nint destinationByteOffset = (nint)destinationOffset * sizeof(T);
|
||||
nint byteLength = length * sizeof(T);
|
||||
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(
|
||||
destination.D3D12Resource,
|
||||
(ulong)destinationByteOffset,
|
||||
D3D12Resource,
|
||||
(ulong)sourceByteOffset,
|
||||
(ulong)byteLength);
|
||||
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified range from the current <see cref="StructuredBuffer{T}"/> instance and writes them into a target <see cref="ReadBackBuffer{T}"/> instance.
|
||||
/// </summary>
|
||||
@@ -138,8 +177,9 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
if (GraphicsDevice.IsCacheCoherentUMA)
|
||||
{
|
||||
@@ -230,27 +270,4 @@ public abstract class StructuredBuffer<T> : Buffer<T>
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void CopyFrom(Buffer<T> source)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
source.ThrowIfDisposed();
|
||||
|
||||
Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
|
||||
|
||||
if (!source.IsPaddingPresent)
|
||||
{
|
||||
// Directly copy the input buffer
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes);
|
||||
copyCommandList.ExecuteAndWaitForCompletion();
|
||||
}
|
||||
else CopyFromWithCpuBuffer(source);
|
||||
}
|
||||
}
|
||||
@@ -57,14 +57,15 @@ public sealed class ConstantBuffer<T> : Buffer<T>
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyTo(ref T destination, int size, int offset)
|
||||
internal override unsafe void CopyTo(ref T destination, int length, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
@@ -77,19 +78,54 @@ public sealed class ConstantBuffer<T> : Buffer<T>
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)sizeof(T),
|
||||
count: (uint)size);
|
||||
count: (uint)length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyFrom(ref T source, int size, int offset)
|
||||
internal override unsafe void CopyTo(Buffer<T> destination, int sourceOffset, int destinationOffset, int length)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
destination.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
destination.ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsBetweenOrEqualTo(length, 0, destination.Length, nameof(length));
|
||||
Guard.IsInRange(sourceOffset, 0, Length, nameof(sourceOffset));
|
||||
Guard.IsLessThanOrEqualTo(sourceOffset + length, Length, nameof(sourceOffset));
|
||||
Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset));
|
||||
Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(destinationOffset));
|
||||
|
||||
if (destination is ConstantBuffer<T> buffer)
|
||||
{
|
||||
using ID3D12ResourceMap sourceMap = D3D12Resource->Map();
|
||||
using ID3D12ResourceMap destinationMap = buffer.D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourceMap.Pointer,
|
||||
destination: destinationMap.Pointer,
|
||||
sourceElementOffset: (uint)sourceOffset,
|
||||
destinationElementOffset: (uint)destinationOffset,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)length);
|
||||
}
|
||||
else CopyToWithCpuBuffer(destination, sourceOffset, destinationOffset, length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal override unsafe void CopyFrom(ref T source, int length, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsBetweenOrEqualTo(length, 0, Length, nameof(length));
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsLessThanOrEqualTo(offset + size, Length, nameof(size));
|
||||
Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(offset));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
@@ -102,39 +138,10 @@ public sealed class ConstantBuffer<T> : Buffer<T>
|
||||
destinationElementOffset: (uint)offset,
|
||||
sourceElementPitchInBytes: (uint)sizeof(T),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)size);
|
||||
count: (uint)length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void CopyFrom(Buffer<T> source)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
source.ThrowIfDeviceMismatch(GraphicsDevice);
|
||||
source.ThrowIfDisposed();
|
||||
|
||||
Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length));
|
||||
|
||||
if (source is ConstantBuffer<T> buffer)
|
||||
{
|
||||
using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map();
|
||||
using ID3D12ResourceMap destinationMap = D3D12Resource->Map();
|
||||
|
||||
MemoryHelper.Copy<T>(
|
||||
source: sourceMap.Pointer,
|
||||
destination: destinationMap.Pointer,
|
||||
sourceElementOffset: 0,
|
||||
destinationElementOffset: 0,
|
||||
sourceElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
destinationElementPitchInBytes: (uint)GetPaddedSize(),
|
||||
count: (uint)source.Length);
|
||||
}
|
||||
else CopyFromWithCpuBuffer(source);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
@@ -224,6 +224,58 @@ public partial class BufferTests
|
||||
buffer.CopyTo(result, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
[Resource(typeof(ReadOnlyBuffer<>))]
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
[AdditionalResource(typeof(ConstantBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadOnlyBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadWriteBuffer<>))]
|
||||
[Data(0, 0, 4096)]
|
||||
[Data(0, 512, 2048)]
|
||||
[Data(512, 0, 2048)]
|
||||
[Data(1024, 127, 587)]
|
||||
[Data(65, 127, 587)]
|
||||
public void CopyTo_BufferToVoid_Ok(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count)
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
|
||||
|
||||
using Buffer<float> source = device.Get().AllocateBuffer(sourceType, array);
|
||||
using Buffer<float> destination = device.Get().AllocateBuffer<float>(destinationType, 4096);
|
||||
|
||||
source.CopyTo(destination, sourceOffset, destinationOffset, count);
|
||||
|
||||
float[] result = destination.ToArray();
|
||||
|
||||
Assert.IsTrue(array.AsSpan(sourceOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count)));
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
[Resource(typeof(ConstantBuffer<>))]
|
||||
[Resource(typeof(ReadOnlyBuffer<>))]
|
||||
[Resource(typeof(ReadWriteBuffer<>))]
|
||||
[AdditionalResource(typeof(ConstantBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadOnlyBuffer<>))]
|
||||
[AdditionalResource(typeof(ReadWriteBuffer<>))]
|
||||
[Data(0, 0, 8196)]
|
||||
[Data(0, -12, 1024)]
|
||||
[Data(-56, 0, 1024)]
|
||||
[Data(0, 512, 4096)]
|
||||
[Data(1024, 12, 3600)]
|
||||
[Data(1024, 12, -2096)]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||
public void CopyTo_BufferToVoid_Fail(Device device, Type sourceType, Type destinationType, int sourceOffset, int destinationOffset, int count)
|
||||
{
|
||||
float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
|
||||
|
||||
using Buffer<float> source = device.Get().AllocateBuffer(sourceType, array);
|
||||
using Buffer<float> destination = device.Get().AllocateBuffer<float>(destinationType, 4096);
|
||||
|
||||
source.CopyTo(destination, sourceOffset, destinationOffset, count);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Dispatch_ConstantBuffer(Device device)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする