]> git.sesse.net Git - movit/blob - test_util.cpp
Unbreak FBO caching in unit tests.
[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         CHECK(init_movit(".", MOVIT_DEBUG_OFF));
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 unsigned char *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_UNSIGNED_BYTE, input_width, input_height);
106         input->set_pixel_data(data);
107         chain.add_input(input);
108         return input;
109 }
110
111 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
112 {
113         internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
114 }
115
116 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
117 {
118         internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
119 }
120
121 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
122 {
123         internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
124 }
125
126 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, float *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
127 {
128         internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
129 }
130
131 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
132 {
133         internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
134 }
135
136 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
137 {
138         internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
139 }
140
141 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
142 {
143         internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
144 }
145
146 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, unsigned char *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
147 {
148         internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
149 }
150
151 void EffectChainTester::run(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
152 {
153         internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format);
154 }
155
156 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
157 {
158         internal_run<uint32_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
159 }
160
161 #ifdef HAVE_BENCHMARK
162
163 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
164 {
165         internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
166 }
167
168 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
169 {
170         internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
171 }
172
173 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
174 {
175         internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
176 }
177
178 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, float *out_data3, float *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
179 {
180         internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
181 }
182
183 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
184 {
185         internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
186 }
187
188 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
189 {
190         internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
191 }
192
193 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
194 {
195         internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
196 }
197
198 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, unsigned char *out_data4, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
199 {
200         internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
201 }
202
203 void EffectChainTester::benchmark(benchmark::State &state, uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
204 {
205         internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format, &state);
206 }
207
208 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)
209 {
210         internal_run<uint32_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format, &state);
211 }
212
213 #endif
214
215 template<class T>
216 void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, T *out_data4, GLenum internal_format, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format
217 #ifdef HAVE_BENCHMARK
218 , benchmark::State *benchmark_state
219 #endif
220 )
221 {
222         if (!finalized) {
223                 finalize_chain(color_space, gamma_curve, alpha_format);
224         }
225
226         GLuint type;
227         if (framebuffer_format == GL_RGBA8) {
228                 type = GL_UNSIGNED_BYTE;
229         } else if (framebuffer_format == GL_RGBA16) {
230                 type = GL_UNSIGNED_SHORT;
231         } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
232                 type = GL_FLOAT;
233         } else if (framebuffer_format == GL_RGB10_A2) {
234                 type = GL_UNSIGNED_INT_2_10_10_10_REV;
235         } else {
236                 // Add more here as needed.
237                 assert(false);
238         }
239
240         unsigned num_outputs;
241         if (out_data4 != nullptr) {
242                 num_outputs = 4;
243         } else if (out_data3 != nullptr) {
244                 num_outputs = 3;
245         } else if (out_data2 != nullptr) {
246                 num_outputs = 2;
247         } else {
248                 num_outputs = 1;
249         }
250
251         vector<EffectChain::DestinationTexture> textures;
252         for (unsigned i = 0; i < num_outputs; ++i) {
253                 GLuint texnum = chain.get_resource_pool()->create_2d_texture(framebuffer_format, width, height);
254                 textures.push_back(EffectChain::DestinationTexture{texnum, framebuffer_format});
255         }
256
257         chain.render_to_texture(textures, width, height);
258
259 #ifdef HAVE_BENCHMARK
260         // If running benchmarks: Now we've warmed up everything, so let's run the
261         // actual benchmark loop.
262         if (benchmark_state != nullptr) {
263                 glFinish();
264                 size_t iters = benchmark_state->max_iterations;
265                 for (auto _ : *benchmark_state) {
266                         chain.render_to_texture(textures, width, height);
267                         if (--iters == 0) {
268                                 glFinish();
269                         }
270                 }
271                 benchmark_state->SetItemsProcessed(benchmark_state->iterations() * width * height);
272         }
273 #endif
274
275         T *data[4] = { out_data, out_data2, out_data3, out_data4 };
276
277         for (unsigned i = 0; i < num_outputs; ++i) {
278                 T *ptr = data[i];
279                 glBindTexture(GL_TEXTURE_2D, textures[i].texnum);
280                 check_error();
281                 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
282                         // GLES will only read GL_RGBA.
283                         T *temp = new T[width * height * 4];
284                         glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, type, temp);
285                         check_error();
286                         if (format == GL_ALPHA) {
287                                 for (unsigned i = 0; i < width * height; ++i) {
288                                         ptr[i] = temp[i * 4 + 3];
289                                 }
290                         } else if (format == GL_BLUE) {
291                                 for (unsigned i = 0; i < width * height; ++i) {
292                                         ptr[i] = temp[i * 4 + 2];
293                                 }
294                         } else {
295                                 for (unsigned i = 0; i < width * height; ++i) {
296                                         ptr[i] = temp[i * 4];
297                                 }
298                         }
299                         delete[] temp;
300                 } else {
301                         glGetTexImage(GL_TEXTURE_2D, 0, format, type, ptr);
302                         check_error();
303                 }
304
305                 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT || type == GL_FLOAT)) {
306                         vertical_flip(ptr, width * 4, height);
307                 } else {
308                         vertical_flip(ptr, width, height);
309                 }
310         }
311
312         for (unsigned i = 0; i < num_outputs; ++i) {
313                 chain.get_resource_pool()->release_2d_texture(textures[i].texnum);
314         }
315 }
316
317 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
318 {
319         chain.add_output(format, alpha_format);
320         output_added = true;
321 }
322
323 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting, GLenum type)
324 {
325         chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting, type);
326         output_added = true;
327 }
328
329 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
330 {
331         assert(!finalized);
332         if (!output_added) {
333                 ImageFormat image_format;
334                 image_format.color_space = color_space;
335                 image_format.gamma_curve = gamma_curve;
336                 chain.add_output(image_format, alpha_format);
337                 output_added = true;
338         }
339         chain.finalize();
340         finalized = true;
341 }
342
343 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
344 {
345         float largest_difference = -1.0f;
346         float squared_difference = 0.0f;
347         int largest_diff_x = -1, largest_diff_y = -1;
348
349         for (unsigned y = 0; y < height; ++y) {
350                 for (unsigned x = 0; x < width; ++x) {
351                         float diff = ref[y * width + x] - result[y * width + x];
352                         if (fabs(diff) > largest_difference) {
353                                 largest_difference = fabs(diff);
354                                 largest_diff_x = x;
355                                 largest_diff_y = y;
356                         }
357                         squared_difference += diff * diff;
358                 }
359         }
360
361         EXPECT_LT(largest_difference, largest_difference_limit)
362                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
363                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
364                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
365
366         float rms = sqrt(squared_difference) / (width * height);
367         EXPECT_LT(rms, rms_limit);
368
369         if (largest_difference >= largest_difference_limit || isnan(rms) || rms >= rms_limit) {
370                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
371
372                 fprintf(stderr, "Reference:\n");
373                 for (unsigned y = 0; y < height; ++y) {
374                         for (unsigned x = 0; x < width; ++x) {
375                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
376                         }
377                         fprintf(stderr, "\n");
378                 }
379
380                 fprintf(stderr, "\nResult:\n");
381                 for (unsigned y = 0; y < height; ++y) {
382                         for (unsigned x = 0; x < width; ++x) {
383                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
384                         }
385                         fprintf(stderr, "\n");
386                 }
387         }
388 }
389
390 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
391 {
392         assert(width > 0);
393         assert(height > 0);
394
395         float *ref_float = new float[width * height];
396         float *result_float = new float[width * height];
397
398         for (unsigned y = 0; y < height; ++y) {
399                 for (unsigned x = 0; x < width; ++x) {
400                         ref_float[y * width + x] = ref[y * width + x];
401                         result_float[y * width + x] = result[y * width + x];
402                 }
403         }
404
405         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
406
407         delete[] ref_float;
408         delete[] result_float;
409 }
410
411 void expect_equal(const uint16_t *ref, const uint16_t *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
412 {
413         assert(width > 0);
414         assert(height > 0);
415
416         float *ref_float = new float[width * height];
417         float *result_float = new float[width * height];
418
419         for (unsigned y = 0; y < height; ++y) {
420                 for (unsigned x = 0; x < width; ++x) {
421                         ref_float[y * width + x] = ref[y * width + x];
422                         result_float[y * width + x] = result[y * width + x];
423                 }
424         }
425
426         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
427
428         delete[] ref_float;
429         delete[] result_float;
430 }
431
432 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
433 {
434         assert(width > 0);
435         assert(height > 0);
436
437         float *ref_float = new float[width * height];
438         float *result_float = new float[width * height];
439
440         for (unsigned y = 0; y < height; ++y) {
441                 for (unsigned x = 0; x < width; ++x) {
442                         ref_float[y * width + x] = ref[y * width + x];
443                         result_float[y * width + x] = result[y * width + x];
444                 }
445         }
446
447         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
448
449         delete[] ref_float;
450         delete[] result_float;
451 }
452
453 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)
454 {
455         double squared_difference = 0.0;
456         for (unsigned i = 0; i < num_values; ++i) {
457                 double absolute_error = fabs(expected[i] - result[i]);
458                 squared_difference += absolute_error * absolute_error;
459                 EXPECT_LT(absolute_error, absolute_error_limit);
460
461                 if (expected[i] > 0.0) {
462                         double relative_error = fabs(absolute_error / expected[i]);
463
464                         EXPECT_LT(relative_error, relative_error_limit);
465                 }
466                 if (i < num_values - 1) {
467                         double delta = expected[i + 1] - expected[i];
468                         double local_relative_error = fabs(absolute_error / delta);
469                         EXPECT_LT(local_relative_error, local_relative_error_limit);
470                 }
471         }
472         double rms = sqrt(squared_difference) / num_values;
473         EXPECT_LT(rms, rms_limit);
474 }
475
476 double srgb_to_linear(double x)
477 {
478         // From the Wikipedia article on sRGB.
479         if (x < 0.04045) {
480                 return x / 12.92;
481         } else {
482                 return pow((x + 0.055) / 1.055, 2.4);
483         }
484 }
485
486 double linear_to_srgb(double x)
487 {
488         // From the Wikipedia article on sRGB.
489         if (x < 0.0031308) {
490                 return 12.92 * x;
491         } else {
492                 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
493         }
494 }
495
496 DisableComputeShadersTemporarily::DisableComputeShadersTemporarily(bool disable_compute_shaders)
497         : disable_compute_shaders(disable_compute_shaders)
498 {
499         init_movit_for_test();
500         saved_compute_shaders_supported = movit_compute_shaders_supported;
501         if (disable_compute_shaders) {
502                 movit_compute_shaders_supported = false;
503         }
504 }
505
506 DisableComputeShadersTemporarily::~DisableComputeShadersTemporarily()
507 {
508         movit_compute_shaders_supported = saved_compute_shaders_supported;
509 }
510
511 bool DisableComputeShadersTemporarily::should_skip()
512 {
513         if (disable_compute_shaders) {
514                 return false;
515         }
516
517         if (!movit_compute_shaders_supported) {
518                 fprintf(stderr, "Compute shaders not supported; skipping.\n");
519                 return true;
520         }
521         return false;
522 }
523
524 #ifdef HAVE_BENCHMARK
525 bool DisableComputeShadersTemporarily::should_skip(benchmark::State *benchmark_state)
526 {
527         if (disable_compute_shaders) {
528                 return false;
529         }
530
531         if (!movit_compute_shaders_supported) {
532                 benchmark_state->SkipWithError("Compute shaders not supported");
533                 return true;
534         }
535         return false;
536 }
537 #endif
538
539 }  // namespace movit