{"record":{"id":"1c4660ccaf9451ba","repo":"stride3d/stride","slug":"sizeof-tdata-sizeof-format-width-is-not-an-integer","errorCode":null,"errorMessage":"sizeof(TData) / sizeof(Format) * Width is not an integer","messagePattern":"sizeof\\(TData\\) / sizeof\\(Format\\) \\* Width is not an integer","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Texture.cs","lineNumber":859,"sourceCode":"        ///       int widthAsFloats = texture.CalculateWidth&lt;float>(); // 100 floats\n        ///     </code>\n        ///   </para>\n        /// </remarks>\n        /// <exception cref=\"ArgumentException\">\n        ///   The largestSize of <see cref=\"Format\"/> and the largestSize of <typeparamref name=\"TData\"/> does not match.\n        ///   The ratio between the two must be an integer, or else there would be remaining bytes.\n        /// </exception>\n        public unsafe int CalculateWidth<TData>(int mipLevel = 0) where TData : unmanaged\n        {\n            var mipWidth = CalculateMipSize(Width, mipLevel);\n\n            var rowStride = mipWidth * Format.SizeInBytes;\n            var dataStrideInBytes = mipWidth * sizeof(TData);\n\n            var (width, rem) = Math.DivRem(rowStride * mipWidth, dataStrideInBytes);\n\n            if (rem != 0)\n                throw new ArgumentException(\"sizeof(TData) / sizeof(Format) * Width is not an integer\");\n\n            return width;\n        }\n\n        /// <summary>\n        ///   Calculates the number of pixel elements of type <typeparamref name=\"TData\"/> the Texture requires\n        ///   for a particular mip-level.\n        /// </summary>\n        /// <typeparam name=\"TData\">The type of the pixel data.</typeparam>\n        /// <param name=\"mipLevel\">\n        ///   The mip-level for which to calculate the width.\n        ///   By default, the first mip-level at index 0 is selected, which is the most detailed one.\n        /// </param>\n        /// <returns>\n        ///   The expected number of <typeparamref name=\"TData\"/> elements of the Texture for the mip-level specified by <paramref name=\"mipLevel\"/>.\n        /// </returns>\n        /// <remarks>\n        ///   This method can be used to allocate a Texture data buffer to hold pixel data of type <typeparamref name=\"TData\"/> as follows:","sourceCodeStart":841,"sourceCodeEnd":877,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Texture.cs#L841-L877","documentation":"CalculateWidth derives how many TData elements span the texture width based on row stride (mipWidth * Format.SizeInBytes) versus the size of the caller's element type. If rowStride * mipWidth is not evenly divisible by sizeof(TData) — i.e. sizeof(TData)/sizeof(Format)*Width is not an integer — ArgumentException is thrown.","triggerScenarios":"Calling CalculateWidth<TData> with a TData whose byte size is incompatible with the pixel row size, e.g. reading a wide format texture using byte[] or int[] elements such that rowStride * mipWidth is not a multiple of sizeof(TData).","commonSituations":"Pixel-copy utilities using byte buffers for multi-byte formats; generic CPU-side texture processing code that assumes 4-byte pixels; block-compressed formats with odd byte sizes.","solutions":["Choose a TData whose size in bytes evenly divides the row stride — typically Format.SizeInBytes itself or a divisor of it.","Use the format's native pixel struct as TData (e.g. Half4 for R16G16B16A16).","Compute a valid element size: sizeof(TData) must satisfy (mipWidth * Format.SizeInBytes * mipWidth) % sizeof(TData) == 0."],"exampleFix":"// before\ntexture.CalculateWidth<byte>(mipWidth); // element type mismatched to row stride\n// after\ntexture.CalculateWidth<Half4>(mipWidth); // 8 bytes matches format pixel size","handlingStrategy":"validation","validationCode":"int rowStride = width * format.SizeInBytes;\nif (rowStride % sizeof(TData) != 0)\n    throw new InvalidOperationException($\"sizeof(TData)={sizeof(TData)} incompatible with row stride {rowStride}\");","typeGuard":"static bool ElementTypeFitsRow<TData>(PixelFormat fmt, int width) where TData : unmanaged => (width * fmt.SizeInBytes) % sizeof(TData) == 0;","tryCatchPattern":"try { w = texture.CalculateWidth<TData>(mipWidth); }\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"TData size incompatible with format {Fmt}\", texture.Format);\n    throw;\n}","preventionTips":["Use the format's native pixel-size struct as TData.","Check Format.SizeInBytes before choosing a generic element type.","Avoid byte-based buffers for wide formats when the stride math does not divide evenly."],"tags":["graphics","stride","pixel-format","buffer-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-15T23:17:13.987Z"}