Advertisement

Gamma correction & shadow mapping

Started by July 13, 2019 02:41 PM
4 comments, last by d07RiV 5 years, 2 months ago

When I try to use any kind of shadowmaps (let's say PCF for simplicity) with gamma correction, I'm getting very blocky shadows. Without gamma correction, PCF generates a smooth gradient, but after applying the power function, the pixels are clearly visible. Am I doing something in the wrong order here, or that's how it should be?

Shadows with gamma correction

image.png.13e63b9307d9ffe4f4bf7337c2fc4c86.png

Without:

image.png.e6033eab41c18b7737e5293f218a06db.png

It's hard to tell with so little information. Which API are you using and how do the functions look like? Which object casts the shadow? Is it a point light or a directional light you are using?

However, when you apply gamma correction, you are changing the local brightness gradients. While the brightness values in the uncorrected colour space might be close to each other (little gradients), they might differ much more (higher gradients) in the corrected colour space. If you have a look at your uncorrected picture, you only see the pixels at the boundaries (where gradients tend to get steep in many cases). The surface looks totally smooth even though there are also differences in the brightness. So I would guess, that is kind of a "normal" artefact of the correction. But I am no shadow mapping expert, so don't take this answer for the truth.

 

Greetings

 

EDIT: Maybe my mind is tricking me here, but it seems like the second picture is also covering a larger area. I think I can see some pixels there at the boundaries too. But they look smaller. Hence the question. If this is correct, this would be another hint, that the effect you see is kind of normal (as explained).

Advertisement

It's opengl and this was PCF with kernel size 2, but I don't think any of that matters. My concern is that if your shadow forms a linear gradient in RGB space, once you apply gamma correction the shadow edge becomes really sharp. With VSM shadowmapping the pixels aren't visible, but the shadow is still very sharp, when it looked really well in uncorrected space.

5 hours ago, d07RiV said:

My concern is that if your shadow forms a linear gradient in RGB space, once you apply gamma correction the shadow edge becomes really sharp.

If you form a linear gradient in sRGB (gamma-RGB) it will not be perceptually linear -- the middle of the gradient will be 3/4ths of the way through it...

If you form a linear gradient in linear-RGB and then convert to the display color space (sRGB / gamma-RGB) then it should look perceptually linear, with the middle of the gradient 1/2 way through it.

 

How are you performing "gamma correction" in your renderer? Do you store any linear-RGB images in 8-bit channels at any point? Do you use the hardware accelerated sRGB conversions, or are you doing the conversions manually in shaders? Which functions are you using for gamma-to-linear and linear-to-gamma, and at what point do you use them?

I use hardware sRGB for material textures, then all the colors are stored and accumulated in RGBA16F buffers until the final step where I simply do `pow(color, 1.0/2.2)` to get the final result.

My light pass shader outputs like this:

    o_Color = vec4(pbr * light.color * shadow, 1.0);

Where pbr is the multiplier from PBR formulas, and shadow is shadowmap sample.

Then the final pass does
    vec3 color = tColor * tEmissive.a + tEmissive.rgb;
    o_Color = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
 

This topic is closed to new replies.

Advertisement