Ian Guthridge.com

Code Sample

Runtime Compression Round 1

by Ian on Dec.24, 2010, under Code Sample, Projects, Sticky Situation

Ok, “A Sticky Situation” was written fast; the bulk of the coding was completed in 10 weeks. So logically its code base has a “few” flaws. The one that brought me here to write this post is located in its runtime texture generation: all the textures created where uncompressed.

Uncompressed textures aren’t necessarily a bad thing, but ASS was pushing a fair number of large unique textures to the screen, eating up all the texture fetch bandwith on older and/or laptop cards. Many of these textures were generated at runtime (most commonly a texture was blurred). So I decided to look into compressing the textures.

Since the whole point is that I am trying to get back some texture fetch bandwith, I need to use a compression technique that is understood by the GPU. This leaves me with one choice: DXT. Specifically DXT5.

Ok I’m going to give you a quick explanation of  DXT right quick.  It is a type of lossy compression that nearly all GPUs “in the wild” can decode.  It works by:

  • taking a 4×4 block of pixels
  • then it uses some fancy math (like Principal Component Analysis) to find the 2 most important colors in the block.
  • It stores these colors as 16- bit values (5,6,5)
  • Then for each of the 16 pixels in the block it stores a 2-bit value that describes how far it is from each of the colors.

Now DXT compression comes in quite a few different flavors DXT1 and DXT5 are the most commonly used (as far as I can tell).  The only real difference between the two is in how they encode alpha.  DXT1 only supports “cutout” alpha (basically a 1 bit alpha, either it is opaque or it is transparent) while DXT5 supports  a gradient alpha.  I am not going to get into exactly how each encodes alpha for length reasons, but let me know if you would like to know more.

(continue reading…)

  • Share/Bookmark
3 Comments :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!