{"record":{"id":"161e38a9624e0b0a","repo":"stride3d/stride","slug":"custom-strides-is-not-supported-with-packed-pixelformats","errorCode":null,"errorMessage":"Custom strides is not supported with packed PixelFormats","messagePattern":"Custom strides is not supported with packed PixelFormats","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Foundation/Graphics/Image.cs","lineNumber":1030,"sourceCode":"\n            for (int j = 0; j < imageDesc.ArraySize; j++)\n            {\n                int w = imageDesc.Width;\n                int h = imageDesc.Height;\n                int d = imageDesc.Depth;\n\n                for (int i = 0; i < imageDesc.MipLevels; i++)\n                {\n                    ComputePitch(imageDesc.Format, w, h, out var rowPitch, out var slicePitch, out var widthPacked, out var heightPacked, pitchFlags);\n\n                    if (rowStride > 0)\n                    {\n                        // Check that stride is ok\n                        if (rowStride < rowPitch)\n                            throw new InvalidOperationException($\"Invalid stride [{rowStride}]. Value can't be lower than actual stride [{rowPitch}]\");\n\n                        if (widthPacked != w || heightPacked != h)\n                            throw new InvalidOperationException(\"Custom strides is not supported with packed PixelFormats\");\n\n                        // Recalculate slice pitch\n                        slicePitch = rowStride * h;\n                    }\n\n                    // Store the number of z-slicec per miplevels\n                    if (j == 0)\n                        mipmapToZIndex.Add(bufferCount);\n\n                    // Keep a trace of indices for the 1st array size, for each mip levels\n                    pixelSizeInBytes += d * slicePitch;\n                    bufferCount += d;\n\n                    if (h > 1)\n                        h >>= 1;\n\n                    if (w > 1)\n                        w >>= 1;","sourceCodeStart":1012,"sourceCodeEnd":1048,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Foundation/Graphics/Image.cs#L1012-L1048","documentation":"ComputePitch can pack pixels (widthPacked/heightPacked differ from w/h) for certain block or packed pixel formats. For those formats a custom rowStride cannot be honored because the pitched layout is fixed by the packing; the API throws rather than computing a wrong slice pitch.","triggerScenarios":"Supplying rowStride > 0 for a packed pixel format (e.g. R8G8_B8G8_UNorm, UYVY-style, or block formats where width/height get packed by ComputePitch) so that widthPacked != w or heightPacked != h.","commonSituations":"Video/YUV packed formats being wrapped with a stride from a capture device; using block-compressed formats (BC*) with a custom stride copied from a GPU texture description.","solutions":["Pass rowStride = 0 for packed pixel formats and let ComputePitch determine the layout.","Convert the image to an unpacked format (e.g. R8G8B8A8_UNorm) before applying a custom stride.","Use PitchFlags appropriate for the format instead of a manual stride.","If the source buffer is truly row-padded packed data, repack it into the expected tight layout first."],"exampleFix":"// before\nvar img = Image.New(desc, ptr, 0, null, false, PitchFlags.None, rowStride: 2048); // packed format\n// after\nvar img = Image.New(desc, ptr, 0, null, false, PitchFlags.None, rowStride: 0); // let ComputePitch handle packed formats","handlingStrategy":"validation","validationCode":"Image.ComputePitch(format, w, h, out _, out _, out var wPacked, out var hPacked, PitchFlags.None);\nbool packed = wPacked != w || hPacked != h;\nif (packed && rowStride > 0)\n    throw new ArgumentException(\"Custom stride unsupported for packed format \" + format);","typeGuard":"null","tryCatchPattern":"try { img = Image.New(desc, ptr, 0, null, false, flags, rowStride); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"packed PixelFormats\"))\n{\n    img = Image.New(desc, ptr, 0, null, false, flags, 0);\n}","preventionTips":["Never combine packed formats (YUV/BC) with manual strides.","Convert packed formats to RGBA before custom-stride wrapping.","Check ComputePitch's packed outputs when supporting new formats.","Prefer PitchFlags over manual stride values for special layouts."],"tags":["graphics","pixel-format","stride","stride"],"backgroundTag":"unsupported-operation","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"}