By what the blocks hold

BC7 (astc_textures=0) also reads what each ASTC texture's blocks can hold, on the GPU after it is made, and holds it in the smallest BCn format that holds that: luminance alone as BC4 (8 bytes a 4x4 block, an eighth of RGBA8), luminance and alpha as BC5 (16, as BC7), opaque colour on one line a 4x4 block as BC1 (8, half of BC7). The rest is held as BC7. Exact (astc_textures=1) and BC7 at load (2) are as they were, and so are 3D textures and textures with holes in their mapping.

The kind (gpu/texture/astc_kind.h) comes from each block's header, its colour endpoint modes and, for the RGB and RGBA modes, its endpoints' values, not from its texels: luminance modes (0, 1) decode R = G = B with alpha 255, luminance and alpha (4, 5) R = G = B, the RGB ones (6, 8, 9) alpha 255, each partition a line through its two endpoints. A partition of an RGB or RGBA mode (6, 8, 9, 10, 12, 13) whose two endpoints, made from its colour values as the decoders make them (the integer sequence, unquantized, then blue contraction, the base and offset's bit transfer and clamp), have R, G and B within a level of each other (SNX_ASTC_GREY_SPREAD) counts as luminance, or luminance and alpha: its texels are grey within two levels, which the BC4 and BC5 views give back as R. Encoders write grey there too: astcenc's -normal encodings of two normal maps (X in R, G and B, Y in alpha) used the RGBA direct mode (12) in 17% and 0.1% of their blocks, which read by mode alone would have kept them from BC5, and 8 and 16 of their texels in a million and four are a level off grey. A second plane on R, G or B weighs that channel apart, two partitions of colour are two lines, and an HDR mode, an illegal block or a void extent with a reserved bit off decode as the error colour, which takes the texture to the usual format. Every test the decoders (tests/support/astc.c, astc_decode.glsl) make a block the error colour by is made here in the same order. BC1's one line a block also needs each 4x4 block inside one ASTC block: at 5x5, 6x6 or 10x8 a 4x4 block takes the texels of two to four ASTC blocks, each a line of its own, and on random blocks BC1 erred by up to 141 levels there against 39 at 4x4, 8x8 and 12x12, so colour goes to BC1 only at those three footprints.

The walk. The kind is that of a walk over every level of every layer as guest memory has them, through the block linear layout (or pitch rows), the layout's padding left out, which stops at the first block that leaves no BCn format (snx_astc_level_kind in tests/support/astc_kind_ref.h, then the same over the levels). The CPU made that walk as each texture was made, and in one game's race, which loads new parts of the track as it goes, it took 58 to 78% of the GPU thread in the bursts of new textures, 21% of it over 15 s. Now a new texture goes up at once as one of unknown kind (BC7 between frames), and its upload keeps its ASTC blocks on the GPU beside its image (vk_astc_keep, also for a level the BC7 cache gives, whose blocks are staged only to be kept). astc_kind.comp (its body astc_kind.glsl, snx_astc_block_read in the part of GLSL that is also C++) reads them, a dispatch a level of every layer, an invocation a block, and leaves four words a level of each layer: for each kind bit the first block in the walk's order that has it, its number in the layout (y * nx + x in pitch rows; in block linear the GOB's number times 32, then the block's row and column in its GOB), a workgroup's least in shared memory and then one atomic a word. Those words come back through the frame's readback ring, as the BC7 cache's blocks do, and the present after the submission is done (vk_tex_kind_frame) makes the walk's kind of them with no block read: snx_astc_level_kind_firsts takes the bits in that order, a block's together, until they leave no BCn format, and OTHER alone where the layout ends first (snx_astc_level_stop), as the walk returns it there; then the levels in the walk's order until that too. So the kind is the walk's, its early stop included, not merely the or of every block. Where it takes BC1, BC4 or BC5, an image of that format is made from the kept blocks (vk_astc_bcn_kept, the strips an upload straight to it makes) and takes the image's place, after the BC7 encoder's and the background's work on the texture is dropped; the kept blocks go either way, unless the BC7 encoder still makes the texture's BC7 image from them. A texture written before its read is back is read again from the blocks its new upload kept (four reads at most). The kinds of bytes read before stay in a memo by the hash of the bytes and their layout, which the change checks take anyway: a texture evicted and made again goes up straight to its format, as all did when the CPU read them. A sparse texture's blocks are not read, nor those of one with no room to keep them: it is held as BC7, as the rest.

The kind's arithmetic. astc_kind.glsl was written as snx_astc_block_read is, then made cheaper by forms that ucas proved equal to it, a function at a time with its inputs as parameters (a prelude of plain globals makes it report values as constant that are not), each equiv exhaustive or by SAT over the inputs the callers give, and the GLSL test held to the CPU's read after each:

sRGB. BC4 and BC5 have no sRGB formats, and their UNORM image would give the shader the sRGB codes unconverted. So an sRGB texture of luminance goes to BC1's sRGB format, the grey line in 565 (a level or two of tint where R and B have 5 bits and G 6), and one of luminance and alpha keeps the usual format.

Sampling. BC4 holds the texels' R and BC5 their R and alpha, in its R and G. The view gives them back as the game's blocks decode (render.c's tex_map): the header's R, G and B read the image's R, its alpha 1 (BC4) or the image's G (BC5), and its constants stay. BC1 is opaque: its blocks are four colours, the first end the greater, or both ends one with every index 0, so no texel decodes transparent.

Encoding. The encoders are a pass each, a fraction of the decoder's cost, so such a texture is made in its format in one frame (astc_bcn.comp, its body astc_bcn.glsl over bcn_encode.glsl, in the strips BC7's upload makes), from its kept blocks after its kind is back, or straight as it loads when the memo knows its bytes. BC4: the least and greatest value as the ends, eight values, then the same pulled in by a sixteenth and a least squares fit to the steps the texels took; BC5 is two of them. BC1: the bounding box's line, each channel turned to run with the widest (the sign of their covariance), as it is and pulled in by a sixteenth, then a least squares fit to the colours chosen; a block of one colour takes, a channel at a time, the 565 ends whose colour a third of the way between comes nearest it. Integer arithmetic in the part of GLSL that is also C++, so the tests run the same file on the CPU and get the GPU's bytes.

Written again. A texture held so and loaded whole again may hold more now, which its format would not show, and the CPU reads none of its blocks (kind_again): an image of the format one of unknown kind is held in (BC7) takes its place, which the load fills, its blocks are kept, and the GPU reads their kind as for a new texture, which takes it to BC1, BC4 or BC5 again where the kind says so. Pieces load it whole, which reads them: a piece of other blocks would show in a format that does not hold them. A texture held as usual is not read again. Such a texture is never sparse for its levels alone (texture_shed, texture_half_res): the image that takes its place is a whole one.

The log says the format and what the blocks hold for each texture held so (the first 32), each texture that turns when loaded again (16), and with the stats of every 600 presents the textures read by outcome, the CPU's time, the levels and megabytes of each format against RGBA8's, and those read again:

vulkan: a 128x96x1 ASTC 8x8 texture of 8 levels holds luminance alone: held as BC4 (read in 22 us)
vulkan: a 128x96x1 ASTC 8x8 texture held as BC4 is loaded again with blocks of luminance and alpha: held as BC5 from now on

What it costs, against the texels the game's blocks decode to (the CPU decoder's), as the view gives them back (snx-test-astc-bcn-glsl --measure [--srgb] FILE.astc..., the shaders' bodies run on the CPU, the GPU's bytes): PSNR over RGBA, and the largest error of R, G, B and A in levels of 256. BC7 is the encoder's fast build on the same blocks, what astc_textures's BC7 gives them. The pictures are astcenc's -medium encodings of the Khronos glTF sample images astc-encoder's tests carry (MIT), and Amberlith's astcimage picture:

BlocksFootprintHeld asIts PSNR, largest errorsBC7's
A 1024x1024 occlusion map (luminance)4x4BC454.36 dB, 12 12 12 059.89 dB, 5 5 5 0
6x6BC454.86 dB, 12 12 12 059.96 dB, 7 7 7 0
8x8BC456.27 dB, 10 10 10 059.74 dB, 5 5 5 0
12x12BC457.47 dB, 10 10 10 059.71 dB, 5 5 5 0
The same map encoded as sRGB4x4BC1 sRGB46.81 dB, 24 22 24 059.90 dB, 6 6 6 0
6x6BC1 sRGB46.88 dB, 27 28 27 059.96 dB, 7 7 7 0
8x8BC1 sRGB47.49 dB, 24 22 24 059.68 dB, 5 5 5 0
12x12BC1 sRGB47.66 dB, 22 23 22 059.67 dB, 5 5 5 0
Amberlith's gradients, 64x648x8BC145.33 dB, 5 4 5 053.57 dB, 2 2 2 0
The same, sRGB8x8BC1 sRGB45.25 dB, 5 4 5 053.72 dB, 2 2 2 0
A 1024x1024 normal map (-normal: X in R, G and B, Y in alpha)4x4BC550.08 dB, 17 17 17 1446.67 dB, 29 29 29 90
6x6BC550.01 dB, 17 17 17 1447.61 dB, 37 37 37 102
8x8BC550.57 dB, 17 17 17 1448.95 dB, 67 67 67 101
12x12BC550.76 dB, 20 20 20 1451.18 dB, 32 32 32 108
A 2048x2048 normal map, the same4x4BC552.73 dB, 17 17 17 2050.63 dB, 28 28 28 74
6x6BC552.71 dB, 17 17 17 1751.73 dB, 29 29 29 66
8x8BC553.15 dB, 20 20 20 1753.01 dB, 30 30 30 78
12x12BC553.11 dB, 18 18 18 1653.83 dB, 35 35 35 69
Random blocks of luminance, every footprintBC441.08 dB, 17 17 17 0
Random blocks of luminance and alphaBC540.20 dB, 17 17 17 19
Random blocks of colour on one line, 4x4, 8x8, 12x12BC136.75 dB, 39 32 33 0
Random blocks of luminance, sRGBBC1 sRGB34.35 dB, 40 43 40 0

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