Author Topic: a bunch of maybe usefull info about dxt texture compression  (Read 3058 times)

0 Members and 1 Guest are viewing this topic.

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
a bunch of maybe usefull info about dxt texture compression
this sorta pertains to the dxt compression used in compressed dds texture. so its sorta useful information to have. i dont really want to start a dds vs tga debate, i just want to improve my understanding about how dxt actually works and thus improve my modding capability.

well everyone associates dxt with microsoft because its got dx in it. while in fact its really an s3 texture compression format format aptly named s3tc (duh). anyway i was trying to fix some problem with dos quake and ended up reading a ton of material regarding how texture compression actually works. i red that john carmack perfers to store some of the normal mapping uv data in the alpha and green channels of dxt5, rather than using a dxt1 or an uncompressed texture. this got me curious as to why. so i thought id look up how DXTx actually stores data.

first of all, all this time i though it was storing 8 bits a channel no matter what and just works some magic to make it smaller. i was wrong. seems that only a pair of 16 bit colors were stored in a 565 format per each block of 16 pixels. this came as a shock to me. because ive worked with 16 bit textures before and they are butt ugly. so i got even more intrested in what it was doing behind the scenes. how the hell does this look so damn good?

now there are only 2 colors actually stored for each 4x4 block. but then each pixel also has a 2 bit index value, a number 0-3 to indicate what pixel color to use. so essentially in stores each of these blocks as indexed color with each one being 1 of 4 different colors. now that would indicate that we should store 2 more pixel colors. rather than storing 2 more colors, the other 2 are interpolated based on the 2 that are stored. i messed around with a pen and paper trying to understand the math that was posted in the wikipedia article. i think i got it but im not sure i got it right.

there are a couple formats which are mysteriously missing from the photoshop plugin, which are dxt2 and dxt4. these are counterparts to 3 and 5 respectively. the even numbered formats seem to have what is called premultiplied alpha, which if im not mistaken means that the rgb values are darkened according to alpha from the imput image and stored as opaque. i assume thees are legacy formats from around the time fs2 came out, when almost all transparency was additive. makes me question if these formats would be useful for beam rendering.

anyway the rgba -> dxt1 conversion process was sorta sketchy and pretty much remains a mystery to me. im not sure how the two 16 bit colors and the 2bit per pixel index came to be but i do know how they can represent 16 pixels. dxt1,2,3,4,5 all store opaque data in the same way. probibly using similar compression routines as photoshop would use for saving an 8 bit indexed image and probably storing the 2 colors from the source pixel with the biggest numerical gap. the 2 colors are selected are ones that actually get stored in the image file and are the first 2 slots of the 2 bit color table. the other 2 colors are calculated via interpolation. the first interpolated color is the sum of 2/3 of the first color in the table and 1/3 of second (this is done for each color channel separately and stored in the respective channel of the calculated pixel color). and the second pixel color to be calculated sums 1/3 of color 1 with 2/3 of color 2.

dxt1a is the exception. it has one bit alpha which is stored depending upon which of the first 2 colors is brighter. if the second color in the table is brighter than the first, then the 4th index is set as a transparent an any pixel flagged with that index is excluded from rendering. otherwise color data for the third and 4th index is stored as described above. the 3rd pixel is interpolated differently, by summing 1/2 of the color data from each of the first 2 colors which comes up with a color which is about average. this means that dxt3 would loose color information on any block with transparency. so you would loose alot of color if just one of the pixels was transparent.

dxt3 simply cheats and stores transparency directly as a store a 4 bit transparency value for each pixel stacked on top. dxt5 on the other hand interpolates transparency in an even more complicated way than color. in addition to the 2 bit color table and 2 base colors theres also a 3 bit transparency table and an extra pair of 8 bit trans values. 3bit means 8 possible values. this also makes the transparent channel alot more detailed than any one of the color channels, being based off of 8 bit values rather than 5 or 6 bit values.

anyway it starts with a color table with only 2 entries in it, 6 blank slots to fill. if the first alpha value is bigger than the second, then the other 6 alpha values are calculated by value (1*m + 1*n) / 7. n and m are 6 and 1 for index 3, 5 and 2 for index 4, 4 and 3 for index 5 and so on until the table is full. but if value 1 in the table is smaller than value 2 the values use a different formula. values 7 and 8 are not interpolated and are set to 0 and 255 respectively, leaving only indexes 3-6 to be interpolated. the formula is the formula is (1*m + 1*n) / 5  but replace m and n with 4 and 1 for index 3, 3 and 2 for index 4, and so on. same subtract from m and add to n for each index.

