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.
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).
0xC88C), and
the unquantization constants per bit count. A sequence's unquantization is chosen once per block, never per value.b bits to 8 or 6 is (v * m) >> s with m and s from a
table, as is the trit and quint formula's B term. The color classes of all partitions are summed with one
multiplication by 0x1111, and a mode shared by all partitions is replicated by one.b * n + extra, which equals the
search because the lengths only grow with the encoding. Trits and quints are decoded a block at a time from
constant field offsets: 3 quints from one word, 5 trits from two. Bits are decoded as many to a word as 31 bits
hold. The stream is shifted down once per such group.vk/astc.c makes one pipeline per footprint, so the weight infill
steps (Ds, Dt, each a division) and everything else that depends on the footprint are constants. Dividing a
tile's texel coordinates by the footprint is a multiplication by a 17 bit reciprocal, exact for every footprint
and coordinate (checked).(e0 * (64 - w) + e1 * w + 32) >> 6, which was checked for every endpoint pair and weight: the UNORM16
expansion and rounding and the sRGB mode's (e << 8) | 0x80 expansion both reduce to it, so the two modes differ
only in void extents. R and B share one word, G and A another, in 16 bit lanes: each word is one multiply-add,
e0 * 64 + 32 + (e1 - e0) * w, whose lanes never carry into each other, the negative differences being exact
modulo 2^32. The second plane's channel adds its own difference times the difference of the weights, which is 0
with one plane.(c * 255 + 32895) >> 16, which equals
(c * 255 + 32767) / 65535 for every c up to 65535 (astc_glsl checks all of them), without the division.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:
| Shader | 4x4 | 6x6 | 8x8 | 12x12 |
|---|---|---|---|---|
| Eden's port | 161 to 172 | 110 to 128 | 97 to 110 | 78 to 92 |
| Block per invocation, texel loop (the previous version) | 63 to 76 | 45 to 49 | 42 to 48 | 40 to 43 |
| Tiles of 64 blocks (this one) | 111 to 133 | 87 to 110 | 79 to 97 | 77 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.
blocklinear.comp (docs/layers/gpu/blocklinear.md). Eden's shader deswizzles in the shader instead. That part
was left out: the deswizzle is one shader for every format.width words.width and height (the size of each slice of the strip in texels), blocks_x and
blocks_y (the same in blocks), srgb, nonzero for an SRGB image (the texture header's sRGB bit, which
vk/astc.c reads from the surface's format), and tile_shift: a tile is 1 << tile_shift blocks wide and
64 >> tile_shift high. vk/astc.c picks the shape that leaves the fewest slots of the strip empty, the widest
of equal ones, and dispatches the strip's tiles across, down and its slices: a workgroup's z is its slice in both
buffers. The footprint is the specialization constants 0 and 1. Texels beyond the image are not stored.Eden's shader stores floats into a storage image. Here the conversion to 8 bits is explicit:
(c * 255 + 32767) / 65535.
This is what storing c / 65535 into an RGBA8 image gives.(e << 8) | 0x80 instead of by bit replication (e * 257), and the decoded byte is the top 8 bits of the
interpolated value, which the specification hands to the sRGB conversion. Alpha decodes as in UNORM. An LDR
void extent's R, G and B are the top 8 bits of its UNORM16 color, its alpha rounds as in UNORM. The bytes are
still copied without a gamma step: the image's SRGB format applies the gamma when the texture is sampled.
The two modes differ only in void extents, by at most one step: for interpolated texels both expansions give the
same byte (see above).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):
bits * 64 / (2^n - 1) instead, which differs for 4 and 5 bit weights.Ds and Dt are computed in integers, as in Eden's CPU decoder. Eden's shader uses floats,
with the same results for the block sizes ASTC has.Made from docs/layers/gpu/astc.md at 69237645142c. GPL-3.0-or-later.