GPU data
Texture compression: blocks, KTX2 and Basis
Textures take most of a scene's video memory. A GPU can read textures that stay compressed in memory, but only in its own block formats, and each kind of GPU reads different ones. This page explains how block compression works, why a PNG does not save any video memory, and how a Basis Universal file in a KTX2 container is turned, at load time, into whatever the GPU reads. The last part shows how RASM loads KTX2 files.
In short
- A block format stores each 4 × 4 tile of texels in 8 or 16 bytes, where plain RGBA8 needs 64. The GPU decodes a block when it samples it, so the texture stays small in video memory.
- PNG and JPEG are small on disk only: the GPU can't sample them, so they are decoded to RGBA8 before the upload.
- Desktop GPUs read BC formats, mobile GPUs ASTC and ETC2. A Basis file (ETC1S or UASTC) is none of them, but it transcodes fast to any of them.
- Pick the format per GPU and per use: color in BC7, a two-channel normal map in BC5, one channel in BC4, RGBA8 when the GPU has none of them.
- A block format can't make its own mips cheaply, so the file brings them. Color space is a matter of how the same blocks are read: an sRGB or a UNORM view.
1Textures are most of the memory
A 2048 × 2048 color texture in RGBA8 is 16 MiB, and its mips add a third. A model with a base color, a normal map and an occlusion-roughness-metal map per material soon needs hundreds of megabytes.
2Blocks of 4 × 4 texels
Texels next to each other are often close in color. A block format cuts the texture into tiles of 4 × 4 texels. Each tile stores a few endpoint colors and, per texel, a short index that says where between the endpoints the texel lies. Every tile has the same size, so the GPU finds the tile of any texel with one multiply: it can sample at random, which a PNG's variable-length stream can't do.
The real formats add tricks on top of this picture: several pairs of endpoints for tiles with two regions, more index bits, a separate line for alpha. The families that matter today:
| Format | Channels | Bytes per 4 × 4 | Bits per texel | Where |
|---|---|---|---|---|
| BC7 | RGBA | 16 | 8 | desktop GPUs (D3D11 and later), Apple Silicon Macs |
| BC5 | RG (two BC4 halves) | 16 | 8 | same; the usual format for normal maps |
| BC4 | R | 8 | 4 | same; masks, occlusion, height |
| ASTC 4 × 4 | RGBA | 16 | 8 | mobile GPUs, Apple GPUs (larger blocks trade quality for size) |
| ETC2 RGBA8 | RGBA | 16 | 8 | every OpenGL ES 3 and Vulkan mobile GPU |
| EAC R11 / RG11 | R / RG | 8 / 16 | 4 / 8 | same, the mobile BC4 and BC5 |
| RGBA8 | RGBA | 64 | 32 | every GPU; the fallback |
3Small on disk is not small on the GPU
A PNG or a JPEG packs the image with a method made for files: a stream that must be decoded from the start. The GPU can't sample that, so the loader decodes it to RGBA8, and that is what reaches video memory. A block-compressed file is already in the shape the GPU reads.
PNG or JPEG
Small file, full-size texture. The decode and the mips also cost CPU time at load.
Block-compressed (KTX2)
The file brings its mips; loading copies the blocks.
4One file for every GPU: Basis Universal
An engine that ships BC7 files can't run them on a phone, and ASTC files can't run on most desktop GPUs. Basis Universal stores a texture in an in-between format that no GPU samples, but that turns into any of them quickly, without the slow search a real encoder does. This step is called transcoding, and it runs on the CPU at load time.
ETC1S
Tiles share palettes of endpoints and index patterns, so files are tiny. Quality is lower: fine for base colors seen from afar, weak for normal maps.
About 1 to 2 bits per texel on disk.
UASTC
One 16-byte block per tile, a subset of ASTC, so it maps almost one to one onto BC7 and ASTC. Near BC7 quality; files are larger, so they are usually zstd-compressed.
8 bits per texel before zstd.
5The KTX2 container
KTX2 (Khronos texture, version 2) is the file around the data. It names the format with Vulkan's format numbers, stores every mip, and says what the channels mean and how the color is encoded. glTF reads it through the KHR_texture_basisu extension.
6Picking the format
Two things decide the target: what the GPU can sample, and what the texture is used for. Color is read as RGB and alpha. A normal map needs only x and y. Occlusion or a height map needs one channel. Each use has a list of formats, best first, and the loader takes the first one the GPU has.
| Use | 1st | 2nd | 3rd | Fallback |
|---|---|---|---|---|
| Color (base color, emissive), sRGB | BC7 sRGB | ASTC 4×4 sRGB | ETC2 RGBA8 sRGB | RGBA8 sRGB |
| Data, 3 or 4 channels (metal-rough, ORM) | BC7 | ASTC 4×4 | ETC2 RGBA8 | RGBA8 |
| Data, 2 channels (normal map x, y) | BC5 | EAC RG11 | — | RGBA8 (z computed) |
| Data, 1 channel (occlusion, mask) | BC4 | EAC R11 | — | RGBA8 |
7Normal maps in two channels
A tangent-space normal is a unit vector that points out of the surface, so z is never negative and follows from x and y. Storing only x and y lets a two-channel format spend all its bits on them: BC5 keeps each in its own BC4 half, with two 8-bit endpoints and a 3-bit index per texel, where BC7 shares its bits among three or four channels.
z = √(1 − x² − y²)
- x, y
- the two stored channels, mapped from 0…1 to −1…1.
- clamp
- use max(0, 1 − x² − y²): filtering and compression can push x² + y² a little over 1.
- layout
- Basis keeps x in RGB and y in alpha (ktx create --normal-mode); its BC5 and EAC RG11 targets read those two, so the GPU format has x in R and y in G.
8Mips and color space
Mips come from the file
Making a mip of a block texture means averaging texels and encoding the result again, which takes an encoder and time. So the tool that makes the file makes the mips (ktx create --generate-mipmap). Without them, a texture shimmers when it is minified.
Color space is the view
BC7, ASTC and ETC2 have an sRGB and a UNORM format with the same bits. Pick it by the texture's use, as for any texture (color spaces): color is sRGB, data is linear.
9Pitfalls
- Bumps look flat or lit from inside
- A two-channel normal map read as three channels: z comes back as 0 (or −1). Rebuild z in the shader, or let the loader do it for an RGBA8 fallback.
- Normal maps look blocky
- ETC1S on a normal map. Use UASTC for normal maps and other data, ETC1S for colors.
- Distant surfaces sparkle
- A block texture without mips. The file needs them.
- Colors too dark or washed out
- A color texture read through a UNORM view, or a data texture through sRGB.
- Odd sizes break the upload
- A 30 × 30 texture still takes 8 × 8 whole blocks. Sizes and copies count blocks, not texels: a level is ⌈w / 4⌉ × ⌈h / 4⌉ × block bytes.
10In RASM
RASM reads KTX2 files with libktx (the KTX::ktx target of the vcpkg port ktx, built without its ETC software decoder: cmake/triplets/x64-windows.cmake). A .ktx2 passed to loadTexture and the images of glTF's KHR_texture_basisu go the same way: the format is picked from the header, the transcode runs on the load's worker thread, and the main thread only copies the levels.
loadTexture returns, so a material can use it at once; it draws once isLoaded is true. For glTF, each image is a job of its own, which transcodes it once per format the slots that use it need.Where RASM follows the page, and where it differs
| Part | This page | RASM |
|---|---|---|
| The slot | color or data, by use | KtxSlot: what the slot reads. glTF: base color and emissive are COLOR, metallic-roughness DATA (every channel: a one-channel file stays (r, r, r) in BC7), the normal map NORMAL (BC5 for a two-channel file), occlusion OCCLUSION (BC4 for a one-channel file). loadTexture's format: R8G8B8A8_SRGB is COLOR, R8G8B8A8_UNORM is CHANNELS: one channel is R, two are a normal map's x and y. The file's own transfer function is ignored. |
| Device formats | what the GPU samples | RenderContext::supportsTextureFormat: the compression feature (textureCompressionBC, textureCompressionASTC_LDR, textureCompressionETC2) is on, and the format can be sampled with linear filtering and filled. |
| Two channels | BC5 or EAC RG11, z rebuilt | The material row gets MATERIAL_NORMAL_XY when its normal texture is BC5_UNORM or EAC_R11G11_UNORM; getNormalFromMap rebuilds z. The RGBA8 fallback gets z on the CPU. A UASTC file with y in green (not in alpha) goes to RGBA8, with a warning, since the transcoder reads y from alpha. |
| A Basis file without mips | the file should have them | Transcoded to RGBA8, mips made on the worker with the same filter as PNGs, and a warning that names the file and the fix. A file that is not Basis keeps its single level, with a warning. |
| Files that are not Basis | — | Loaded as stored (zstd inflated) if their format is one of RASM's (BC4_UNORM … EAC_R11G11_UNORM, RGBA8) and the device samples it; the slot picks the sRGB or UNORM variant. |
| Upload | copy the blocks | RenderContext::fillTexture with a TextureLevel per mip, from finishLoads under its budget; a glTF's textures are queued in pendingTextures with their levels, so a model's copies spread over frames. The staging ring, or host image copy where it costs the GPU nothing for the format (texture uploads). EngineConfig::hostImageCopy = false forces the staging path. |
| A file that fails | — | The texture already exists, so it is filled with constant blocks (makeBlankKtxLevels): white, or a flat normal for a two-channel format, never ASTC's all-zero error block. |
| Not covered | — | BC1 to BC3, BC6H and HDR, ASTC blocks larger than 4 × 4, cube and array KTX2 files, encoding at load time. |
finishMeshLoad, Release; whole load 870 → 540 msWhere it lives in code
| What | Where |
|---|---|
| Header, format choice, transcode, warnings | readKtxInfo, chooseKtxFormat, getKtxMipCount, decodeKtx, warnAboutKtxFormat in src/assets/ktxImage.cpp; tested by tests/ktxImageTests.cpp, which encodes small Basis files with libktx |
.ktx2 through loadTexture | AssetLoader::loadTexture, AssetLoader::Impl::finishTextureLoad in src/assets/texture.cpp |
| glTF KHR_texture_basisu | getBasisuSource and the worker's transcode in src/assets/meshImport.cpp; AssetLoader::Impl::loadMaterial in src/assets/material.cpp |
| Block sizes, device formats, upload | getFormatBlock, getTextureLevelSize in src/rhi/device.cpp; VulkanDevice::supportsTextureFormat, VulkanDevice::fillTexture; UploadManager::uploadTexture |
| Two-channel normals | MATERIAL_NORMAL_XY in src/renderer/uberMaterialTypes.h and shaders/lib/core.slang; getNormalFromMap in shaders/lib/pbr.slang |
| Example | examples/10_ktx2_textures: the Flight Helmet in KTX2 and a .ktx2 through loadTexture; --model and --measure compare it with another glTF |
- A new
Formatgets its block ingetFormatBlock, its Vulkan format intoVkFormat, and, if it is a compressed one, its feature check inVulkanDevice::queryFormatSupport.R16G16_SFLOATis the last value; the Vulkan format table ends there (VulkanDevice::FORMAT_COUNT). - The format lists in
ktxImage.cppand the lab of this page are the same lists. - Color slots are sRGB and data slots UNORM, for KTX2 files too: the slot decides, not the file.
Further reading
- Khronos, KTX File Format Specification, version 2.0, and the KHR_texture_basisu glTF extension.
- Binomial, Basis Universal (GitHub wiki): ETC1S, UASTC and the transcoder's targets.
- Nathan Reed, Understanding BCn Texture Compression Formats (2012).
- Microsoft, BC7 Format (Direct3D 11 documentation): the modes and their bit layouts.