that answered my question about the dxt5nm format for normal maps. you use the alpha channel instead of red to store x, because the you have 8 shades of 8 bit transparency instead of maybe 4 shades of 5 bit red. green has an extra bit and keeps the y values while holding a better accuracy. im not sure if z is stored in blue, or if its not stored at all and calculated based off the x and y vales, or if the blue and red are put together to get a bigger range. anyway i answer a few questions about the mysteryous format and brought up a bunch more. i hope i was informative, you cant use a tool if you dont know anything about it.
« Last Edit: October 14, 2007, 05:20:08 am by Nuke »
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 
Re: a bunch of maybe usefull info about dxt texture compression
well everyone associates dxt with microsoft because its got dx in it. while in fact its really an s3 texture compression format format aptly named s3tc (duh).

Ohhh so thats what happened to s3.  I remember when a patch for unreal came out that supported S3 texture compression and high quality texture
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
Re: a bunch of maybe usefull info about dxt texture compression
z is calculated on the fly. red and blue are waisted in the dxt5nm format, I'm in favor of using the red and green channels to store height map info, but taylor says most modders don't want to do that, but I don't think most would mind if there was needed a preprocessing step (to combine the channels a special tool would be needed to process the image, however this tool could easily be made to take a height map and compute the normal map in addition to spanning the height map across the two channels)
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 
Re: a bunch of maybe usefull info about dxt texture compression
Could you use a bumpmap to get the heighmap from?
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: a bunch of maybe usefull info about dxt texture compression
just microsoft tagged it with its own designation for the dds file format. snece s3 still holds the patent on the format they get a sizeable chunk of income from pretty much everyone that uses it for profit. i was also reading about 3dfx's old compressed formats and they dont sound like they work so well. its amazing that dxt manages to look as good as it does. the data seems to be 16 bit but the video card probibly ramps it up to 32 bit on the fly. not sure exactly how that works.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: a bunch of maybe usefull info about dxt texture compression
z is calculated on the fly. red and blue are waisted in the dxt5nm format, I'm in favor of using the red and green channels to store height map info, but taylor says most modders don't want to do that, but I don't think most would mind if there was needed a preprocessing step (to combine the channels a special tool would be needed to process the image, however this tool could easily be made to take a height map and compute the normal map in addition to spanning the height map across the two channels)

i myself would perfer the dxt5nm format, sence it would look better, and the photoshop plugin supports it, probibly so well that you could save any format and it would put the right info in the right place for you without having to manually swap the channels. but im sure some would like the option for an uncompressed 16 bit 88 format. anyway i think forexperimental reasons it might be advisavble to implement all potential normal map formats and let people test to find which one looks and performs the best.

Could you use a bumpmap to get the heighmap from?

yea, the photoshop plugin will do that for you.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 
Re: a bunch of maybe usefull info about dxt texture compression
If you decide to go down that road, you'll have to show me where and how with that plugin.

« Last Edit: October 14, 2007, 05:38:20 am by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: a bunch of maybe usefull info about dxt texture compression
i use the one from nvidia. its simple and has alot of features. you can conver a height map to a normal. i hear you can bake normal maps in max but ive never tried it. ive been thinking of making a normal map paint ptogram. you have a grid with each square representing a pixel with a dot representing the normal. you use a brush to tilt the notmals in various directions, essentially letting you countour it any way you want. then you can export a bitmap. it would be simple but i can think of alot of ways to use it.
« Last Edit: October 14, 2007, 05:40:56 am by Nuke »
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

  
Re: a bunch of maybe usefull info about dxt texture compression
You mean the normal map filter? I've been using that to generate normal maps.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
Re: a bunch of maybe usefull info about dxt texture compression
ive been thinking of making a normal map paint ptogram.

I've had a similar thought, though taken no action as of yet.
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: a bunch of maybe usefull info about dxt texture compression
normal maps really only effect reflected light direction, they dont really set height. height mapping would actually need to project the texel out along the normal to be rendered at a specific altitude. probibly would just be an 8 bit map and could maybe be done with a shader.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN