Refactored public Buffer<T> APIs

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-06 21:46:43 +01:00
1 親 ccec496316
コミット 0e2d604f47
4ファイル変更
+173 -124

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

+3 -121
ファイルの表示
@@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.Diagnostics.Contracts;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Core.Interop;
@@ -94,77 +92,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Reads the contents of the current <see cref="Buffer{T}"/> instance and returns an array.
/// </summary>
/// <returns>A <typeparamref name="T"/> array with the contents of the current buffer.</returns>
[Pure]
public T[] GetData()
{
return GetData(0, Length);
}
/// <summary>
/// Reads the contents of the current <see cref="Buffer{T}"/> instance in a given range and returns an array.
/// </summary>
/// <param name="offset">The offset to start reading data from.</param>
/// <param name="count">The number of items to read.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the specified range from the current buffer.</returns>
[Pure]
public T[] GetData(int offset, int count)
{
Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count));
T[] data = GC.AllocateUninitializedArray<T>(count);
GetData(data.AsSpan(), offset);
return data;
}
/// <summary>
/// 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);
}
/// <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="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 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);
}
/// <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>
public void GetData(Span<T> destination, int offset)
{
GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset);
}
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Buffer{T}"/> instance and writes them into a target memory area.
/// </summary>
@@ -173,49 +100,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
/// <param name="offset">The offset to start reading data from.</param>
internal abstract void GetData(ref T destination, nint size, int offset);
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to the current <see cref="Buffer{T}"/> instance.
/// </summary>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public void SetData(T[] source)
{
SetData(source.AsSpan());
}
/// <summary>
/// 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="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 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);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Buffer{T}"/> instance.
/// </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>
public void SetData(ReadOnlySpan<T> source, int offset)
{
SetData(ref MemoryMarshal.GetReference(source), source.Length, offset);
}
/// <summary>
/// Writes the contents of a given memory area to a specified area of the current <see cref="Buffer{T}"/> instance.
/// </summary>
@@ -240,11 +124,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
try
{
Span<T> span = array.AsSpan(0, source.Length);
source.GetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0);
source.GetData(span);
SetData(span);
SetData(ref MemoryMarshal.GetArrayDataReference(array), source.Length, 0);
}
finally
{
+11 -2
ファイルの表示
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Buffers.Abstract;
namespace ComputeSharp.Graphics.Buffers.Views
@@ -16,7 +18,14 @@ namespace ComputeSharp.Graphics.Buffers.Views
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance with the items to display.</param>
public BufferDebugView(Buffer<T>? buffer)
{
Items = buffer?.GetData();
if (buffer is not null)
{
var items = GC.AllocateUninitializedArray<T>(buffer.Length);
buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0);
Items = items;
}
}
/// <summary>
+3 -1
ファイルの表示
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Commands;
@@ -136,8 +137,9 @@ namespace ComputeSharp.Shaders
// Initialize the loop targets and the captured values
ConstantBuffer<Int4> variablesBuffer = ShaderRunner.GetVariablesBuffer(device);
ReadOnlySpan<Int4> variables = dispatchData.Variables;
variablesBuffer.SetData(dispatchData.Variables);
variablesBuffer.SetData(ref MemoryMarshal.GetReference(variables), variables.Length, 0);
commandList.SetComputeRootDescriptorTable(0, variablesBuffer.D3D12GpuDescriptorHandle);
+156
ファイルの表示
@@ -0,0 +1,156 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using ComputeSharp.Graphics.Buffers.Abstract;
using Microsoft.Toolkit.Diagnostics;
namespace ComputeSharp
{
/// <summary>
/// A <see langword="class"/> that contains extension methods for the <see cref="Buffer{T}"/> type.
/// </summary>
public static class BufferExtensions
{
/// <summary>
/// Reads the contents of a <see cref="Buffer{T}"/> instance and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the input buffer.</returns>
[Pure]
public static T[] GetData<T>(this Buffer<T> buffer)
where T : unmanaged
{
return buffer.GetData(0, buffer.Length);
}
/// <summary>
/// Reads the contents of a <see cref="Buffer{T}"/> instance in a given range and returns an array.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <param name="offset">The offset to start reading data from.</param>
/// <param name="count">The number of items to read.</param>
/// <returns>A <typeparamref name="T"/> array with the contents of the specified range from the current buffer.</returns>
[Pure]
public static T[] GetData<T>(this Buffer<T> buffer, int offset, int count)
where T : unmanaged
{
Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count));
T[] data = GC.AllocateUninitializedArray<T>(count);
buffer.GetData(data.AsSpan(), offset);
return data;
}
/// <summary>
/// Reads the contents of s <see cref="Buffer{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</param>
public static void GetData<T>(this Buffer<T> buffer, T[] destination)
where T : unmanaged
{
buffer.GetData(destination.AsSpan(), 0);
}
/// <summary>
/// Reads the contents of the specified range from s <see cref="Buffer{T}"/> instance and writes them into a target array.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <param name="destination">The input array to write data to.</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 static void GetData<T>(this Buffer<T> buffer, T[] destination, int destinationOffset, int bufferOffset, int count)
where T : unmanaged
{
Span<T> span = destination.AsSpan(destinationOffset, count);
buffer.GetData(span, bufferOffset);
}
/// <summary>
/// Reads the contents of a <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
public static void GetData<T>(this Buffer<T> buffer, Span<T> destination)
where T : unmanaged
{
buffer.GetData(destination, 0);
}
/// <summary>
/// Reads the contents of the specified range from a <see cref="Buffer{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The input <see cref="Buffer{T}"/> instance to read data from.</param>
/// <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>
public static void GetData<T>(this Buffer<T> buffer, Span<T> destination, int offset)
where T : unmanaged
{
buffer.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a <see cref="Buffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The target <see cref="Buffer{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</param>
public static void SetData<T>(this Buffer<T> buffer, T[] source)
where T : unmanaged
{
buffer.SetData(source.AsSpan(), 0);
}
/// <summary>
/// Writes the contents of a given <typeparamref name="T"/> array to a specified area of a <see cref="Buffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The target <see cref="Buffer{T}"/> instance to write data to.</param>
/// <param name="source">The input <typeparamref name="T"/> array to read data from.</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 static void SetData<T>(this Buffer<T> buffer, T[] source, int sourceOffset, int bufferOffset, int count)
where T : unmanaged
{
ReadOnlySpan<T> span = source.AsSpan(sourceOffset, count);
buffer.SetData(span, bufferOffset);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a <see cref="Buffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The target <see cref="Buffer{T}"/> instance to write data to.</param>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
public static void SetData<T>(this Buffer<T> buffer, ReadOnlySpan<T> source)
where T : unmanaged
{
buffer.SetData(source, 0);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of a <see cref="Buffer{T}"/> instance.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
/// <param name="buffer">The target <see cref="Buffer{T}"/> instance to write data to.</param>
/// <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>
public static void SetData<T>(this Buffer<T> buffer, ReadOnlySpan<T> source, int offset)
where T : unmanaged
{
buffer.SetData(ref MemoryMarshal.GetReference(source), source.Length, offset);
}
}
}