mono/MonoGame — pull request #2071 by nkast (2013-10-09 22:30:11+00:00)
Title: fix for VertexBuffer.GetData<>()

-Argument check now throws exceptions/Messages same as XNA.
-Copy now copies only as many as elementCount items.
-Use of offsetInBytes argument.

I accidentally deleted the branch. Original thread at
https://github.com/mono/MonoGame/pull/2033
--------------

Issue:
https://github.com/mono/MonoGame/issues/1076#issuecomment-24449713

I made some quick testing to see if this behaves as XNA.
https://github.com/mono/MonoGame/issues/2032

Here is the tests I used.
private VertexBuffer _vertexBuffer;
private DynamicVertexBuffer _dynamicVertexBuffer;
VertexPositionColor[] _vertices;

private void SetUpVertexBuffers()
{
//setup
_vertexBuffer = new VertexBuffer(this.Game.GraphicsDevice,
VertexPositionColor.VertexDeclaration, 2, BufferUsage.None);
_dynamicVertexBuffer = new DynamicVertexBuffer(this.Game.GraphicsDevice,
VertexPositionColor.VertexDeclaration, 2, BufferUsage.None);
_vertices = new VertexPositionColor[2];
_vertices[0].Position = new Vector3(11f, 12f, 13f);
_vertices[1].Position = new Vector3(21f, 22f, 23f);
_vertices[0].Color = Color.FromNonPremultiplied(127, 63, 31, 255);
_vertices[1].Color = Color.FromNonPremultiplied(15, 7, 3, 255);
}

private void TestVertexBuffer()
{
VertexPositionColor[] output;
Vector3[] outputvec3;
Vector2[] outputvec2;
float[] outputZ;
Color[] outputColor;
int vertexPCStride = VertexPositionColor.VertexDeclaration.VertexStride;

//copy all
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[2];
_vertexBuffer.GetData(output);
Debug.Assert(output[0].Position == _vertices[0].Position);
Debug.Assert(output[0].Color == _vertices[0].Color);
Debug.Assert(output[1].Position == _vertices[1].Position);
Debug.Assert(output[1].Color == _vertices[1].Color);

//copy only first vertex in first position
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[2];
_vertexBuffer.GetData(output, 0, 1);
Debug.Assert(output[0].Position == _vertices[0].Position);
Debug.Assert(output[0].Color == _vertices[0].Color);
Debug.Assert(output[1].Position == Vector3.Zero);
Debug.Assert(output[1].Color == Color.Transparent);

//copy first vertex in first position
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[1];
_vertexBuffer.GetData(output, 0, 1);
Debug.Assert(output[0].Position == _vertices[0].Position);
Debug.Assert(output[0].Color == _vertices[0].Color);

//copy first vertex in second position
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[2];
_vertexBuffer.GetData(output,1,1);
Debug.Assert(output[0].Position == Vector3.Zero);
Debug.Assert(output[0].Color == Color.Transparent);
Debug.Assert(output[1].Position == _vertices[0].Position);
Debug.Assert(output[1].Color == _vertices[0].Color);

//copy second vertex in first position
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[1];
_vertexBuffer.GetData(vertexPCStride, output, 0, 1, vertexPCStride);
Debug.Assert(output[0].Position == _vertices[1].Position);
Debug.Assert(output[0].Color == _vertices[1].Color);

//copy second vertex in second position
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
output = new VertexPositionColor[2];
_vertexBuffer.GetData(vertexPCStride, output, 1, 1, vertexPCStride);
Debug.Assert(output[0].Position == Vector3.Zero);
Debug.Assert(output[0].Color == Color.Transparent);
Debug.Assert(output[1].Position == _vertices[1].Position);
Debug.Assert(output[1].Color == _vertices[1].Color);

//read positions
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
outputvec3 = new Vector3[2];
_vertexBuffer.GetData(0, outputvec3, 0, 2, vertexPCStride);
Debug.Assert(outputvec3[0] == _vertices[0].Position);
Debug.Assert(outputvec3[1] == _vertices[1].Position);

//read positions 2D
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
outputvec2 = new Vector2[2];
_vertexBuffer.GetData(0, outputvec2, 0, 2, vertexPCStride);
Debug.Assert(outputvec2[0].X == _vertices[0].Position.X);
Debug.Assert(outputvec2[0].Y == _vertices[0].Position.Y);
Debug.Assert(outputvec2[1].X == _vertices[1].Position.X);
Debug.Assert(outputvec2[1].Y == _vertices[1].Position.Y);

//read depth
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
outputZ = new float[2];
_vertexBuffer.GetData(sizeof(float) * 2, outputZ, 0, 2, vertexPCStride);
Debug.Assert(outputZ[0] == _vertices[0].Position.Z);
Debug.Assert(outputZ[1] == _vertices[1].Position.Z);

//read colors
SetUpVertexBuffers();
_vertexBuffer.SetData(_vertices);
outputColor = new Color[2];
_vertexBuffer.GetData(sizeof(float)*3, outputColor, 0, 2,
vertexPCStride);
Debug.Assert(outputColor[0] == _vertices[0].Color);
Debug.Assert(outputColor[1] == _vertices[1].Color);

return;
}