From 0e2d604f47ab9739201b69ca6cc2e5d47388fcc1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 6 Dec 2020 21:46:43 +0100 Subject: [PATCH] Refactored public Buffer APIs --- .../Buffers/Abstract/Buffer{T}.cs | 124 +------------- .../Buffers/Views/BufferDebugView{T}.cs | 13 +- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 4 +- .../Extensions/BufferExtensions.cs | 156 ++++++++++++++++++ 4 files changed, 173 insertions(+), 124 deletions(-) create mode 100644 src/ComputeSharp/Extensions/BufferExtensions.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index dcd3c52e..a7b5c7f9 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -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 /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - /// - /// Reads the contents of the current instance and returns an array. - /// - /// A array with the contents of the current buffer. - [Pure] - public T[] GetData() - { - return GetData(0, Length); - } - - /// - /// Reads the contents of the current instance in a given range and returns an array. - /// - /// The offset to start reading data from. - /// The number of items to read. - /// A array with the contents of the specified range from the current buffer. - [Pure] - public T[] GetData(int offset, int count) - { - Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); - - T[] data = GC.AllocateUninitializedArray(count); - - GetData(data.AsSpan(), offset); - - return data; - } - - /// - /// Reads the contents of the current instance and writes them into a target array. - /// - /// The input array to write data to. - public void GetData(T[] destination) - { - GetData(destination.AsSpan(), 0); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target array. - /// - /// The input array to write data to. - /// The starting offset within to write data to. - /// The offset to start reading data from. - /// The number of items to read. - public void GetData(T[] destination, int destinationOffset, int bufferOffset, int count) - { - Span span = destination.AsSpan(destinationOffset, count); - - GetData(span, bufferOffset); - } - - /// - /// Reads the contents of the current instance and writes them into a target . - /// The input data will be read from the start of the buffer. - /// - /// The input to write data to. - public void GetData(Span destination) - { - GetData(destination, 0); - } - - /// - /// Reads the contents of the specified range from the current instance and writes them into a target . - /// - /// The input to write data to. - /// The offset to start reading data from. - public void GetData(Span destination, int offset) - { - GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); - } - /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -173,49 +100,6 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The offset to start reading data from. internal abstract void GetData(ref T destination, nint size, int offset); - /// - /// Writes the contents of a given array to the current instance. - /// - /// The input array to read data from. - public void SetData(T[] source) - { - SetData(source.AsSpan()); - } - - /// - /// Writes the contents of a given array to a specified area of the current instance. - /// - /// The input array to read data from. - /// The starting offset within to read data from. - /// The offset to start writing data to. - /// The number of items to write. - public void SetData(T[] source, int sourceOffset, int bufferOffset, int count) - { - ReadOnlySpan span = source.AsSpan(sourceOffset, count); - - SetData(span, bufferOffset); - } - - /// - /// Writes the contents of a given to the current instance. - /// The input data will be written to the start of the buffer, and all input items will be copied. - /// - /// The input to read data from. - public void SetData(ReadOnlySpan source) - { - SetData(source, 0); - } - - /// - /// Writes the contents of a given to a specified area of the current instance. - /// - /// The input to read data from. - /// The offset to start writing data to. - public void SetData(ReadOnlySpan source, int offset) - { - SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); - } - /// /// Writes the contents of a given memory area to a specified area of the current instance. /// @@ -240,11 +124,9 @@ namespace ComputeSharp.Graphics.Buffers.Abstract try { - Span 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 { diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs index ad14b604..76b35a60 100644 --- a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -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 /// The input instance with the items to display. public BufferDebugView(Buffer? buffer) { - Items = buffer?.GetData(); + if (buffer is not null) + { + var items = GC.AllocateUninitializedArray(buffer.Length); + + buffer.GetData(ref MemoryMarshal.GetArrayDataReference(items), buffer.Length, 0); + + Items = items; + } } /// diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 7f9248a2..5bec10c2 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -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 variablesBuffer = ShaderRunner.GetVariablesBuffer(device); + ReadOnlySpan variables = dispatchData.Variables; - variablesBuffer.SetData(dispatchData.Variables); + variablesBuffer.SetData(ref MemoryMarshal.GetReference(variables), variables.Length, 0); commandList.SetComputeRootDescriptorTable(0, variablesBuffer.D3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp/Extensions/BufferExtensions.cs b/src/ComputeSharp/Extensions/BufferExtensions.cs new file mode 100644 index 00000000..95ddd2f5 --- /dev/null +++ b/src/ComputeSharp/Extensions/BufferExtensions.cs @@ -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 +{ + /// + /// A that contains extension methods for the type. + /// + public static class BufferExtensions + { + /// + /// Reads the contents of a instance and returns an array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// A array with the contents of the input buffer. + [Pure] + public static T[] GetData(this Buffer buffer) + where T : unmanaged + { + return buffer.GetData(0, buffer.Length); + } + + /// + /// Reads the contents of a instance in a given range and returns an array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The offset to start reading data from. + /// The number of items to read. + /// A array with the contents of the specified range from the current buffer. + [Pure] + public static T[] GetData(this Buffer buffer, int offset, int count) + where T : unmanaged + { + Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count)); + + T[] data = GC.AllocateUninitializedArray(count); + + buffer.GetData(data.AsSpan(), offset); + + return data; + } + + /// + /// Reads the contents of s instance and writes them into a target array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input array to write data to. + public static void GetData(this Buffer buffer, T[] destination) + where T : unmanaged + { + buffer.GetData(destination.AsSpan(), 0); + } + + /// + /// Reads the contents of the specified range from s instance and writes them into a target array. + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input array to write data to. + /// The starting offset within to write data to. + /// The offset to start reading data from. + /// The number of items to read. + public static void GetData(this Buffer buffer, T[] destination, int destinationOffset, int bufferOffset, int count) + where T : unmanaged + { + Span span = destination.AsSpan(destinationOffset, count); + + buffer.GetData(span, bufferOffset); + } + + /// + /// Reads the contents of a instance and writes them into a target . + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input to write data to. + public static void GetData(this Buffer buffer, Span destination) + where T : unmanaged + { + buffer.GetData(destination, 0); + } + + /// + /// Reads the contents of the specified range from a instance and writes them into a target . + /// + /// The type of items stored on the buffer. + /// The input instance to read data from. + /// The input to write data to. + /// The offset to start reading data from. + public static void GetData(this Buffer buffer, Span destination, int offset) + where T : unmanaged + { + buffer.GetData(ref MemoryMarshal.GetReference(destination), destination.Length, offset); + } + + /// + /// Writes the contents of a given array to a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input array to read data from. + public static void SetData(this Buffer buffer, T[] source) + where T : unmanaged + { + buffer.SetData(source.AsSpan(), 0); + } + + /// + /// Writes the contents of a given array to a specified area of a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input array to read data from. + /// The starting offset within to read data from. + /// The offset to start writing data to. + /// The number of items to write. + public static void SetData(this Buffer buffer, T[] source, int sourceOffset, int bufferOffset, int count) + where T : unmanaged + { + ReadOnlySpan span = source.AsSpan(sourceOffset, count); + + buffer.SetData(span, bufferOffset); + } + + /// + /// Writes the contents of a given to a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input to read data from. + public static void SetData(this Buffer buffer, ReadOnlySpan source) + where T : unmanaged + { + buffer.SetData(source, 0); + } + + /// + /// Writes the contents of a given to a specified area of a instance. + /// + /// The type of items stored on the buffer. + /// The target instance to write data to. + /// The input to read data from. + /// The offset to start writing data to. + public static void SetData(this Buffer buffer, ReadOnlySpan source, int offset) + where T : unmanaged + { + buffer.SetData(ref MemoryMarshal.GetReference(source), source.Length, offset); + } + } +}