{"record":{"id":"eaeccb9fc973e318","repo":"stride3d/stride","slug":"the-offset-of-the-nameof-vertexelement-is-not-a-multiple-of","errorCode":null,"errorMessage":"The offset of the {nameof(VertexElement)} is not a multiple of 4 bytes","messagePattern":"The offset of the (.+?) is not a multiple of 4 bytes","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexElementValidator.cs","lineNumber":91,"sourceCode":"        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\n            int typeSize = elements[elementIndex].Format.SizeInBytes;\n            if (elementOffset == VertexElement.AppendAligned || (elementOffset + typeSize) > vertexStride)\n            {\n                throw new ArgumentException($\"The {nameof(VertexElement)}'s offset and size makes it extend beyond the vertex stride\", nameof(elements));\n            }\n            if ((elementOffset & 3) != 0)\n            {\n                throw new ArgumentException($\"The offset of the {nameof(VertexElement)} is not a multiple of 4 bytes\", nameof(elements));\n            }\n\n            // Check for duplicate semantic names and indices\n            for (int i = 0; i < elementIndex; i++)\n            {\n                if (elements[elementIndex].SemanticName.Equals(elements[i].SemanticName) &&\n                    elements[elementIndex].SemanticIndex == elements[i].SemanticIndex)\n                {\n                    throw new ArgumentException($\"Duplicate {nameof(VertexElement)}s found with the same semantic name and index\", nameof(elements));\n                }\n            }\n\n            // Check for overlap with existing elements\n            for (int i = elementOffset; i < (elementOffset + typeSize); i++)\n            {\n                // Check for overlap with existing elements\n                if (overlapCheckArray[i] >= 0)\n                {","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexElementValidator.cs#L73-L109","documentation":"A validation guard in VertexElementValidator.Validate: it fires when a vertex element's computed byte offset is not a multiple of 4. Graphics APIs require 4-byte alignment for vertex element offsets; an element that starts at an unaligned offset would be rejected by the GPU driver, so the validator rejects the vertex declaration early with ArgumentException on the elements parameter.","triggerScenarios":"Assigning offsets like 2, 6, or 10 in a vertex declaration — typically after adding a small 2-byte element and computing subsequent offsets naively.","commonSituations":"Packed layouts with 16-bit elements stacked at odd offsets; hand-written offsets copied from struct layouts with packing quirks.","solutions":["Pad preceding elements so every offset is a multiple of 4","Widen small elements to 4-byte formats (e.g. R16G16 to R32G32)","Recompute offsets sequentially, rounding each up to the next multiple of 4"],"exampleFix":"// before\n// R16 at offset 0 (2 bytes), R32G32 at offset 2 -> misaligned\n// after\n// R32 at offset 0 (4 bytes), R32G32 at offset 4","handlingStrategy":"validation","validationCode":"bool IsAligned(int offset) => (offset & 3) == 0;","typeGuard":null,"tryCatchPattern":"try { VertexElementValidator.Validate(stride, elements); } catch (ArgumentException) { alignOffsetsTo4(); }","preventionTips":["Round every element offset up to the next multiple of 4","Widen 16-bit elements to 32-bit formats to keep alignment","Lay out elements in 4-byte units"],"tags":["graphics","vertex-declaration","alignment","offset"],"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"}