Skip to content

Commit

Permalink
Add span overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
deccer committed Dec 8, 2023
1 parent 869663d commit ed2b34b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
10 changes: 10 additions & 0 deletions src/EngineKit/Graphics/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public void Update<TElement>(TElement[] data, int elementOffset = 0)
}
GL.NamedBufferSubData(Id, elementOffset * Stride, data);
}

public void Update<TElement>(Span<TElement> 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)
{
Expand Down
11 changes: 7 additions & 4 deletions src/EngineKit/Graphics/IBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ void AllocateStorage<TElement>(TElement[] elements, StorageAllocationFlags stora

void Update(nint dataPtr, int offsetInBytes, int sizeInBytes);

void Update<T>(T item, int elementOffset = 0)
where T : unmanaged;
void Update<TElement>(TElement item, int elementOffset = 0)
where TElement : unmanaged;

void Update<T>(T[] data, int elementOffset = 0)
where T : unmanaged;
void Update<TElement>(TElement[] data, int elementOffset = 0)
where TElement : unmanaged;

void Update<TElement>(Span<TElement> data, int elementOffset = 0)
where TElement : unmanaged;
}
71 changes: 42 additions & 29 deletions src/EngineKit/Native/OpenGL/GL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,26 +591,26 @@ public static void NamedBufferStorage(
NamedBufferStorage(buffer, size, dataPtr, bufferStorageFlags);
}

public static void NamedBufferStorage<TData>(
public static void NamedBufferStorage<TElement>(
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<TData>(
public static void NamedBufferStorage<TElement>(
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);
Expand All @@ -626,26 +626,39 @@ private static void NamedBufferStorage(
_glNamedBufferStorageDelegate(buffer, size, dataPtr, bufferStorageFlags);
}

public static void NamedBufferSubData<TData>(
public static void NamedBufferSubData<TElement>(
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<TElement>(
uint buffer,
nint offset,
Span<TElement> data)
where TElement : unmanaged
{
var size = (long)(data.Length * sizeof(TElement));
fixed (void* dataPtr = data)
{
NamedBufferSubData(buffer, offset, size, dataPtr);
}
}

public static void NamedBufferSubData<TData>(
public static void NamedBufferSubData<TElement>(
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);
Expand All @@ -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(
Expand All @@ -671,41 +684,41 @@ public static void NamedBufferData(
NamedBufferDataInternal(buffer, size, dataPtr, usage);
}

public static void NamedBufferData<TData>(
public static void NamedBufferData<TElement>(
uint buffer,
Span<TData> data,
Span<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<TData>(
public static void NamedBufferData<TElement>(
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<TData>(
public static void NamedBufferData<TElement>(
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);
}
}

Expand Down

0 comments on commit ed2b34b

Please sign in to comment.