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