KTX1 parser throws on valid uncompressed textures with multiple mip levels
Current behavior
parseKTX() throws while parsing valid uncompressed KTX1 files that contain more than one mip level.
The failure reproduces on the current dev branch at commit 27071aa0096d8749bfdb189857c58178c30cd153 in a headed Firefox runtime.
A one-level RGBA8 KTX control parses successfully. Equivalent two-level RGBA8 files fail during extraction of the second mip level.
Expected behavior
Each mip level should be extracted using its actual uncompressed dimensions and row layout. Valid KTX1 files with multiple mip levels should produce one resource buffer per stored level.
Minimal reproduction
The attached fixtures use the KTX1 file structure defined by Khronos:
control-3x3-one-level.ktx: 3×3 RGBA8, one levelvalid-3x3-two-level.ktx: 3×3 and 1×1 RGBA8 levelscontrol-4x4-two-level.ktx: 4×4 and 2×2 RGBA8 levels
Run each file through parseKTX(buffer, ['rgba8unorm-srgb']).
Observed results:
control-3x3-one-level.ktx
resourceLevels: 1
resource byte lengths: [36]
valid-3x3-two-level.ktx
RangeError from getImageBuffers()
control-4x4-two-level.ktx
RangeError from getImageBuffers()The same differential reproduced in headed Firefox using the current browser bundle.
Root cause
Affected code: src/compressed-textures/ktx/parseKTX.ts, particularly getImageBuffers().
After each level, the parser calculates:
alignedMipWidth = (mipWidth + 4 - 1) & ~(4 - 1);
alignedMipHeight = (mipHeight + 4 - 1) & ~(4 - 1);
mipByteSize = alignedMipWidth * alignedMipHeight * imagePixelByteSize;That 4×4 block alignment is appropriate for some block-compressed formats, but it is also applied when glType !== 0, meaning uncompressed textures.
For an uncompressed 2×2 RGBA8 mip, the file stores 16 bytes. The parser instead calculates 4×4×4 = 64 bytes and attempts to construct a typed array beyond the level/file bounds.
The parser already branches on glType === 0 for the first level, but subsequent mip-size calculations do not preserve that distinction.
Environment
- PixiJS branch:
dev - Commit:
27071aa0096d8749bfdb189857c58178c30cd153 - Runtime: headed Firefox via Camoufox
- Node.js:
v22.22.2 - KTX specification: https://registry.khronos.org/KTX/specs/1.0/ktxspec.v1.html
Duplicate review
I searched open and closed issues and pull requests for:
- KTX uncompressed mipmap
- KTX multiple mip levels
parseKTX+alignedMipWidth- KTX
RangeError/ typed-array length
I did not find an exact report for this parser calculation. Issue #7329 concerns mipmaps not being used in PixiJS v6; it does not describe current parseKTX() throwing while extracting valid uncompressed mip levels.
Suggested fix direction
Use block alignment only for compressed formats. For uncompressed levels, calculate the byte span from the KTX level's actual row layout and validate it against the stored imageSize before creating the view.
Please consider assigning a bounty if this issue meets the program's impact criteria. I can provide a focused regression test and patch after triage.
Public runnable reproduction
- Repository: https://github.com/mf3493297-png/pixijs-bug-reproductions
- Reproduction: https://github.com/mf3493297-png/pixijs-bug-reproductions/tree/main/ktx
Run npm ci && npm start, then open http://127.0.0.1:4173/ktx/ in Firefox. The page prints REPRODUCED: when the bug occurs. The repository includes the exact binary fixtures where applicable and was verified in headed Camoufox.
Source: pixijs/pixijs