Monday, June 28, 2010

DDS Has No Gamma (Which is Very Sad)

A few years ago I blogged about gamma correction for png files. Here's the very, very short version:
  • PC and Mac monitors are calibrated differently. Dark tones on a PC appear darker than on a Mac. The curve of how colors are mapped to the monitor is the gamma correction curve, typically expressed as a number like 1.8 for Mac and 2.2 for PC. The higher the number, the more Gothic your dark tones.
  • A png file can have a gamma value written into the file, which tells X-Plane (and anyone else) what kind of monitor the png was drawn on. This lets X-Plane brighten a png from a Mac when you are on a PC, and darken a png from a PC when you are on a Mac.
  • If you leave off the gamma value on your png, we assume 1.8 (Mac) which can be bad if you're a PC author.
While this is confusing, it was an improvement over the BMP situation (where everything was set up for a Mac and PC users had to simply crank their monitor brightness).

In version 9 we added a gamma correction setting to X-Plane. The setting you enter in the rendering settings is how "dark" your monitor is (bigger number = darker). We then attempt to compensate by lightening the textures more; thus a bigger number results in a lighter looking X-Plane (because you told us your monitor was dark and we tried to "fix it").

There are two other developments since the original png situation which have unfortunately been a step backward in terms of X-Plane color correction.

DDS and Gamma

The handling of DDS and gamma is, to put it mildly, quite problematic. The problem is two-fold:
  • DDS doesn't actually have gamma information, so we can't tag DDSes as having originated on Macs and PCs. So we assume a DDS is authored at a gamma of 1.8 (Mac). I think DDSTool correctly does a gamma correction when grinding files at other gammas.
  • (If you are a real graphics programmer, please do not read this next sentence.) X-Plane attempts to adjust the color of the DDS in its compressed form. This is a big hack designed to keep framerate high, but it's really not a very good idea. The result can be color distortion when a DDS is viewed at 2.2 gamma.
So that's not good, but what happened next made things a lot worse.

Apple Goes Gothic

Apple adopted the sRGB color profile for OS X 10.6, which has a gamma curve of about 2.2. So now the situation with DDS is particularly ugly:
  • All DDS are authored at a gamma of 1.8.
  • All users are moving toward a display gamma of 2.2.
  • X-Plane thus has to always color correct, but its color correction is low quality for performance reasons.
This is...very sad.

There are two things we can do about this:
  • In the short term, we can provide post-decompression color correction. This will cost a (hopefully) small amount of framerate and improve color fidelity for users with 2.2 gamma. This is the kind of thing that any user with a modern card would want, but that we might make optional for users with very old hardware.
  • In the long term, we can provide a gamma calibration in the text files that wrap DDS files so that authors can mark their DDS as already being 2.2. This will mean that for most users X-Plane won't have to do any color correction at all.


kaminari said...

Not willing to add fuel to the fire, but I'm trying to make some sense out of it. Why do you assume a gamma of 1.8 for DDS files, when this Microsoft format is essentially used by Windows developers?

DDS is a neat format with a few shortcomings, but most of all I don't remember any Mac developers using DDS textures in their X-Plane products, be they aircrafts or sceneries.

It would be fair to assume that most DDS textures have been created in Windows environments and thus require a default gamma of 2.2.

Benjamin Supnik said...

The assumption that DDS content originates on Windows is not a safe assumption.

We in particular didn't assume this because our internal art team originated DDS files on Mac!

Anonymous said...

I wonder about gamma handling of other things moving through the graphics pipeline, primarily lighting. With dark tints and/or light coming from the side, the usual assume-things-are-linear approach becomes a lot darker than it should be (and a darker at higher gamma values, in case you went tweaking). Unfortunately you can't simply preprocess the gamma-corrected dot product, like you could with the light's color and textures.