diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 0e17e3be..cf3cab04 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -89,12 +89,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract public GraphicsDevice GraphicsDevice { get; } /// - /// Gets the size of the current buffer, as in the number of values it contains + /// Gets the size of the current buffer, as in the number of values it contains. /// public int Size { get; } /// - /// Gets whether or not there is some padding between elements in the current buffer + /// Gets whether or not there is some padding between elements in the current buffer. /// internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes; @@ -104,92 +104,96 @@ namespace ComputeSharp.Graphics.Buffers.Abstract internal ID3D12Resource* D3D12Resource => this.d3D12Resource; /// - /// Reads the contents of the current instance and returns an array + /// Reads the contents of the current instance and returns an array. /// - /// A array with the contents of the current buffer + /// A array with the contents of the current buffer. [Pure] public T[] GetData() => GetData(0, Size); /// - /// Reads the contents of the current instance in a given range and returns an array + /// 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 + /// 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) { T[] data = new T[count]; + GetData(data, offset, count); return data; } /// - /// Reads the contents of the current instance and writes them into a target + /// Reads the contents of the current instance and writes them into a target . /// - /// The input to write data to + /// The input to write data to. public void GetData(Span span) => GetData(span, 0, Size); /// - /// Reads the contents of the specified range from the current instance and writes them into a target + /// 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 - /// The number of items to read + /// The input to write data to. + /// The offset to start reading data from. + /// The number of items to read. public abstract void GetData(Span span, int offset, int count); /// - /// Writes the contents of a given array to the current instance + /// Writes the contents of a given array to the current instance. /// - /// The input array to read data from + /// The input array to read data from. public void SetData(T[] array) => SetData(array.AsSpan()); /// - /// Writes the contents of a given array to a specified area of the current instance + /// Writes the contents of a given array to a specified area of the current instance. /// - /// The input array to read data from - /// The offset to start writing data to - /// The number of items to write + /// The input array to read data from. + /// The offset to start writing data to. + /// The number of items to write. public void SetData(T[] array, int offset, int count) => SetData(array.AsSpan(), offset, count); /// - /// Writes the contents of a given to the current instance + /// Writes the contents of a given to the current instance. /// - /// The input to read data from + /// The input to read data from. public void SetData(ReadOnlySpan span) => SetData(span, 0, Size); /// - /// Writes the contents of a given to a specified area of the current instance + /// 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 - /// The number of items to write + /// The input to read data from. + /// The offset to start writing data to. + /// The number of items to write. public abstract void SetData(ReadOnlySpan span, int offset, int count); /// - /// Writes the contents of a given to the current instance + /// Writes the contents of a given to the current instance. /// - /// The input to read data from + /// The input to read data from. public abstract void SetData(Buffer buffer); /// - /// Writes the contents of a given to the current instance, using a temporary CPU buffer + /// Writes the contents of a given to the current instance, using a temporary CPU buffer. /// - /// The input to read data from + /// The input to read data from. protected void SetDataWithCpuBuffer(Buffer buffer) { - // Create a temporary array T[] array = ArrayPool.Shared.Rent(buffer.Size); - Span span = array.AsSpan(0, buffer.Size); - // Get the unpadded data from the read write buffer - buffer.GetData(span); + try + { + Span span = array.AsSpan(0, buffer.Size); - // Set the data, adding the padding if needed - SetData(span); + buffer.GetData(span); - ArrayPool.Shared.Return(array); + SetData(span); + } + finally + { + ArrayPool.Shared.Return(array); + } } ///