{"record":{"id":"390963d5be6e35b5","repo":"stride3d/stride","slug":"the-length-of-vertex-buffer-0-doesn-t-match-the-expected","errorCode":null,"errorMessage":"The length of vertex buffer [{0}] doesn't match the expected length with the vertex stride [{1}]","messagePattern":"The length of vertex buffer \\[(.+?)\\] doesn't match the expected length with the vertex stride \\[(.+?)\\]","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexHelper.cs","lineNumber":70,"sourceCode":"        /// <exception cref=\"System.ArgumentNullException\">\n        /// vertexDeclaration\n        /// or\n        /// vertexBufferData\n        /// </exception>\n        /// <remarks>The original vertex buffer must contain at least a TEXCOORD[0-9] attribute in order for this method to work.\n        /// This method will copy the value of the first existing TEXCOORD found in the vertex buffer to the newly created TEXCOORDS.</remarks>\n        public static unsafe VertexTransformResult GenerateMultiTextureCoordinates(VertexDeclaration vertexDeclaration, byte[] vertexBufferData, int vertexStride = 0, int maxTexcoord = 9)\n        {\n            if (vertexDeclaration == null) throw new ArgumentNullException(\"vertexDeclaration\");\n            if (vertexBufferData == null) throw new ArgumentNullException(\"vertexBufferData\");\n            if (vertexStride == 0)\n            {\n                vertexStride = vertexDeclaration.VertexStride;\n            }\n            var vertexCount = ((int)(vertexBufferData.Length / vertexStride));\n            if (vertexBufferData.Length != (vertexCount * vertexStride))\n            {\n                throw new ArgumentOutOfRangeException(\"vertexBufferData\", \"The length of vertex buffer [{0}] doesn't match the expected length with the vertex stride [{1}]\".ToFormat(vertexBufferData.Length, vertexCount * vertexStride));\n            }\n\n            fixed (void* vertexBufferPtr = vertexBufferData)\n                return GenerateMultiTextureCoordinates(vertexDeclaration, (nint)vertexBufferPtr, vertexCount, 0, vertexStride, maxTexcoord);\n        }\n\n        /// <summary>\n        /// Generates multi texture coordinates for an existing vertex buffer. See remarks.\n        /// </summary>\n        /// <param name=\"transform\">The result of a previous transform.</param>\n        /// <param name=\"vertexStride\">The vertex stride.</param>\n        /// <param name=\"maxTexcoord\">The maximum texcoord.</param>\n        /// <returns>A new vertex buffer with additional texture coordinates.</returns>\n        /// <remarks>The original vertex buffer must contain at least a TEXCOORD[0-9] attribute in order for this method to work.\n        /// This method will copy the value of the first existing TEXCOORD found in the vertex buffer to the newly created TEXCOORDS.</remarks>\n        public static VertexTransformResult GenerateMultiTextureCoordinates(VertexTransformResult transform, int vertexStride = 0, int maxTexcoord = 9)\n        {\n            return GenerateMultiTextureCoordinates(transform.Layout, transform.VertexBuffer, vertexStride, maxTexcoord);","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexHelper.cs#L52-L88","documentation":"The byte[] overload validates that buffer length is an exact multiple of the vertex stride; if not it throws ArgumentOutOfRangeException('vertexBufferData') with a message showing the buffer length and expected length. This guards against reading garbage when the raw buffer is truncated, padded, or described by the wrong stride.","triggerScenarios":"vertexBufferData.Length is not divisible by vertexStride — e.g. byteCount = 100 with stride 32 (indices, extra trailing bytes, or interleaved data not accounted for).","commonSituations":"Manually packed vertex data with extra padding; passing index data instead of vertex data; stride mismatch after changing the vertex layout without regenerating the buffer; off-by-one when exporting from a tool.","solutions":["Verify buffer length == vertexCount * vertexStride and trim padding/extra bytes.","Ensure the VertexDeclaration matches the actual packed layout (correct VertexStride).","Confirm you are passing the vertex buffer, not the index buffer, and that the source exporter packs vertices contiguously."],"exampleFix":"// before\nvar result = VertexHelper.GenerateMultiTextureCoordinates(declaration, packedBytes); // packedBytes.Length = 100, stride = 32\n// after\nvar usableLength = packedBytes.Length - (packedBytes.Length % declaration.VertexStride);\nArray.Resize(ref packedBytes, usableLength);\nvar result = VertexHelper.GenerateMultiTextureCoordinates(declaration, packedBytes);","handlingStrategy":"validation","validationCode":"var vertexCount = vertexBufferData.Length / vertexStride;\nif (vertexCount == 0 || vertexBufferData.Length % vertexStride != 0)\n    throw new InvalidOperationException($\"Buffer length {vertexBufferData.Length} is not a multiple of stride {vertexStride}\");","typeGuard":null,"tryCatchPattern":"try { var r = VertexHelper.GenerateMultiTextureCoordinates(decl, bytes); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"vertexBufferData\") { /* fix buffer packing or stride */ }","preventionTips":["Always verify length % stride == 0 when importing raw vertex data","Match the declaration's VertexStride to the exporter's actual packing","Never pad or interleave extra bytes into vertex buffers without updating the declaration"],"tags":["argument-out-of-range","graphics","buffer-size"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}