X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=gamma_compression_effect.frag;h=e1e786cdf91426174011568b1367b913f3ff9d80;hp=8944982f6ad19c20161bcd077811b50cb20c46a5;hb=0cdd6e9e30a807056699ed9b3fa023a23ce83af9;hpb=2ced784c6599cb0b21427481ee17f4c8f6afdada diff --git a/gamma_compression_effect.frag b/gamma_compression_effect.frag index 8944982..e1e786c 100644 --- a/gamma_compression_effect.frag +++ b/gamma_compression_effect.frag @@ -1,11 +1,24 @@ -// Compress to sRGB gamma curve. +// Compress gamma curve. + +uniform float PREFIX(linear_scale); +uniform float PREFIX(c0), PREFIX(c1), PREFIX(c2), PREFIX(c3), PREFIX(c4); +uniform float PREFIX(beta); vec4 FUNCNAME(vec2 tc) { - vec4 x = LAST_INPUT(tc); + vec4 x = INPUT(tc); + + // We could reasonably get values outside (0.0, 1.0), but the formulas below + // are not valid outside that range, so clamp before we do anything else. + x.rgb = clamp(x.rgb, 0.0, 1.0); + + vec3 a = x.rgb * PREFIX(linear_scale); + + // Fourth-order polynomial approximation to pow(). See the .cpp file for details. + vec3 s = sqrt(x.rgb); + vec3 b = PREFIX(c0) + (PREFIX(c1) + (PREFIX(c2) + (PREFIX(c3) + PREFIX(c4) * s) * s) * s) * s; - x.r = texture1D(PREFIX(compression_curve_tex), x.r).x; - x.g = texture1D(PREFIX(compression_curve_tex), x.g).x; - x.b = texture1D(PREFIX(compression_curve_tex), x.b).x; + vec3 f = vec3(greaterThan(x.rgb, vec3(PREFIX(beta)))); + x = vec4(mix(a, b, f), x.a); return x; }