[2.1] PBS + HDR Gamma space questions

Discussion area about developing with Ogre2 branches (2.1, 2.2 and beyond)
Post Reply
al2950
OGRE Expert User
OGRE Expert User
Posts: 1214
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 149

[2.1] PBS + HDR Gamma space questions

Post by al2950 »

The PBS shaders will always ensure that the final output from PBS is in 'Gamma' space. However when applying HDR surely you want to do this in 'Linear' space. I am slightly confused looking at the shader of FinalToneMapping_ps as to exactly what its doing and if its correct. More to the point do you or dont you set the final render target to do automatic gamma correction or not?

I hate gamma!!

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4212
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 802
Contact:

Re: [2.1] PBS + HDR Gamma space questions

Post by dark_sylinc »

Ummm "nope". There's a bit of confusion. This is normal. Even I get confused sometimes.

Before I explain, let's be clear about the terms:
Linear space: The "true" space in what the colours should be in. And the space in which the PBS math should take place.
Gamma space: The space in which most pictures are stored in jpg files. Also the space that gets sent to the monitor and then displayed on screen.

The PBS shaders work in linear space, and output linear space data. Our samples use an sRGB framebuffer, in which the GPU receives the linear data from our shaders, automatically converts it to gamma space; and sends it to the monitor.
If we need to use it as a texture (e.g. RenderTexture) then the GPU automatically converts it back from gamma space to linear space again.

So... the PBS shaders take linear space as input, and output linear space too. It's the GPU that via its sRGB format performs the conversion to keep the data internally stored in gamma space.

When you're working with HDR, you'll often use a FLOAT16 texture (or an RGBA_1010102). These do not have sRGB variants, which means that they store the data in linear space as they receive it from the PBS shaders, and gets sampled as linear data too.

Once all the postprocessing is done (all in linear space), eventually it will reach the sRGB framebuffer and the GPU will convert it from linear to gamma for us, and send it to the monitor.
So basically the output of FinalToneMapping_ps will be in linear space, but it is often the final step which gets sent to an sRGB framebuffer, so it gets converted to gamma.

That's it.

Notably remarks:
  • The HDR sample uses an RGBA_1010102 framebuffer for the bloom instead of a FLOAT_RGBA_16 because GCN hardware is faster filtering the former (it has visible impact on framerate btw). 10 bits is not enough to represent the whole range we bloom, so I convert them to gamma space via shader code and then back to linear space when sampling (btw it's not really sRGB space, but rather an approximation that assumes gamma = 2.0). This trick is done purely because of precision, that's all. You get very noticeable banding otherwise. But the math is still in linear space (ok ok... the GPU will bilinear filter in gamma space but it's an acceptable tradeoff). I suspect this is what confused you.
  • When gamma correction is not supported (aka GLES2 mobile... which now isn't working anyway) the shaders will add some snippets to always convert input from gamma and then output in gamma. This may need to get reviewed so that it works well with HDR (otherwise HDR will be done in gamma space). Anyway not something you need to worry since you don't do mobile, but is worth mentioning.

al2950
OGRE Expert User
OGRE Expert User
Posts: 1214
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 149

Re: [2.1] PBS + HDR Gamma space questions

Post by al2950 »

Ok that makes more sense.

In short (ignoring broken mobile API's), all shaders should really output in 'linear' space, and any developer should ensure "sRGB Gamma Conversion" is set to true when initialising Ogre.

For one this makes everything meet a 'standard' and two all post processing shaders, which can not be 'corrected' by the HLMS, that are being written by you and the Ogre team will output in 'Linear' space expecting the GPU to store them in 'Gamma' space, which of course includes outputting to the monitor in 'Gamma' space.

Hopefully that is correct, and clear to anyone who may read it!

Post Reply