Jump to content

  • Log In with Google      Sign In   
  • Create Account

sRGB approximations


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Hodgman   Moderators   -  Reputation: 30424

Like
0Likes
Like

Posted 22 March 2012 - 11:21 PM

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 linear->sRGB and sRGB->linear 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 sRGB is the same as gamma2.2 colour space, and use pow(y, 1/2.2) and pow(x,2.2) to perform linear->gamma2.2 and gamma2.2->linear conversions.

..however, these old GPU's are also pretty slow at implementing pow, 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:


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):


Cost: 2x madd, 1x sqrt

Sponsor:

#2 Promit   Moderators   -  Reputation: 7214

Like
1Likes
Like

Posted 22 March 2012 - 11:26 PM

How much fidelity do you need? You could simply go with the old school technique of a sample from a 1D texture lookup table.

#3 Chris_F   Members   -  Reputation: 2388

Like
1Likes
Like

Posted 23 March 2012 - 02:06 AM

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.

#4 mikiex   Members   -  Reputation: 237

Like
0Likes
Like

Posted 24 March 2012 - 03:53 PM

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

#5 Hodgman   Moderators   -  Reputation: 30424

Like
0Likes
Like

Posted 04 April 2012 - 12:33 AM

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 Posted Image
Left is my quadratic, right is gamma 2.2:
Posted Image
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.

#6 Zoner   Members   -  Reputation: 232

Like
0Likes
Like

Posted 08 April 2012 - 03:43 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS