Encoded again as BC7

Most desktop GPUs sample BCn but not ASTC, and a decoded ASTC texture is four bytes a texel where the game's blocks take one (4x4) or less. Held as BC7 (the astc_textures setting, below), format.c's TA() gives such a texture a VK_FORMAT_BC7_UNORM_BLOCK or _SRGB_BLOCK image in place of RGBA8, which texture.c makes between frames (below), and vk_astc_upload, where it uploads straight to BC7, runs another shader in the decoder's place: src/nx/gpu/texture/astc_bc7.comp, with its body in astc_bc7.glsl, makes the ASTC blocks BC7 blocks in one dispatch, an invocation a 4x4 block, into a scratch buffer of their own, and the copy writes those blocks into the image (Straight from the blocks). A BC7 strip starts on a row of whole 4x4 blocks, so its ASTC block rows are a multiple of 4 / gcd(block height, 4), and whole slices are counted by the blocks' bytes too, which for a slice smaller than a block are more than its texels'. device.c turns textureCompressionBC on for it whatever native_bc says, and format.c then samples BCn as it is only when native_bc asks too. A 3D texture keeps RGBA8 where the device has no 3D BC7 images, as native block formats do. Every 600 frames the log says how many levels went up as BC7 and their megabytes against RGBA8's.

