Improved Buffer<T> copy APIs, fixed validation
このコミットが含まれているのは:
1 親
3cfec22d8c
コミット
1142d7bbcd
4ファイル変更
+45
-40
検索条件に一致するファイルはありません
@@ -116,7 +116,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
{
|
||||
T[] data = GC.AllocateUninitializedArray<T>(count);
|
||||
|
||||
GetData(data, offset, count);
|
||||
GetData(data.AsSpan(), offset);
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -125,29 +125,35 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and writes them into a target array.
|
||||
/// </summary>
|
||||
/// <param name="destination">The input array to write data to.</param>
|
||||
public void GetData(T[] destination) => GetData(destination.AsSpan(), 0, Length);
|
||||
public void GetData(T[] destination) => GetData(destination.AsSpan(), 0);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target array.
|
||||
/// </summary>
|
||||
/// <param name="destination">The input array to write data to.</param>
|
||||
/// <param name="offset">The offset to start reading data from.</param>
|
||||
/// <param name="destinationOffset">The starting offset within <paramref name="source"/> to write data to.</param>
|
||||
/// <param name="bufferOffset">The offset to start reading data from.</param>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
public void GetData(T[] destination, int offset, int count) => GetData(destination.AsSpan(), offset, count);
|
||||
public void GetData(T[] destination, int destinationOffset, int bufferOffset, int count)
|
||||
{
|
||||
Span<T> span = destination.AsSpan(destinationOffset, count);
|
||||
|
||||
GetData(span, bufferOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
|
||||
/// The input data will be read from the start of the buffer.
|
||||
/// </summary>
|
||||
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
|
||||
public void GetData(Span<T> destination) => GetData(destination, 0, Length);
|
||||
public void GetData(Span<T> destination) => GetData(destination, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
|
||||
/// <param name="offset">The offset to start reading data from.</param>
|
||||
/// <param name="count">The number of items to read.</param>
|
||||
public abstract void GetData(Span<T> destination, int offset, int count);
|
||||
public abstract void GetData(Span<T> destination, int offset);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Buffer{T}"/> instance.
|
||||
@@ -159,23 +165,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of the current <see cref="Buffer{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
|
||||
/// <param name="offset">The offset to start writing data to.</param>
|
||||
/// <param name="sourceOffset">The starting offset within <paramref name="source"/> to read data from.</param>
|
||||
/// <param name="bufferOffset">The offset to start writing data to.</param>
|
||||
/// <param name="count">The number of items to write.</param>
|
||||
public void SetData(T[] source, int offset, int count) => SetData(source.AsSpan(), offset, count);
|
||||
public void SetData(T[] source, int sourceOffset, int bufferOffset, int count)
|
||||
{
|
||||
ReadOnlySpan<T> span = source.AsSpan(sourceOffset, count);
|
||||
|
||||
SetData(span, bufferOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to the current <see cref="Buffer{T}"/> instance.
|
||||
/// The input data will be written to the start of the buffer, and all input items will be copied.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
|
||||
public void SetData(ReadOnlySpan<T> source) => SetData(source, 0, Length);
|
||||
public void SetData(ReadOnlySpan<T> source) => SetData(source, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Buffer{T}"/> instance.
|
||||
/// The input data will be written into the buffer starting at the specified offset, and all input items will be copied.
|
||||
/// </summary>
|
||||
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
|
||||
/// <param name="offset">The offset to start writing data to.</param>
|
||||
/// <param name="count">The number of items to write.</param>
|
||||
public abstract void SetData(ReadOnlySpan<T> source, int offset, int count);
|
||||
public abstract void SetData(ReadOnlySpan<T> source, int offset);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the contents of a given <see cref="Buffer{T}"/> to the current <see cref="Buffer{T}"/> instance.
|
||||
|
||||
@@ -30,20 +30,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> destination, int offset, int count)
|
||||
public override unsafe void GetData(Span<T> destination, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsInRange(count, 0, Length, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count));
|
||||
Guard.IsInRangeFor(count, destination, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination));
|
||||
|
||||
nint
|
||||
byteOffset = (nint)offset * ElementSizeInBytes,
|
||||
byteSize = (nint)count * ElementSizeInBytes;
|
||||
byteSize = (nint)destination.Length * ElementSizeInBytes;
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.ReadBack, (ulong)byteSize);
|
||||
|
||||
@@ -55,30 +53,28 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
|
||||
|
||||
using ID3D12ResourceMap resource = d3D12Resource.Get()->Map();
|
||||
|
||||
MemoryHelper.Copy(resource.Pointer, 0, destination, count);
|
||||
MemoryHelper.Copy(resource.Pointer, 0, destination);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> source, int offset, int count)
|
||||
public override unsafe void SetData(ReadOnlySpan<T> source, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsInRange(count, 0, Length, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count));
|
||||
Guard.IsInRangeFor(count, source, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source));
|
||||
|
||||
nint
|
||||
byteOffset = (nint)offset * ElementSizeInBytes,
|
||||
byteSize = (nint)count * ElementSizeInBytes;
|
||||
byteSize = (nint)source.Length * ElementSizeInBytes;
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(BufferType.Upload, (ulong)byteSize);
|
||||
|
||||
using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map())
|
||||
{
|
||||
MemoryHelper.Copy(source, resource.Pointer, 0, count);
|
||||
MemoryHelper.Copy(source, resource.Pointer, 0);
|
||||
}
|
||||
|
||||
using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY);
|
||||
|
||||
@@ -54,21 +54,20 @@ namespace ComputeSharp
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void GetData(Span<T> destination, int offset, int count)
|
||||
public override unsafe void GetData(Span<T> destination, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsInRange(count, 0, Length, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count));
|
||||
Guard.IsInRangeFor(count, destination, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + destination.Length, (uint)Length, nameof(destination));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
int count = destination.Length;
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(destination);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>(resource.Pointer);
|
||||
|
||||
@@ -82,25 +81,24 @@ namespace ComputeSharp
|
||||
Unsafe.Add(ref spanRef, i) = Unsafe.As<byte, T>(ref targetRef);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(resource.Pointer, offset, destination, count);
|
||||
else MemoryHelper.Copy(resource.Pointer, offset, destination);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe void SetData(ReadOnlySpan<T> source, int offset, int count)
|
||||
public override unsafe void SetData(ReadOnlySpan<T> source, int offset)
|
||||
{
|
||||
GraphicsDevice.ThrowIfDisposed();
|
||||
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsInRange(offset, 0, Length, nameof(offset));
|
||||
Guard.IsInRange(count, 0, Length, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + count, (uint)Length, nameof(count));
|
||||
Guard.IsInRangeFor(count, source, nameof(count));
|
||||
Guard.IsLessThanOrEqualTo((uint)offset + source.Length, (uint)Length, nameof(source));
|
||||
|
||||
using ID3D12ResourceMap resource = D3D12Resource->Map();
|
||||
|
||||
if (IsPaddingPresent)
|
||||
{
|
||||
int count = source.Length;
|
||||
ref T spanRef = ref MemoryMarshal.GetReference(source);
|
||||
ref byte resourceRef = ref Unsafe.AsRef<byte>(resource.Pointer);
|
||||
|
||||
@@ -114,7 +112,7 @@ namespace ComputeSharp
|
||||
Unsafe.As<byte, T>(ref targetRef) = Unsafe.Add(ref spanRef, i);
|
||||
}
|
||||
}
|
||||
else MemoryHelper.Copy(source, resource.Pointer, offset, count);
|
||||
else MemoryHelper.Copy(source, resource.Pointer, offset);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -15,12 +15,11 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
/// <param name="source">The source <see cref="ReadOnlySpan{T}"/> to read.</param>
|
||||
/// <param name="destination">The pointer for the destination memory area.</param>
|
||||
/// <param name="destinationOffset">The destination offset to start writing data to.</param>
|
||||
/// <param name="count">The total number of items to copy.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy<T>(ReadOnlySpan<T> source, void* destination, int destinationOffset, int count)
|
||||
public static unsafe void Copy<T>(ReadOnlySpan<T> source, void* destination, int destinationOffset)
|
||||
where T : unmanaged
|
||||
{
|
||||
source.CopyTo(new Span<T>((T*)destination + destinationOffset, count));
|
||||
source.CopyTo(new Span<T>((T*)destination + destinationOffset, source.Length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -30,13 +29,11 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
/// <param name="source">The <see cref="IntPtr"/> 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>
|
||||
/// <param name="destinationOffset">The destination offset to start writing data to.</param>
|
||||
/// <param name="count">The total number of items to copy.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy<T>(void* source, int sourceOffset, Span<T> destination, int count)
|
||||
public static unsafe void Copy<T>(void* source, int sourceOffset, Span<T> destination)
|
||||
where T : unmanaged
|
||||
{
|
||||
new Span<T>((T*)source + sourceOffset, count).CopyTo(destination.Slice(0, count));
|
||||
new Span<T>((T*)source + sourceOffset, destination.Length).CopyTo(destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする