]> git.sesse.net Git - movit/blobdiff - test_util.cpp
Fix another set of test breakages on NVIDIA.
[movit] / test_util.cpp
index 4543ee3354c40213bcb9575db9993fe90a314a6e..d2ed9f8196069c7c0c539e002d6320f9d3620894 100644 (file)
@@ -23,7 +23,7 @@ namespace {
 // Not thread-safe, but this isn't a big problem for testing.
 ResourcePool *get_static_pool()
 {
 // Not thread-safe, but this isn't a big problem for testing.
 ResourcePool *get_static_pool()
 {
-       static ResourcePool *resource_pool = NULL;
+       static ResourcePool *resource_pool = nullptr;
        if (!resource_pool) {
                resource_pool = new ResourcePool();
        }
        if (!resource_pool) {
                resource_pool = new ResourcePool();
        }
@@ -42,6 +42,11 @@ void vertical_flip(T *data, unsigned width, unsigned height)
        }
 }
 
        }
 }
 
+void init_movit_for_test()
+{
+       CHECK(init_movit(".", MOVIT_DEBUG_OFF));
+}
+
 }  // namespace
 
 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
 }  // namespace
 
 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
@@ -56,7 +61,7 @@ EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned
 {
        CHECK(init_movit(".", MOVIT_DEBUG_OFF));
 
 {
        CHECK(init_movit(".", MOVIT_DEBUG_OFF));
 
-       if (data != NULL) {
+       if (data != nullptr) {
                add_input(data, pixel_format, color_space, gamma_curve);
        }
 }
                add_input(data, pixel_format, color_space, gamma_curve);
        }
 }
@@ -105,17 +110,17 @@ Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat
 
 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 
 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<float>(out_data, NULL, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
+       internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
 }
 
 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 }
 
 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<float>(out_data, out_data2, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
+       internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
 }
 
 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 }
 
 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<float>(out_data, out_data2, out_data3, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
+       internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
 }
 
 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)
 }
 
 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)
@@ -125,17 +130,17 @@ void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3,
 
 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 
 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<unsigned char>(out_data, NULL, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
+       internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
 }
 
 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 }
 
 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<unsigned char>(out_data, out_data2, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
+       internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
 }
 
 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)
 {
 }
 
 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)
 {
-       internal_run<unsigned char>(out_data, out_data2, out_data3, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
+       internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
 }
 
 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)
 }
 
 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)
