{"record":{"id":"43366ae40f13bf85","repo":"stride3d/stride","slug":"the-vertex-stride-is-not-a-multiple-of-4-bytes","errorCode":null,"errorMessage":"The vertex stride is not a multiple of 4 bytes","messagePattern":"The vertex stride is not a multiple of 4 bytes","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexElementValidator.cs","lineNumber":65,"sourceCode":"    /// <exception cref=\"ArgumentOutOfRangeException\">\n    ///   <paramref name=\"vertexStride\"/> is not a positive number greater than zero.\n    /// </exception>\n    /// <exception cref=\"ArgumentException\">\n    ///   Thrown if:\n    ///   <list type=\"bullet\">\n    ///     <item><paramref name=\"vertexStride\"/> is not a multiple of 4 bytes.</item>\n    ///     <item>Any Vertex Element extends beyond the bounds of the vertex stride.</item>\n    ///     <item>Any Vertex Element has an offset that is not a multiple of 4 bytes.</item>\n    ///     <item>Duplicate Vertex Elements are found with the same semantic name and index.</item>\n    ///     <item>Any Vertex Element overlaps with another element.</item>\n    ///   </list>\n    /// </exception>\n    internal static void Validate(int vertexStride, VertexElement[] elements)\n    {\n        ArgumentOutOfRangeException.ThrowIfNegativeOrZero(vertexStride);\n\n        if ((vertexStride & 3) != 0)\n            throw new ArgumentException($\"The vertex stride is not a multiple of 4 bytes\", nameof(vertexStride));\n\n        // For checking overlaps, we use an array to track occupied bytes\n        var overlapCheckArray = new int[vertexStride];\n        for (int i = 0; i < overlapCheckArray.Length; i++)\n            overlapCheckArray[i] = -1;\n\n        int totalOffset = 0;\n        for (int elementIndex = 0; elementIndex < elements.Length; elementIndex++)\n        {\n            // Compute the offset for this element\n            int elementOffset = elements[elementIndex].AlignedByteOffset;\n            if (elementOffset == VertexElement.AppendAligned)\n            {\n                elementOffset = elementIndex == 0 ? 0 : totalOffset + elements[elementIndex - 1].Format.SizeInBytes;\n            }\n            totalOffset = elementOffset;\n\n            // Validate the element offset","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexElementValidator.cs#L47-L83","documentation":"VertexElementValidator.Validate requires the vertex stride to be a multiple of 4 bytes, matching GPU vertex-buffer alignment requirements. A stride of e.g. 6 or 10 bytes is rejected with ArgumentException before any buffer is created.","triggerScenarios":"Building a VertexBufferBinding/vertex declaration whose summed element sizes total a non-multiple of 4, e.g. three R16G16 elements (6 bytes) or adding a single R8 element to a 9-byte stride.","commonSituations":"Compact packed vertex layouts (byte-based colors, 16-bit normals) that don't pad to 4 bytes; hand-computed strides missing padding.","solutions":["Add a padding element (e.g. an R8G8B8A8 or an unused component) so the stride is a multiple of 4","Widen an undersized element format (e.g. R8_UInt to R32_UInt)","Recompute the stride as the sum of element Format.SizeInBytes and pad to the next multiple of 4"],"exampleFix":"// before\n// elements: R16G16 (4) + R16 (2) => stride 6\n// after\nelements: R16G16 (4) + R32 (4) => stride 8","handlingStrategy":"validation","validationCode":"int stride = elements.Sum(e => e.Format.SizeInBytes);\nif (stride <= 0 || (stride & 3) != 0)\n    throw new ArgumentException(\"Vertex stride must be a positive multiple of 4 bytes\");","typeGuard":null,"tryCatchPattern":"try { VertexElementValidator.Validate(stride, elements); } catch (ArgumentException ex) { padLayoutTo4Bytes(); }","preventionTips":["Pad vertex layouts to a 4-byte-aligned stride","Avoid odd-sized trailing elements like a lone R8","Compute stride from Format.SizeInBytes sums, not from C# struct size"],"tags":["graphics","vertex-declaration","alignment","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-23T08:17:48.524Z"}