From fe520dc9cc3a939431be1836336fa16254d1d224 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 23 Feb 2024 15:40:01 -0800 Subject: [PATCH] add element-wise buffer upload --- src/Graphics/CommandBuffer.cs | 51 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index e44d53b..554bc53 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1938,10 +1938,10 @@ namespace MoonWorks.Graphics } /// - /// Uploads data from a CpuBuffer to a TextureSlice. + /// Uploads data from a TransferBuffer to a TextureSlice. /// This copy occurs on the GPU timeline. /// - /// Overwriting the contents of the CpuBuffer before the command buffer + /// Overwriting the contents of the TransferBuffer before the command buffer /// has finished execution will cause undefined behavior. /// /// You MAY assume that the copy has finished for subsequent commands. @@ -1982,10 +1982,10 @@ namespace MoonWorks.Graphics } /// - /// Uploads data from a CpuBuffer to a GpuBuffer. + /// Uploads data from a TransferBuffer to a GpuBuffer. /// This copy occurs on the GPU timeline. /// - /// Overwriting the contents of the CpuBuffer before the command buffer + /// Overwriting the contents of the TransferBuffer before the command buffer /// has finished execution will cause undefined behavior. /// /// You MAY assume that the copy has finished for subsequent commands. @@ -2012,7 +2012,7 @@ namespace MoonWorks.Graphics } /// - /// Copies the entire contents of a CpuBuffer to a GpuBuffer. + /// Copies the entire contents of a TransferBuffer to a GpuBuffer. /// public void UploadToBuffer( TransferBuffer transferBuffer, @@ -2026,10 +2026,37 @@ namespace MoonWorks.Graphics } /// - /// Downloads data from a Texture to a CpuBuffer. + /// Copies data element-wise into from a TransferBuffer to a GpuBuffer. + /// + public void UploadToBuffer( + TransferBuffer transferBuffer, + GpuBuffer gpuBuffer, + uint sourceStartElement, + uint destinationStartElement, + uint numElements + ) where T : unmanaged + { + var elementSize = Marshal.SizeOf(); + var dataLengthInBytes = (uint) (elementSize * numElements); + var srcOffsetInBytes = (uint) (elementSize * sourceStartElement); + var dstOffsetInBytes = (uint) (elementSize * destinationStartElement); + + UploadToBuffer( + transferBuffer, + gpuBuffer, + new BufferCopy( + srcOffsetInBytes, + dstOffsetInBytes, + dataLengthInBytes + ) + ); + } + + /// + /// Downloads data from a Texture to a TransferBuffer. /// This copy occurs on the GPU timeline. /// - /// You MAY NOT assume that the data in the CpuBuffer is + /// You MAY NOT assume that the data in the TransferBuffer is /// fully copied until the command buffer has finished execution. /// public void DownloadFromTexture( @@ -2053,7 +2080,7 @@ namespace MoonWorks.Graphics } /// - /// Downloads the contents of a Texture with no mips into a CpuBuffer. + /// Downloads the contents of a Texture with no mips into a TransferBuffer. /// public void DownloadFromTexture( Texture texture, @@ -2067,10 +2094,10 @@ namespace MoonWorks.Graphics } /// - /// Downloads data from a GpuBuffer to a CpuBuffer. + /// Downloads data from a GpuBuffer to a TransferBuffer. /// This copy occurs on the GPU timeline. /// - /// You MAY NOT assume that the data in the CpuBuffer is + /// You MAY NOT assume that the data in the TransferBuffer is /// fully copied until the command buffer has finished execution. /// public void DownloadFromBuffer( @@ -2094,10 +2121,10 @@ namespace MoonWorks.Graphics } /// - /// Downloads data from a GpuBuffer to a CpuBuffer. + /// Downloads data from a GpuBuffer to a TransferBuffer. /// This copy occurs on the GPU timeline. /// - /// You MAY NOT assume that the data in the CpuBuffer is + /// You MAY NOT assume that the data in the TransferBuffer is /// fully copied until the command buffer has finished execution. /// public void DownloadFromBuffer(