Questions about offsets in GetData methods of buffers

Hi, I would like to ask a few questions about the GetData properties of the VertexBuffer and IndexBuffer to make sure I understand correctly. At first i was confused about the difference between OffsetInBytes and StartIndex. For the VertexBuffer I am thinking that the OffsetInBytes is the number of bytes into EACH vertex to find a specific element. This is because I have seen the Element.Offset used to return just the position part of the vertex data. And the StartIndex the first vertex to start reading data from. Is this correct?

VertexBuffer.GetData(OffsetInBytes, Data, StartIndex, ElementCount, VertexStride);

For the index buffer I am still confused as this has no elements, its just a list of integers. What is the difference here between OffsetInBytes and StartIndex? Is the offset the position in the return array to start writing indices to?

IndexBuffer.GetData(OffsetInBytes, Data, StartIndex, ElementCount);

I had thought that each ModelMeshPart has its own unique buffers, but then why does each part have a ‘VertexOffset’ and ‘StartIndex’? Do ModelMeshParts share buffers?

Thanks

StartIndex is the index into the array you pass where the first data should be written. OffsetInBytes is the offset in bytes into the buffer to read.
Check out the docs in the source file: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs#L56
For some reason the docs here on the website did not get updated :confused:

Thanks Jjagg, I had looked at the source but didn’t notice VertexBuffer was a partial class and i was looking at the part with no comments. oops

The IndexBuffer class also has no comments, as there is only one element, the indices, the offset here is a offset into the whole buffer?

The thing I am wanting to know mostly is what is ModelMeshPart VertexOffset and StartIndex, I know the code documents say they are offsets in the Buffers to start reading, but why? This would indicate that that the buffers are shared between multiple parts else the data before these offsets would be junk.

You might have a data block that contains several different items, such as a vertex buffer followed by an index buffer. To avoid having to copy the index buffer part out into a separate byte array, you can pass the same data block to both VertexBuffer.SetData and IndexBuffer.SetData and provide the byte offset to the beginning of the index buffer portion of the data.

Thanks KonajuGames, but right now its GetData I am wondering about.

Basically for IndexBuffer.GetData, is the ByteOffset for the source array being read OR the destination array being written to?

And each ModelMeshPart having StartIndex and VertexOffset does this mean that Buffers can be shared between multiple ModelMeshParts? I assume it must do but i want to be 100% sure

Edit: Also i notice that the following two calls return different numbers?

ModelMeshPart.VertexBuffer.VertexCount;
ModelMeshPart.NumVertices;

I was having trouble working these out because my code was not working as expected, but this now appears to be a bug in monogame. The problem is when returning vertex data to an array of vertex classes.

MyVertex[] MyArray = new MyVertex[VertexBuffer.TotalVerts];
VertexBuffer.GetData(MyArray);

The above code works fine for most of my simple models although I have one model i like to test with because its quite complicated. This was the model that was not working for me. Its also the only test model i have that has parts with a Vertex Offset greater than Zero. I had thought my code was at fault because I was not handling this offset correctly. I have now found that some of the vectors returned for this model had NaN values in them. When i call for the vertices and explicitly pass the full set of inputs, the NaNs are gone and the vertices are correct.

MyVertex[] MyArray = new MyVertex[VertexBuffer.TotalVerts];
VertexBuffer.GetData(0, MyArray, 0, TotalVerts, Stride);