Subject: Gamma Correction, Shader
Recently in the study of PBR (physical-based Rendering) related things. So I looked at a few articles about linear color space, and learned how to ensure that our shader calculated color will faithfully be reflected by the display-gamma Correction concept. Search the relevant content on the Internet, found this article is very easy to understand, so the translation to share to everyone!!!
rendering without gamma correction and gamma correction
When you look at a picture on your monitor, the pixels you see are actually modified to be more consistent with our human visual experience. Human beings are more sensitive to the shadow parts, but are less sensitive to midtones and specular tones. Hardware manufacturers are releasing an additional color value from the past
, which makes the color not appear linear changes. And this specific index value is called the gamma value. It is also because of this "user-friendly" operation of the display manufacturer that we need to consider this issue when our graphics game developers want to produce the correct images that we expect. And the purpose of this article is to tell you what gamma correction is and how to perform gamma correction.
What is Gamma?
Gamma is an exponential curve that the display uses to adjust the actual output to the color values on the display screen to better suit the human visual experience.
The graph above reflects an exponential curve with a 2.2gamma value. The above curve can reflect the fact that the display is actually much darker than the value we really pass to it. For example, when we pass a pixel with a display strength of 0.5, the output is not actually the value of that intensity. 0.5^2.2 = 0.21, smaller than half of the input strength value. Looking at this curve from another angle, we can see that in this curve 50% of the color intensity is less than 21%, and only 27% of the color intensity is higher than 50%. We really like the shadows very much. To calculate the color intensity value of the output, the monitor imposes an exponential function on the color value passed in.
O = I^gamma
The top o is the color of the display's final display, I is the color that our software transmits to the display, and gamma is the gamma value used by the monitor.
It is important to note that not all displays will use a gamma of 2.2. Other commonly used gamma values are like 2.4,2.0,1.8 and so on. If you can use the correct gamma value better, but in most cases, we can use the gamma value of 2.2 to meet the requirements. If you are not satisfied, then you can only allow the user to adjust the display settings of the display. If you can add a similar gadget to your game and let the user use gamma correction only in your game, this will not affect the user's use of the desktop system. Once you get the right gamma, you'll be able to calculate the correct color value in your shader.
What is Gamma correction?
Gamma is the action that the monitor uses to modify the color values we enter, so the gamma correction is the process of reversing the display changes, allowing US graphics developers to light and color in a real linear RGB space. For example, if we want the light intensity of an object to be 0.5, then we will not save 0.5 directly as its light intensity, but save 0.5^1.0/2.2 = 0.73. When we send this 0.73 value to the monitor, the monitor imposes a gamma function on the value, which is 0.73^2.2 = 0.5, which is the value of the light intensity that we want the display to display. To achieve this effect, you need to apply a rollover gamma function.
O = I^1.0/gamma
The above o is the color value after gamma correction, I is the input color value, gamma is the gamma value used by the monitor.
After adding one of the above functions, a curve like the one shown below is generated.
The blue lines in the show are the flip gamma correction curves we want to apply. Then when we apply this curve, the result is sent to the display, and the monitor will apply his gamma function on this basis, that is, the red line, the final result becomes the middle Green Line, that is, to get a linear color change. If you do not perform this gamma correction, the displayed color values are much darker than the true linear space RGB values. The most important thing is that it is not the color value that our shader calculates.
The following illustration shows a grayscale change from 0-255:
The correct gradient
The incorrect grayscale gradient in the above image is what our monitor presents to us. When we make a gamma correction, the linear gradient changes are presented. When we render an image, we actually need a linear color space to work, but the color image is not linear. In other words, a+b! = C. So in the process of mapping, we first need to get the map to be linearized. To achieve this, we have to apply a gamma function (not a gamma correction function), and then we can manipulate it correctly (translator: Imagine that we have mapped a map in Photoshop, and this map is based on our observations, That is, if the color of the map we observe the result is 0.5, then actually save the value is 0.73, in order to be able to make the image of the real color of our data, we need to apply a map to the data of the same as the gamma function of the display, so that we can ensure that the subsequent use of the map is what we have observed The value of the color, not the display's modified value). Once we've rendered the scene image with a decal, we're applying a flipped gamma correction function to get a gamma-corrected image. Then the image will be adjusted by the monitor, so that we can realistically present the results we want.
How do I implement gamma Correction?
The best and most robust way to implement gamma correction is by using a post-processing shader. This shader the last rendered image as a map, then calculates the color value of the inverted gamma correction on the basis of the map, and then passes it to the display. This requires that we render the scene to a map and send it to the shader of the gamma correction. If we're worried about performance, we're going to put gamma correction in somewhere else, which can improve performance to some extent, but it loses the flexibility.
Again, we have to remember that we need to apply an extra gamma function to the external input map to ensure that the map color values that are linearly varying are used in shader. However, it is important to note that not all textures need to undergo this process, and some automatically generated images like normal maps do not need to add gamma functions, just the gamma-encoded map requires an extra gamma function.
The following tests can be used to get the gamma value of the monitor. The test tool uses an image with black and white and a gray line after a gamma correction. Half of the lines are black, the other half of the lines are white, so the average output value of the display is 50%. The RGB value of 50% is 128, but 128 is a linear color value. We need to find the gamma-corrected color value, which is 0.5^1.0/2.2 = 0.729 * 255 = 186, which is the middle Gray square. When you are standing on the monitor a few meters away to see this image, if your monitor's gamma value is 2.2, you will find that the image has only one color. Gamma 2.2 Display test image
To test other gamma values, you need to replace the color values of the middle gray squares of this picture with the new gamma values. This test may be difficult to do on the LCD screen, as the display will vary depending on the viewing angle. Also be aware that this image cannot be filtered because it destroys the test.
You have no way to change the gamma value of a monitor, but you can adjust the calculations in the shader so that we can produce a more satisfying grayscale change. This type of adjustment requires the player to have a certain ability to recognize. This means that when we put a white line and a black line together, the player needs to be able to see it. For some people or some poor display, this can be a very difficult task. We tend to divide the RGB values into 10 brightness variations.
This is a very simple gradient. Users should be able to easily identify them. If any of the above is difficult to discern, then the user needs to be adjusted so that the 10 order changes can be clearly identified.
Translation Gamma Correction