Jump to content
  • Advertisement
Sign in to follow this  
Hodgman

sRGB approximations

This topic is 2380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

These days, "gamma correct rendering" (or, doing your shading math in linear space) is standard. It's also easy to implement due to the fact that modern GPUs support [font=courier new,courier,monospace]linear->sRGB[/font] and [font=courier new,courier,monospace]sRGB->linear[/font] transformations in hardware.

...however, I'm supporting some old GPU's that don't provide this hardware. The standard solution here is to pretend that [font=courier new,courier,monospace]sRGB[/font] is the same as [font=courier new,courier,monospace]gamma[sup]2.2[/sup][/font] colour space, and use [font=courier new,courier,monospace]pow(y, 1/2.2)[/font] and [font=courier new,courier,monospace]pow(x,2.2)[/font] to perform [font=courier new,courier,monospace]linear->gamma[sup]2.2[/sup][/font] and [font=courier new,courier,monospace]gamma[sup]2.2[/sup]->linear[/font] conversions.

..however, these old GPU's are also pretty slow at implementing [font=courier new,courier,monospace]pow[/font], so I'm looking for other sRGB approximations. The best I've come up with is the quadratic below, but I'd appreciate any other links/hints on the subject!

From gamma-space to linear-space:
[eqn]y = x^{2.2}[/eqn]
[eqn]y \approx x^2 \cdot 1.08933 - x \cdot 0.0893293[/eqn]
Correct for 0.0 and 1.0
Maximum error of 0.0149833 (~4/255) at x=0.706.
Cost: 2x mul, 1x madd

Reverse transform (linear-space to gamma-space):
[eqn]x = y^{1/2.2}[/eqn]
[eqn]x \approx 0.0410019+4.58998 \cdot 10^{-8} \cdot \sqrt{7.97972 \cdot 10^{11}+4.35732 \cdot 10^{14} y}[/eqn]
Cost: 2x madd, 1x sqrt

Share this post


Link to post
Share on other sites
Advertisement
How much fidelity do you need? You could simply go with the old school technique of a sample from a 1D texture lookup table.

Share this post


Link to post
Share on other sites
Does it really have to be sRGB?

You could store your textures with a gamma of 2.0, which should be much easier to convert in shader (2x mul to encode, 1x sqrt to decode) and still offers a good distribution of values. It would only require you to alter you asset pipeline.

Share this post


Link to post
Share on other sites
I'd be interested to see some comparisons visually as you have to be careful with approximations in Gamma as the error can be low - yet have a big effect. Microsoft used a piecewise approximation in the 360 which is not good at all - as John Hable explains
http://filmicgames.com/archives/14

Share this post


Link to post
Share on other sites
Thanks for the replies.
How much fidelity do you need? You could simply go with the old school technique of a sample from a 1D texture lookup table.
Just enough for the game to look ok on a TV, and for the artist to not complain.
The tone-mapping pass (linear->gamma) is ALU bound, not texture bound, so a LUT could be a good idea there. However, the forward passes (gamma textures->linear) are very sensitive to extra texture fetches at the moment.

Does it really have to be sRGB?
You could store your textures with a gamma of 2.0, which should be much easier to convert in shader (2x mul to encode, 1x sqrt to decode) and still offers a good distribution of values. It would only require you to alter you asset pipeline.
Our textures are currently authored in 8-bit sRGB, so transforming the data into a different 8-bit gammma space is only going to lose information.
On the next project I'm pushing a 16-bit texture authoring pipeline, which would allow us to do things like this.
I'd be interested to see some comparisons visually as you have to be careful with approximations in Gamma as the error can be low - yet have a big effect.
I checked my approximation into source control and deliberately didn't tell the artists just to see if they'd notice. Three hours later, I'd been sent two almost-identical screenshots of the game, one from the previous build and one from the current build, and was being asked why the gamma had changed!

Below is a cropped version on that screenshot showing the problematic area. The rest of the image is mostly perfect, but there's a range of dark values that have all been squished together in my approximation unsure.png
Left is my quadratic, right is gamma 2.2:
gammatest.png
I'm going to tweak my coefficients some more and see if I can't get some better results in the blacks without ruining everything else.

Share this post


Link to post
Share on other sites
It is worth noting that the real sRGB equation has a linear toe to transition from the pow curve to 0. If you skip this step the values come out quite a bit brighter (much like the X360's piecewise linear sRGB snafu). In addition, if you use pow you will notice you also cant use a base of 0 (NaNs are returned as the pow intrinsic is doing an exp2 & log2 behind the scenes to do the pow). The traditional technique of setting a very tiny minimum allowed non-zero value for the base also causes true black to not ever be output, which does horrible horrible things to the image quality in dark scenes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!