A texture with holes in its mapping (a game's sparse texture, texture.c) is BC7 whatever astc_textures says, where the GPU samples BCn: one game's are 68 layers of 8192x8192 ASTC 8x8 with seven of them mapped, 1.7 GB each as RGBA8 (16 times the game's bytes), which took the GPU past its memory. So it goes up straight to BC7, not RGBA8 first and BC7 between frames (below). device.c turns textureCompressionBC on wherever the device has it for this. A sparse texture's levels are decoded only in the strips that have memory bound (vk_image_sparse_bound), in strips as high as the device's sparse blocks.

An array texture goes up a level at a time over all its layers (vk_astc_upload_layers), the layers as a 3D level's slices: a strip takes as many as the scratch holds, and the encoder runs once for them. A layer and level at a time, a small level gave the encoder too little to fill the GPU and each layer's dispatch waited for the one before: one game's arrays of 118 layers of 128x128 in 8 levels took 667 ms of an RX 6700 XT, where a level of one in a dispatch takes 13.5 ms, and the game stopped 1.4 s for two of them.

vk_astc_upload waits for the GPU once 256 thousand blocks are queued. The encoder is slow on a small GPU, and without the wait a load queued up tens of seconds of it and the device was lost in most runs of one game, on a GT 1030, with nothing in the validation layer or the kernel's log. Why the depth of the queue does that is not known: it is the driver's. Splitting the work over submissions with no wait did not help, and neither did smaller dispatches.

Straight from the blocks

The BC7 blocks were first made in two dispatches: astc.comp decoded a strip into RGBA8 texels, four bytes each, and a second shader read them back and encoded them. astc_bc7.comp does both. An invocation takes one 4x4 block, decodes the ASTC blocks its 16 texels lie in (astc_decode.glsl's DecodeBlock, then SlotTexel for each texel, the code astc.comp runs), and encodes them (bc7_encode.glsl's Encode). The texels lie in one ASTC block where the footprint is 4x4 or 8 or 12 a side, else in up to two each way, so an ASTC block is decoded once for each 4x4 block over it, up to nine times (12x12). No texel is written out, there is one dispatch and no barrier between two, and the blocks are the same bytes the two dispatches made (astc_bc7_glsl, below).

A texture made BC7 between frames has no texels to hand the shader, and no longer needs any: it keeps its ASTC blocks (below).

Two passes

A GPU runs a shader's invocations 32 or 64 abreast, each group at the pace of its slowest. A block mode 6 holds is done in a hundredth of the time of one that takes the whole search, and with one such block in six nearly every group has one and waits for it: one game's 8x8 level with 18% of its blocks past mode 6 took a GeForce GT 1030 392 ms, and a level of the same game that mode 6 holds throughout 18 ms, 21 times, where the operations counted are 5 times.

So the full and the fast build are two dispatches (SNX_BC7_PASS, astc_bc7.glsl). The first runs over the strip's blocks, an invocation a block by its number in the strip (64 a workgroup, whatever the width of a row), gives each mode 6 alone, and writes those the search would have stopped at there (Encode's enc_more). It lists the others in a work list, a third buffer: their numbers, counted by an atomic add, with the second pass's workgroups kept beside the count as an indirect dispatch reads them. The second is that indirect dispatch: each invocation takes a listed block, decodes its ASTC blocks again and gives it the whole search, so every group is at the same work. The blocks are the bytes one pass writes. Mode 6 alone and the quick build have no search past mode 6, and stay one dispatch.

On the GT 1030, a 2040x2040 level as BC7 (snx-test-astc-vulkan --bench FILE, the fast build), on levels one game loads (astc_dump=DIR keeps them as .astc files) repeated over it:

LevelOne passTwo passes
8x8, mode 6 holds every block18.5 ms20.3 ms
8x8, 18% of the blocks past mode 6392 ms94 ms
4x4, 512x512205 ms46 ms
8x6, 1000x800, mostly one colour89 ms13.5 ms
Random 4x4 blocks164 ms30 ms
Random 8x8 blocks220 ms46 ms

The fast build's second pass takes what the ASTC blocks say of each block (SNX_BC7_HINTS, astc_bc7.glsl), in place of the search's own choice of partitions and rotations. Texels of one ASTC partition lie on one line, so a block of one such set takes mode 6 alone; a block of two sets (two partitions, or two ASTC blocks' texels) takes the partition of BC7's 64 with the fewest texels on the wrong side, found by counting bits where the search sums 128 partitions' spreads; a second plane names the rotation of modes 4 and 5. More than two sets, two planes of different channels, or a partition more than two texels from every one of BC7's leave the block to the whole search. It took the two levels above with hard blocks from 95 to 67 ms and from 69 to 51. It costs PSNR: over 11 of that game's levels 51.13 dB against the ASTC blocks' own texels where the search alone gives 51.27 (the full build 51.70, mode 6 alone 47.52), and one level of text lost 1.8 dB (snx-test-astc-bc7-glsl --measure FILE... gives each build's PSNR, the modes written and the CPU's seconds). The full build keeps the whole search. With the hints the fast build's blocks are no longer the encoder's of the decoded texels alone, so the tests hold the GPU's blocks to the same shader run on the CPU over the same ASTC blocks (tests/glsl/astc_bc7_cpu.cpp); vulkan_headless reads those blocks back from what the texture keeps.

One more cut is an option of the build that no build takes: SNX_BC7_QUICK (bc7_encode.glsl), with which mode 6 first takes its box's line with one choice of p-bits and each texel at the weight nearest its place, and keeps it under an error. Under 64 it took the first pass of a level mode 6 holds from 18.5 to 12.8 ms for 0.3 to 0.8 dB, and made the levels with hard blocks no faster.

Approximate, Exact and BC7 at load

astc_textures (a startup setting) says how a GPU that samples BCn but not ASTC holds the ASTC textures it decodes, from the start of the game to its end:

ValueNameHeld as
0Approximate, the defaultRGBA8 at once, then BC7 made between frames and the RGBA8 image freed, a byte a texel; by what the blocks hold (below) BC1, BC4 or BC5
1ExactRGBA8, as the game's blocks decode, four bytes a texel
2BC7 at loadBC7 as each texture loads, with no RGBA8 image before it

Memory is what a desktop GPU runs short of. As RGBA8, one game's textures took 5.7 GB of a 5.7 GB budget and were evicted and uploaded again every frame (600 to 1000 ms frames); as BC7 they took 2.5 GB. A mode that held them Exact while the memory had room and turned BC7 once it filled (astc_mode's Auto, which astc_textures took the place of) kept the GPU's memory full of RGBA8 that was never encoded, and turned back and forth: 32 turns in one racing game's race, each reading hundreds of MB of textures again, and the frames at the turn the slowest. BC7 from the start encodes each texture once and frees its RGBA8 image as soon as the BC7 one is made. BC7 is not exact (its PSNR is below), mostly seen in UI text; Exact keeps them exact for whoever chooses it.

BC7 at load makes the frames that load new textures wait for the encoder. In one Ounce game's race on an RX 9070 XT (14.4 GB budget), it made 54 frames of 50 ms or more where Exact made 7, the worst 196 ms against 114 ms: its pace waited for the GPU in the draws that loaded new parts of the track (45 to 134 ms each).

Its line says the mode as the device starts:

vulkan: astc_textures=0 (Approximate): ASTC textures are shown at once decoded to RGBA8 and encoded again as BC7 between frames, the RGBA8 image freed then, a quarter of its memory; those whose blocks hold luminance alone are BC4, luminance and alpha BC5, opaque colour on one line a 4x4 block BC1

Held as RGBA8 by Exact (astc_textures=1), the log suggests astc_textures=0 (Approximate) once a run, with the budget, the use and the megabytes of ASTC textures decoded to RGBA8, when the images run short (surface.c's astc_bc_suggest): an image allocation finds no memory, a trim leaves the caches past their budget, or a trim takes what the last eight presents used (the working set, which the next frames load again).


Made from docs/layers/gpu/astc.md at 69237645142c. GPL-3.0-or-later.