]> git.sesse.net Git - movit/blob - test_util.cpp
Fix some small whitespace errors in SingleResamplePassEffect.
[movit] / test_util.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <algorithm>
5 #include <epoxy/gl.h>
6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
8
9 #include "flat_input.h"
10 #include "init.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 using namespace std;
16
17 namespace movit {
18
19 class Input;
20
21 namespace {
22
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
25 {
26         static ResourcePool *resource_pool = nullptr;
27         if (!resource_pool) {
28                 resource_pool = new ResourcePool();
29         }
30         return resource_pool;
31 }
32
33 // Flip upside-down to compensate for different origin.
34 template<class T>
35 void vertical_flip(T *data, unsigned width, unsigned height)
36 {
37         for (unsigned y = 0; y < height / 2; ++y) {
38                 unsigned flip_y = height - y - 1;
39                 for (unsigned x = 0; x < width; ++x) {
40                         swap(data[y * width + x], data[flip_y * width + x]);
41                 }
42         }
43 }
44
45 void init_movit_for_test()
46 {
47        CHECK(init_movit(".", MOVIT_DEBUG_OFF));
48 }
49
50 }  // namespace
51
52 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
53                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
54                                      GLenum framebuffer_format)
55         : chain(width, height, get_static_pool()),
56           width(width),
57           height(height),
58           framebuffer_format(framebuffer_format),
59           output_added(false),
60           finalized(false)
61 {
62         init_movit_for_test();
63
64         if (data != nullptr) {
65                 add_input(data, pixel_format, color_space, gamma_curve);
66         }
67 }
68
69 EffectChainTester::~EffectChainTester()
70 {
71 }
72
73 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
74 {
75         ImageFormat format;
76         format.color_space = color_space;
77         format.gamma_curve = gamma_curve;
78
79         if (input_width == -1) {
80                 input_width = width;
81         }
82         if (input_height == -1) {
83                 input_height = height;
84         }
85
86         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
87         input->set_pixel_data(data);
88         chain.add_input(input);
89         return input;
90 }
91
92 Input *EffectChainTester::add_input(const fp16_int_t *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
93 {
94         ImageFormat format;
95         format.color_space = color_space;
96         format.gamma_curve = gamma_curve;
97
98         if (input_width == -1) {
99                 input_width = width;
100         }
101         if (input_height == -1) {
102                 input_height = height;
103         }
104
105         FlatInput *input = new FlatInput(format, pixel_format, GL_HALF_FLOAT, input_width, input_height);
106         input->set_pixel_data_fp16(data);
107         chain.add_input(input);
108         return input;
109 }
110
111 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
112 {
113         ImageFormat format;
114         format.color_space = color_space;
115         format.gamma_curve = gamma_curve;
116
117         if (input_width == -1) {
118                 input_width = width;
119         }
120         if (input_height == -1) {
121                 input_height = height;
122         }
123
124         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
125         input->set_pixel_data(data);
126         chain.add_input(input);
127         return input;
128 }
129
130 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
131 {
132         internal_run<float>({out_data}, format, color_space, gamma_curve, alpha_format);
133 }
134
135 void EffectChainTester::run(const std::vector<float *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
136 {
137         internal_run<float>(out_data, format, color_space, gamma_curve, alpha_format);
138 }
139
140 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
141 {
142         internal_run<unsigned char>({out_data}, format, color_space, gamma_curve, alpha_format);
143 }
144
145 void EffectChainTester::run(const std::vector<unsigned char *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
146 {
147         internal_run<unsigned char>(out_data, format, color_space, gamma_curve, alpha_format);
148 }
149
150 void EffectChainTester::run(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
151 {
152         internal_run<uint16_t>({out_data}, format, color_space, gamma_curve, alpha_format);
153 }
154
155 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
156 {
157         internal_run<uint32_t>({out_data}, format, color_space, gamma_curve, alpha_format);
158 }
159
160 #ifdef HAVE_BENCHMARK
161
162 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
163 {
164         internal_run<float>({out_data}, format, color_space, gamma_curve, alpha_format, &state);
165 }
166
167 void EffectChainTester::benchmark(benchmark::State &state, const std::vector<float *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
168 {
169         internal_run<float>(out_data, format, color_space, gamma_curve, alpha_format, &state);
170 }
171
172 void EffectChainTester::benchmark(benchmark::State &state, fp16_int_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
173 {
174         internal_run<fp16_int_t>({out_data}, format, color_space, gamma_curve, alpha_format, &state);
175 }
176
177 void EffectChainTester::benchmark(benchmark::State &state, const std::vector<fp16_int_t *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
178 {
179         internal_run<fp16_int_t>(out_data, format, color_space, gamma_curve, alpha_format, &state);
180 }
181
182 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
183 {
184         internal_run<unsigned char>({out_data}, format, color_space, gamma_curve, alpha_format, &state);
185 }
186
187 void EffectChainTester::benchmark(benchmark::State &state, const std::vector<unsigned char *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
188 {
189         internal_run<unsigned char>(out_data, format, color_space, gamma_curve, alpha_format, &state);
190 }
191
192 void EffectChainTester::benchmark(benchmark::State &state, uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
193 {
194         internal_run<uint16_t>({out_data}, format, color_space, gamma_curve, alpha_format, &state);
195 }
196
197 void EffectChainTester::benchmark_10_10_10_2(benchmark::State &state, uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
198 {
199         internal_run<uint32_t>({out_data}, format, color_space, gamma_curve, alpha_format, &state);
200 }
201
202 #endif
203
204 template<class T>
205 void EffectChainTester::internal_run(const std::vector<T *> &out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format
206 #ifdef HAVE_BENCHMARK
207 , benchmark::State *benchmark_state
208 #endif
209 )
210 {
211         if (!finalized) {
212                 finalize_chain(color_space, gamma_curve, alpha_format);
213         }
214
215         GLuint type;
216         if (framebuffer_format == GL_RGBA8) {
217                 type = GL_UNSIGNED_BYTE;
218         } else if (framebuffer_format == GL_RGBA16) {
219                 type = GL_UNSIGNED_SHORT;
220         } else if (framebuffer_format == GL_RGBA16F && sizeof(T) == 2) {
221                 type = GL_HALF_FLOAT;
222         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
223                 type = GL_FLOAT;
224         } else if (framebuffer_format == GL_RGB10_A2) {
225                 type = GL_UNSIGNED_INT_2_10_10_10_REV;
226         } else {
227                 // Add more here as needed.
228                 assert(false);
229         }
230
231         glActiveTexture(GL_TEXTURE0);
232         check_error();
233
234         vector<EffectChain::DestinationTexture> textures;
235         for (unsigned i = 0; i < out_data.size(); ++i) {
236                 GLuint texnum = chain.get_resource_pool()->create_2d_texture(framebuffer_format, width, height);
237                 textures.push_back(EffectChain::DestinationTexture{texnum, framebuffer_format});
238
239                 // The output texture needs to have valid state to be written to by a compute shader.
240                 glBindTexture(GL_TEXTURE_2D, texnum);
241                 check_error();
242                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
243                 check_error();
244         }
245
246         chain.render_to_texture(textures, width, height);
247
248 #ifdef HAVE_BENCHMARK
249         // If running benchmarks: Now we've warmed up everything, so let's run the
250         // actual benchmark loop.
251         if (benchmark_state != nullptr) {
252                 glFinish();
253                 size_t iters = benchmark_state->max_iterations;
254                 for (auto _ : *benchmark_state) {
255                         chain.render_to_texture(textures, width, height);
256                         if (--iters == 0) {
257                                 glFinish();
258                         }
259                 }
260                 benchmark_state->SetItemsProcessed(benchmark_state->iterations() * width * height);
261         }
262 #endif
263
264         for (unsigned i = 0; i < out_data.size(); ++i) {
265                 T *ptr = out_data[i];
266                 glBindTexture(GL_TEXTURE_2D, textures[i].texnum);
267                 check_error();
268                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
269                         // GLES will only read GL_RGBA.
270                         std::unique_ptr<T[]> temp(new T[width * height * 4]);
271                         glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, type, temp.get());
272                         check_error();
273                         if (format == GL_ALPHA) {
274                                 for (unsigned j = 0; j < width * height; ++j) {
275                                         ptr[j] = temp[j * 4 + 3];
276                                 }
277                         } else if (format == GL_BLUE) {
278                                 for (unsigned j = 0; j < width * height; ++j) {
279                                         ptr[j] = temp[j * 4 + 2];
280                                 }
281                         } else {
282                                 for (unsigned j = 0; j < width * height; ++j) {
283                                         ptr[j] = temp[j * 4];
284                                 }
285                         }
286                 } else {
287                         glGetTexImage(GL_TEXTURE_2D, 0, format, type, ptr);
288                         check_error();
289                 }
290
291                 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT || type == GL_FLOAT)) {
292                         vertical_flip(ptr, width * 4, height);
293                 } else {
294                         vertical_flip(ptr, width, height);
295                 }
296         }
297
298         for (unsigned i = 0; i < out_data.size(); ++i) {
299                 chain.get_resource_pool()->release_2d_texture(textures[i].texnum);
300         }
301 }
302
303 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
304 {
305         chain.add_output(format, alpha_format);
306         output_added = true;
307 }
308
309 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting, GLenum type)
310 {
311         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting, type);
312         output_added = true;
313 }
314
315 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
316 {
317         assert(!finalized);
318         if (!output_added) {
319                 ImageFormat image_format;
320                 image_format.color_space = color_space;
321                 image_format.gamma_curve = gamma_curve;
322                 chain.add_output(image_format, alpha_format);
323                 output_added = true;
324         }
325         chain.finalize();
326         finalized = true;
327 }
328
329 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
330 {
331         float largest_difference = -1.0f;
332         float squared_difference = 0.0f;
333         int largest_diff_x = -1, largest_diff_y = -1;
334
335         for (unsigned y = 0; y < height; ++y) {
336                 for (unsigned x = 0; x < width; ++x) {
337                         float diff = ref[y * width + x] - result[y * width + x];
338                         if (fabs(diff) > largest_difference) {
339                                 largest_difference = fabs(diff);
340                                 largest_diff_x = x;
341                                 largest_diff_y = y;
342                         }
343                         squared_difference += diff * diff;
344                 }
345         }
346
347         EXPECT_LT(largest_difference, largest_difference_limit)
348                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
349                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
350                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
351
352         float rms = sqrt(squared_difference) / (width * height);
353         EXPECT_LT(rms, rms_limit);
354
355         if (largest_difference >= largest_difference_limit || isnan(rms) || rms >= rms_limit) {
356                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
357
358                 fprintf(stderr, "Reference:\n");
359                 for (unsigned y = 0; y < height; ++y) {
360                         for (unsigned x = 0; x < width; ++x) {
361                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
362                         }
363                         fprintf(stderr, "\n");
364                 }
365
366                 fprintf(stderr, "\nResult:\n");
367                 for (unsigned y = 0; y < height; ++y) {
368                         for (unsigned x = 0; x < width; ++x) {
369                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
370                         }
371                         fprintf(stderr, "\n");
372                 }
373         }
374 }
375
376 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
377 {
378         assert(width > 0);
379         assert(height > 0);
380
381         float *ref_float = new float[width * height];
382         float *result_float = new float[width * height];
383
384         for (unsigned y = 0; y < height; ++y) {
385                 for (unsigned x = 0; x < width; ++x) {
386                         ref_float[y * width + x] = ref[y * width + x];
387                         result_float[y * width + x] = result[y * width + x];
388                 }
389         }
390
391         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
392
393         delete[] ref_float;
394         delete[] result_float;
395 }
396
397 void expect_equal(const uint16_t *ref, const uint16_t *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
398 {
399         assert(width > 0);
400         assert(height > 0);
401
402         float *ref_float = new float[width * height];
403         float *result_float = new float[width * height];
404
405         for (unsigned y = 0; y < height; ++y) {
406                 for (unsigned x = 0; x < width; ++x) {
407                         ref_float[y * width + x] = ref[y * width + x];
408                         result_float[y * width + x] = result[y * width + x];
409                 }
410         }
411
412         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
413
414         delete[] ref_float;
415         delete[] result_float;
416 }
417
418 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
419 {
420         assert(width > 0);
421         assert(height > 0);
422
423         float *ref_float = new float[width * height];
424         float *result_float = new float[width * height];
425
426         for (unsigned y = 0; y < height; ++y) {
427                 for (unsigned x = 0; x < width; ++x) {
428                         ref_float[y * width + x] = ref[y * width + x];
429                         result_float[y * width + x] = result[y * width + x];
430                 }
431         }
432
433         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
434
435         delete[] ref_float;
436         delete[] result_float;
437 }
438
439 void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
440 {
441         double squared_difference = 0.0;
442         for (unsigned i = 0; i < num_values; ++i) {
443                 double absolute_error = fabs(expected[i] - result[i]);
444                 squared_difference += absolute_error * absolute_error;
445                 EXPECT_LT(absolute_error, absolute_error_limit);
446
447                 if (expected[i] > 0.0) {
448                         double relative_error = fabs(absolute_error / expected[i]);
449
450                         EXPECT_LT(relative_error, relative_error_limit);
451                 }
452                 if (i < num_values - 1) {
453                         double delta = expected[i + 1] - expected[i];
454                         double local_relative_error = fabs(absolute_error / delta);
455                         EXPECT_LT(local_relative_error, local_relative_error_limit);
456                 }
457         }
458         double rms = sqrt(squared_difference) / num_values;
459         EXPECT_LT(rms, rms_limit);
460 }
461
462 double srgb_to_linear(double x)
463 {
464         // From the Wikipedia article on sRGB.
465         if (x < 0.04045) {
466                 return x / 12.92;
467         } else {
468                 return pow((x + 0.055) / 1.055, 2.4);
469         }
470 }
471
472 double linear_to_srgb(double x)
473 {
474         // From the Wikipedia article on sRGB.
475         if (x < 0.0031308) {
476                 return 12.92 * x;
477         } else {
478                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
479         }
480 }
481
482 DisableComputeShadersTemporarily::DisableComputeShadersTemporarily(bool disable_compute_shaders)
483         : disable_compute_shaders(disable_compute_shaders)
484 {
485         init_movit_for_test();
486         saved_compute_shaders_supported = movit_compute_shaders_supported;
487         if (disable_compute_shaders) {
488                 movit_compute_shaders_supported = false;
489         }
490 }
491
492 DisableComputeShadersTemporarily::~DisableComputeShadersTemporarily()
493 {
494         movit_compute_shaders_supported = saved_compute_shaders_supported;
495 }
496
497 bool DisableComputeShadersTemporarily::should_skip()
498 {
499         if (disable_compute_shaders) {
500                 return false;
501         }
502
503         if (!movit_compute_shaders_supported) {
504                 fprintf(stderr, "Compute shaders not supported; skipping.\n");
505                 return true;
506         }
507         return false;
508 }
509
510 #ifdef HAVE_BENCHMARK
511 bool DisableComputeShadersTemporarily::should_skip(benchmark::State *benchmark_state)
512 {
513         if (disable_compute_shaders) {
514                 return false;
515         }
516
517         if (!movit_compute_shaders_supported) {
518                 benchmark_state->SkipWithError("Compute shaders not supported");
519                 return true;
520         }
521         return false;
522 }
523 #endif
524
525 }  // namespace movit