3 #include "effect_util.h"
4 #include "gamma_compression_effect.h"
11 GammaCompressionEffect::GammaCompressionEffect()
12 : destination_curve(GAMMA_LINEAR)
14 register_int("destination_curve", (int *)&destination_curve);
17 string GammaCompressionEffect::output_fragment_shader()
19 if (destination_curve == GAMMA_LINEAR) {
20 return read_file("identity.frag");
22 if (destination_curve == GAMMA_sRGB ||
23 destination_curve == GAMMA_REC_709 || // Also includes Rec. 601, and 10-bit Rec. 2020.
24 destination_curve == GAMMA_REC_2020_12_BIT) {
25 return read_file("gamma_compression_effect.frag");
30 void GammaCompressionEffect::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 // See GammaExpansionEffect for more details about the approximations in use;
35 // we will primarily deal with the differences here.
37 // Like in expansion, we have a piecewise curve that for very low values
38 // (up to some β) are linear. Above β, we have a power curve that looks
41 // y = ɑ x^ɣ - (ɑ - 1)
43 // Like in expansion, we want to approximate this by some minimax polynomial
44 // in the range β..1. However, in this case, ɣ is typically around 0.4, and
45 // x^0.4 is actually very hard to approximate accurately in this range.
46 // We do a little trick by instead asking for a polynomial of s=sqrt(x),
47 // which means we instead need something like s^0.8, which is much easier.
48 // This warps the input space a bit as seen by the minimax algorithm,
49 // but since we are optimizing for _maximum_ error and not _average_,
50 // we should not add any extra weighting factors.
52 // However, since we have problems reaching the desired accuracy (~25%
53 // of a pixel level), especially for sRGB, we modify w(x) from
54 // GammaExpansionEffect to remove the special handling of the area
55 // around β; it is not really as useful when the next step is just a
56 // dither and round anyway. We keep it around 1, though, since that
57 // seems to hurt less.
59 // The Maple commands this time around become (again using sRGB as an example):
62 // > beta := 0.0031308;
63 // > gamma_ := 1.0/2.4;
64 // > w := x -> piecewise(x > 0.999, 10, 1);
65 // > numapprox[minimax](alpha * (x^2)^gamma_ - (alpha - 1), x=sqrt(beta)..1, [4,0], w(x^2), 'maxerror');
67 // Since the error here is possible to interpret on a uniform scale,
68 // we also show it as a value relative to a 8-, 10- or 12-bit pixel level,
71 if (destination_curve == GAMMA_sRGB) {
72 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
73 // β = 0.0031308, ɣ = 1/2.4.
74 // maxerror = 0.000785 = 0.200 * 255
75 // error at 1.0 = 0.000078 = 0.020 * 255
76 set_uniform_float(glsl_program_num, prefix, "linear_scale", 12.92);
77 set_uniform_float(glsl_program_num, prefix, "c0", -0.03679675939);
78 set_uniform_float(glsl_program_num, prefix, "c1", 1.443803073);
79 set_uniform_float(glsl_program_num, prefix, "c2", -0.9239780987);
80 set_uniform_float(glsl_program_num, prefix, "c3", 0.8060491596);
81 set_uniform_float(glsl_program_num, prefix, "c4", -0.2891558568);
82 set_uniform_float(glsl_program_num, prefix, "beta", 0.0031308);
84 if (destination_curve == GAMMA_REC_709) { // Also includes Rec. 601, and 10-bit Rec. 2020.
85 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018, ɣ = 0.45.
86 // maxerror = 0.000131 = 0.033 * 255 = 0.134 * 1023
87 // error at 1.0 = 0.000013 = 0.003 * 255 = 0.013 * 1023
88 set_uniform_float(glsl_program_num, prefix, "linear_scale", 4.5);
89 set_uniform_float(glsl_program_num, prefix, "c0", -0.08541688528);
90 set_uniform_float(glsl_program_num, prefix, "c1", 1.292793370);
91 set_uniform_float(glsl_program_num, prefix, "c2", -0.4070417645);
92 set_uniform_float(glsl_program_num, prefix, "c3", 0.2923891828);
93 set_uniform_float(glsl_program_num, prefix, "c4", -0.09273699351);
94 set_uniform_float(glsl_program_num, prefix, "beta", 0.018);
96 if (destination_curve == GAMMA_REC_2020_12_BIT) {
97 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181, ɣ = 0.45.
98 // maxerror = 0.000130 = 0.533 * 4095
99 // error at 1.0 = 0.000013 = 0.053 * 4095
101 // Note that this error is above one half of a pixel level,
102 // which means that a few values will actually be off in the lowest
103 // bit. (Removing the constraint for x=1 will only take this down
104 // from 0.553 to 0.501; adding a fifth order can get it down to
105 // 0.167, although this assumes working in fp64 and not fp32.)
106 set_uniform_float(glsl_program_num, prefix, "linear_scale", 4.5);
107 set_uniform_float(glsl_program_num, prefix, "c0", -0.08569685663);
108 set_uniform_float(glsl_program_num, prefix, "c1", 1.293000900);
109 set_uniform_float(glsl_program_num, prefix, "c2", -0.4067291321);
110 set_uniform_float(glsl_program_num, prefix, "c3", 0.2919741179);
111 set_uniform_float(glsl_program_num, prefix, "c4", -0.09256205770);
112 set_uniform_float(glsl_program_num, prefix, "beta", 0.0181);