Thursday, June 21, 2007

Compressing Textures.....the DirectX way

Compression of textures is something that DirectX can take care of for us, but it makes sense to understand how it does this.

Consider the following gradient:

If you were required to store this in a texture, you would end up having to store hunderds of RGB values to represent it If you instead stored say 2 colors (the ends) you perform linear interpolation to get the remaining colors and you have are only storing to values. A crude calculation to do just that is here:

Color_Gradient = (Color2 - Color1) / (GradientWidth - 1);
for(i=0; i <>
SetPixelColor(i, 0, Color1 + (Color_Gradient * i));


This is roughly how DirectX can compress textures resulting in massive savings. Of course on a big texture you will probably have more than 2 colors and a simple bar object, so DirectX will partition the texture into a 4 x 4 matrix and based on the Texture Compression state chosen, will use either 1 or 2 of these grids (one for RGB and one for ALPHA) or just one and two RGB color values. The specifics of how the interpolation is performed again is dictated by the state of the Texture Compression chosen. More to come on this..

No comments: