Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/ianguth/ on line 18

Strict Standards: Declaration of Walker_Comment::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/ianguth/ on line 0

Strict Standards: Declaration of Walker_Comment::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/ianguth/ on line 0

Strict Standards: Declaration of Walker_Comment::start_el() should be compatible with Walker::start_el(&$output) in /home/ianguth/ on line 0

Strict Standards: Declaration of Walker_Comment::end_el() should be compatible with Walker::end_el(&$output) in /home/ianguth/ on line 0

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GoogleSitemapGeneratorLoader::Enable() should not be called statically in /home/ianguth/ on line 339
Runtime Compression Round 1 | Ian

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.

So after much searching I found a nice little paper about a Real-Time DXT compression.  It had code samples, and I couldn’t find anyone who had implemented the DXT5 compression in C#, so I decided to go ahead and port it!


Porting it took a while, and I had some issues… mostly with making it multi-threaded… It works now, and it looks pretty good! If a texture is too blurry (like the back grounds), then there will be a very distinct loss in quality but that is just a limitation of DXT. The compressed foreground objects look rather decent though (such as the platform the gum is standing on).

Now I hate it when people talk about a cool thing they ported and then refuse to share the code. So here it is, enjoy! (Just a heads up, it needs XNA 4.0 to work).

Hope this is helpful! Let me know if you have any problems/suggestions/questions/etc.

  • Share/Bookmark
:, , , , ,
3 comments for this entry:
  1. Jaska

    Hi, this seems to be exacly what i need.. but the sourcecode part is not visible! :(

    Would it be possible for u to send the source for me by mail? I would appreciate that a million times!

  2. joan

    Hi, It’s very interesting, XNA games on windows sometimes use too much memory. Where is the code link btw :P

  3. Ian

    Sorry. It looks like my Pastebin expired. I haven’t had much of a chance to moderate this site lately. I’ll update it as soon as I can.

Leave a Reply

Powered by WP Hashcash

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!