]> git.sesse.net Git - movit/blob - gamma_compression_effect.frag
Hard-assert on something that has bitten me too many times now.
[movit] / gamma_compression_effect.frag
1 // Compress gamma curve.
2
3 // Implicit uniforms:
4 // uniform float PREFIX(linear_scale);
5 // uniform float PREFIX(c)[5];
6 // uniform float PREFIX(beta);
7
8 vec4 FUNCNAME(vec2 tc) {
9         vec4 x = INPUT(tc);
10
11         // We could reasonably get values outside (0.0, 1.0), but the formulas below
12         // are not valid outside that range, so clamp before we do anything else.
13         x.rgb = clamp(x.rgb, 0.0, 1.0);
14
15         vec3 a = x.rgb * PREFIX(linear_scale);
16
17         // Fourth-order polynomial approximation to pow(). See the .cpp file for details.
18         vec3 s = sqrt(x.rgb);
19         vec3 b = PREFIX(c)[0] + (PREFIX(c)[1] + (PREFIX(c)[2] + (PREFIX(c)[3] + PREFIX(c)[4] * s) * s) * s) * s;
20
21         vec3 f = vec3(greaterThan(x.rgb, vec3(PREFIX(beta))));
22         x = vec4(mix(a, b, f), x.a);
23
24         return x;
25 }