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