From ed2b34b010d032e90ec2158600edb9df39bcd4a2 Mon Sep 17 00:00:00 2001 From: deccer Date: Fri, 8 Dec 2023 20:22:30 +0100 Subject: [PATCH] Add span overloads --- src/EngineKit/Graphics/Buffer.cs | 10 +++++ src/EngineKit/Graphics/IBuffer.cs | 11 +++-- src/EngineKit/Native/OpenGL/GL.cs | 71 ++++++++++++++++++------------- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/EngineKit/Graphics/Buffer.cs b/src/EngineKit/Graphics/Buffer.cs index 114ceed..548e469 100644 --- a/src/EngineKit/Graphics/Buffer.cs +++ b/src/EngineKit/Graphics/Buffer.cs @@ -82,6 +82,16 @@ public void Update(TElement[] data, int elementOffset = 0) } GL.NamedBufferSubData(Id, elementOffset * Stride, data); } + + public void Update(Span data, int elementOffset = 0) + where TElement : unmanaged + { + if ((elementOffset * Stride) + data.Length * Stride > SizeInBytes) + { + throw new ArgumentOutOfRangeException(nameof(elementOffset)); + } + GL.NamedBufferSubData(Id, elementOffset * Stride, data); + } public static implicit operator uint(Buffer buffer) { diff --git a/src/EngineKit/Graphics/IBuffer.cs b/src/EngineKit/Graphics/IBuffer.cs index b3f7316..3b3be3d 100644 --- a/src/EngineKit/Graphics/IBuffer.cs +++ b/src/EngineKit/Graphics/IBuffer.cs @@ -22,9 +22,12 @@ void AllocateStorage(TElement[] elements, StorageAllocationFlags stora void Update(nint dataPtr, int offsetInBytes, int sizeInBytes); - void Update(T item, int elementOffset = 0) - where T : unmanaged; + void Update(TElement item, int elementOffset = 0) + where TElement : unmanaged; - void Update(T[] data, int elementOffset = 0) - where T : unmanaged; + void Update(TElement[] data, int elementOffset = 0) + where TElement : unmanaged; + + void Update(Span data, int elementOffset = 0) + where TElement : unmanaged; } \ No newline at end of file diff --git a/src/EngineKit/Native/OpenGL/GL.cs b/src/EngineKit/Native/OpenGL/GL.cs index 78b9844..9e77b34 100644 --- a/src/EngineKit/Native/OpenGL/GL.cs +++ b/src/EngineKit/Native/OpenGL/GL.cs @@ -591,26 +591,26 @@ public static void NamedBufferStorage( NamedBufferStorage(buffer, size, dataPtr, bufferStorageFlags); } - public static void NamedBufferStorage( + public static void NamedBufferStorage( uint buffer, - in TData data, + in TElement data, uint bufferStorageFlags) - where TData : unmanaged + where TElement : unmanaged { - var size = (long)sizeof(TData); + var size = (long)sizeof(TElement); fixed (void* dataPtr = &data) { NamedBufferStorage(buffer, size, dataPtr, bufferStorageFlags); } } - public static void NamedBufferStorage( + public static void NamedBufferStorage( uint buffer, - TData[] data, + TElement[] data, uint bufferStorageFlags) - where TData : unmanaged + where TElement : unmanaged { - var size = (long)(data.Length * sizeof(TData)); + var size = (long)(data.Length * sizeof(TElement)); fixed (void* dataPtr = data) { NamedBufferStorage(buffer, size, dataPtr, bufferStorageFlags); @@ -626,26 +626,39 @@ private static void NamedBufferStorage( _glNamedBufferStorageDelegate(buffer, size, dataPtr, bufferStorageFlags); } - public static void NamedBufferSubData( + public static void NamedBufferSubData( uint buffer, nint offset, - TData[] data) - where TData : unmanaged + TElement[] data) + where TElement : unmanaged { - var size = (long)(data.Length * sizeof(TData)); + var size = (long)(data.Length * sizeof(TElement)); fixed (void* dataPtr = data) { NamedBufferSubData(buffer, offset, size, dataPtr); } } + + public static void NamedBufferSubData( + uint buffer, + nint offset, + Span data) + where TElement : unmanaged + { + var size = (long)(data.Length * sizeof(TElement)); + fixed (void* dataPtr = data) + { + NamedBufferSubData(buffer, offset, size, dataPtr); + } + } - public static void NamedBufferSubData( + public static void NamedBufferSubData( uint buffer, nint offset, - in TData data) - where TData : unmanaged + in TElement data) + where TElement : unmanaged { - var size = (long)sizeof(TData); + var size = (long)sizeof(TElement); fixed (void* dataPtr = &data) { NamedBufferSubData(buffer, offset, size, dataPtr); @@ -658,7 +671,7 @@ public static void NamedBufferSubData( long size, void* data) { - _glNamedBufferSubDataDelegate(buffer, offset, size, (void*)data); + _glNamedBufferSubDataDelegate(buffer, offset, size, data); } public static void NamedBufferData( @@ -671,41 +684,41 @@ public static void NamedBufferData( NamedBufferDataInternal(buffer, size, dataPtr, usage); } - public static void NamedBufferData( + public static void NamedBufferData( uint buffer, - Span data, + Span data, BufferUsage usage) - where TData : unmanaged + where TElement : unmanaged { - var size = (nint)(data.Length * sizeof(TData)); + var size = (nint)(data.Length * sizeof(TElement)); fixed (void* dataPtr = data) { NamedBufferDataInternal(buffer, size, dataPtr, usage); } } - public static void NamedBufferData( + public static void NamedBufferData( uint buffer, - TData[] data, + TElement[] data, BufferUsage usage) - where TData : unmanaged + where TElement : unmanaged { - var size = (nint)(data.Length * sizeof(TData)); + var size = (nint)(data.Length * sizeof(TElement)); fixed (void* dataPtr = data) { NamedBufferDataInternal(buffer, size, dataPtr, usage); } } - public static void NamedBufferData( + public static void NamedBufferData( uint buffer, - in TData data, + in TElement data, BufferUsage usage) - where TData : unmanaged + where TElement : unmanaged { fixed (void* dataPtr = &data) { - NamedBufferDataInternal(buffer, sizeof(TData), dataPtr, usage); + NamedBufferDataInternal(buffer, sizeof(TElement), dataPtr, usage); } }