diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 1d8fa479..34d3befd 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -43,5 +43,17 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The input to read data from public abstract void SetData(Span span); + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public abstract void SetData(ReadOnlyBuffer buffer); + + /// + /// Writes the contents of a given to the current instance + /// + /// The input to read data from + public abstract void SetData(ReadWriteBuffer buffer); } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 52427119..4ceeea12 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -21,12 +21,18 @@ namespace ComputeSharp.Graphics.Buffers internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, size * (Unsafe.SizeOf() / 16 + 1) * 16, HeapType.Upload) { PaddedElementSizeInBytes = SizeInBytes / Size; + IsPaddingPresent = PaddedElementSizeInBytes > ElementSizeInBytes; } /// /// Gets the size in bytes of the current buffer /// - private int PaddedElementSizeInBytes { get; } + internal int PaddedElementSizeInBytes { get; } + + /// + /// Gets whether or not there is some padding between elements in the current buffer + /// + internal bool IsPaddingPresent { get; } /// /// Gets a single value from the current read write buffer @@ -38,14 +44,7 @@ namespace ComputeSharp.Graphics.Buffers /// public override void GetData(Span span) { - // Directly copy the data back if there is no padding - if (PaddedElementSizeInBytes == ElementSizeInBytes) - { - Map(0); - MemoryHelper.Copy(MappedResource, span); - Unmap(0); - } - else + if (IsPaddingPresent) { // Create the temporary array byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); @@ -68,19 +67,19 @@ namespace ComputeSharp.Graphics.Buffers ArrayPool.Shared.Return(temporaryArray); } + else + { + // Directly copy the data back if there is no padding + Map(0); + MemoryHelper.Copy(MappedResource, span); + Unmap(0); + } } /// public override void SetData(Span span) { - // Directly copy the input span if there is no padding - if (PaddedElementSizeInBytes == ElementSizeInBytes) - { - Map(0); - MemoryHelper.Copy(span, MappedResource); - Unmap(0); - } - else + if (IsPaddingPresent) { // Create the temporary array byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); @@ -102,6 +101,38 @@ namespace ComputeSharp.Graphics.Buffers ArrayPool.Shared.Return(temporaryArray); } + else + { + // Directly copy the input span if there is no padding + Map(0); + MemoryHelper.Copy(span, MappedResource); + Unmap(0); + } + } + + /// + public override void SetData(ReadOnlyBuffer buffer) + { + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes); + copyCommandList.Flush(true); + } + + /// + public override void SetData(ReadWriteBuffer buffer) + { + // Create a temporary array + T[] array = ArrayPool.Shared.Rent(buffer.Size); + Span span = array.AsSpan(buffer.Size); + + // Get the unpadded data from the read write buffer + buffer.GetData(span); + + // Set the data, adding the padding if needed + SetData(span); + + ArrayPool.Shared.Return(array); } } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index 446719b5..4414c1b1 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Helpers; @@ -48,14 +49,58 @@ namespace ComputeSharp.Graphics.Buffers public override void SetData(Span span) { using Buffer uploadBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, HeapType.Upload); - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); uploadBuffer.Map(0); MemoryHelper.Copy(span, uploadBuffer.MappedResource); uploadBuffer.Unmap(0); + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + copyCommandList.CopyBufferRegion(uploadBuffer, 0, this, 0, SizeInBytes); copyCommandList.Flush(true); } + + /// + public override void SetData(ReadOnlyBuffer buffer) + { + if (buffer.IsPaddingPresent) + { + // Create a temporary array + T[] array = ArrayPool.Shared.Rent(buffer.Size); + Span span = array.AsSpan(buffer.Size); + + // Get the data from the readonly array, removing the padding + buffer.GetData(span); + + // Set the data to the current buffer + SetData(span); + + ArrayPool.Shared.Return(array); + } + else + { + // Directly copy the input buffer + using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); + + copyCommandList.CopyBufferRegion(buffer, 0, this, 0, SizeInBytes); + copyCommandList.Flush(true); + } + } + + /// + public override void SetData(ReadWriteBuffer buffer) + { + // Create a temporary array + T[] array = ArrayPool.Shared.Rent(buffer.Size); + Span span = array.AsSpan(buffer.Size); + + // Get the unpadded data from the read write buffer + buffer.GetData(span); + + // Set the data to the current buffer + SetData(span); + + ArrayPool.Shared.Return(array); + } } }