From 9edbdcfdc3b9d1bf55d7033f887d6dec618b804f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 13 Aug 2019 21:38:39 +0200 Subject: [PATCH] Code refactoring --- .../Buffers/Abstract/HlslBuffer{T}.cs | 24 ++++++++++++-- .../Buffers/ReadOnlyBuffer{T}.cs | 16 ---------- .../Buffers/ReadWriteBuffer{T}.cs | 31 +------------------ 3 files changed, 23 insertions(+), 48 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs index 34d3befd..d0873434 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslBuffer{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Diagnostics.Contracts; using SharpDX.Direct3D12; @@ -48,12 +49,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// Writes the contents of a given to the current instance /// /// The input to read data from - public abstract void SetData(ReadOnlyBuffer buffer); + public virtual void SetData(ReadOnlyBuffer buffer) => SetDataWithCpuBuffer(buffer); /// /// Writes the contents of a given to the current instance /// /// The input to read data from - public abstract void SetData(ReadWriteBuffer buffer); + public virtual void SetData(ReadWriteBuffer buffer) => SetDataWithCpuBuffer(buffer); + + /// + /// Writes the contents of a given to the current instance, using a temporary CPU buffer + /// + /// The input to read data from + protected void SetDataWithCpuBuffer(HlslBuffer 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/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 4ceeea12..8aac27b4 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -118,21 +118,5 @@ namespace ComputeSharp.Graphics.Buffers 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 4414c1b1..a444baa8 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -63,20 +63,7 @@ namespace ComputeSharp.Graphics.Buffers /// 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); - } + if (buffer.IsPaddingPresent) SetDataWithCpuBuffer(buffer); else { // Directly copy the input buffer @@ -86,21 +73,5 @@ namespace ComputeSharp.Graphics.Buffers 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); - } } }