/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include "Magnum/Image.h" #include "Magnum/ImageView.h" #include "Magnum/PixelFormat.h" #include "Magnum/DebugTools/CompareImage.h" #include "Magnum/GL/Framebuffer.h" #include "Magnum/GL/Mesh.h" #include "Magnum/GL/OpenGLTester.h" #include "Magnum/GL/Renderbuffer.h" #include "Magnum/GL/RenderbufferFormat.h" #include "Magnum/GL/Texture.h" #include "Magnum/GL/TextureFormat.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/MeshTools/Compile.h" #include "Magnum/Primitives/Plane.h" #include "Magnum/Primitives/Square.h" #include "Magnum/Shaders/Vector.h" #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" #include "Magnum/Trade/MeshData.h" #include "configure.h" namespace Magnum { namespace Shaders { namespace Test { namespace { struct VectorGLTest: GL::OpenGLTester { explicit VectorGLTest(); template void construct(); template void constructMove(); void renderSetup(); void renderTeardown(); void renderDefaults2D(); void renderDefaults3D(); void render2D(); void render3D(); private: PluginManager::Manager _manager{"nonexistent"}; std::string _testDir; GL::Renderbuffer _color{NoCreate}; #ifndef MAGNUM_TARGET_GLES2 GL::Renderbuffer _objectId{NoCreate}; #endif GL::Framebuffer _framebuffer{NoCreate}; }; /* Rendering tests done on: - Mesa Intel - Mesa AMD - SwiftShader ES2/ES3 - ARM Mali (Huawei P10) ES2/ES3 - WebGL 1 / 2 (on Mesa Intel) - iPhone 6 w/ iOS 12.4 */ using namespace Math::Literals; VectorGLTest::VectorGLTest() { addTests({ &VectorGLTest::construct<2>, &VectorGLTest::construct<3>, &VectorGLTest::constructMove<2>, &VectorGLTest::constructMove<3>}); addTests({&VectorGLTest::renderDefaults2D, &VectorGLTest::renderDefaults3D, &VectorGLTest::render2D, &VectorGLTest::render3D}, &VectorGLTest::renderSetup, &VectorGLTest::renderTeardown); /* Load the plugins directly from the build tree. Otherwise they're either static and already loaded or not present in the build tree */ #ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif #ifdef TGAIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT(_manager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif #ifdef CORRADE_TARGET_APPLE if(Utility::Directory::isSandboxed() #if defined(CORRADE_TARGET_IOS) && defined(CORRADE_TESTSUITE_TARGET_XCTEST) /** @todo Fix this once I persuade CMake to run XCTest tests properly */ && std::getenv("SIMULATOR_UDID") #endif ) { _testDir = Utility::Directory::path(Utility::Directory::executableLocation()); } else #endif { _testDir = SHADERS_TEST_DIR; } } template void VectorGLTest::construct() { setTestCaseTemplateName(std::to_string(dimensions)); Vector shader; CORRADE_VERIFY(shader.id()); { #ifdef CORRADE_TARGET_APPLE CORRADE_EXPECT_FAIL("macOS drivers need insane amount of state to validate properly."); #endif CORRADE_VERIFY(shader.validate().first); } MAGNUM_VERIFY_NO_GL_ERROR(); } template void VectorGLTest::constructMove() { setTestCaseTemplateName(std::to_string(dimensions)); Vector a; const GLuint id = a.id(); CORRADE_VERIFY(id); MAGNUM_VERIFY_NO_GL_ERROR(); Vector b{std::move(a)}; CORRADE_COMPARE(b.id(), id); CORRADE_VERIFY(!a.id()); Vector c{NoCreate}; c = std::move(b); CORRADE_COMPARE(c.id(), id); CORRADE_VERIFY(!b.id()); } constexpr Vector2i RenderSize{80, 80}; void VectorGLTest::renderSetup() { /* Pick a color that's directly representable on RGBA4 as well to reduce artifacts */ GL::Renderer::setClearColor(0x111111_rgbf); GL::Renderer::enable(GL::Renderer::Feature::FaceCulling); _color = GL::Renderbuffer{}; _color.setStorage( #if !defined(MAGNUM_TARGET_GLES2) || !defined(MAGNUM_TARGET_WEBGL) GL::RenderbufferFormat::RGBA8, #else GL::RenderbufferFormat::RGBA4, #endif RenderSize); _framebuffer = GL::Framebuffer{{{}, RenderSize}}; _framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, _color) .clear(GL::FramebufferClear::Color) .bind(); } void VectorGLTest::renderTeardown() { _framebuffer = GL::Framebuffer{NoCreate}; _color = GL::Renderbuffer{NoCreate}; } constexpr GL::TextureFormat TextureFormatR = #ifndef MAGNUM_TARGET_GLES2 GL::TextureFormat::R8 #else GL::TextureFormat::Luminance #endif ; void VectorGLTest::renderDefaults2D() { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found."); GL::Mesh square = MeshTools::compile(Primitives::squareSolid(Primitives::SquareTextureCoords::Generate)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/vector.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge); #ifdef MAGNUM_TARGET_GLES2 /* Don't want to bother with the fiasco of single-channel formats and texture storage extensions on ES2 */ texture.setImage(0, TextureFormatR, *image); #else texture.setStorage(1, TextureFormatR, image->size()) .setSubImage(0, {}, *image); #endif Vector2D{} .bindVectorTexture(texture) .draw(square); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has off-by-one differences on edges, ARM Mali a bit more of them */ const Float maxThreshold = 1.0f, meanThreshold = 0.022f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 17.0f, meanThreshold = 0.359f; #endif CORRADE_COMPARE_WITH( /* Dropping the alpha channel, as it's always 1.0 */ Containers::arrayCast(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels()), Utility::Directory::join(_testDir, "VectorTestFiles/defaults.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } void VectorGLTest::renderDefaults3D() { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found."); GL::Mesh plane = MeshTools::compile(Primitives::planeSolid(Primitives::PlaneTextureCoords::Generate)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/vector.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge); #ifdef MAGNUM_TARGET_GLES2 /* Don't want to bother with the fiasco of single-channel formats and texture storage extensions on ES2 */ texture.setImage(0, TextureFormatR, *image); #else texture.setStorage(1, TextureFormatR, image->size()) .setSubImage(0, {}, *image); #endif Vector3D{} .bindVectorTexture(texture) .draw(plane); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has off-by-one differences on edges, ARM Mali a bit more of them */ const Float maxThreshold = 1.0f, meanThreshold = 0.022f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 17.0f, meanThreshold = 0.359f; #endif CORRADE_COMPARE_WITH( /* Dropping the alpha channel, as it's always 1.0 */ Containers::arrayCast(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels()), Utility::Directory::join(_testDir, "VectorTestFiles/defaults.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } void VectorGLTest::render2D() { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found."); GL::Mesh square = MeshTools::compile(Primitives::squareSolid(Primitives::SquareTextureCoords::Generate)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/vector.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge); #ifdef MAGNUM_TARGET_GLES2 /* Don't want to bother with the fiasco of single-channel formats and texture storage extensions on ES2 */ texture.setImage(0, TextureFormatR, *image); #else texture.setStorage(1, TextureFormatR, image->size()) .setSubImage(0, {}, *image); #endif Vector2D{} .setBackgroundColor(0x9999ff_rgbf) .setColor(0xffff99_rgbf) .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::rotation(5.0_degf)) .bindVectorTexture(texture) .draw(square); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has differently rasterized edges on four pixels */ const Float maxThreshold = 170.0f, meanThreshold = 0.146f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 170.0f, meanThreshold = 0.962f; #endif CORRADE_COMPARE_WITH( /* Dropping the alpha channel, as it's always 1.0 */ Containers::arrayCast(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels()), Utility::Directory::join(_testDir, "VectorTestFiles/vector2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } void VectorGLTest::render3D() { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found."); GL::Mesh plane = MeshTools::compile(Primitives::planeSolid(Primitives::PlaneTextureCoords::Generate)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/vector.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge); #ifdef MAGNUM_TARGET_GLES2 /* Don't want to bother with the fiasco of single-channel formats and texture storage extensions on ES2 */ texture.setImage(0, TextureFormatR, *image); #else texture.setStorage(1, TextureFormatR, image->size()) .setSubImage(0, {}, *image); #endif Vector3D{} .setBackgroundColor(0x9999ff_rgbf) .setColor(0xffff99_rgbf) .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationZ(15.0_degf)) .bindVectorTexture(texture) .draw(plane); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has differently rasterized edges on four pixels */ const Float maxThreshold = 170.0f, meanThreshold = 0.171f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 170.0f, meanThreshold = 0.660f; #endif CORRADE_COMPARE_WITH( /* Dropping the alpha channel, as it's always 1.0 */ Containers::arrayCast(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels()), Utility::Directory::join(_testDir, "VectorTestFiles/vector3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } }}}} CORRADE_TEST_MAIN(Magnum::Shaders::Test::VectorGLTest)