• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pekarn

Gamma Correction

7 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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.

Edited by TiagoCosta
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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 );
Edited by Hodgman
1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

Edited by DwarvesH
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0