MonoGame/MonoGame · error · InvalidOperationException

The vertex stride is larger than the vertex buffer.

Error message

The vertex stride is larger than the vertex buffer.

What it means

Thrown by SetDataInternal when elementCount > 1 and elementCount * vertexStride exceeds bufferSize (the total buffer byte capacity). The upload would overflow the GPU buffer. This is an InvalidOperationException because individual params are valid but their product exceeds capacity.

Source

Thrown at MonoGame.Framework/Graphics/Vertices/VertexBuffer.cs:262

        protected void SetDataInternal<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride, SetDataOptions options) where T : struct
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var elementSizeInBytes = ReflectionHelpers.FastSizeOf<T>();
            var bufferSize = VertexCount * VertexDeclaration.VertexStride;

            if (vertexStride == 0)
                vertexStride = elementSizeInBytes;

            var vertexByteSize = VertexCount * VertexDeclaration.VertexStride;
            if (vertexStride > vertexByteSize)
                throw new ArgumentOutOfRangeException("vertexStride", "Vertex stride can not be larger than the vertex buffer size.");

            if (startIndex + elementCount > data.Length || elementCount <= 0)
                throw new ArgumentOutOfRangeException("data","The array specified in the data parameter is not the correct size for the amount of data requested.");
            if (elementCount > 1 && (elementCount * vertexStride > bufferSize))
                throw new InvalidOperationException("The vertex stride is larger than the vertex buffer.");
            if (vertexStride < elementSizeInBytes)
                throw new ArgumentOutOfRangeException("The vertex stride must be greater than or equal to the size of the specified data (" + elementSizeInBytes + ").");

            PlatformSetData<T>(offsetInBytes, data, startIndex, elementCount, vertexStride, options, bufferSize, elementSizeInBytes);
        }

#if NATIVE
        /// <summary>
        /// Sets the vertex buffer data, uses a Span including only relevant data to be copied rather than the full source array,
        /// and the first index in the buffer to start copying to. Assumes the full Span will be copied with no padding between elements.
        /// </summary>
        /// <typeparam name="T">Type of elements in the data Span.</typeparam>
        /// <param name="destinationStartIndex">The first index in the destination buffer you want to copy data to</param>
        /// <param name="data">Data array to be passed to the shader as a Span.</param>
        /// elementCount will be inferred to be the number of elements in <paramref name="data"/>
        /// since the Span should only contain the relevant data to be copied.
        /// <remarks>
        /// If <c>T</c> is <see cref="VertexPositionTexture"/>, and you want to only update the first 10 elements of your array of

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Ensure elementCount <= VertexCount (when stride equals the vertex size).
  2. If using a custom stride, ensure elementCount * vertexStride <= VertexCount * VertexDeclaration.VertexStride.
  3. Recreate the buffer with a larger VertexCount if you need to store more vertices.

Example fix

// before
var vb = new VertexBuffer(gd, decl, 256, usage);
vb.SetData(verts1000); // 1000 verts into 256-capacity buffer
// after
var vb = new VertexBuffer(gd, decl, verts1000.Length, usage);
vb.SetData(verts1000);
Defensive patterns

Strategy: validation

Validate before calling

if (elementCount > 1 && (long)elementCount * vertexStride > vertexBuffer.VertexCount * vertexBuffer.VertexDeclaration.VertexStride)
    throw new InvalidOperationException("SetData would overflow the vertex buffer.");

Type guard

static bool UploadFits(VertexBuffer vb, int elementCount, int vertexStride)
    => elementCount <= 1 || (long)elementCount * vertexStride <= (long)vb.VertexCount * vb.VertexDeclaration.VertexStride;

Try / catch

try { vertexBuffer.SetData(0, data, 0, elementCount, vertexStride); }
catch (InvalidOperationException ex) when (ex.Message.Contains("vertex buffer"))
{ /* recreate buffer larger or reduce count */ }

Prevention

When it happens

Trigger: Uploading more elements than the buffer was allocated for (VertexCount), or using a stride that, multiplied by the count, exceeds VertexCount * VertexDeclaration.VertexStride.

Common situations: Buffer allocated for N vertices but attempting to upload M>N, or a stale VertexCount after the mesh changed. Also when vertexStride defaults to element size but elementCount is too large.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/352159c7df16520d. Report an issue: GitHub.