]> git.sesse.net Git - movit/blob - gamma_expansion_effect_test.cpp
We've run with many more GPUs than just those three now.
[movit] / gamma_expansion_effect_test.cpp
1 // Unit tests for GammaExpansionEffect.
2
3 #include <epoxy/gl.h>
4 #include <math.h>
5
6 #include "gamma_expansion_effect.h"
7 #include "gtest/gtest.h"
8 #include "gtest/gtest-message.h"
9 #include "test_util.h"
10
11 namespace movit {
12
13 TEST(GammaExpansionEffectTest, sRGB_KeyValues) {
14         float data[] = {
15                 0.0f, 1.0f,
16                 0.040f, 0.041f,  // On either side of the discontinuity.
17         };
18         float expected_data[] = {
19                 0.0f, 1.0f,
20                 0.00309f, 0.00317f, 
21         };
22         float out_data[4];
23         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
24         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
25
26         expect_equal(expected_data, out_data, 2, 2);
27 }
28
29 TEST(GammaExpansionEffectTest, sRGB_RampAlwaysIncreases) {
30         float data[256], out_data[256];
31         for (unsigned i = 0; i < 256; ++i) {
32                 data[i] = i / 255.0f;
33         }
34         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
35         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
36
37         for (unsigned i = 1; i < 256; ++i) {
38                 EXPECT_GT(out_data[i], out_data[i - 1])
39                    << "No increase between " << i-1 << " and " << i;
40         }
41 }
42
43 TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
44         float data[] = {
45                 0.0f, 0.0f, 0.0f, 0.0f,
46                 0.0f, 0.0f, 0.0f, 0.25f,
47                 0.0f, 0.0f, 0.0f, 0.5f,
48                 0.0f, 0.0f, 0.0f, 0.75f,
49                 0.0f, 0.0f, 0.0f, 1.0f,
50         };
51         float out_data[5 * 4];
52         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
53         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
54
55         expect_equal(data, out_data, 5, 1);
56 }
57
58 TEST(GammaExpansionEffectTest, sRGB_Accuracy) {
59         float data[256], expected_data[256], out_data[256];
60
61         for (int i = 0; i < 256; ++i) {
62                 double x = i / 255.0;
63
64                 data[i] = x;
65                 expected_data[i] = srgb_to_linear(x);
66         }
67
68         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
69         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
70
71         // Accuracy limits; for comparison, limits for a straightforward ALU solution
72         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
73         // indicate limitations of float arithmetic etc.:
74         //
75         //   Maximum absolute error: 0.1% of max energy (0.051%)
76         //   Maximum relative error: 2.5% of correct answer (0.093%)
77         //                           25% of difference to next pixel level (6.18%)
78         //   Allowed RMS error:      0.0001 (0.000010)
79         //
80         test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
81 }
82
83 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
84         float data[] = {
85                 0.0f, 1.0f,
86                 0.080f, 0.082f,  // On either side of the discontinuity.
87         };
88         float expected_data[] = {
89                 0.0f, 1.0f,
90                 0.017778f, 0.018167f,
91         };
92         float out_data[4];
93         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
94         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
95
96         expect_equal(expected_data, out_data, 2, 2);
97 }
98
99 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
100         float data[256], out_data[256];
101         for (unsigned i = 0; i < 256; ++i) {
102                 data[i] = i / 255.0f;
103         }
104         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
105         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
106
107         for (unsigned i = 1; i < 256; ++i) {
108                 EXPECT_GT(out_data[i], out_data[i - 1])
109                    << "No increase between " << i-1 << " and " << i;
110         }
111 }
112
113 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
114         float data[] = {
115                 0.0f, 0.0f, 0.0f, 0.0f,
116                 0.0f, 0.0f, 0.0f, 0.25f,
117                 0.0f, 0.0f, 0.0f, 0.5f,
118                 0.0f, 0.0f, 0.0f, 0.75f,
119                 0.0f, 0.0f, 0.0f, 1.0f,
120         };
121         float out_data[5 * 4];
122         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
123         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
124
125         expect_equal(data, out_data, 5, 1);
126 }
127
128 TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
129         float data[256], expected_data[256], out_data[256];
130
131         for (int i = 0; i < 256; ++i) {
132                 double x = i / 255.0;
133
134                 data[i] = x;
135
136                 // Rec. 2020, page 3.
137                 if (x < 0.018 * 4.5) {
138                         expected_data[i] = x / 4.5;
139                 } else {
140                         expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
141                 }
142         }
143
144         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
145         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
146
147         // Accuracy limits; for comparison, limits for a straightforward ALU solution
148         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
149         // indicate limitations of float arithmetic etc.:
150         //
151         //   Maximum absolute error: 0.1% of max energy (0.046%)
152         //   Maximum relative error: 1.0% of correct answer (0.080%)
153         //                           10% of difference to next pixel level (6.19%)
154         //   Allowed RMS error:      0.0001 (0.000010)
155         //
156         test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
157 }
158
159 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
160 // input range and somewhat looser error bounds. (One could claim that this is
161 // already on the limit of what we can reasonably do with fp16 input, if you
162 // look at the local relative error.)
163 TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
164         float data[1024], expected_data[1024], out_data[1024];
165
166         for (int i = 0; i < 1024; ++i) {
167                 double x = i / 1023.0;
168
169                 data[i] = x;
170
171                 // Rec. 2020, page 3.
172                 if (x < 0.018 * 4.5) {
173                         expected_data[i] = x / 4.5;
174                 } else {
175                         expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
176                 }
177         }
178
179         EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
180         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
181
182         // Accuracy limits; for comparison, limits for a straightforward ALU solution
183         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
184         // indicate limitations of float arithmetic etc.:
185         //
186         //   Maximum absolute error: 0.1% of max energy (0.036%)
187         //   Maximum relative error: 1.0% of correct answer (0.064%)
188         //                           30% of difference to next pixel level (24.9%)
189         //   Allowed RMS error:      0.0001 (0.000005)
190         //
191         test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
192 }
193
194 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
195         float data[256];
196         for (unsigned i = 0; i < 256; ++i) {
197                 data[i] = i / 255.0f;
198         }
199         float out_data_709[256];
200         float out_data_2020[256];
201
202         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
203         tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
204         EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
205         tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
206
207         double sqdiff = 0.0;
208         for (unsigned i = 0; i < 256; ++i) {
209                 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
210                 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
211         }
212         EXPECT_GT(sqdiff, 1e-6);
213 }
214
215 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
216 // all of the possible 12-bit input values (every other level translates to the
217 // same value). Thus, this test has extremely loose bounds; if we ever decide
218 // to start supporting fp32, we should re-run this and tighten them a lot.
219 TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
220         float data[4096], expected_data[4096], out_data[4096];
221
222         for (int i = 0; i < 4096; ++i) {
223                 double x = i / 4095.0;
224
225                 data[i] = x;
226
227                 // Rec. 2020, page 3.
228                 if (x < 0.0181 * 4.5) {
229                         expected_data[i] = x / 4.5;
230                 } else {
231                         expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
232                 }
233         }
234
235         EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
236         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
237
238         // Accuracy limits; for comparison, limits for a straightforward ALU solution
239         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
240         // indicate limitations of float arithmetic etc.:
241         //
242         //   Maximum absolute error: 0.1% of max energy (0.050%)
243         //   Maximum relative error: 1.0% of correct answer (0.050%)
244         //                           250% of difference to next pixel level (100.00%)
245         //   Allowed RMS error:      0.0001 (0.000003)
246         //
247         test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);
248 }
249
250 }  // namespace movit