]> git.sesse.net Git - movit/blob - gamma_expansion_effect.cpp
Reduce the boilerplate around uniforms a bit.
[movit] / gamma_expansion_effect.cpp
1 #include <assert.h>
2
3 #include "effect_util.h"
4 #include "gamma_expansion_effect.h"
5 #include "util.h"
6
7 using namespace std;
8
9 namespace movit {
10
11 GammaExpansionEffect::GammaExpansionEffect()
12         : source_curve(GAMMA_LINEAR)
13 {
14         register_int("source_curve", (int *)&source_curve);
15         register_uniform_float("linear_scale", &uniform_linear_scale);
16         register_uniform_float("c0", &uniform_c0);
17         register_uniform_float("c1", &uniform_c1);
18         register_uniform_float("c2", &uniform_c2);
19         register_uniform_float("c3", &uniform_c3);
20         register_uniform_float("c4", &uniform_c4);
21         register_uniform_float("beta", &uniform_beta);
22 }
23
24 string GammaExpansionEffect::output_fragment_shader()
25 {
26         if (source_curve == GAMMA_LINEAR) {
27                 return read_file("identity.frag");
28         }
29         if (source_curve == GAMMA_sRGB ||
30             source_curve == GAMMA_REC_709 ||  // Also includes Rec. 601, and 10-bit Rec. 2020.
31             source_curve == GAMMA_REC_2020_12_BIT) {
32                 return read_file("gamma_expansion_effect.frag");
33         }
34         assert(false);
35 }
36
37 void GammaExpansionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
38 {
39         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
40
41         // All of these curves follow a continuous curve that's piecewise defined;
42         // very low values (up to some β) are linear. Above β, we have a power curve
43         // that looks like this:
44         //
45         //   y = ((x + ɑ - 1) / ɑ)^ɣ
46         //
47         // However, pow() is relatively slow in GLSL, so we approximate this
48         // part by a minimax polynomial, whose coefficients are precalculated
49         // in Maple. (It is very hard to accurately model the curve as a whole
50         // using minimax polynomials; both Maple and Mathematica generally
51         // just error out if you ask them to optimize over 0..1 with a higher-degree
52         // polynomial.)
53         //
54         // We put some extra weight on areas near β to keep a continuous curve,
55         // and near 1.0, since we'd really like f(1.0) = 1.0, or approximately so.
56         // The following Maple commands, using sRGB below as an example, will
57         // compute the coefficients:
58         //
59         // > alpha := 1.055;
60         // > beta := 0.04045;
61         // > gamma_ := 2.4;
62         // > w := x -> piecewise(x < beta + 0.001, 10, x > 0.999, 10, 1);
63         // > numapprox[minimax](((x + alpha - 1) / alpha)^gamma_, x=beta..1, [4,0], w(x), 'maxerror');
64         //
65         // The variable 'maxerror' will then contain the maximum absolute error
66         // at any point of the curve, and we report this along with the absolute
67         // error at beta and at 1.0. Keep in mind that along this curve,
68         // the smallest minimum difference between any two 8-bit sRGB pixel levels
69         // (in the exponential part of the curve) in linear light is that
70         // between 11/255 and 12/255, which is about 0.00033 (or three to four
71         // times of the sRGB maxerror below). The choice of a fourth-degree
72         // polynomial was made with this in mind; we have not cared equally
73         // much about 10- and 12-bit Rec. 2020.
74         //
75         // NOTE: The error at beta is compared to the _linear_ part of the curve.
76         // Since the standards give these with only a few decimals, it means that
77         // the linear and exponential parts will not match up exactly, and even
78         // a perfect approximation will have error > 0 here; sometimes, even larger
79         // than maxerror for the curve itself.
80
81         if (source_curve == GAMMA_sRGB) {
82                 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
83                 // β = 0.04045, ɣ = 2.4.
84                 // maxerror      = 0.000094
85                 // error at beta = 0.000012
86                 // error at 1.0  = 0.000012
87                 //
88                 // Note that the worst _relative_ error by far is just at the beginning
89                 // of the exponential curve, ie., just around β.
90                 uniform_linear_scale = 1.0 / 12.92;
91                 uniform_c0 = 0.001324469581;
92                 uniform_c1 = 0.02227416690;
93                 uniform_c2 = 0.5917615253;
94                 uniform_c3 = 0.4733532353;
95                 uniform_c4 = -0.08880738120;
96                 uniform_beta = 0.04045;
97         }
98         if (source_curve == GAMMA_REC_709) {  // Also includes Rec. 601, and 10-bit Rec. 2020.
99                 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018 * 4.5, ɣ = 1/0.45.
100                 // maxerror      = 0.000043
101                 // error at beta = 0.000051 (see note above!)
102                 // error at 1.0  = 0.000004
103                 //
104                 // Note that Rec. 2020 only gives the other direction, which is why
105                 // our beta and gamma are different from the numbers mentioned
106                 // (we've inverted the formula).
107                 uniform_linear_scale = 1.0 / 4.5;
108                 uniform_c0 = 0.005137028744;
109                 uniform_c1 = 0.09802596889;
110                 uniform_c2 = 0.7255768864;
111                 uniform_c3 = 0.2135067966;
112                 uniform_c4 = -0.04225094667;
113                 uniform_beta = 0.018 * 4.5;
114         }
115         if (source_curve == GAMMA_REC_2020_12_BIT) {
116                 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181 * 4.5, ɣ = 1/0.45.
117                 // maxerror      = 0.000042
118                 // error at beta = 0.000005
119                 // error at 1.0  = 0.000004
120                 //
121                 // Note that Rec. 2020 only gives the other direction, which is why
122                 // our beta and gamma are different from the numbers mentioned
123                 // (we've inverted the formula).
124                 uniform_linear_scale = 1.0 / 4.5;
125                 uniform_c0 = 0.005167545928;
126                 uniform_c1 = 0.09835585809;
127                 uniform_c2 = 0.7254820139;
128                 uniform_c3 = 0.2131291155;
129                 uniform_c4 = -0.04213877222;
130                 uniform_beta = 0.0181 * 4.5;
131         }
132 }
133
134 }  // namespace movit