{"record":{"id":"5f5ddfd7bc4ea17d","repo":"stride3d/stride","slug":"the-length-and-stride-of-source-does-not-match-the-vertices","errorCode":null,"errorMessage":"The length and stride of source does not match the vertices required ({source.Length / SourceStride} / {vertexCount})","messagePattern":"The length and stride of source does not match the vertices required \\((.+?) / (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexBufferHelper.cs","lineNumber":439,"sourceCode":"                T* dest = ptrDest;\n                for (; sourcePointer < end; sourcePointer += stride, dest++)\n                    TConverter.Convert(*(TSource*)sourcePointer, out *dest);\n            }\n        }\n    }\n\n    private readonly ref struct InterleavedParameters\n    {\n        public readonly Span<byte> Source, Destination;\n        public readonly int SourceStride, DestStride;\n        public readonly int VertexCount;\n\n        public InterleavedParameters(Span<byte> source, Span<byte> destination, int sourceStride, int destStride, int vertexCount)\n        {\n            if (destination.Length / destStride != vertexCount)\n                throw new ArgumentException($\"The length and stride of {nameof(destination)} does not match the vertices required ({destination.Length / DestStride} / {vertexCount})\");\n            if (source.Length / sourceStride != vertexCount)\n                throw new ArgumentException($\"The length and stride of {nameof(source)} does not match the vertices required ({source.Length / SourceStride} / {vertexCount})\");\n            \n            Source = source;\n            Destination = destination;\n            SourceStride = sourceStride;\n            DestStride = destStride;\n            VertexCount = vertexCount;\n        }\n    }\n\n    /// <example>\n    /// Implementing <see cref=\"Copy{TSemantic,TValue}\"/> manually:\n    /// <code>\n    /// <![CDATA[\n    /// Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);\n    /// var vertexPositions = new Vector3[count];\n    /// var myReader = new CopyTo();\n    /// helper.Read<PositionSemantic, Vector3, CopyTo>(vertexPositions, myReader);\n    /// ","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexBufferHelper.cs#L421-L457","documentation":"Thrown by InterleavedParameters when the source buffer length divided by the source stride does not equal the vertex count. The source span must contain exactly vertexCount vertices of sourceStride bytes each.","triggerScenarios":"Calling the interleave API with a source Span<byte> whose length is not exactly vertexCount * sourceStride — e.g. reading fewer bytes than the full vertex stream from a file, or passing a stale vertexCount after the mesh changed.","commonSituations":"Loading a subset of vertices but passing the full vertexCount; packing raw vertex data whose stride was computed from an older vertex declaration; slicing a larger buffer with an incorrect length.","solutions":["Resize the source span to exactly vertexCount * sourceStride bytes","Recompute sourceStride from the actual source vertex declaration","Pass the correct vertexCount matching the source data actually available","Slice the buffer to the exact vertex range being interleaved"],"exampleFix":"// before\nvar src = vertexData.AsSpan(0, (int)(vertexData.Length * 0.5f));\nhelper.Interleave(src, srcStride, dest, destStride, totalVertexCount);\n// after\nvar src = vertexData.AsSpan(0, totalVertexCount * srcStride);\nhelper.Interleave(src, srcStride, dest, destStride, totalVertexCount);","handlingStrategy":"validation","validationCode":"if (source.Length != vertexCount * sourceStride)\n    throw new ArgumentException(\"source must be exactly vertexCount * sourceStride bytes\");","typeGuard":null,"tryCatchPattern":"try { Interleave(...); } catch (ArgumentException ex) { log(ex.Message); reloadVertexData(); }","preventionTips":["Slice source spans with vertexCount * sourceStride as the exact length","Recompute sourceStride whenever the source vertex declaration changes","Validate mesh vertex counts after loading"],"tags":["graphics","vertex-buffer","argument-validation","stride"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}