{"record":{"id":"e7afbe6c18560d16","repo":"stride3d/stride","slug":"typeof-tsource-does-not-match-element-size-sizeof-tsource","errorCode":null,"errorMessage":"{typeof(TSource)} does not match element size ({sizeof(TSource)} != {element.Size})","messagePattern":"(.+?) does not match element size \\((.+?) != (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/VertexBufferHelper.cs","lineNumber":273,"sourceCode":"                case PixelFormat.R16G16B16A16_UInt: InnerRead<TSemantic, TReader, UShort4, TDest>(destination, reader, elementData); break;\n                case PixelFormat.R8G8B8A8_UInt: InnerRead<TSemantic, TReader, Byte4, TDest>(destination, reader, elementData); break;\n                case PixelFormat.R8G8B8A8_UNorm: InnerRead<TSemantic, TReader, Color, TDest>(destination, reader, elementData); break;\n                default: throw new NotImplementedException($\"Unsupported format when converting vertex element ({elementData.VertexElement.Format})\");\n            }\n\n            return true;\n        }\n\n        return false;\n    }\n\n    private unsafe void InnerRead<TConverter, TReader, TSource, TDest>(Span<TDest> destination, TReader reader, VertexElementWithOffset element) \n        where TConverter : IConverter<TSource, TDest> \n        where TSource : unmanaged\n        where TReader : IReader<TDest>, allows ref struct\n    {\n        if (sizeof(TSource) != element.Size)\n            throw new ArgumentException($\"{typeof(TSource)} does not match element size ({sizeof(TSource)} != {element.Size})\");\n\n        var stride = Binding.Declaration.VertexStride;\n        var offset = element.Offset;\n        var count = Binding.Count;\n            \n        fixed (byte* ptrSr = DataInner)\n        {\n            byte* firstElement = ptrSr + offset;\n            reader.Read<TConverter, TSource>(firstElement, count, stride, destination);\n        }\n    }\n\n    /// <summary>\n    /// Lower level access to write directly to the vertex buffer\n    /// </summary>\n    /// <param name=\"writer\">\n    /// An implementation of <see cref=\"IWriter{TDestValue}\"/>, implement this interface to write directly into the vertex buffer\n    /// while making use of the auto-conversion of the <typeparamref name=\"TSemantic\"/> provided <br/>","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/VertexBufferHelper.cs#L255-L291","documentation":"InnerRead validates that the generic source type chosen for a PixelFormat matches the element's declared byte size (sizeof(TSource) == element.Size). A mismatch means the format-to-type mapping is inconsistent with the element size in the vertex declaration, so raw reads would be misaligned. It throws ArgumentException immediately to prevent corrupt data or memory overreads.","triggerScenarios":"Calling a Read overload where the vertex declaration's element Size does not equal the byte size of the type selected for that format, e.g. an element marked R32G32B32A32_Float (16 bytes) but with VertexElement size computed as 12, or a custom declaration with wrong element sizes.","commonSituations":"Hand-built VertexDeclaration with wrong size fields; edited mesh headers after changing formats without updating sizes; converter/reader generic mismatch when extending the helper.","solutions":["Fix the VertexElement size in the declaration so it matches the actual format's byte size (e.g. 16 for R32G32B32A32_Float)","Regenerate the vertex declaration from the vertex struct layout instead of hand-writing sizes","If writing a custom reader, pick a TSource whose sizeof matches element.Size","Print element.Size and the expected sizeof for the format to find which is wrong"],"exampleFix":"// before\nnew VertexElement(\"POSITION\", 0, PixelFormat.R32G32B32A32_Float, 0, /*size*/ 12) // mismatch\n// after\nnew VertexElement(\"POSITION\", 0, PixelFormat.R32G32B32A32_Float, 0, /*size*/ 16)","handlingStrategy":"validation","validationCode":"if (sizeof(Vector4) != element.Size)\n    throw new InvalidOperationException($\"Element size {element.Size} does not match format type size\");","typeGuard":null,"tryCatchPattern":"try { InnerRead<...>(dest, reader, element); }\ncatch (ArgumentException ex) { logger.Error(ex, \"Element size mismatch in vertex declaration\"); throw; }","preventionTips":["Generate declarations from struct layouts, not by hand","Cross-check element.Size against the format's byte width","Add import-time validation of declaration sizes"],"tags":["csharp","graphics","vertex-buffer","size-mismatch"],"backgroundTag":"type-mismatch","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"}