Uploading straight to BC7 held the game for the encoder: the encoder makes about 10 million blocks a second on an
RX 6700 XT, and two arrays coming in one frame cost 30 to 40 ms of it, frames of 50 to 90 ms, where RGBA8 left none
over 50 ms on the same route. So a texture held as BC7 goes up decoded to RGBA8, which the game samples at once, and
texture.c (vk_tex_later_frame, after each present, in a submission of its own that the present doesn't wait for)
makes its BC7 image from the texture's ASTC blocks. The texture keeps them on the GPU beside the RGBA8 image
(vk_astc_keep): each strip the decoder reads is also copied into its place in a buffer of the texture's own, a
region's rows too, a quarter of the RGBA8 image's bytes at most (4x4 blocks; a ninth for 6x6). Guest memory is not read
again: the game may have written or unmapped it, and the BC7 image has to hold what the RGBA8 one showed.
vk_bc7_encode_kept runs astc_bc7.comp on a strip of them, as an upload does, so the blocks are the same an upload
straight to BC7 makes. The buffer goes once the BC7 image is whole or the texture is freed; a texture that finds no
memory for it stays RGBA8. A frame encodes VK_BC7_LATER_BLOCKS (32768) blocks at most: 3.3 ms of the
RX 6700 XT, a tenth of a frame at 30 fps. Its strips are whole slices of a level (an array's layers, a 3D level's
depth slices, as many as the blocks left take), or whole rows of ASTC blocks of one when a slice is more than that,
from a row that a row of 4x4 blocks starts on too; a frame's first strip goes whatever its size, and one that the
blocks left don't hold waits for the next frame. Whole levels one after another that the blocks left hold are one
strip (vk_bc7_encode_kept_levels), up to a level of another build or one the BC7 cache has: astc_bc7.glsl walks
from the first level to the one a block lies in, each half the one before (and as deep, a volume's), their ASTC
blocks one after another as the texture keeps them and their BC7 blocks one after another in the scratch buffer,
which one copy with a region a level writes into the image. A texture's small levels took an encode each, its fills,
copies and ten barriers, which cost the GPU about as much as one of thousands of blocks: in a race of one game 54% of
the strips were 256 blocks or fewer, 28% of the encoder's GPU time for 0.4% of its blocks. As one strip, that race's
160 s took 6767 strips and 5.5 s of the RX 6700 XT where it took 20028 and 9.8 s, and 6498 and 3.1 s where it took
20494 and 6.1 s with the choices the first run kept (every strip timed). Textures go in the order
they were loaded, one at a time. The step does nothing when vk_cmd would wait for the GPU first, so it never holds
the frame after the present.
Once the BC7 image is whole it takes the RGBA8 one's place in the texture, moved to the layout the RGBA8 one was
sampled in, and the RGBA8 image and its views go once the frames that sample them are done (vk_defer_image), three
quarters of its memory. Meanwhile the texture takes RGBA8's memory, a quarter more for the BC7 image being made, and
its blocks' bytes.
later_pieces_whole).A texture the game writes often is held Exact instead (later_wrote): written again before the encoder could make its
BC7 image and the game sample that a frame, that is no more frames after the write before than the encoder takes for it
(a frame for each 32768 blocks) and one, it leaves the queue and stays RGBA8, and its writes load as RGBA8 alone,
pieces too: as BC7 each write would drop the encoder's work, or encode the pieces where the game waits for them. A
write every frame is held so whatever the texture's size; a 2048x2048 one with its mips, 349,525 BC7 blocks and 11
frames of the encoder, is held when written every 12 frames or more often. Read unwritten for four times those frames and one, and 30
frames at least (later_cool), it is queued to be made BC7 again: a texture read much and written little stays BC7.
Every 600 frames the log says how many textures were made BC7 between frames, the RGBA8 memory that freed, the blocks encoded and the encodes started over, how many textures wait with their RGBA8 memory, and how many are held Exact for their writes, went so and were made BC7 again.
The encoder tries each block in the modes that suit it and writes the one with the least squared error. Every block takes mode 6, one line through RGBA with 4-bit weights, which holds a block of one gradient closely. An opaque block (every alpha 255) also takes the modes that store no alpha and so keep it 255: modes 1 and 3, two subsets of one of the format's 64 partitions, each with a line of its own (6-bit ends and 3-bit weights, or 7-bit and 2-bit), for an edge between two colours, and mode 2, three subsets, for three colours meeting. Any other block takes mode 7, two subsets with alpha. The partitions tried are the two of 64 two-subset ones, and the one of 64 three-subset ones, whose subsets spread least around their means, a pass over the block each. Last, every block takes modes 4 and 5, one subset whose colour and alpha are two lines, each with weights of its own, in each of the format's four rotations: alpha apart, or red, green or blue apart in alpha's place, as ASTC's second plane keeps one channel. Mode 5 stores the colour in 7 bits and alpha in 8, both with 2-bit weights; mode 4 the colour in 5 and alpha in 6, one of the two with 3-bit weights (the index selection bit says which). The colour's line is fitted first, and the alpha's only when the colour's error alone is still under the best block so far.
A subset's line starts from its bounding box, each channel turned to run with the channel the subset spans most (the sign of their covariance), pulled in by a sixteenth from each end and tried as it is too; then a least squares fit to the weights the texels took, twice, solved from whole sums, so a channel of one value keeps that value. The ends' p-bits are tried in every combination the mode allows, but a subset whose every alpha is 255 (or 0) takes the one bit that keeps it exactly: a nearly opaque texel blends, and a nearly clear one passes an alpha test of more than nought. Modes 4 and 5 have no p-bits and hold 255 and 0 as they are. Each texel takes the best of the three weights around its projection. All of it is 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.
The encoder is written for the code the GPU runs, not only for what it computes. Each partition is read from its
table once a shape (SetShape), into registers: the tables are arrays indexed by a value that differs between
invocations, which the GPU keeps in local memory, and reading them for each texel of each fit made a block with
subsets cost hundreds of times one without. Each body is called from one place, in a loop: the shapes and modes 4 and
5 are jobs of one loop in Encode, a subset's four candidate lines one loop in FitSubset, and the least squares
fit sums every channel in one pass. glslang inlines every call, and the copies came to 1.2 MB of machine code for
NVIDIA's driver; it is 145 KB now, and an 8192x8192 level that took 10.9 seconds takes 0.86. The blocks are the same,
byte for byte: the old and new files, run on the CPU over 65,536 blocks of noise, opaque noise, edges and two
colours with alpha, wrote the same bytes, and a broken anchor in the new one showed.
Nearly all of the encoder's time is Eval, which every line of every subset of every shape runs over the subset's
texels. A texel's place along a line is p.q1 - p.q0 - q0.(q1 - q0), two dot products of the words' four bytes and a
constant of the line, where it was four multiplications of unpacked channels. Its error at a weight interpolates all
four channels in two multiplications, two channels a word in 16 bit lanes as the decoder's texels are, and is
I.I - 2 I.p + p.p. The place's division by the line's squared length is a multiplication by its reciprocal in
floating point, the quotient made exact from the integer remainder, so no byte depends on the driver's rounding. The
blocks are the same bytes as before (the test's digests), and the GT 1030 encodes a quarter faster: 232 to 172 ms for
a 2040x2040 level of random 4x4 blocks, 312 to 232 ms of 8x8. Where the GPU says it has an instruction for the packed
dot product (Vulkan 1.3's shaderIntegerDotProduct with integerDotProduct4x8BitPackedUnsignedAccelerated),
astc.c takes four more builds of the shader that use it (SNX_BC7_DOT): 5% less again on that GPU (305 to 291 ms
of 6x6), the same blocks. ucas, asked for cheaper forms of the shader's functions under its model of that GPU, makes
the same sums dot products and finds under 1% elsewhere.
A block of one colour takes no search: Solid writes it as mode 5, colour ends c >> 1 and
(c >> 1) + (c & 1) - (c >> 7) at weight 21 of 64, which interpolate to c for every value of a channel. Mode 6
holds such a block only when its channels agree in their low bit or the odd ones are neither 0 nor 255 (an end's
p-bit is shared by its four channels, and no weight brings one channel to 0 and another to 255: opaque black), so
the search went through every partition and mode for it and could still end a step off.
A block stops trying modes once its squared error over all its texels and channels is SNX_BC7_GOOD_ENOUGH (16) or less,
a mean squared error of a quarter, 54 dB, for that block. A hard block still gets the whole search. Within a fit,
Eval leaves a candidate once its partial error proves it cannot win, and a second least squares runs only on
weights that changed. Quality tests compare against the prior unpruned search and require each block to stay
within the 16-error allowance or produce no more error than that reference.
What it costs, as PSNR against what it encodes, on Amberlith's astcimage picture (gradients, hard edges where three colours meet in a block, noise, and alpha) decoded from ASTC: 39.9 dB over the picture at 4x4 (gradients 53.1, edges 48.9, noise 34.2, alpha 50.9), 40.4 at 6x6, 46.2 at 8x8 and 45.2 at 12x12. Mode 6 alone gave 27.6 at 4x4, its edges 23.5. Noise is what BC7 holds worst: a block of independent texels is a line or two of them at best.
Made from docs/layers/gpu/astc.md at 69237645142c. GPL-3.0-or-later.