As ASTC

ETC2 RGBA8 holds less than BC7 does (below: 5.5 to 11 dB lost against the picture), and the GPUs that sample ETC2 and not BCn sample ASTC too, whose 4x4 blocks are the same bytes a texel. With bcn_astc=1, the default, BC2, BC3 and BC7 are encoded again as ASTC 4x4 there: texconv.comp built a third time with SNX_TC_ASTC, the BCn decoders followed by gpu/texture/astc_encode.glsl, a pipeline of its own (vk/texconv.c). BC4 and BC5 keep EAC.

BC1 is half ASTC 4x4's bytes, and the ASTC encoder is several times the ETC2 one's speed, so BC1 is tiered: ASTC 4x4 while memory lasts, for the loads' sake, and ETC2 RGB8A1 once it runs short. Short is what the trim finds (surface.c: an image that found no memory, the caches past their budget after a trim, or a trim that took what the last eight presents used); vk_bc1_tight then sets g_vk.bc1_tight for the rest of the run and the log says so once. A texture keeps the format it was made with, so nothing is encoded twice: the BC1 textures made from then on are ETC2, and ETC2 takes ASTC's place as the trim evicts textures and the game loads them again. bcn_astc=2 keeps BC1 as ASTC whatever the memory, and 0 sends all four to ETC2. No texel is written out between the decoder and the encoder: an invocation decodes a block and encodes it.

The fitting is the BC7 encoder's (bc7_encode.glsl built with SNX_ASTC_ENC, ASTC compute uploads): ASTC interpolates as BC7 does, ((64 - w) a + w b + 32) >> 6, with the same weights of 2 and 3 bits, so a subset's line is fitted by the same code, packed bytes and all. What differs is what a block's 128 bits buy, which is what the candidates are:

How many values an end's channel takes is not in the block: the decoder works it out from the bits the header and the weights leave, as the largest encoding that fits. So each layout has exactly one, and the ends are quantized to it while they are fitted. The encodings with a trit or a quint are worked out, with no table (astc_quant.glsl): an encoding's values come in two halves that mirror each other, and in a half the value of rank r is a multiply and a shift (the decoder's own rule), while the rank of the value nearest a colour is one more multiply and shift, with constants found by search over every colour. The bits five trits or three quints share are the decoder's rule turned round, a few selects. A table of 768 words read at a colour is a cache line a read, and an array a shader indexes is memory mobile GPUs are slow with; the arithmetic costs the search pass about 6% on a GT 1030 and the pass most blocks take nothing. tests/glsl/astc_encode_glsl.cpp holds every answer to the decoder's rules. The one table left is the partition seeds' (astc_encode_tables.glsl, from tools/astc_tables.py), 64 words that only blocks of two partitions read.

The source's own lines. A BCn block is lines already, so the encoder is told them (as_hint, set by texconv.glsl as it decodes the block) and tries them before any search:

SourceTaken as
BC7 mode 6one line: an opaque block's as RGB ends with 4 bit weights, any other's as RGBA ends with 3 bit weights and with 4, the better kept
BC7 modes 4 and 5two planes, the rotated channel apart; an opaque block's with RGB ends (160 values an end, where RGBA ends have 48); with alpha apart and every alpha 255, one line as RGB ends with the source's 2 or 3 bit weights and 8 bit ends
BC7 modes 1 and 3two partitions, BC7's own partition number (ASTC_P2 is in BC7's order), each subset's line the source's
BC2, BC3two planes, alpha apart, the colour's line BC1's two ends; opaque, one line as RGB ends with 2 bit weights and 8 bit ends, which hold BC1's ends and weights as they are
BC1that one line; a block of three colours, whose third is halfway, with 3 bit weights; one with a transparent texel (black, off the line) nothing
BC7 modes 0, 2 and 7nothing: three subsets, and two with alpha, have no layout here

A hinted line is taken as it is: its ends quantized to the layout's colour encoding and each texel given the weight nearest its place, one pass of the texels where the search fits a box and least squares over several. Its error is still measured on the texels, so a hint the layout holds badly (ASTC's partition is not always BC7's, and two partitions leave an end 40 values) costs time and little else: past SNX_ASTC_HINT_ENOUGH (a squared error of 1024 a block, 4 a channel as a root mean square) the block is searched for, and the search takes the hint as well, the source's partition fitted in place of 64 scored, and no partition at all for a source of one line. The line was 256 at first, the BC7 encoder's fast build's: the blocks searched for were then 6% of one game's level and nearly all of its time, and what the search finds for them is seldom much nearer. At 1024 that level takes 3.5 ms where it took 6.2, for 0.27 dB.

No texel decoded. Some BCn blocks are an ASTC layout already, field for field, and those are moved, not encoded (gpu/texture/astc_direct.glsl): the ends are read out of the block and written in ASTC's encoding, and the weights are moved as the 32 or 64 bits they are, with the bit BC7 leaves out of its anchor index put back and BC1's indices put in ASTC's order by two masks. Nothing is fitted and nothing measured.

