mellinoe / veldrid-samples

Sample projects for Veldrid
https://mellinoe.github.io/veldrid-docs/
121 stars 49 forks source link

Consider adding Span<T> to UpdateBuffer #15

Open xposure opened 5 years ago

xposure commented 5 years ago

I have a lot of low level code that creates memory outside of the heap. My entity system could be thought of like the new unity ECS using unmanaged structs in blocks.

Since i'm not working with standard c# arrays, it would be useful if I could pass a span to UpdateBuffer in place of an array.

mellinoe commented 5 years ago

I'm considering adding some Span overloads where it makes sense in the next version. In the meantime, you can use Spans with some of the existing overloads:

void UpdateBuffer<T>(DeviceBuffer buffer, uint bufferOffsetInBytes, ref T source, uint sizeInBytes);

// For example:
Span<uint> data = ...;
graphicsDevice.UpdateBuffer(buffer, offset, ref data[0], (uint)data.Length * sizeof(uint));

// You can also use the IntPtr overload if you pin your data:
void UpdateBuffer(DeviceBuffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes);
xposure commented 5 years ago

@mellinoe Yea, I was going to note that I'm using the last option, since the memory is off the heap there is no need to pin it and I'm just passing the raw pointer wrapped in a IntPtr.

More of a quality of life suggestion than anything else.