The shader

A workgroup of 64 invocations decodes a tile of 64 blocks in two phases. First each invocation decodes one 128 bit block into the block's slot of the tile in shared memory: the weights, the partitions' endpoints and the numbers that place a texel in the weight grid and the partitions. After a barrier the 64 invocations write the tile's texels together, one texel each at a time, in rows. The shader uses only storage buffers, push constants, shared memory and 32 bit integer arithmetic, with no floating point, so its output does not depend on the driver's rounding.

The block decoding is serial by nature (a sequence of fields of variable length), and one block per invocation runs it once per block. The texels are not: an invocation that also wrote its own block's texels, as Eden's port does, ran a loop of up to 144 texels whose stores land a footprint apart in each store of a subgroup, whose branches differ from invocation to invocation with the blocks' modes, and which leaves most of the GPU idle on a small texture or mip level. In the second phase each store of the workgroup is 64 neighbors in a row, the texel code has no branch but the image's edge, and a tile of few blocks still keeps all 64 invocations busy. The cost is that a texel finds again what the loop kept in registers: its block and coordinates, its partition, its endpoints' interpolation terms and the second plane's weight. That is about 110 operations and 13 shared memory reads a texel where the loop took about 45.

astc.comp holds only the declarations: the workgroup size, the footprint, the push constants, the two buffers and the tile in shared memory (39 words a block, 9984 bytes). The decoder is astc_decode.glsl, which it includes, written in the part of GLSL that is also C++ so that the tests run the same file on the CPU (see Tests). The rules are at the top of tests/glsl/glsl.hpp.

Written for the machine

Some mobile drivers neither inline across functions nor fold constants, unroll loops or strength-reduce, so the shader does that work in its source, and glslang's optimizer (-O) inlines everything into one function with no local variables: every value is an SSA register, and memory is touched only through the two buffers and shared memory (53 access chains, where Eden's port had 383 and 88 function-local variables).

On lavapipe, decoding a 4096x4096 picture encoded by astcenc (-fast) takes these milliseconds, including the copy into staging and into the image. Lavapipe runs on the CPU: it counts operations, and none of what the tiles are for (coalesced stores, invocations that don't diverge, a busy GPU on small textures) shows there, while its barriers cost extra (it switches between a workgroup's invocations at each). The tiled version measured 107, 68, 66 and 67 milliseconds with its barrier removed:

Shader4x46x68x812x12
Eden's port161 to 172110 to 12897 to 11078 to 92
Block per invocation, texel loop (the previous version)63 to 7645 to 4942 to 4840 to 43
Tiles of 64 blocks (this one)111 to 13387 to 11079 to 9777 to 105

Which of the last two is faster on a GPU is not measured: the tiles do more arithmetic per texel, and win back what scattered stores, divergence and idle invocations cost there.

Interface

Eden's shader stores floats into a storage image. Here the conversion to 8 bits is explicit:

Where it differs from Eden

Eden's two decoders do not agree with each other on every block, and both accept blocks that the ASTC specification treats as errors. Both ports make the same choices, checked against ARM's astcenc (see below):


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