From 3584e670ee455b7f737936c3a7301241ffc3ffe1 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 5 Apr 2023 12:40:34 -0700 Subject: [PATCH] add array overloads to avoid explicit generic parameter --- src/Graphics/CommandBuffer.cs | 61 ++++++++++++++++++++++++++++++++ src/Graphics/Resources/Buffer.cs | 51 +++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index 508c5d0..8992819 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1731,6 +1731,28 @@ namespace MoonWorks.Graphics ); } + /// + /// Copies array data into a buffer. + /// + /// The buffer to copy to. + /// The array to copy from. + /// Specifies where in the buffer to start copying. + /// Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred. + public unsafe void SetBufferData( + Buffer buffer, + T[] data, + uint bufferOffsetInBytes = 0 + ) where T : unmanaged + { + SetBufferData( + buffer, + new Span(data), + bufferOffsetInBytes, + 0, + (uint) data.Length + ); + } + /// /// Copies arbitrary data into a buffer. /// @@ -1797,6 +1819,26 @@ namespace MoonWorks.Graphics } } + /// + /// Copies array data into a buffer. + /// + /// The buffer to copy to. + /// The span to copy from. + /// Specifies where in the buffer to start copying. + /// The index of the first element to copy from the array. + /// How many elements to copy. + /// Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred. + public unsafe void SetBufferData( + Buffer buffer, + T[] data, + uint bufferOffsetInBytes, + uint startElement, + uint numElements + ) where T : unmanaged + { + SetBufferData(buffer, new Span(data), bufferOffsetInBytes, startElement, numElements); + } + public unsafe void SetBufferData( Buffer buffer, IntPtr dataPtr, @@ -1826,6 +1868,15 @@ namespace MoonWorks.Graphics SetTextureData(new TextureSlice(texture), data); } + /// + /// Asynchronously copies data into a texture. + /// + /// An array of data to copy into the texture. + public unsafe void SetTextureData(Texture texture, T[] data) where T : unmanaged + { + SetTextureData(new TextureSlice(texture), new Span(data)); + } + /// /// Asynchronously copies data into a texture slice. /// @@ -1851,6 +1902,16 @@ namespace MoonWorks.Graphics } } + /// + /// Asynchronously copies data into a texture slice. + /// + /// The texture slice to copy into. + /// An array of data to copy into the texture. + public unsafe void SetTextureData(in TextureSlice textureSlice, T[] data) where T : unmanaged + { + SetTextureData(textureSlice, new Span(data)); + } + /// /// Asynchronously copies data into a texture slice. /// diff --git a/src/Graphics/Resources/Buffer.cs b/src/Graphics/Resources/Buffer.cs index 1d653b1..54503c0 100644 --- a/src/Graphics/Resources/Buffer.cs +++ b/src/Graphics/Resources/Buffer.cs @@ -58,7 +58,7 @@ namespace MoonWorks.Graphics } /// - /// Reads data out of a buffer and into an array. + /// Reads data out of a buffer and into a span. /// This operation is only guaranteed to read up-to-date data if GraphicsDevice.Wait is called first. /// /// The span that data will be copied to. @@ -68,6 +68,13 @@ namespace MoonWorks.Graphics uint dataLengthInBytes ) where T : unmanaged { +#if DEBUG + if (dataLengthInBytes > Size) + { + Logger.LogWarn("Requested too many bytes from buffer!"); + } +#endif + fixed (T* ptr = data) { Refresh.Refresh_GetBufferData( @@ -79,6 +86,48 @@ namespace MoonWorks.Graphics } } + /// + /// Reads data out of a buffer and into an array. + /// This operation is only guaranteed to read up-to-date data if GraphicsDevice.Wait is called first. + /// + /// The span that data will be copied to. + /// The length of the data to read. + public unsafe void GetData( + T[] data, + uint dataLengthInBytes + ) where T : unmanaged + { + GetData(new Span(data), dataLengthInBytes); + } + + /// + /// Reads data out of a buffer and into a span. + /// This operation is only guaranteed to read up-to-date data if GraphicsDevice.Wait is called first. + /// Fills the span with as much data from the buffer as it can. + /// + /// The span that data will be copied to. + public unsafe void GetData( + Span data + ) where T : unmanaged + { + var lengthInBytes = System.Math.Min(data.Length * Marshal.SizeOf(), Size); + GetData(data, (uint) lengthInBytes); + } + + /// + /// Reads data out of a buffer and into an array. + /// This operation is only guaranteed to read up-to-date data if GraphicsDevice.Wait is called first. + /// Fills the array with as much data from the buffer as it can. + /// + /// The span that data will be copied to. + public unsafe void GetData( + T[] data + ) where T : unmanaged + { + var lengthInBytes = System.Math.Min(data.Length * Marshal.SizeOf(), Size); + GetData(new Span(data), (uint) lengthInBytes); + } + public static implicit operator BufferBinding(Buffer b) { return new BufferBinding(b, 0);