Issue with VertexBuffer.SetData

Hello all. I have been converting one of the XNA samples to MonoGame. After modifying many of the fx files, I have finally got it to build. However, when I run it, it gets to a spot in the code where I get a System.InvalidOperationException. The message says “The vertex stride is larger than the vertex buffer.”

The two lines of code that do this work are:

vertexBuffer = new VertexBuffer(
                BaseGame.Device,
                typeof(TangentVertex),
                vertices.Length,
                BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);

The exception occurs at the vertexBuffer.SetData() call. As you can see, however, there should be no issue as the VertexBuffer is created with the number of vertices required (66049 to be precise).

Vertices is declared as

TangentVertex[] vertices = new TangentVertex[GridWidth * GridHeight];

GridWidth = GridHeight = 257, so GridWidth*GridHeight = 66049 as it is supposed to.

I am using the HiDef profile in a Windows DirectX project. Any ideas?

As an addition, if I comment out the SetData, it also happens at the next SetData call that is only setting 6660 vertices.

Huh, that is very interesting. After posting, I tracked down the VertexDeclaration for the vertices, and it was

private static VertexElement[] GenerateVertexElements()
        {
            VertexElement[] decl = new VertexElement[]
                {
                    // Construct new vertex declaration with tangent info
                    // First the normal stuff (we should already have that)
                    new VertexElement(0, VertexElementFormat.Vector3,
                        VertexElementUsage.Position, 0),
                    new VertexElement(0, VertexElementFormat.Vector2,
                        VertexElementUsage.TextureCoordinate, 0),
                    new VertexElement(0, VertexElementFormat.Vector3,
                        VertexElementUsage.Normal, 0),
                    // And now the tangent
                    new VertexElement(0, VertexElementFormat.Vector3,
                        VertexElementUsage.Tangent, 0),
                };
            return decl;
        }

Notice that all of the elements have an offset of 0?

Changing this to:

private static VertexElement[] GenerateVertexElements()
        {
            VertexElement[] decl = new VertexElement[]
                {
                    // Construct new vertex declaration with tangent info
                    // First the normal stuff (we should already have that)
                    new VertexElement(0, VertexElementFormat.Vector3,
                        VertexElementUsage.Position, 0),
                    new VertexElement(sizeof(float)*3, VertexElementFormat.Vector2,
                        VertexElementUsage.TextureCoordinate, 0),
                    new VertexElement(sizeof(float)*5, VertexElementFormat.Vector3,
                        VertexElementUsage.Normal, 0),
                    // And now the tangent
                    new VertexElement(sizeof(float)*8, VertexElementFormat.Vector3,
                        VertexElementUsage.Tangent, 0),
                };
            return decl;
        }

allowed it to work. Now, I have a whole other issue, but I will hopefully track that down soon.

Why would this have worked on XNA but not MonoGame? It seems that MonoGame likely has the correct behavior, but I know that this game did indeed work on XNA.

1 Like