@@ -145,16 +150,74 @@ void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, u
 
 void EffectChainTester::run(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 
 void EffectChainTester::run(uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<uint16_t>(out_data, NULL, NULL, NULL, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format);
+       internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format);
 }
 
 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
 }
 
 void EffectChainTester::run_10_10_10_2(uint32_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
 {
-       internal_run<uint32_t>(out_data, NULL, NULL, NULL, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
+       internal_run<uint32_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_INT_2_10_10_10_REV, format, color_space, gamma_curve, alpha_format);
+}
+
+#ifdef HAVE_BENCHMARK
+
+void EffectChainTester::benchmark(benchmark::State &state, float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
+{
+       internal_run<float>(out_data, nullptr, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+void EffectChainTester::benchmark(benchmark::State &state, float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
+{
+       internal_run<float>(out_data, out_data2, nullptr, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       internal_run<float>(out_data, out_data2, out_data3, nullptr, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       internal_run(out_data, out_data2, out_data3, out_data4, GL_FLOAT, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+void EffectChainTester::benchmark(benchmark::State &state, unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
+{
+       internal_run<unsigned char>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       internal_run<unsigned char>(out_data, out_data2, nullptr, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       internal_run<unsigned char>(out_data, out_data2, out_data3, nullptr, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       internal_run(out_data, out_data2, out_data3, out_data4, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format, &state);
 }
 
 }
 
+void EffectChainTester::benchmark(benchmark::State &state, uint16_t *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
+{
+       internal_run<uint16_t>(out_data, nullptr, nullptr, nullptr, GL_UNSIGNED_SHORT, format, color_space, gamma_curve, alpha_format, &state);
+}
+
+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)
+{
+       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);
+}
+
+#endif
+
 template<class T>
 template<class T>
-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)
+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
+#ifdef HAVE_BENCHMARK
+, benchmark::State *benchmark_state
+#endif
+)
 {
        if (!finalized) {
                finalize_chain(color_space, gamma_curve, alpha_format);
 {
        if (!finalized) {
                finalize_chain(color_space, gamma_curve, alpha_format);
@@ -175,57 +238,59 @@ void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, T
        }
 
        unsigned num_outputs;
        }
 
        unsigned num_outputs;
-       if (out_data4 != NULL) {
+       if (out_data4 != nullptr) {
                num_outputs = 4;
                num_outputs = 4;
-       } else if (out_data3 != NULL) {
+       } else if (out_data3 != nullptr) {
                num_outputs = 3;
                num_outputs = 3;
-       } else if (out_data2 != NULL) {
+       } else if (out_data2 != nullptr) {
                num_outputs = 2;
        } else {
                num_outputs = 1;
        }
 
                num_outputs = 2;
        } else {
                num_outputs = 1;
        }
 
-       GLuint fbo, texnum[4];
-
-       glGenTextures(num_outputs, texnum);
+       glActiveTexture(GL_TEXTURE0);
        check_error();
        check_error();
+
+       vector<EffectChain::DestinationTexture> textures;
        for (unsigned i = 0; i < num_outputs; ++i) {
        for (unsigned i = 0; i < num_outputs; ++i) {
-               glBindTexture(GL_TEXTURE_2D, texnum[i]);
+               GLuint texnum = chain.get_resource_pool()->create_2d_texture(framebuffer_format, width, height);
+               textures.push_back(EffectChain::DestinationTexture{texnum, framebuffer_format});
+
+               // The output texture needs to have valid state to be written to by a compute shader.
+               glBindTexture(GL_TEXTURE_2D, texnum);
                check_error();
                check_error();
-               glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                check_error();
        }
 
                check_error();
        }
 
-       glGenFramebuffers(1, &fbo);
-       check_error();
-       glBindFramebuffer(GL_FRAMEBUFFER, fbo);
-       check_error();
-       for (unsigned i = 0; i < num_outputs; ++i) {
-               glFramebufferTexture2D(
-                       GL_FRAMEBUFFER,
-                       GL_COLOR_ATTACHMENT0 + i,
-                       GL_TEXTURE_2D,
-                       texnum[i],
-                       0);
-               check_error();
+       chain.render_to_texture(textures, width, height);
+
+#ifdef HAVE_BENCHMARK
+       // If running benchmarks: Now we've warmed up everything, so let's run the
+       // actual benchmark loop.
+       if (benchmark_state != nullptr) {
+               glFinish();
+               size_t iters = benchmark_state->max_iterations;
+               for (auto _ : *benchmark_state) {
+                       chain.render_to_texture(textures, width, height);
+                       if (--iters == 0) {
+                               glFinish();
+                       }
+               }
+               benchmark_state->SetItemsProcessed(benchmark_state->iterations() * width * height);
        }
        }
-
-       GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
-       glDrawBuffers(num_outputs, bufs);
-
-       chain.render_to_fbo(fbo, width, height);
+#endif
 
        T *data[4] = { out_data, out_data2, out_data3, out_data4 };
 
 
        T *data[4] = { out_data, out_data2, out_data3, out_data4 };
 
-       glBindFramebuffer(GL_FRAMEBUFFER, fbo);
-       check_error();
        for (unsigned i = 0; i < num_outputs; ++i) {
                T *ptr = data[i];
        for (unsigned i = 0; i < num_outputs; ++i) {
                T *ptr = data[i];
-               glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
+               glBindTexture(GL_TEXTURE_2D, textures[i].texnum);
+               check_error();
                if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
                        // GLES will only read GL_RGBA.
                        T *temp = new T[width * height * 4];
                if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
                        // GLES will only read GL_RGBA.
                        T *temp = new T[width * height * 4];
-                       glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
+                       glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, type, temp);
                        check_error();
                        if (format == GL_ALPHA) {
                                for (unsigned i = 0; i < width * height; ++i) {
                        check_error();
                        if (format == GL_ALPHA) {
                                for (unsigned i = 0; i < width * height; ++i) {
@@ -242,7 +307,7 @@ void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, T
                        }
                        delete[] temp;
                } else {
                        }
                        delete[] temp;
                } else {
-                       glReadPixels(0, 0, width, height, format, internal_format, ptr);
+                       glGetTexImage(GL_TEXTURE_2D, 0, format, type, ptr);
                        check_error();
                }
 
                        check_error();
                }
 
@@ -253,10 +318,9 @@ void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, T
                }
        }
 
                }
        }
 
-       glDeleteFramebuffers(1, &fbo);
-       check_error();
-       glDeleteTextures(num_outputs, texnum);
-       check_error();
+       for (unsigned i = 0; i < num_outputs; ++i) {
+               chain.get_resource_pool()->release_2d_texture(textures[i].texnum);
+       }
 }
 
 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
 }
 
 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
@@ -311,7 +375,7 @@ void expect_equal(const float *ref, const float *result, unsigned width, unsigne
        float rms = sqrt(squared_difference) / (width * height);
        EXPECT_LT(rms, rms_limit);
 
        float rms = sqrt(squared_difference) / (width * height);
        EXPECT_LT(rms, rms_limit);
 
-       if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
+       if (largest_difference >= largest_difference_limit || isnan(rms) || rms >= rms_limit) {
                fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
 
                fprintf(stderr, "Reference:\n");
                fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
 
                fprintf(stderr, "Reference:\n");
@@ -438,4 +502,47 @@ double linear_to_srgb(double x)
        }
 }
 
        }
 }
 
+DisableComputeShadersTemporarily::DisableComputeShadersTemporarily(bool disable_compute_shaders)
+       : disable_compute_shaders(disable_compute_shaders)
+{
+       init_movit_for_test();
+       saved_compute_shaders_supported = movit_compute_shaders_supported;
+       if (disable_compute_shaders) {
+               movit_compute_shaders_supported = false;
+       }
+}
+
+DisableComputeShadersTemporarily::~DisableComputeShadersTemporarily()
+{
+       movit_compute_shaders_supported = saved_compute_shaders_supported;
+}
+
+bool DisableComputeShadersTemporarily::should_skip()
+{
+       if (disable_compute_shaders) {
+               return false;
+       }
+
+       if (!movit_compute_shaders_supported) {
+               fprintf(stderr, "Compute shaders not supported; skipping.\n");
+               return true;
+       }
+       return false;
+}
+
+#ifdef HAVE_BENCHMARK
+bool DisableComputeShadersTemporarily::should_skip(benchmark::State *benchmark_state)
+{
+       if (disable_compute_shaders) {
+               return false;
+       }
+
+       if (!movit_compute_shaders_supported) {
+               benchmark_state->SkipWithError("Compute shaders not supported");
+               return true;
+       }
+       return false;
+}
+#endif
+
 }  // namespace movit
 }  // namespace movit