6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
9 #include "flat_input.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
26 static ResourcePool *resource_pool = nullptr;
28 resource_pool = new ResourcePool();
33 // Flip upside-down to compensate for different origin.
35 void vertical_flip(T *data, unsigned width, unsigned height)
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]);
45 void init_movit_for_test()
47 CHECK(init_movit(".", MOVIT_DEBUG_OFF));
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()),
58 framebuffer_format(framebuffer_format),
62 CHECK(init_movit(".", MOVIT_DEBUG_OFF));
64 if (data != nullptr) {
65 add_input(data, pixel_format, color_space, gamma_curve);
69 EffectChainTester::~EffectChainTester()
73 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
76 format.color_space = color_space;
77 format.gamma_curve = gamma_curve;
79 if (input_width == -1) {
82 if (input_height == -1) {
83 input_height = height;
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);
92 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
95 format.color_space = color_space;
96 format.gamma_curve = gamma_curve;
98 if (input_width == -1) {
101 if (input_height == -1) {
102 input_height = height;
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);
111 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
113 internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
116 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
118 internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
121 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
123 internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
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)
128 internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
131 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
133 internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
136 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
138 internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
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)
143 internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
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)
148 internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
151 void EffectChainTester::run(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
153 internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format);
156 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
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);
161 #ifdef HAVE_BENCHMARK
163 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
165 internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
168 void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
170 internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
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)
175 internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
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)
180 internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
183 void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
185 internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
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)
190 internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
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)
195 internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
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)
200 internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
203 void EffectChainTester::benchmark(benchmark::State &state, uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
205 internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format, &state);
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)
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);
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
223 finalize_chain(color_space, gamma_curve, alpha_format);
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) {
233 } else if (framebuffer_format == GL_RGB10_A2) {
234 type = GL_UNSIGNED_INT_2_10_10_10_REV;
236 // Add more here as needed.
240 unsigned num_outputs;
241 if (out_data4 != nullptr) {
243 } else if (out_data3 != nullptr) {
245 } else if (out_data2 != nullptr) {
251 GLuint fbo, texnum[4];
253 glGenTextures(num_outputs, texnum);
255 for (unsigned i = 0; i < num_outputs; ++i) {
256 glBindTexture(GL_TEXTURE_2D, texnum[i]);
258 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, nullptr);
262 glGenFramebuffers(1, &fbo);
264 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
266 for (unsigned i = 0; i < num_outputs; ++i) {
267 glFramebufferTexture2D(
269 GL_COLOR_ATTACHMENT0 + i,
276 GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
277 glDrawBuffers(num_outputs, bufs);
279 chain.render_to_fbo(fbo, width, height);
281 #ifdef HAVE_BENCHMARK
282 // If running benchmarks: Now we've warmed up everything, so let's run the
283 // actual benchmark loop.
284 if (benchmark_state != nullptr) {
286 size_t iters = benchmark_state->max_iterations;
287 for (auto _ : *benchmark_state) {
288 chain.render_to_fbo(fbo, width, height);
293 benchmark_state->SetItemsProcessed(benchmark_state->iterations() * width * height);
297 T *data[4] = { out_data, out_data2, out_data3, out_data4 };
299 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
301 for (unsigned i = 0; i < num_outputs; ++i) {
303 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
304 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
305 // GLES will only read GL_RGBA.
306 T *temp = new T[width * height * 4];
307 glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
309 if (format == GL_ALPHA) {
310 for (unsigned i = 0; i < width * height; ++i) {
311 ptr[i] = temp[i * 4 + 3];
313 } else if (format == GL_BLUE) {
314 for (unsigned i = 0; i < width * height; ++i) {
315 ptr[i] = temp[i * 4 + 2];
318 for (unsigned i = 0; i < width * height; ++i) {
319 ptr[i] = temp[i * 4];
324 glReadPixels(0, 0, width, height, format, internal_format, ptr);
328 if (format == GL_RGBA && (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT || type == GL_FLOAT)) {
329 vertical_flip(ptr, width * 4, height);
331 vertical_flip(ptr, width, height);
335 glDeleteFramebuffers(1, &fbo);
337 glDeleteTextures(num_outputs, texnum);
341 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
343 chain.add_output(format, alpha_format);
347 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting, GLenum type)
349 chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting, type);
353 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
357 ImageFormat image_format;
358 image_format.color_space = color_space;
359 image_format.gamma_curve = gamma_curve;
360 chain.add_output(image_format, alpha_format);
367 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
369 float largest_difference = -1.0f;
370 float squared_difference = 0.0f;
371 int largest_diff_x = -1, largest_diff_y = -1;
373 for (unsigned y = 0; y < height; ++y) {
374 for (unsigned x = 0; x < width; ++x) {
375 float diff = ref[y * width + x] - result[y * width + x];
376 if (fabs(diff) > largest_difference) {
377 largest_difference = fabs(diff);
381 squared_difference += diff * diff;
385 EXPECT_LT(largest_difference, largest_difference_limit)
386 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
387 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
388 << "Result: " << result[largest_diff_y * width + largest_diff_x];
390 float rms = sqrt(squared_difference) / (width * height);
391 EXPECT_LT(rms, rms_limit);
393 if (largest_difference >= largest_difference_limit || isnan(rms) || rms >= rms_limit) {
394 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
396 fprintf(stderr, "Reference:\n");
397 for (unsigned y = 0; y < height; ++y) {
398 for (unsigned x = 0; x < width; ++x) {
399 fprintf(stderr, "%7.4f ", ref[y * width + x]);
401 fprintf(stderr, "\n");
404 fprintf(stderr, "\nResult:\n");
405 for (unsigned y = 0; y < height; ++y) {
406 for (unsigned x = 0; x < width; ++x) {
407 fprintf(stderr, "%7.4f ", result[y * width + x]);
409 fprintf(stderr, "\n");
414 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
419 float *ref_float = new float[width * height];
420 float *result_float = new float[width * height];
422 for (unsigned y = 0; y < height; ++y) {
423 for (unsigned x = 0; x < width; ++x) {
424 ref_float[y * width + x] = ref[y * width + x];
425 result_float[y * width + x] = result[y * width + x];
429 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
432 delete[] result_float;
435 void expect_equal(const uint16_t *ref, const uint16_t *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
440 float *ref_float = new float[width * height];
441 float *result_float = new float[width * height];
443 for (unsigned y = 0; y < height; ++y) {
444 for (unsigned x = 0; x < width; ++x) {
445 ref_float[y * width + x] = ref[y * width + x];
446 result_float[y * width + x] = result[y * width + x];
450 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
453 delete[] result_float;
456 void expect_equal(const int *ref, const int *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
461 float *ref_float = new float[width * height];
462 float *result_float = new float[width * height];
464 for (unsigned y = 0; y < height; ++y) {
465 for (unsigned x = 0; x < width; ++x) {
466 ref_float[y * width + x] = ref[y * width + x];
467 result_float[y * width + x] = result[y * width + x];
471 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
474 delete[] result_float;
477 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)
479 double squared_difference = 0.0;
480 for (unsigned i = 0; i < num_values; ++i) {
481 double absolute_error = fabs(expected[i] - result[i]);
482 squared_difference += absolute_error * absolute_error;
483 EXPECT_LT(absolute_error, absolute_error_limit);
485 if (expected[i] > 0.0) {
486 double relative_error = fabs(absolute_error / expected[i]);
488 EXPECT_LT(relative_error, relative_error_limit);
490 if (i < num_values - 1) {
491 double delta = expected[i + 1] - expected[i];
492 double local_relative_error = fabs(absolute_error / delta);
493 EXPECT_LT(local_relative_error, local_relative_error_limit);
496 double rms = sqrt(squared_difference) / num_values;
497 EXPECT_LT(rms, rms_limit);
500 double srgb_to_linear(double x)
502 // From the Wikipedia article on sRGB.
506 return pow((x + 0.055) / 1.055, 2.4);
510 double linear_to_srgb(double x)
512 // From the Wikipedia article on sRGB.
516 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;
520 DisableComputeShadersTemporarily::DisableComputeShadersTemporarily(bool disable_compute_shaders)
521 : disable_compute_shaders(disable_compute_shaders)
523 init_movit_for_test();
524 saved_compute_shaders_supported = movit_compute_shaders_supported;
525 if (disable_compute_shaders) {
526 movit_compute_shaders_supported = false;
530 DisableComputeShadersTemporarily::~DisableComputeShadersTemporarily()
532 movit_compute_shaders_supported = saved_compute_shaders_supported;
535 bool DisableComputeShadersTemporarily::should_skip()
537 if (disable_compute_shaders) {
541 if (!movit_compute_shaders_supported) {
542 fprintf(stderr, "Compute shaders not supported; skipping.\n");