Friday, August 24, 2007

The case for DDS

Jonathan Harris suggested I look into .dds as an image format for X-Plane. I think he's got a good idea.

First, what is DDS? It stands for "direct draw surface", but as a file format, it's basically a file whose structure is the same as the memory layout for a texture in DirectX. It's a simple image format but it does two useful things for X-Plane (or any other 3-d application):
  • It can contain compressed images using the same compression OpenGL and DirectX use (DXT compression).
  • It can contain mipmaps - smaller versions of the same image.
Now before things get out of hand, using .dds files in X-Plane would not mean converting X-Plane to use DirectX!! .dds is simply an agreement about how to arrange a file. You can easily write code to load a .dds file into an OpenGL application, and that is what we would do.

There would be three fundamental benefits to using DDS files:

File size on disk (and download).

PNG is a losslessly compressed file format. This kind of compression (the internal algorithm used by PNG is the same as ZIP files) preserves the image, but the reduction in image size is a function of how "simple" the image is. Images with a lot of detail and high frequency information (rapid changes in color) do not become much smaller than the amount of VRAM they use when compressed in a PNG. A lot of the terrain textures we use (4 MB in VRAM) are still 3 to 3.5 MB on disk!

By comparison, the texture compression used by OpenGL is lossy - the image looks worse. But the image is reduced by a factor of 4 to 8, every time. So one of these images (4 MB in VRAM when uncompressed) would be 1 MB with S3TC compression (available in a DDS file). Even for very simple PNG files, you rarely see such a reduction in size, so .dds files would be smaller, meaning faster updates and downloads.

(Another thought: if a PNG file is smaller than its VRAM requirement, it means the PNG file has lots of areas with the same color. This means the PNG file is WASTING VRAM! We encourage our artists to make use of every little pixel in an image, and this means that PNG files that are well-created for X-Plane tend to get relatively little benefit from PNG's internal compression.)

Load Time

Image load time is a significant factor in X-Plane. There are several reasons why DDS files could load a lot faster than PNG.
  1. The actual DDS file is smaller - less data to load and process means faster performance.
  2. Because the DDS file can contain smaller copies of the image, X-Plane doesn't have to load a huge image and reduce its size using the CPU - it can just load the small version in the file.
  3. When compressed textures are used (and they almost always should be, see my previous blog post) it takes CPU power to compress the image, slowing down load. A DDS file containing a compressed image can be sent directly to the card!
Image Quality

Now one is a little bit tricky. First, please review my previous blog post, which argues that texture compression is the best way to maximize VRAM. A DDS file will look better than a PNG file when texture compression is on. Here's why:

The S3TC compression formats specify an EXACT way to "uncompress" and draw a compressed image. But there are MANY possible ways to compress the image. Remember that S3TC is a lossy compression - the compressed image is only an approximation of the original.

Well, it turns out that algorithms that find the best approximation of an image using S3TC are slow. So we can't use the highest quality compression inside X-Plane when compressing PNGs for the graphics card - it would be too slow.

But the image in a DDS file is already compressed -- because someone else has compressed it when creating the scenery. When converting the scenery from PNG to DDS that author can use a very slow, very high quality compression algorithm to make the DDS files. It might take 12 hours to convert the entire package from PNG to DDS, but who cares? It's something you do once and then everyone enjoys better looking images.

In other words, because the compression is done ahead of time for a DDS file, a higher quality algorithm can produce better approximations of the original images. Since we want to use compression anyway (to optimize our use of VRAM) this is a good thing.

When is DDS not appropriate?

Any time an image must not be compressed, DDS is inappropriate, and PNG is best. There are three basic cases in X-Plane where we avoid compression (see my previous blog entry for the real examples):
  1. Any time we need to preserve tiny detail accurately, like lettering and text.
  2. Any time the alpha channel needs to be smooth.
  3. Any time a texture is magnified so large that small color changes between two pixels is noticeable. (For example, compressing the color washes used for the sky make obvious artifacts.)
DDS wouldn't replace PNG, it would augment it.

8 comments:

Unknown said...

Does it work on a Mac though?

Benjamin Supnik said...

Does it work for a Mac...the answer is...well...

DDS is a _file format_...in other words, it's an agreement about how to encode data onto a hard drive. (Computer scientists, stop cringing. :-) So it's not specific to Mac or PC, and doesn't really depend on a particular computer.

THEORETICALY speaking, any file format should be POSSIBLE for any operating system.

Now a file format is useless unless you can READ the file, and that's where the question comes: can somebody program the Mac to read this file?

In the answer of DDS the answer is: yes definitely. You don't need DirectX to read a DDS file...you just need to write a program.

Most file formats can be supported on any operating system.

Ryusennin said...

Great news. DDS is the way to go.

I'm dealing almost every day with DDS textures in Oblivion; even in DXT1, the compression artifacts are not a problem. The only case where an agressive compression can make a very noticeable difference is when compressing normal maps in DXT1, and I dont remember X-Plane using them anywhere in the sim.

Benjamin Supnik said...

Yes - DXT + normal maps = world of hurt. :-) We don't ship any in the sim but we are using them in some of the under-development features, and they are flagged as "do not compress", just like textures with very small text details and textures where we need the alpha channel to be unmolested.

(4-bit alpha from DXT3 is inadequate for us a lot of the time, and DXT5 limits the high frequency possibilities of the alpha channel for us in others.)

Anonymous said...

GraphicConverter could save image as DDS on a Mac

Ryusennin said...

"but we are using them in some of the under-development features"

By Jove, you mean, like in normal maps for aircraft textures? Woot! Although they could certainly be useful for terrain as well. Mmm...

Anonymous said...

And do you think we will have this DDS compression in XP9 ... or in the next "major" v8 update that Austin talked about recently ????
it would be great for all the orthophoto sceneries developpers like me and will help both for the size adn the loading of the scenery !

Jack Skieczius said...

Awesome stuff. i should have done a search in the blog for this.
thanks for pointing me in the right direction.

as always i enjoy your blog and always check in to see whats new from time to time.