Gamma space and linear space

Gamma space and linear space

http://blog.csdn.net/bill2ccssddnn/article/details/53423410

With the advent of physically based rendering (PBR) with higher realism, linear space (Linear space) lighting calculations are becoming more and more frequently mentioned. While linear space and its "opposite" gamma space (gamma space) are simple and important concepts, many developers don't understand what they really mean. This document will introduce gamma space and linear space, the difference between them and the application in the Unity engine.

LINEAR SPACE

What is linear space? To put it simply, the result obtained by adding and multiplying the digitized color and light intensity in linear space can still be consistent with the real result. A space that does not have this property is called a "nonlinear" space. See below: 
Write picture description here

GAMMA SPACE

There are two main reasons for gamma: the first is the historical reason of CRT monitors, and the processing of displayed images is nonlinear. The second is because human eyes have different recognition of light and dark color gamuts. Sometimes it is necessary to perform non-linear processing on the image to obtain a suitable recognition. These non-linear processing is called gamma correction. Do a power function operation for each pixel of . Gamma refers to the exponential value of this power function. See the figure below: 
Write picture description here 
In the figure above, it is obvious that gamma values ​​other than 1 are non-linear. The figure below shows the results of gamma correction of the image with different gamma values ​​in the figure above: 
Write picture description here
important! ! ! In reality, most of the pictures are corrected with a gamma value of 0.45, which means that the picture files store the color values ​​with gamma correction. Doing gamma correction is pretty much what every camera, every image editing software does by default when storing an image.

But why does the image display correctly enough instead of looking blown out like the one on the left? This is caused by non-linear processing when the display is displayed. When the CRT monitor displays the image, the gamma value is corrected to 2.2, and the subsequent LCD monitor is also corrected for compatibility. 2.2 is the reciprocal of 0.45, offsetting each other to obtain an image consistent with the original. If the picture is not gamma corrected, the displayed image will be like the picture on the right, the color is suppressed black.

COLOR SPACES AND THE RENDERING PIPELINE

In the real-time rendering pipeline, if the gamma space is used, the gamma-corrected texture directly participates in the lighting calculation in the shader, and the obtained image is output and displayed after being gamma-corrected by the display. The process is simple, but physically incorrect. In the real world, lighting behavior is linear, and digital lighting calculations in shaders are also linear, while input textures and colors are non-linear, which means that shading results are not accurate. Today, with the increasing pursuit of picture immersion and authenticity, lighting calculations in gamma space cannot meet the needs.

Therefore, PBR uses linear space. The texture and color are converted to linear space without gamma correction, and then passed to the shader for lighting calculation and coloring. The post-processing (post effects) is also performed in linear space. The final obtained The image is then corrected with a gamma value of 0.45 to offset the gamma correction of 2.2 when displayed on the monitor. The following figure is a comparison of the process and results of the rendering pipeline using gamma space and linear space: the 
Write picture description here
rendering difference of the simple sphere in the above figure can be noticed: the gamma space highlights are brighter and the attenuation range is larger. These are renderings that lack realism.

COLOR SPACES IN UNITY

The rendering pipeline of the Unity engine uses gamma space by default. Currently, only PC, Xbox and PlayStation platforms support linear space, which can be switched here in the editor: 
Edit -> Project Settings -> Player -> Other Settings -> Color Space 
to switch to linear After space, the texture and color without gamma correction will be passed to the shader. If the project under development is switched, the scene will look different from before the switch, and the lighting and textures need to be re-adjusted to achieve the desired effect, and the light textures also need to be re-baked.

If both linear space and HDR are set, all post effects in Unity will be performed in linear space. If only linear space is set, Unity still uses the gamma framebuffer (framebuffer), but when reading or writing to the framebuffer, Unity will automatically correct the color to ensure that post-processing is still performed in linear space.

Although Unity does not support linear space for mobile platforms by default. But you can do a similar implementation in the shader: perform pow() function processing with a power of 2.2 on the incoming texture, and then perform pow() function processing with a power of 0.45 before the color return value. This implementation has computational overhead and cannot be abused.

CONCLUSION

Master the difference between gamma space and linear space, so that you can avoid some common-sense errors in color management in actual game projects, so as to ensure the picture quality. In short, the use of linear space is an important part of achieving picture immersion and authenticity.


Attached the original link: 
http://www.kinematicsoup.com/news/2016/6/15/gamma-and-linear-space-what-they-are-how-they-differ

