ohmy
ohmy

Reputation: 91

dxt4 texture compression

When performance are kept in mind, we (unwillingly) use texture compression. The artefacts introduced by the compression may be more or less acceptable. what are the different possibilities, workarrounds that can be applied on the original image level to minimize the artefacts introduced by the compression algorithm. In my current situation, most of the artefacts are seen when using gradients.

Upvotes: 1

Views: 994

Answers (1)

Damon
Damon

Reputation: 70126

DXT compression is all about interpolation, or gradients if you will. However, you must understand well what exactly it does. DXT compression is a compromise, it offers pretty bad quality at pretty bad compression, but it does offer some compression and is almost trivial to implement in hardware and runs at practically zero cost. That's why it is used.

There are a few means to improve quality, but if the quality issues are not not acceptable, the only solution is to not use DXT. (Besides, DXT4 which you have in your question's title is not very widely used, this is DXT5-premultiplied)

First of all, note that:

  • DXT encodes the colors of 4x4 blocks of texels by storing two 5:6:5 colors and interpolating along this line in RGB space. The interpolation is quantized to 2 bits, so you only have 4 values to choose from per pixel.
  • The alpha channel in DXT4/5 stores two 8-bit alpha values and uses 3-bit interpolators.
    • DXT2/3 uses an explicit 4 bit per texel alpha channel (i.e. not interpolating between some chosen 8-bit values).
    • DXT1 can feint an 1-bit alpha channel, but this is a sorting/encoding trick, and a different story.
  • It is impossible to get pure greys in DXT without converting to another color space (due to 5:6:5 storage of endpoints).

This means that DXT can in principle (more or less) perfectly reproduce many horizontal, vertical, or diagonal 1D gradients that do not have too harsh changes, but it is entirely unable to reproduce most other patterns (though it can usually reproduce something close).
For example, if you have a 2D gradient or a rotated gradient, there is no way (except by sheer coincidence!) that there exists a pair of two colors which will allow the entire 4x4 block to interpolate nicely. Also, since the interpolation is quantized to only 4 choices, the vast majority of "odd rotations" simply cannot be encoded, nor can many combinations of colors. However, for most "kind of natural" textures, this is acceptable.

The DXT compressor will usually make an attempt at finding a best possible fit within the 4x4 cell (though some compressors will/may do something else). This can lead to stepping in gradients even if the gradient inside the cell is represented well.

What you can do about DXT is:

  • Use different compressors and choose the best result. There are at least 3 different (non-brute force) strategies used by different compressors, giving quite different results.
  • Give the crunch library a try. There is a Windows GUI compressor for it around somewhere too. While crunch primarily aims to produce smaller (after zip compression) files, and it is finally bound by the technical limitations of the DXT block format, it uses an unusual search technique that takes much greater ranges into account. It might happen that it manages to give one or the other of your gradients a better look.
  • Avoid gradients that are 2D or that are not aligned to u/v or diagonal because you know that it is impossible to encode these.
  • Convert natural images to a non-RGB color space before compressing. YCoCg and YCbCr may be candidates, or the JPEG-LS transform. The human eye is not equally sensitive in every respect. Some errors are less obvious. Using a different color space exploits this.
  • Use the alpha channel for the most important channel if you don't need alpha, since you know that it has 8 bit endpoint resolution (instead of 5/6) and 3 bit interpolators instead of 2 bit. Otherwise, use green for the most important channel since it has one more bit.
  • For hand-drawn textures, you may try to use 5:6:5 matched colors, which will at least give perfect hits for these. Though for "drawn" textures (something that looks like a comic strip) DXT is a very unwise choice in general.
  • For cases where the quality just doesn't cut it, don't use DXT (yes, this sounds like a stupid advice, but it is really that... if it doesn't fit, don't use it).

Upvotes: 4

Related Questions