This pass has no arrays and reads no table. GPU drivers each have a kind of memory they are slow with: an array of the invocation's own indexed by a value (scratch memory on Adreno and Mali), or the workgroup's shared memory (fewer workgroups in flight on NVIDIA). So the block is its four words in registers, a field is cut out of them at a bit that is a constant of its mode, an end is the word of its four channels, the ends' codes are two words of a byte each, and the ASTC block is four words put together with shifts. The compiled pass declares no array but the work list's head.

SourceMoved asOff by, a channel
BC7 mode 6, opaqueRGB ends of 192 values, the same 16 indices of 4 bits1.0 on average on random blocks, 7 at most: ASTC's 4 bit weights are BC7's within 1 of 64
BC7 mode 6 with alphaRGBA ends: of 48 values with the 4 bit indices where the ends lie more than 40 apart, else of 192 with 3 bit weights, an index's top three bits (the nearest 3 bit weight for all 16)within the same 7
BC7 modes 4 and 5, one alpha, no rotationthe colour's line with its own 2 or 3 bit weights, 8 bit ends0: the weights are BC7's and the ends fit
BC7 mode 5, a rotation onto a channel 255 at both endstwo planes of an opaque block, RGB ends of 160 values, both streams of 2 bit indicesan end's step
BC1 of four colours, alone or under BC2's or BC3's alpha where it is 255 throughoutRGB ends of 8 bits, BC1's own, 2 bit weights0.28 on average, 4 at most: BC1's thirds as the Switch's GPU rounds them against ASTC's 21 and 43 of 64
any of them with one colour (ends alike, or every index alike)a void extent0

Three passes (SNX_ASTC_PASS, texconv.glsl's ASTC main), as BC7's are made in two (ASTC compute uploads): a GPU runs invocations abreast at the pace of the slowest, so each way of making a block is a dispatch of its own. With hints and the search in one dispatch the hints made a GeForce GT 1030 slower, a third of the operations though they are.

  1. Over the strip's blocks: those above, moved. The others are listed, by what the block says it is: with lines of its own to try (BC7's modes 1, 3, 4 and 5, BC1's blocks of three colours with no transparent texel, BC2 and BC3 with alpha), or without.
  2. Over the first list: the block decoded and its lines tried (the hints above); what they don't settle joins the second list.
  3. Over the second list: the block decoded and searched for.

The second and third are indirect dispatches, as many workgroups as their list has blocks (the work lists, a third buffer). The blocks are the bytes one invocation doing all three would write, which is what the tests run on the CPU.

The second pass keeps no arrays either (SNX_NO_ARRAYS), for the same GPUs the first is written for:

Its compiled SPIR-V declares no array but the work lists' head, and executes no floating point for the layouts it writes. On a GT 1030 it is as fast as with arrays or a little faster for BC7 (a 2048x2048 level of random blocks 3.8 ms where it took 4.2, a game's 2.4 where it took 2.9) and a tenth slower for BC3 (5.5 where it took 5.0); nothing is measured on a phone yet. It is twice the size, each texel's decode being written out 16 times. The third pass, the search, keeps its arrays: it is the fitter the BC7 encoder shares.

What it gives, on nine levels of two games' textures as BC7 blocks (the BC7 encoder's fast build of each) made ASTC (snx-test-astc-encode-glsl --measure FILE.astc...): 50.67 dB against what the BC7 blocks decode to. Every block decoded, its lines tried and held to 256, gave 51.31, and searched for with no hint 50.29: moving the blocks and the wider line cost two thirds of a dB of what the hints had gained. Against the textures themselves 47.58 dB, where the BC7 blocks are at 50.55.

Time, on the GT 1030 (which samples neither ASTC nor ETC2, so only the conversions run): a 2048x2048 level, 262,144 blocks, its blocks in device memory and its output left there, the dispatches alone (snx-test-astc-vulkan --bench-etc2 FILE.astc; read back over the bus, as that bench did at first, the output's 4 MB took another 10 ms and hid all of this):

Level, as BC7 blocksDecoded to RGBA8As ETC2 RGBA8As ASTC 4x4
8x8, smooth: every block mode 61.9 ms96 ms0.6 ms
8x6, mostly one colour2.2 ms78 ms1.2 ms
6x62.8 ms150 ms2.6 ms
4x4, with alpha2.3 ms134 ms2.7 ms
8x52.4 ms135 ms3.0 ms
8x8, 18% hard blocks4.9 ms167 ms3.5 ms

Random blocks, which nearly all take the search: BC1 26 ms (ETC2 172), BC3 36 ms (186), BC7 88 ms (198).

What it does not have yet: three and four partitions, two partitions with alpha, and blocks moved for BC7's modes 1 and 3 (two partitions of 2 bit weights are ASTC's too, where its partition is BC7's, but their ends have 40 values a channel), for mode 4 with a rotation and for BC1 with a transparent texel.


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