http://blog.csdn.net/bill2ccssddnn/article/details/53423410

With the advent of physically based rendering (PBR) with higher realism, linear space (Linear space) lighting calculations are becoming more and more frequently mentioned. While linear space and its "opposite" gamma space (gamma space) are simple and important concepts, many developers don't understand what they really mean. This document will introduce gamma space and linear space, the difference between them and the application in the Unity engine.

LINEAR SPACE

What is linear space? To put it simply, the result obtained by adding and multiplying the digitized color and light intensity in linear space can still be consistent with the real result. A space that does not have this property is called a "nonlinear" space. See below: 
Write picture description here

GAMMA SPACE

There are two main reasons for gamma: the first is the historical reason of CRT monitors, and the processing of displayed images is nonlinear. The second is because human eyes have different recognition of light and dark color gamuts. Sometimes it is necessary to perform non-linear processing on the image to obtain a suitable recognition. These non-linear processing is called gamma correction. Do a power function operation for each pixel of . Gamma refers to the exponential value of this power function. See the figure below: 
Write picture description here 
In the figure above, it is obvious that gamma values ​​other than 1 are non-linear. The figure below shows the results of gamma correction of the image with different gamma values ​​in the figure above: 
Write picture description here
important! ! ! In reality, most of the pictures are corrected with a gamma value of 0.45, which means that the picture files store the color values ​​with gamma correction. Doing gamma correction is pretty much what every camera, every image editing software does by default when storing an image.

But why does the image display correctly enough instead of looking blown out like the one on the left? This is caused by non-linear processing when the display is displayed. When the CRT monitor displays the image, the gamma value is corrected to 2.2, and the subsequent LCD monitor is also corrected for compatibility. 2.2 is the reciprocal of 0.45, offsetting each other to obtain an image consistent with the original. If the picture is not gamma corrected, the displayed image will be like the picture on the right, the color is suppressed black.

COLOR SPACES AND THE RENDERING PIPELINE

In the real-time rendering pipeline, if the gamma space is used, the gamma-corrected texture directly participates in the lighting calculation in the shader, and the obtained image is output and displayed after being gamma-corrected by the display. The process is simple, but physically incorrect. In the real world, lighting behavior is linear, and digital lighting calculations in shaders are also linear, while input textures and colors are non-linear, which means that shading results are not accurate. Today, with the increasing pursuit of picture immersion and authenticity, lighting calculations in gamma space cannot meet the needs.

Therefore, PBR uses linear space. The texture and color are converted to linear space without gamma correction, and then passed to the shader for lighting calculation and coloring. The post-processing (post effects) is also performed in linear space. The final obtained The image is then corrected with a gamma value of 0.45 to offset the gamma correction of 2.2 when displayed on the monitor. The following figure is a comparison of the process and results of the rendering pipeline using gamma space and linear space: the 
Write picture description here
rendering difference of the simple sphere in the above figure can be noticed: the gamma space highlights are brighter and the attenuation range is larger. These are renderings that lack realism.

COLOR SPACES IN UNITY

The rendering pipeline of the Unity engine uses gamma space by default. Currently, only PC, Xbox and PlayStation platforms support linear space, which can be switched here in the editor: 
Edit -> Project Settings -> Player -> Other Settings -> Color Space 
to switch to linear After space, the texture and color without gamma correction will be passed to the shader. If the project under development is switched, the scene will look different from before the switch, and the lighting and textures need to be re-adjusted to achieve the desired effect, and the light textures also need to be re-baked.

If both linear space and HDR are set, all post effects in Unity will be performed in linear space. If only linear space is set, Unity still uses the gamma framebuffer (framebuffer), but when reading or writing to the framebuffer, Unity will automatically correct the color to ensure that post-processing is still performed in linear space.

Although Unity does not support linear space for mobile platforms by default. But you can do a similar implementation in the shader: perform pow() function processing with a power of 2.2 on the incoming texture, and then perform pow() function processing with a power of 0.45 before the color return value. This implementation has computational overhead and cannot be abused.

CONCLUSION

Master the difference between gamma space and linear space, so that you can avoid some common-sense errors in color management in actual game projects, so as to ensure the picture quality. In short, the use of linear space is an important part of achieving picture immersion and authenticity.


Attached the original link: 
http://www.kinematicsoup.com/news/2016/6/15/gamma-and-linear-space-what-they-are-how-they-differ

Guess you like

Origin blog.csdn.net/lejian/article/details/125313878