From 19454b4e586ff7d0a852caa17bb31113f5f3fa3a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 20 Aug 2019 00:28:30 +0200 Subject: [PATCH] Refactored buffers with new APIs --- .../Abstract/HlslStructuredBuffer{T}.cs | 16 +++++++-------- .../Buffers/ConstantBuffer{T}.cs | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs index 944adb56..7829264f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/HlslStructuredBuffer{T}.cs @@ -22,31 +22,31 @@ namespace ComputeSharp.Graphics.Buffers.Abstract internal HlslStructuredBuffer(GraphicsDevice device, int size, BufferType bufferType) : base(device, size, size * Unsafe.SizeOf(), bufferType) { } /// - public override void GetData(Span span) + public override void GetData(Span span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); + using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(this, 0, transferBuffer, 0, SizeInBytes); + copyCommandList.CopyBufferRegion(this, offset, transferBuffer, 0, count * ElementSizeInBytes); copyCommandList.Flush(); transferBuffer.Map(0); - MemoryHelper.Copy(transferBuffer.MappedResource, span); + MemoryHelper.Copy(transferBuffer.MappedResource, 0, span, 0, count); transferBuffer.Unmap(0); } /// - public override void SetData(Span span) + public override void SetData(Span span, int offset, int count) { - using Buffer transferBuffer = new Buffer(GraphicsDevice, Size, SizeInBytes, BufferType.Transfer); + using Buffer transferBuffer = new Buffer(GraphicsDevice, count, count * ElementSizeInBytes, BufferType.Transfer); transferBuffer.Map(0); - MemoryHelper.Copy(span, transferBuffer.MappedResource); + MemoryHelper.Copy(span, 0, transferBuffer.MappedResource, 0, count); transferBuffer.Unmap(0); using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - copyCommandList.CopyBufferRegion(transferBuffer, 0, this, 0, SizeInBytes); + copyCommandList.CopyBufferRegion(transferBuffer, 0, this, offset, count * ElementSizeInBytes); copyCommandList.Flush(); } diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 85321b41..6e19703b 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -41,17 +41,17 @@ namespace ComputeSharp.Graphics.Buffers public T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); /// - public override void GetData(Span span) + public override void GetData(Span span, int offset, int count) { if (IsPaddingPresent) { // Create the temporary array - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(); + byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(count * PaddedElementSizeInBytes); // Copy the padded data to the temporary array Map(0); - MemoryHelper.Copy(MappedResource, temporarySpan); + MemoryHelper.Copy(MappedResource, offset * PaddedElementSizeInBytes, temporarySpan, 0, count * PaddedElementSizeInBytes); Unmap(0); ref byte tin = ref temporarySpan.GetPinnableReference(); @@ -70,19 +70,19 @@ namespace ComputeSharp.Graphics.Buffers { // Directly copy the data back if there is no padding Map(0); - MemoryHelper.Copy(MappedResource, span); + MemoryHelper.Copy(MappedResource, offset, span, 0, count); Unmap(0); } } /// - public override void SetData(Span span) + public override void SetData(Span span, int offset, int count) { if (IsPaddingPresent) { // Create the temporary array - byte[] temporaryArray = ArrayPool.Shared.Rent(SizeInBytes); - Span temporarySpan = temporaryArray.AsSpan(0, SizeInBytes); // Array pool arrays can be longer + byte[] temporaryArray = ArrayPool.Shared.Rent(count * PaddedElementSizeInBytes); + Span temporarySpan = temporaryArray.AsSpan(0, count * PaddedElementSizeInBytes); // Array pool arrays can be longer ref T tin = ref span.GetPinnableReference(); ref byte tout = ref temporarySpan.GetPinnableReference(); @@ -95,7 +95,7 @@ namespace ComputeSharp.Graphics.Buffers // Copy the padded data to the GPU Map(0); - MemoryHelper.Copy(temporarySpan, MappedResource); + MemoryHelper.Copy(temporarySpan, 0, MappedResource, offset * PaddedElementSizeInBytes, count * PaddedElementSizeInBytes); Unmap(0); ArrayPool.Shared.Return(temporaryArray); @@ -104,7 +104,7 @@ namespace ComputeSharp.Graphics.Buffers { // Directly copy the input span if there is no padding Map(0); - MemoryHelper.Copy(span, MappedResource); + MemoryHelper.Copy(span, 0, MappedResource, offset, count); Unmap(0); } }