From 1142d7bbcd4416bbc31b639b2a65cebc499c5757 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 25 Nov 2020 01:14:44 +0100 Subject: [PATCH] Improved Buffer copy APIs, fixed validation --- .../Buffers/Abstract/Buffer{T}.cs | 36 +++++++++++++------ .../Buffers/Abstract/StructuredBuffer{T}.cs | 20 +++++------ .../Buffers/ConstantBuffer{T}.cs | 18 +++++----- .../Helpers/MemoryHelper.cs | 11 +++--- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index db6884e5..9149926c 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -116,7 +116,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract { T[] data = GC.AllocateUninitializedArray(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 instance and writes them into a target array. /// /// The input array to write data to. - public void GetData(T[] destination) => GetData(destination.AsSpan(), 0, Length); + 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 offset to start reading data from. + /// 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 offset, int count) => GetData(destination.AsSpan(), offset, count); + 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, Length); + 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. - /// The number of items to read. - public abstract void GetData(Span destination, int offset, int count); + public abstract void GetData(Span destination, int offset); /// /// Writes the contents of a given array to the current instance. @@ -159,23 +165,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// 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 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 offset, int count) => SetData(source.AsSpan(), offset, count); + 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, Length); + public void SetData(ReadOnlySpan source) => SetData(source, 0); /// /// Writes the contents of a given to a specified area of the current instance. + /// The input data will be written into the buffer starting at the specified offset, and all input items will be copied. /// /// The input to read data from. /// The offset to start writing data to. /// The number of items to write. - public abstract void SetData(ReadOnlySpan source, int offset, int count); + public abstract void SetData(ReadOnlySpan source, int offset); /// /// Writes the contents of a given to the current instance. diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index 86bb4fde..ea0e9705 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -30,20 +30,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - public override unsafe void GetData(Span destination, int offset, int count) + public override unsafe void GetData(Span 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 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); } /// - public override unsafe void SetData(ReadOnlySpan source, int offset, int count) + public override unsafe void SetData(ReadOnlySpan 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 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); diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 4689e75a..6ef0e723 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -54,21 +54,20 @@ namespace ComputeSharp } /// - public override unsafe void GetData(Span destination, int offset, int count) + public override unsafe void GetData(Span 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(resource.Pointer); @@ -82,25 +81,24 @@ namespace ComputeSharp Unsafe.Add(ref spanRef, i) = Unsafe.As(ref targetRef); } } - else MemoryHelper.Copy(resource.Pointer, offset, destination, count); + else MemoryHelper.Copy(resource.Pointer, offset, destination); } /// - public override unsafe void SetData(ReadOnlySpan source, int offset, int count) + public override unsafe void SetData(ReadOnlySpan 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(resource.Pointer); @@ -114,7 +112,7 @@ namespace ComputeSharp Unsafe.As(ref targetRef) = Unsafe.Add(ref spanRef, i); } } - else MemoryHelper.Copy(source, resource.Pointer, offset, count); + else MemoryHelper.Copy(source, resource.Pointer, offset); } /// diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 252f0463..41945ac2 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -15,12 +15,11 @@ namespace ComputeSharp.Graphics.Helpers /// The source to read. /// The pointer for the destination memory area. /// The destination offset to start writing data to. - /// The total number of items to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(ReadOnlySpan source, void* destination, int destinationOffset, int count) + public static unsafe void Copy(ReadOnlySpan source, void* destination, int destinationOffset) where T : unmanaged { - source.CopyTo(new Span((T*)destination + destinationOffset, count)); + source.CopyTo(new Span((T*)destination + destinationOffset, source.Length)); } /// @@ -30,13 +29,11 @@ namespace ComputeSharp.Graphics.Helpers /// The that indicates the memory area to read from. /// The source offset to start reading data from. /// The destination to write. - /// The destination offset to start writing data to. - /// The total number of items to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(void* source, int sourceOffset, Span destination, int count) + public static unsafe void Copy(void* source, int sourceOffset, Span destination) where T : unmanaged { - new Span((T*)source + sourceOffset, count).CopyTo(destination.Slice(0, count)); + new Span((T*)source + sourceOffset, destination.Length).CopyTo(destination); } } }