From 1fa1869ba7de2ecb7cff1fb642388aa5b1326003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 5 Nov 2018 10:33:46 +0100 Subject: [PATCH] TextureTools: add a benchmark for distance field processing. It's a bit shitty because we're recreating the shader and everything every iteration. --- .../TextureTools/Test/DistanceFieldGLTest.cpp | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/Magnum/TextureTools/Test/DistanceFieldGLTest.cpp b/src/Magnum/TextureTools/Test/DistanceFieldGLTest.cpp index e1d2439ea..696d22b93 100644 --- a/src/Magnum/TextureTools/Test/DistanceFieldGLTest.cpp +++ b/src/Magnum/TextureTools/Test/DistanceFieldGLTest.cpp @@ -41,6 +41,10 @@ #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" +#ifndef MAGNUM_TARGET_WEBGL +#include "Magnum/GL/DebugOutput.h" +#endif + #include "configure.h" namespace Magnum { namespace TextureTools { namespace Test { @@ -49,6 +53,7 @@ struct DistanceFieldGLTest: GL::OpenGLTester { explicit DistanceFieldGLTest(); void test(); + void benchmark(); private: PluginManager::Manager _manager; @@ -57,6 +62,10 @@ struct DistanceFieldGLTest: GL::OpenGLTester { DistanceFieldGLTest::DistanceFieldGLTest() { addTests({&DistanceFieldGLTest::test}); + #ifndef MAGNUM_TARGET_WEBGL + addBenchmarks({&DistanceFieldGLTest::benchmark}, 5, BenchmarkType::GpuTime); + #endif + /* Load the plugin directly from the build tree. Otherwise it's either static and already loaded or not present in the build tree */ #if defined(ANYIMAGEIMPORTER_PLUGIN_FILENAME) && defined(TGAIMPORTER_PLUGIN_FILENAME) @@ -154,6 +163,80 @@ void DistanceFieldGLTest::test() { DebugTools::CompareImageToFile{_manager}); } +#ifndef MAGNUM_TARGET_WEBGL +void DistanceFieldGLTest::benchmark() { + std::unique_ptr importer; + if(!(importer = _manager.loadAndInstantiate("TgaImporter"))) + CORRADE_SKIP("TgaImporter plugin not found."); + + CORRADE_VERIFY(importer->openFile(Utility::Directory::join(DISTANCEFIELDGLTEST_FILES_DIR, "input.tga"))); + CORRADE_COMPARE(importer->image2DCount(), 1); + Containers::Optional inputImage = importer->image2D(0); + CORRADE_VERIFY(inputImage); + CORRADE_COMPARE(inputImage->format(), PixelFormat::R8Unorm); + + #ifndef MAGNUM_TARGET_GLES2 + const GL::TextureFormat inputFormat = GL::TextureFormat::R8; + #elif !defined(MAGNUM_TARGET_WEBGL) + GL::TextureFormat inputFormat; + if(GL::Context::current().isExtensionSupported()) + inputFormat = GL::TextureFormat::R8; + else + inputFormat = GL::TextureFormat::Luminance; /** @todo Luminance8 */ + #else + const GL::TextureFormat inputFormat = GL::TextureFormat::Luminance; + #endif + + GL::Texture2D input; + input.setMinificationFilter(GL::SamplerFilter::Nearest, GL::SamplerMipmap::Base) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setStorage(1, inputFormat, inputImage->size()); + + #if !defined(MAGNUM_TARGET_GLES2) || defined(MAGNUM_TARGET_WEBGL) + input.setSubImage(0, {}, *inputImage); + #else + if(GL::Context::current().isExtensionSupported()) + input.setSubImage(0, {}, ImageView2D{inputImage->storage(), GL::PixelFormat::Red, GL::PixelType::UnsignedByte, inputImage->size(), inputImage->data()}); + else + input.setSubImage(0, {}, *inputImage); + #endif + + #ifndef MAGNUM_TARGET_GLES2 + const GL::TextureFormat outputFormat = GL::TextureFormat::R8; + #else + GL::TextureFormat outputFormat; + if(GL::Context::current().isExtensionSupported()) + outputFormat = GL::TextureFormat::R8; + else + outputFormat = GL::TextureFormat::Luminance; /** @todo Luminance8 */ + #endif + + GL::Texture2D output; + output.setMinificationFilter(GL::SamplerFilter::Nearest, GL::SamplerMipmap::Base) + .setMagnificationFilter(GL::SamplerFilter::Nearest) + .setStorage(1, outputFormat, Vector2i{64}); + + MAGNUM_VERIFY_NO_GL_ERROR(); + + /* So it doesn't spam too much */ + GL::DebugOutput::setCallback(nullptr); + + CORRADE_BENCHMARK(5) { + /* This is creating the shader from scratch every time, so no wonder + it's so freaking slow */ + TextureTools::distanceField(input, output, {{}, Vector2i{64}}, 32 + #ifdef MAGNUM_TARGET_GLES + , inputImage->size() + #endif + ); + + MAGNUM_VERIFY_NO_GL_ERROR(); + } + + GL::DebugOutput::setDefaultCallback(); +} +#endif + }}} CORRADE_TEST_MAIN(Magnum::TextureTools::Test::DistanceFieldGLTest)