Gamma Correction

Started by
6 comments, last by DwarvesH 10 years, 4 months ago

Hello!

I'm currently trying to make sure that my engine handles gamma in a correct way. The problem is the results that I'm getting contradict my understanding of how it should work (based on what I've read).

The test that I'm doing is from the shader use the screen uv to output a linear gradient from black to white.

I currently do no correction at all (my backbuffer format is not a srgb format, I don't set any gamma ramp, the shader doesn't do any correction). Based on my understanding this would result in the gradient that I see on my monitor being incorrect since I don't do any correction. However the result I see is a gradient that looks linear. When I try to apply correction the gradient ends up looking non-linear.

So basically when I don't do any correction at all I get what seems like correct results. According to the settings on my monitor it's using 2.2 gamma. I looked in NVIDIA control panel and the gamma override setting there is turned off as it should be. Interestingly if I enable it and change the gamma the image will change gamma for a bit less than a second and then it seems something else will override it and the image will look normal again even though the slider is still set to something different.

I've tried messing around with this (http://mynameismjp.wordpress.com/2010/04/30/a-closer-look-at-tone-mapping/) tonemapping sample. If I do the same thing and output a linear gradient from the shader in that sample I will get an incorrect output on the monitor because it uses an srgb backbuffer.

I'm just really confused right now as to what's going on so any help to sort it out would be great.

Thanks

Advertisement

output a linear gradient from black to white.

The point of gamma correction isn't to output colors in linear space...

You should convert gamma space colors to linear space, do lighting and apply post processing, convert the final color back to gamma space and write to backbuffer.

Something like this:


color = pow(abs(texture.Sample(sampler, texC)), 2.2);

//Apply lighting

output = pow( color, 1/2.2 )?;

Also read this awesome presentation about gamma correction and HDR ligthing.

Your perception of light is not linear, so a linear gradient will not be perceived as being linear.

As above, gamma correction is important to ensure that your math is done with linear values, while your display (and probably also your texture data) are using "gamma" values.

e.g.

float gradient = uv.x;// a color
gradient = pow(gradient, 2.2); // gamma -> linear
gradient *= 2; // do math on linear values
gradient = pow(gradient, 1/2.2); // linear -> gamma
return gradient; // a color

Yes and that is my understanding of how it should work. What I'm saying is that if I output a linear gradient without any correction my eyes perceive it as linear on the monitor aswell, if I apply correction to it then it doesn't look linear anymore. Basically the color that I perceive on the monitor matches the linear values that I output from the shader. Which, as you say, is not what should happen, and that's why I'm confused.

What I'm saying is that if I output a linear gradient without any correction my eyes perceive it as linear on the monitor aswell, if I apply correction to it then it doesn't look linear anymore.
Which, as you say, is not what should happen, and that's why I'm confused.

Your perception of light is not linear, so a linear gradient will not be perceived as being linear.


The monitor is applying a gamma curve when it displays your content (e.g. display = pow(output, 2.2)), which means that if you output content without any correction, then you're not displaying linear-content, you're displaying gamma-content.

When you say you're displaying a "linear gradient without correction", you're actually displaying a "gamma gradient".


The first bit of code is displaying a gamma-space gradient (purple on the graph image).
The second bit of code is displaying a linear gradient (blue on the graph image).

float gradient = uv.x; // this is a gamma gradient
//hidden: display = pow( gradient, 2.2 )

float gradient = uv.x; // this is a linear gradient
gradient = pow( gradient, 1/2.2 );
//hidden: display = pow( gradient, 2.2 )
alnNQBQ.png
Your own human perception of light is also not linear, so the purple line will actually look "smoother" to you.
The linear, blue line, is mathematically linear, but will appear much more "contrasty" to you.

This is what you're observing, and it is to be expected. It's not the point though.

All of this is very far removed from the point or purpose of gamma correction though... The important objective is simply to ensure that you always convert source data to linear before performing math on it, and then converting to the appropriate curve for the display (the opposite of the display's in-built curve).

With your gradient, you're just procedurally generating a gradient, so it could be in any number system. Because you're just making up these gradient values out of nowhere, you can say whether those gradient numbers are in linear-space or in gamma-space arbitrarily, and then the correct "correction" steps will be different depending on your arbitrary choice, which is why this is a bad/confusing example.

Correct use of gamma correction:

float gradient = uv.x; // this is a gamma gradient -- arbitrarily stated as being true.
gradient = pow( gradient, 2.2 ); //now it's in linear-space
gradient *= 2; // math is done in linear space: correct!
display = pow( gradient, 1/2.2 ) //now it's gamma-space again
//hidden: display = pow( gradient, 2.2 ) 
float gradient = uv.x; // this is a linear gradient -- arbitrarily stated as being true.
gradient *= 2; // math is done in linear space: correct!
gradient = pow( gradient, 1/2.2 );//convert to gamma-space for display
//hidden: display = pow( gradient, 2.2 )
Incorrect:
float gradient = uv.x; // this is a gamma gradient -- arbitrarily stated as being true.
gradient *= 2; // math is done in gamma space: incorrect!
//hidden: display = pow( gradient, 2.2 ) 
float gradient = uv.x; // this is a linear gradient -- arbitrarily stated as being true.
gradient = pow( gradient, 1/2.2 );//convert to gamma-space for display
gradient *= 2; // math is done in gamma space: incorrect!
//hidden: display = pow( gradient, 2.2 )
In a real example, you don't just make up the input data arbitrarily though. You'll have a photo, which your camera has converted from Linear to SRGB for example -- in that case, you know the input is in SRGB-space (approximately the same as "gamma 2.2").

If your monitor is calibrated to "gamma-2.2" and you draw a bitmap in MSPaint, then that bitmap is in "gamma 2.2" space.
If someone else's monitor is calibrated to "gamma 1.8" and they draw a bitmap in MSPaint, then their bitmap is in "gamma 1.8" space.
The correct way to display these bitmaps blended 50% together on a "gamma 2.2" display would be:
//decode both bitmaps to linear
myBitmap = pow(myBitmap, 2.2);
theirBitmap = pow(theirBitmap, 1.8);
//blend them in linear space
result = (myBitmap + theirBitmap) * 0.5;
//my monitor is "gamma 2.2" calibrated, so correct the linear image for display:
return pow( result, 1/2.2 );

Hi!

As it turns out I was just investigating gamma correction a few days back. It seems very simple. I'm using XNA so I can't use the sRGB support, so I tried the shader version, but the results are very strange. Before:

[attachment=18069:08_01.png]

After:

[attachment=18070:08_03.png]

I don't know if the after shot is more correct from a gamma standpoint or not, but I don't like how it looks. Why does it look like this? The normal mapping effect is very washed out. Do I also need to rebalance my entire lighting scheme and parameters for gamma correct rendering?

Do I also need to rebalance my entire lighting scheme and parameters for gamma correct rendering?


Yes! When performing gamma incorrect lighting people are likely to use incorrect (hacky) lighting parameters in order to achieve "good" lighting, so after implementing gamma correction you have to tweak the lighting for it to look good.

When I first implemented gamma correction I had to do the exact same thing.

The normal mapping effect is very washed out.

Are you converting the normal maps to linear space when you sample it? That's incorrect, because normal maps should already be in linear space.

If not, tweaking the lighting parameters like I said above should fix it.

Sorry to bring this topic back!

I'm trying to be gamma correct, but I think one of the biggest obstacles is familiarity! After working so much with gamma incorrect rendering, I am used to what is wrong and I perceive it as better.

Take this image as an example: http://dl.dropboxusercontent.com/u/45638513/gamm1.png

This look to me better than: http://dl.dropboxusercontent.com/u/45638513/gamm2.png

Is the second one the way it is supposed to look?

One good thing is that spherical harmonics seem to look more natural with gamma correct rendering: Here is the before shot with an extremely poorly balanced spherical harmonics scheme (I got a lot better results meanwhile):

http://dl.dropboxusercontent.com/u/45638513/gamm3.png

And with gamma correctness:

http://dl.dropboxusercontent.com/u/45638513/gamm4.png

So what should I do? Stick by gamma correct rendering until I get used with it and my eye perceives it as the correct way?

This topic is closed to new replies.

Advertisement