ImGui.NET icon indicating copy to clipboard operation
ImGui.NET copied to clipboard

Expose Spans for vertex and index ranges

Open RoyAwesome opened this issue 2 years ago • 1 comments

I was implementing imgui into Monogame with an eye on reducing the amount of unsafe code that the library needs, and I was able to remove just about every instance of unsafe except for copying vertices out of unmanaged memory and into managed memory.

The only place you must use unsafe is constructing a Span from a cmdlist vertex array pointer, like so:

unsafe
{
  Span<byte> cmdListVtx = new Span<byte>(cmdList.VtxBuffer.Data.ToPointer(), cmdList.VtxBuffer.Size * ImVertexSize);
}

If the VtxBuffer.Data object were able to return a Span itself, then you could do something like:

Span<byte> cmdListVtx = cmdList.VtxBuffer.Data.ToSpan();
Span<byte> vtxDst = new Span<byte>(_VertexData, vtxOffset * ImVertexSize, cmdList.VtxBuffer.Size * ImVertexSize);

cmdListVtx.CopyTo(vtxDst);

And not need to use unsafe when implementing an imgui renderer.

Furthermore, being able to acquire a Span from these buffer pointers would allow users to just push those spans around, potentially removing the need to copy data out of unmanaged memory entirely when submitting these to vertex buffers (although, monogame doesn't support this, so the results wouldn't be immediate).

This would help quite a bit with the ergonomics of the API and library.

Thanks!

RoyAwesome avatar Nov 01 '22 17:11 RoyAwesome

I dont think the owner of this library really wanna do these changes himself anymore I feel, we have to kinda get used to manipulate the code ourself..

CodingMadness avatar May 04 '23 13:05 CodingMadness