add array overloads to avoid explicit generic parameter

pull/48/head
cosmonaut 2023-04-05 12:40:34 -07:00
parent 5a2f7eadb8
commit 3584e670ee
2 changed files with 111 additions and 1 deletions

View File

@ -1731,6 +1731,28 @@ namespace MoonWorks.Graphics
);
}
/// <summary>
/// Copies array data into a buffer.
/// </summary>
/// <param name="buffer">The buffer to copy to.</param>
/// <param name="data">The array to copy from.</param>
/// <param name="bufferOffsetInBytes">Specifies where in the buffer to start copying.</param>
/// <param name="setDataOption">Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred.</param>
public unsafe void SetBufferData<T>(
Buffer buffer,
T[] data,
uint bufferOffsetInBytes = 0
) where T : unmanaged
{
SetBufferData(
buffer,
new Span<T>(data),
bufferOffsetInBytes,
0,
(uint) data.Length
);
}
/// <summary>
/// Copies arbitrary data into a buffer.
/// </summary>
@ -1797,6 +1819,26 @@ namespace MoonWorks.Graphics
}
}
/// <summary>
/// Copies array data into a buffer.
/// </summary>
/// <param name="buffer">The buffer to copy to.</param>
/// <param name="data">The span to copy from.</param>
/// <param name="bufferOffsetInBytes">Specifies where in the buffer to start copying.</param>
/// <param name="startElement">The index of the first element to copy from the array.</param>
/// <param name="numElements">How many elements to copy.</param>
/// <param name="setDataOption">Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred.</param>
public unsafe void SetBufferData<T>(
Buffer buffer,
T[] data,
uint bufferOffsetInBytes,
uint startElement,
uint numElements
) where T : unmanaged
{
SetBufferData<T>(buffer, new Span<T>(data), bufferOffsetInBytes, startElement, numElements);
}
public unsafe void SetBufferData<T>(
Buffer buffer,
IntPtr dataPtr,
@ -1826,6 +1868,15 @@ namespace MoonWorks.Graphics
SetTextureData(new TextureSlice(texture), data);
}
/// <summary>
/// Asynchronously copies data into a texture.
/// </summary>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(Texture texture, T[] data) where T : unmanaged
{
SetTextureData(new TextureSlice(texture), new Span<T>(data));
}
/// <summary>
/// Asynchronously copies data into a texture slice.
/// </summary>
@ -1851,6 +1902,16 @@ namespace MoonWorks.Graphics
}
}
/// <summary>
/// Asynchronously copies data into a texture slice.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(in TextureSlice textureSlice, T[] data) where T : unmanaged
{
SetTextureData(textureSlice, new Span<T>(data));
}
/// <summary>
/// Asynchronously copies data into a texture slice.
/// </summary>

View File

@ -58,7 +58,7 @@ namespace MoonWorks.Graphics
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The span that data will be copied to.</param>
@ -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
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The span that data will be copied to.</param>
/// <param name="dataLengthInBytes">The length of the data to read.</param>
public unsafe void GetData<T>(
T[] data,
uint dataLengthInBytes
) where T : unmanaged
{
GetData(new Span<T>(data), dataLengthInBytes);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The span that data will be copied to.</param>
public unsafe void GetData<T>(
Span<T> data
) where T : unmanaged
{
var lengthInBytes = System.Math.Min(data.Length * Marshal.SizeOf<T>(), Size);
GetData(data, (uint) lengthInBytes);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data">The span that data will be copied to.</param>
public unsafe void GetData<T>(
T[] data
) where T : unmanaged
{
var lengthInBytes = System.Math.Min(data.Length * Marshal.SizeOf<T>(), Size);
GetData(new Span<T>(data), (uint) lengthInBytes);
}
public static implicit operator BufferBinding(Buffer b)
{
return new BufferBinding(b, 0);