Skip to content

HowItWorks

Rudolf Polzer edited this page Jul 18, 2011 · 15 revisions

S2TC - How It Works

A simple explanation of S2TC is given here for the simplest case - the DXT1 format, with no alpha channel.

We are working with this image, which was especially designed to compress badly with block compression schemes:

The original image (768KiB in RGB, 1024KiB in RGBA)

Step 1 - Convert To RGB565 (16bpp)

Experience showed that doing the RGB565 conversion as a step prior to the actual compression can yield better results than doing it during block compression, and it also yields faster running code if you want dithering. As we sure do want dithering, we do it here.

The reason why doing it here yields better result, is that doing dithering here tends to avoid "high amplitude" dither patterns, which may happen if dithering took place during the block compression. But if one does it this way, it basically leads to "high amplitude" blocks to not use dithering, and "low amplitude" blocks to retain the dither from this step, which tends to look better and clearer in the end.

Now reduced to 16bpp

Step 2 - Divide The Image Into Blocks

The image is split into 4x4 blocks.

Now divided into blocks

Step 3 - Palette-Reduce The Blocks

Each 4x4 block is reduced to a 2-colors palette. It is crucial to select these two colors well; this is the selection GIMP would use for color reducing:

Original blocks --> Reduced to 2 colors

The reference compressor uses a different color selection method which works better at retaining detail, yielding these colors:

Original blocks --> Reduced to 2 colors

Step 4 - Encode The Blocks

For each block, 64 bits are stored as follows: 16 bits for the first palette color, 16 bits for the second palette color, and then 2 bits for each pixel (which always are 00 or 01, when no alpha channel is involved). For further details on this, see FileFormats.

Compressed image (128KiB in DXT1)

Of course this does lose some visual quality, but reduces graphics memory utilization by factor 6 (or factor 8, if we upload in RGBA format which generally performs better due to aligned memory access). This reduction allows us to use more detail on textures or geometry, which will be more than enough to compensate for the quality loss.

Importance Of Good Color Selection

How important a good compression algorithm, especially the selection of the palette colors, is, can be seen by comparing to S3TC compression tools. These have more degrees of freedom to encode an image, thus they should always get better quality.

NVidia Texture Tools's nvcompress tool for example would yield this on the same input image:

nvcompress (128KiB in DXT1)

This looks clearly worse than what S2TC produces, mainly because nvcompress does a poor selection of the reference colors!