Ktx-codec-0.0.2.1, with even more KTX

Since this is a first announcement for this package here, I’ll let the specs speak for themselves:

KTX™️ (Khronos TeXture) is an efficient lightweight container file format for reliably distributing GPU textures to diverse platforms and applications. It is distinguished by the simplicity of the loader required to instantiate texture objects from the file contents. The contents of a KTX file can range from a simple base-level 2D texture to a cubemap array texture with mipmaps. KTX files hold all the parameters needed for efficient texture loading into 3D APIs such as OpenGL® and Vulkan®

In this major release I added the KTX2 format:

Version 2 extends the functionality of version 1 with easier loading of Vulkan textures, easier use by non-OpenGL and non-Vulkan applications, the possibility of streaming, through sending small mip levels first, universal textures using Basis Universal technology and supercompression. Providing this new functionality requires a significantly different file structure from version 1.

Speaking less officially, KTX lets you transcode your JPEGs and PNGs into special GPU formats like BCx family. Those are compressed formats, but unlike common “image” formats, your GPU know how to sample those directly. This allows to save some GPU memory and cut loading times (since no decoding takes place).

There are, as always, trade-offs. Texture formats are compressed per block and don’t have access to global information like e.g. PNGs do. That means a huge black square will only have ratio close to 1:4, where with global information it can be compressed to “its a black pixel, repeated many times”.

With KTX1 I had to apply zstd compression on resulting files to recover “offline” size. This works, but prevents partial reading (unless involving lazy IO) and kinda inefficient.
KTX2, recognizing this issue, brings native supercompression schemes. This allows leaving metadata in the open, while keeping image data compressed (with each mip level independent of each other).

The ktx-codec package now exposes that partial reads API for KTX2 files and allows reading image data into a user-provided pointer (which can be a Zstd or GPU staging buffer).

The package itself does only low-level processing, so you’d need some wrapping code. I hope that there would be integration packages in the future, like ktx-sdl or ktx-vulkan that will make reading (and writing) textures even more easy.

6 Likes