{"record":{"id":"f9521f86624794ed","repo":"stride3d/stride","slug":"invalid-texture-data-length","errorCode":null,"errorMessage":"Invalid Texture data length","messagePattern":"Invalid Texture data length","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Texture.cs","lineNumber":1810,"sourceCode":"        /// <param name=\"width\">The width in pixels of the Texture to calculate the <see cref=\"DataBox\"/> for.</param>\n        /// <param name=\"height\">The height in pixels of the Texture to calculate the <see cref=\"DataBox\"/> for.</param>\n        /// <param name=\"depth\">The depth in pixels of the Texture to calculate the <see cref=\"DataBox\"/> for.</param>\n        /// <param name=\"textureData\">The data buffer to upload to the Texture.</param>\n        /// <param name=\"fixedPointer\">A pointer to the data buffer to upload to the Texture.</param>\n        /// <returns>A <see cref=\"DataBox\"/> structure describing the data and its memory layout.</returns>\n        /// <exception cref=\"ArgumentException\">\n        ///   The length of <paramref name=\"textureData\"/> and data type <typeparamref name=\"TData\"/> are incorrect for\n        ///   the specified size and pixel <paramref name=\"format\"/>.\n        /// </exception>\n        private static unsafe DataBox GetDataBox<TData>(PixelFormat format, int width, int height, int depth, TData[] textureData, IntPtr fixedPointer) where TData : unmanaged\n        {\n            ArgumentNullException.ThrowIfNull(textureData);\n\n            // Check that the textureData has the correct size for the Texture's data\n            Image.ComputePitch(format, width, height, out var rowPitch, out var slicePitch, out _, out _);\n\n            if (sizeof(TData) * textureData.Length != (slicePitch * depth))\n                throw new ArgumentException(\"Invalid Texture data length\", nameof(textureData));\n\n            return new DataBox(fixedPointer, rowPitch, slicePitch);\n        }\n\n        /// <summary>\n        ///   Swaps the Texture's internal data with another Texture.\n        /// </summary>\n        /// <param name=\"other\">The other Texture.</param>\n        internal void Swap([NotNull] Texture other)\n        {\n            if (ParentTexture is not null || other.ParentTexture is not null)\n                throw new NotSupportedException(\"Cannot swap texture views; only root textures can be swapped.\");\n\n            (textureDescription, other.textureDescription) = (other.textureDescription, textureDescription);\n            (textureViewDescription, other.textureViewDescription) = (other.textureViewDescription, textureViewDescription);\n            (mipmapDescriptions, other.mipmapDescriptions) = (other.mipmapDescriptions, mipmapDescriptions);\n            (fullQualitySize, other.fullQualitySize) = (other.fullQualitySize, fullQualitySize);\n","sourceCodeStart":1792,"sourceCodeEnd":1828,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Texture.cs#L1792-L1828","documentation":"Thrown when the TData[] passed to a Texture DataBox-building method does not exactly match the texture's computed data size: sizeof(TData) * Length must equal slicePitch * depth, where pitches come from Image.ComputePitch for the format. Ensures the fixed pointer covers the whole texture. Raised as ArgumentException.","triggerScenarios":"Calling a Texture method that wraps raw data into a DataBox with a buffer sized to width*height*bytesPerPixel but ignoring row/slice pitch padding, or depth not accounted for.","commonSituations":"Compressed formats (BC1/BC3) where block-based pitch differs from naive width*4; padded rows on odd widths; 3D texture depth forgotten; buffer allocated for the wrong format.","solutions":["Compute the required size with Image.ComputePitch(format, width, height, out rowPitch, out slicePitch, ...) and allocate textureData.Length = slicePitch * depth / sizeof(TData).","Verify the generic type TData matches the format's bytes-per-pixel (or blocks for compressed formats).","Log sizeof(TData) * Length vs slicePitch * depth to find the exact discrepancy."],"exampleFix":"// before\nvar data = new byte[width * height * 4]; // ignores pitch padding\ntexture.SetData(commandList, data);\n// after\nImage.ComputePitch(format, width, height, out var rowPitch, out var slicePitch, out _, out _);\nvar data = new byte[slicePitch * depth];\ntexture.SetData(commandList, data);","handlingStrategy":"validation","validationCode":"Image.ComputePitch(format, width, height, out _, out var slicePitch, out _, out _);\nif (sizeof(TData) * textureData.Length != slicePitch * depth)\n    throw new InvalidOperationException($\"textureData size {sizeof(TData) * textureData.Length} != expected {slicePitch * depth}\");","typeGuard":null,"tryCatchPattern":"try { texture.SetData(cmd, data); }\ncatch (ArgumentException e) when (e.Message.Contains(\"Invalid Texture data length\")) { log.Error(\"Texture buffer size mismatch\", e); }","preventionTips":["Use Image.ComputePitch for compressed/block formats too.","Keep TData element size aligned with the format.","Unit-test buffer sizing per format."],"tags":["graphics","buffer-size","texture"],"backgroundTag":"shape-mismatch","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"}