A KTX 2 texture is a stack of mip levels, and each level is a stack of images: one for every array layer, cube face and depth slice. This page says how many images a level holds, how many bytes each takes, and where it is. The viewer on this site uses exactly these rules to show each image as a picture.
The size of a level
Level L is the base size shifted right by L, never below 1:
width(L) = max(1, pixelWidth >> L)
height(L) = max(1, pixelHeight >> L) (pixelHeight 0, a 1D texture, counts as 1)
depth(L) = max(1, pixelDepth >> L) (pixelDepth 0 counts as 1)
The data format descriptor gives the texel block: its size in texels
(texelBlockDimension, each stored minus one) and its size in bytes
(bytesPlane0). A plain texel is a block of 1 × 1 × 1. Then:
blocksX = ceil(width(L) / blockWidth)
blocksY = ceil(height(L) / blockHeight)
slices = ceil(depth(L) / blockDepth)
imageBytes = blocksX * blocksY * bytesPlane0
images = max(1, layerCount) * faceCount * slices
level size = images * imageBytes (before supercompression)
Rows of blocks are packed with no padding. PVRTC data is never smaller than 2 × 2 blocks, so a small PVRTC level has more blocks than its size suggests.
The order of the images
Inside a level the images follow each other with nothing between them, in this order, the last item changing fastest:
- array layer, from 0 to
layerCount − 1(one layer whenlayerCountis 0); - cube face: +X, −X, +Y, −Y, +Z, −Z (one face unless
faceCountis 6); - depth slice, from 0 (one slice unless the texture is 3D).
In a cube map array, layerCount counts whole cube maps, so layer 1 face 0
is image 6.
Where the levels are
- The level index gives each level’s file offset, its stored length and its uncompressed length. Level 0, the base level, is first in the index, but the data is stored smallest level first, so level 0 is last in the file.
- Without supercompression the stored length equals the uncompressed length, which is
the level size above. Each level starts on a multiple of
lcm(block size in bytes, 4), with zero bytes before it. - With Zstandard or ZLIB, the stored bytes are one stream for the whole level. Decompressed, they are
the images exactly as above, and
uncompressedByteLengthis the level size. - With BasisLZ, the level holds, for every image, a slice of colour data and
sometimes a slice of alpha data. The slices are found through the descriptors in the
supercompression global data, and
uncompressedByteLengthis 0. A BasisLZ texture is 2D: it has no depth slices, and its images are the layers and faces.
In the viewer
- The tree shows each level as a node, with a node for every image under it (or every slice, for BasisLZ), so the bytes of any image can be selected and read in the hex view.
- The Images table lists every image of every level, however many there are, a page at a time. Each row has a Show button that opens its picture.
- A supercompressed level is also offered as a file of its own: the decompressed bytes of the level.
- A picture of more than 16 million pixels is shown in a band of rows; the First row shown option moves the band.
- The viewer holds one decompressed level at a time, up to 512 MiB, and reads a compressed level of up to 128 MiB. A larger level is listed but not decompressed.
Sources
- KTX File Format Specification, version 2.0 – Khronos Group: the level index, “Mip levels” and the mip padding rules