stride3d/stride · error · ArgumentException

destination length does not match the amount of vertices…

Error message

destination length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})

What it means

The typed vertex reader requires the destination Span<T> length to exactly equal the number of vertices being read (elementCount). Since it reads elementCount vertices sequentially into the span, a size mismatch would either overflow or leave stale data, so it throws ArgumentException. There is no partial-read mode; caller and buffer must agree on the vertex count.

Solutions

  1. Make destination.Length exactly elementCount before calling Read (resize or slice the span)
  2. Derive both elementCount and the span length from the same source (Binding.Count)
  3. If reading a subset, slice the span: destination[..n] and pass n as elementCount
  4. Verify stride/byte-length math used to compute elementCount matches the real buffer size

Example fix

// before
var verts = new PositionNormal[100];
reader.Read<PositionConverter, PositionNormal>(ptr, 150, stride, verts); // throws
// after
var verts = new PositionNormal[150];
reader.Read<PositionConverter, PositionNormal>(ptr, 150, stride, verts);
Defensive patterns

Strategy: validation

Validate before calling

if (destination.Length != elementCount)
    destination = destination[..elementCount]; // or resize

Type guard

static bool SpanMatches<T>(Span<T> dest, int elementCount) => dest.Length == elementCount;

Try / catch

try { reader.Read<C, T>(ptr, elementCount, stride, destination); }
catch (ArgumentException ex) { logger.Error(ex, "Vertex read destination size mismatch"); throw; }

Prevention

When it happens

Trigger: Calling Read<TConverter,TSource> with a destination span shorter or longer than elementCount, e.g. allocating the span from Binding.Count while passing a different elementCount parameter, or from a vertex struct array whose length was computed with a different count.

Common situations: Reading a subset of vertices without slicing the span; reusing destination arrays across meshes of different sizes; elementCount derived from stride-divided byte length that disagrees with the span's allocation.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/ca46ea26afa02b4e. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Graphics/VertexBufferHelper.cs:416

                {
                    foreach (var index in indices16)
                    {
                        TConverter.Convert(*(TSource*)(sourcePointer + index * stride), out *dest);
                        dest++;
                    }
                }
            }
        }
    }

    private struct CopyToDest<T> : IReader<T> where T : unmanaged
    {
        public unsafe void Read<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride, Span<T> destination) 
            where TConverter : IConverter<TSource, T> 
            where TSource : unmanaged
        {
            if (destination.Length != elementCount)
                throw new ArgumentException($"{nameof(destination)} length does not match the amount of vertices contained within this vertex buffer ({destination.Length} / {elementCount})");
            
            fixed (T* ptrDest = destination)
            {
                byte* end = sourcePointer + elementCount * stride;
                T* dest = ptrDest;
                for (; sourcePointer < end; sourcePointer += stride, dest++)
                    TConverter.Convert(*(TSource*)sourcePointer, out *dest);
            }
        }
    }

    private readonly ref struct InterleavedParameters
    {
        public readonly Span<byte> Source, Destination;
        public readonly int SourceStride, DestStride;
        public readonly int VertexCount;

        public InterleavedParameters(Span<byte> source, Span<byte> destination, int sourceStride, int destStride, int vertexCount)

View on GitHub (pinned to 96fad776d2)