/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 #include #include #include "Magnum/Image.h" #include "Magnum/ImageView.h" #include "Magnum/PixelFormat.h" #include "Magnum/DebugTools/CompareImage.h" #include "Magnum/GL/Context.h" #include "Magnum/GL/Extensions.h" #include "Magnum/GL/Mesh.h" #include "Magnum/GL/Framebuffer.h" #include "Magnum/GL/Renderer.h" #include "Magnum/GL/Renderbuffer.h" #include "Magnum/GL/RenderbufferFormat.h" #include "Magnum/GL/Texture.h" #include "Magnum/GL/TextureFormat.h" #include "Magnum/GL/OpenGLTester.h" #include "Magnum/Math/Color.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/MeshTools/Compile.h" #include "Magnum/Primitives/Circle.h" #include "Magnum/Primitives/UVSphere.h" #include "Magnum/Shaders/FlatGL.h" #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/ImageData.h" #include "Magnum/Trade/MeshData.h" #ifndef MAGNUM_TARGET_GLES2 #include "Magnum/GL/MeshView.h" #include "Magnum/MeshTools/Concatenate.h" #include "Magnum/MeshTools/GenerateIndices.h" #include "Magnum/Primitives/Cone.h" #include "Magnum/Primitives/Plane.h" #include "Magnum/Primitives/Square.h" #include "Magnum/Shaders/Generic.h" #include "Magnum/Shaders/Flat.h" #endif #include "configure.h" namespace Magnum { namespace Shaders { namespace Test { namespace { struct FlatGLTest: GL::OpenGLTester { explicit FlatGLTest(); template void construct(); #ifndef MAGNUM_TARGET_GLES2 template void constructUniformBuffers(); #endif template void constructMove(); #ifndef MAGNUM_TARGET_GLES2 template void constructMoveUniformBuffers(); #endif template void constructTextureTransformationNotTextured(); #ifndef MAGNUM_TARGET_GLES2 template void constructUniformBuffersZeroDraws(); #endif #ifndef MAGNUM_TARGET_GLES2 template void setUniformUniformBuffersEnabled(); template void bindBufferUniformBuffersNotEnabled(); #endif template void bindTextureNotEnabled(); template void setAlphaMaskNotEnabled(); template void setTextureMatrixNotEnabled(); #ifndef MAGNUM_TARGET_GLES2 template void bindTextureTransformBufferNotEnabled(); #endif #ifndef MAGNUM_TARGET_GLES2 template void setObjectIdNotEnabled(); #endif #ifndef MAGNUM_TARGET_GLES2 template void setWrongDrawOffset(); #endif void renderSetup(); void renderTeardown(); template void renderDefaults2D(); template void renderDefaults3D(); template void renderColored2D(); template void renderColored3D(); template void renderSinglePixelTextured2D(); template void renderSinglePixelTextured3D(); template void renderTextured2D(); template void renderTextured3D(); template void renderVertexColor2D(); template void renderVertexColor3D(); void renderAlphaSetup(); void renderAlphaTeardown(); template void renderAlpha2D(); template void renderAlpha3D(); #ifndef MAGNUM_TARGET_GLES2 void renderObjectIdSetup(); void renderObjectIdTeardown(); template void renderObjectId2D(); template void renderObjectId3D(); #endif template void renderInstanced2D(); template void renderInstanced3D(); #ifndef MAGNUM_TARGET_GLES2 void renderMulti2D(); void renderMulti3D(); #endif 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 - Mesa llvmpipe - SwiftShader ES2/ES3 - ARM Mali (Huawei P10) ES2/ES3 (except instancing) - WebGL 1 / 2 (on Mesa Intel) (except instancing) - NVidia Windows (except instancing) - Intel Windows (except instancing) - AMD on macOS (except instancing) - iPhone 6 w/ iOS 12.4 (except instancing) */ using namespace Math::Literals; constexpr struct { const char* name; FlatGL2D::Flags flags; } ConstructData[]{ {"", {}}, {"textured", FlatGL2D::Flag::Textured}, {"textured + texture transformation", FlatGL2D::Flag::Textured|FlatGL2D::Flag::TextureTransformation}, {"alpha mask", FlatGL2D::Flag::AlphaMask}, {"alpha mask + textured", FlatGL2D::Flag::AlphaMask|FlatGL2D::Flag::Textured}, {"vertex colors", FlatGL2D::Flag::VertexColor}, {"vertex colors + textured", FlatGL2D::Flag::VertexColor|FlatGL2D::Flag::Textured}, #ifndef MAGNUM_TARGET_GLES2 {"object ID", FlatGL2D::Flag::ObjectId}, {"instanced object ID", FlatGL2D::Flag::InstancedObjectId}, {"object ID + alpha mask + textured", FlatGL2D::Flag::ObjectId|FlatGL2D::Flag::AlphaMask|FlatGL2D::Flag::Textured}, #endif {"instanced transformation", FlatGL2D::Flag::InstancedTransformation}, {"instanced texture offset", FlatGL2D::Flag::Textured|FlatGL2D::Flag::InstancedTextureOffset} }; #ifndef MAGNUM_TARGET_GLES2 constexpr struct { const char* name; FlatGL2D::Flags flags; UnsignedInt drawCount; } ConstructUniformBuffersData[]{ {"classic fallback", {}, 1}, {"", FlatGL2D::Flag::UniformBuffers, 1}, /* SwiftShader has 256 uniform vectors at most, per-draw is 4+2 in 3D case and 3+2 in 2D */ {"multiple draws", FlatGL2D::Flag::UniformBuffers, 42}, {"texture transformation", FlatGL2D::Flag::UniformBuffers|FlatGL2D::Flag::Textured|FlatGL2D::Flag::TextureTransformation, 1}, {"alpha mask", FlatGL2D::Flag::UniformBuffers|FlatGL2D::Flag::AlphaMask, 1}, {"object ID", FlatGL2D::Flag::UniformBuffers|FlatGL2D::Flag::ObjectId, 1} }; #endif const struct { const char* name; FlatGL2D::Flags flags; Matrix3 textureTransformation; bool flip; } RenderTexturedData[]{ {"", FlatGL2D::Flag::Textured, {}, false}, {"texture transformation", FlatGL2D::Flag::Textured|FlatGL2D::Flag::TextureTransformation, Matrix3::translation(Vector2{1.0f})*Matrix3::scaling(Vector2{-1.0f}), true}, }; const struct { const char* name; const char* expected2D; const char* expected3D; bool blending; FlatGL2D::Flags flags; Float threshold; } RenderAlphaData[] { /* All those deliberately have a non-white diffuse in order to match the expected data from textured() */ {"none", "FlatTestFiles/textured2D.tga", "FlatTestFiles/textured3D.tga", false, FlatGL2D::Flag::Textured, 0.0f}, {"blending", "FlatTestFiles/textured2D-alpha.tga", "FlatTestFiles/textured3D-alpha.tga", true, FlatGL2D::Flag::Textured, 0.0f}, {"masking 0.0", "FlatTestFiles/textured2D.tga", "FlatTestFiles/textured3D.tga", false, FlatGL2D::Flag::Textured|FlatGL2D::Flag::AlphaMask, 0.0f}, {"masking 0.5", "FlatTestFiles/textured2D-alpha-mask0.5.tga", "FlatTestFiles/textured3D-alpha-mask0.5.tga", false, FlatGL2D::Flag::Textured|FlatGL2D::Flag::AlphaMask, 0.5f}, {"masking 1.0", "TestFiles/alpha-mask1.0.tga", "TestFiles/alpha-mask1.0.tga", false, FlatGL2D::Flag::Textured|FlatGL2D::Flag::AlphaMask, 1.0f} }; #ifndef MAGNUM_TARGET_GLES2 constexpr struct { const char* name; FlatGL2D::Flags flags; UnsignedInt uniformId; UnsignedInt instanceCount; UnsignedInt expected; } RenderObjectIdData[] { {"", /* Verify that it can hold 16 bits at least */ FlatGL2D::Flag::ObjectId, 48526, 0, 48526}, {"instanced, first instance", FlatGL2D::Flag::InstancedObjectId, 13524, 1, 24526}, {"instanced, second instance", FlatGL2D::Flag::InstancedObjectId, 13524, 2, 62347} }; #endif #ifndef MAGNUM_TARGET_GLES2 constexpr struct { const char* name; const char* expected2D; const char* expected3D; FlatGL2D::Flags flags; UnsignedInt drawCount; UnsignedInt uniformIncrement; Float maxThreshold, meanThreshold; } RenderMultiData[] { {"bind with offset, colored", "multidraw2D.tga", "multidraw3D.tga", {}, 1, 16, 0.0f, 0.0f}, {"bind with offset, textured", "multidraw-textured2D.tga", "multidraw-textured3D.tga", FlatGL2D::Flag::TextureTransformation|FlatGL2D::Flag::Textured, 1, 16, 0.0f, 0.0f}, {"draw offset, colored", "multidraw2D.tga", "multidraw3D.tga", {}, 3, 1, 0.0f, 0.0f}, {"draw offset, textured", "multidraw-textured2D.tga", "multidraw-textured3D.tga", FlatGL2D::Flag::TextureTransformation|FlatGL2D::Flag::Textured, 3, 1, 0.0f, 0.0f} }; #endif FlatGLTest::FlatGLTest() { addInstancedTests({ &FlatGLTest::construct<2>, &FlatGLTest::construct<3>}, Containers::arraySize(ConstructData)); #ifndef MAGNUM_TARGET_GLES2 addInstancedTests({ &FlatGLTest::constructUniformBuffers<2>, &FlatGLTest::constructUniformBuffers<3>}, Containers::arraySize(ConstructUniformBuffersData)); #endif addTests({ &FlatGLTest::constructMove<2>, &FlatGLTest::constructMove<3>, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::constructMoveUniformBuffers<2>, &FlatGLTest::constructMoveUniformBuffers<3>, #endif &FlatGLTest::constructTextureTransformationNotTextured<2>, &FlatGLTest::constructTextureTransformationNotTextured<3>, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::constructUniformBuffersZeroDraws<2>, &FlatGLTest::constructUniformBuffersZeroDraws<3>, #endif #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::setUniformUniformBuffersEnabled<2>, &FlatGLTest::setUniformUniformBuffersEnabled<3>, &FlatGLTest::bindBufferUniformBuffersNotEnabled<2>, &FlatGLTest::bindBufferUniformBuffersNotEnabled<3>, #endif &FlatGLTest::bindTextureNotEnabled<2>, &FlatGLTest::bindTextureNotEnabled<3>, &FlatGLTest::setAlphaMaskNotEnabled<2>, &FlatGLTest::setAlphaMaskNotEnabled<3>, &FlatGLTest::setTextureMatrixNotEnabled<2>, &FlatGLTest::setTextureMatrixNotEnabled<3>, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::bindTextureTransformBufferNotEnabled<2>, &FlatGLTest::bindTextureTransformBufferNotEnabled<3>, #endif #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::setObjectIdNotEnabled<2>, &FlatGLTest::setObjectIdNotEnabled<3>, #endif #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::setWrongDrawOffset<2>, &FlatGLTest::setWrongDrawOffset<3>, #endif }); /* MSVC needs explicit type due to default template args */ addTests({ &FlatGLTest::renderDefaults2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderDefaults2D, #endif &FlatGLTest::renderDefaults3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderDefaults3D, #endif &FlatGLTest::renderColored2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderColored2D, #endif &FlatGLTest::renderColored3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderColored3D, #endif &FlatGLTest::renderSinglePixelTextured2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderSinglePixelTextured2D, #endif &FlatGLTest::renderSinglePixelTextured3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderSinglePixelTextured3D #endif }, &FlatGLTest::renderSetup, &FlatGLTest::renderTeardown); /* MSVC needs explicit type due to default template args */ addInstancedTests({ &FlatGLTest::renderTextured2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderTextured2D, #endif &FlatGLTest::renderTextured3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderTextured3D #endif }, Containers::arraySize(RenderTexturedData), &FlatGLTest::renderSetup, &FlatGLTest::renderTeardown); /* MSVC needs explicit type due to default template args */ addTests({ &FlatGLTest::renderVertexColor2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderVertexColor2D, #endif &FlatGLTest::renderVertexColor2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderVertexColor2D, #endif &FlatGLTest::renderVertexColor3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderVertexColor3D, #endif &FlatGLTest::renderVertexColor3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderVertexColor3D #endif }, &FlatGLTest::renderSetup, &FlatGLTest::renderTeardown); /* MSVC needs explicit type due to default template args */ addInstancedTests({ &FlatGLTest::renderAlpha2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderAlpha2D, #endif &FlatGLTest::renderAlpha3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderAlpha3D #endif }, Containers::arraySize(RenderAlphaData), &FlatGLTest::renderAlphaSetup, &FlatGLTest::renderAlphaTeardown); #ifndef MAGNUM_TARGET_GLES2 /* MSVC needs explicit type due to default template args */ addInstancedTests({ &FlatGLTest::renderObjectId2D, &FlatGLTest::renderObjectId2D, &FlatGLTest::renderObjectId3D, &FlatGLTest::renderObjectId3D}, Containers::arraySize(RenderObjectIdData), &FlatGLTest::renderObjectIdSetup, &FlatGLTest::renderObjectIdTeardown); #endif /* MSVC needs explicit type due to default template args */ addTests({ &FlatGLTest::renderInstanced2D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderInstanced2D, #endif &FlatGLTest::renderInstanced3D, #ifndef MAGNUM_TARGET_GLES2 &FlatGLTest::renderInstanced3D #endif }, &FlatGLTest::renderSetup, &FlatGLTest::renderTeardown); #ifndef MAGNUM_TARGET_GLES2 addInstancedTests({&FlatGLTest::renderMulti2D, &FlatGLTest::renderMulti3D}, Containers::arraySize(RenderMultiData), &FlatGLTest::renderObjectIdSetup, &FlatGLTest::renderObjectIdTeardown); #endif /* 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_OUTPUT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); #endif #ifdef TGAIMPORTER_PLUGIN_FILENAME CORRADE_INTERNAL_ASSERT_OUTPUT(_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 FlatGLTest::construct() { setTestCaseTemplateName(std::to_string(dimensions)); auto&& data = ConstructData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES if((data.flags & FlatGL2D::Flag::ObjectId) && !GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::EXT::gpu_shader4::string() << "is not supported."); #endif FlatGL shader{data.flags}; CORRADE_COMPARE(shader.flags(), data.flags); 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(); } #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::constructUniformBuffers() { setTestCaseTemplateName(std::to_string(dimensions)); auto&& data = ConstructUniformBuffersData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES if((data.flags & FlatGL2D::Flag::UniformBuffers) && !GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); if((data.flags & FlatGL2D::Flag::ObjectId) && !GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::EXT::gpu_shader4::string() << "is not supported."); #endif FlatGL shader{data.flags, data.drawCount}; CORRADE_COMPARE(shader.flags(), data.flags); CORRADE_COMPARE(shader.drawCount(), data.drawCount); 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(); } #endif template void FlatGLTest::constructMove() { setTestCaseTemplateName(std::to_string(dimensions)); FlatGL a{FlatGL::Flag::Textured}; const GLuint id = a.id(); CORRADE_VERIFY(id); MAGNUM_VERIFY_NO_GL_ERROR(); FlatGL b{std::move(a)}; CORRADE_COMPARE(b.id(), id); CORRADE_COMPARE(b.flags(), FlatGL::Flag::Textured); CORRADE_VERIFY(!a.id()); FlatGL c{NoCreate}; c = std::move(b); CORRADE_COMPARE(c.id(), id); CORRADE_COMPARE(c.flags(), FlatGL::Flag::Textured); CORRADE_VERIFY(!b.id()); } #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::constructMoveUniformBuffers() { setTestCaseTemplateName(std::to_string(dimensions)); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif FlatGL a{FlatGL::Flag::UniformBuffers, 5}; const GLuint id = a.id(); CORRADE_VERIFY(id); MAGNUM_VERIFY_NO_GL_ERROR(); FlatGL b{std::move(a)}; CORRADE_COMPARE(b.id(), id); CORRADE_COMPARE(b.flags(), FlatGL::Flag::UniformBuffers); CORRADE_COMPARE(b.drawCount(), 5); CORRADE_VERIFY(!a.id()); FlatGL c{NoCreate}; c = std::move(b); CORRADE_COMPARE(c.id(), id); CORRADE_COMPARE(c.flags(), FlatGL::Flag::UniformBuffers); CORRADE_COMPARE(c.drawCount(), 5); CORRADE_VERIFY(!b.id()); } #endif template void FlatGLTest::constructTextureTransformationNotTextured() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; FlatGL{FlatGL::Flag::TextureTransformation}; CORRADE_COMPARE(out.str(), "Shaders::FlatGL: texture transformation enabled but the shader is not textured\n"); } #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::constructUniformBuffersZeroDraws() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif std::ostringstream out; Error redirectError{&out}; FlatGL{FlatGL::Flag::UniformBuffers, 0}; CORRADE_COMPARE(out.str(), "Shaders::FlatGL: draw count can't be zero\n"); } #endif #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::setUniformUniformBuffersEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif std::ostringstream out; Error redirectError{&out}; FlatGL shader{FlatGL::Flag::UniformBuffers}; shader.setTransformationProjectionMatrix({}) .setTextureMatrix({}) .setColor({}) .setAlphaMask({}) .setObjectId({}); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::setTransformationProjectionMatrix(): the shader was created with uniform buffers enabled\n" "Shaders::FlatGL::setTextureMatrix(): the shader was created with uniform buffers enabled\n" "Shaders::FlatGL::setColor(): the shader was created with uniform buffers enabled\n" "Shaders::FlatGL::setAlphaMask(): the shader was created with uniform buffers enabled\n" "Shaders::FlatGL::setObjectId(): the shader was created with uniform buffers enabled\n"); } template void FlatGLTest::bindBufferUniformBuffersNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; GL::Buffer buffer; FlatGL shader; shader.bindTransformationProjectionBuffer(buffer) .bindTransformationProjectionBuffer(buffer, 0, 16) .bindDrawBuffer(buffer) .bindDrawBuffer(buffer, 0, 16) .bindTextureTransformationBuffer(buffer) .bindTextureTransformationBuffer(buffer, 0, 16) .setDrawOffset(0); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::bindTransformationProjectionBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::bindTransformationProjectionBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::bindDrawBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::bindDrawBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::bindTextureTransformationBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::bindTextureTransformationBuffer(): the shader was not created with uniform buffers enabled\n" "Shaders::FlatGL::setDrawOffset(): the shader was not created with uniform buffers enabled\n"); } #endif template void FlatGLTest::bindTextureNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; GL::Texture2D texture; FlatGL shader; shader.bindTexture(texture); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::bindTexture(): the shader was not created with texturing enabled\n"); } template void FlatGLTest::setAlphaMaskNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; FlatGL shader; shader.setAlphaMask(0.75f); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::setAlphaMask(): the shader was not created with alpha mask enabled\n"); } template void FlatGLTest::setTextureMatrixNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; FlatGL shader; shader.setTextureMatrix({}); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::setTextureMatrix(): the shader was not created with texture transformation enabled\n"); } #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::bindTextureTransformBufferNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif std::ostringstream out; Error redirectError{&out}; GL::Buffer buffer{GL::Buffer::TargetHint::Uniform}; FlatGL shader{FlatGL::Flag::UniformBuffers}; shader.bindTextureTransformationBuffer(buffer) .bindTextureTransformationBuffer(buffer, 0, 16); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::bindTextureTransformationBuffer(): the shader was not created with texture transformation enabled\n" "Shaders::FlatGL::bindTextureTransformationBuffer(): the shader was not created with texture transformation enabled\n"); } #endif #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::setObjectIdNotEnabled() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif std::ostringstream out; Error redirectError{&out}; FlatGL shader; shader.setObjectId(33376); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::setObjectId(): the shader was not created with object ID enabled\n"); } #endif #ifndef MAGNUM_TARGET_GLES2 template void FlatGLTest::setWrongDrawOffset() { setTestCaseTemplateName(std::to_string(dimensions)); #ifdef CORRADE_NO_ASSERT CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif std::ostringstream out; Error redirectError{&out}; FlatGL{FlatGL::Flag::UniformBuffers, 5} .setDrawOffset(5); CORRADE_COMPARE(out.str(), "Shaders::FlatGL::setDrawOffset(): draw offset 5 is out of bounds for 5 draws\n"); } #endif constexpr Vector2i RenderSize{80, 80}; void FlatGLTest::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 FlatGLTest::renderTeardown() { _framebuffer = GL::Framebuffer{NoCreate}; _color = GL::Renderbuffer{NoCreate}; } template void FlatGLTest::renderDefaults2D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32)); FlatGL2D shader{flag}; if(flag == FlatGL2D::Flag{}) { shader.draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} }}; shader .bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); 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, "FlatTestFiles/defaults.tga"), /* SwiftShader has 8 different pixels on the edges */ (DebugTools::CompareImageToFile{_manager, 238.0f, 0.2975f})); } template void FlatGLTest::renderDefaults3D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32)); FlatGL3D shader{flag}; if(flag == FlatGL3D::Flag{}) { shader.draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} }}; shader .bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); 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, "FlatTestFiles/defaults.tga"), /* SwiftShader has 8 different pixels on the edges */ (DebugTools::CompareImageToFile{_manager, 238.0f, 0.2975f})); } template void FlatGLTest::renderColored2D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32)); FlatGL2D shader{flag}; if(flag == FlatGL2D::Flag{}) { shader .setColor(0x9999ff_rgbf) .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; shader .bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) const Float maxThreshold = 0.0f, meanThreshold = 0.0f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 11.34f, meanThreshold = 0.51f; #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, "FlatTestFiles/colored2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderColored3D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32)); FlatGL3D shader{flag}; if(flag == FlatGL3D::Flag{}) { shader .setColor(0x9999ff_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::rotationX(15.0_degf)) .draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; shader .bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has 5 different pixels on the edges */ const Float maxThreshold = 170.0f, meanThreshold = 0.133f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 170.0f, meanThreshold = 0.456f; #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, "FlatTestFiles/colored3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } constexpr GL::TextureFormat TextureFormatRGB = #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) GL::TextureFormat::RGB8 #else GL::TextureFormat::RGB #endif ; constexpr GL::TextureFormat TextureFormatRGBA = #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) GL::TextureFormat::RGBA8 #else GL::TextureFormat::RGBA #endif ; template void FlatGLTest::renderSinglePixelTextured2D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates)); const Color4ub diffuseData[]{ 0x9999ff_rgb }; ImageView2D diffuseImage{PixelFormat::RGBA8Unorm, Vector2i{1}, diffuseData}; GL::Texture2D texture; texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGBA, Vector2i{1}) .setSubImage(0, {}, diffuseImage); FlatGL2D shader{FlatGL2D::Flag::Textured|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { shader.setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has 5 different pixels on the edges */ const Float maxThreshold = 170.0f, meanThreshold = 0.133f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 11.34f, meanThreshold = 0.51f; #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, "FlatTestFiles/colored2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderSinglePixelTextured3D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates)); const Color4ub diffuseData[]{ 0x9999ff_rgb }; ImageView2D diffuseImage{PixelFormat::RGBA8Unorm, Vector2i{1}, diffuseData}; GL::Texture2D texture; texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGBA, Vector2i{1}) .setSubImage(0, {}, diffuseImage); FlatGL3D shader{FlatGL3D::Flag::Textured|flag}; shader.bindTexture(texture); if(flag == FlatGL3D::Flag{}) { shader.setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) .draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has 5 different pixels on the edges */ const Float maxThreshold = 170.0f, meanThreshold = 0.133f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 170.0f, meanThreshold = 0.456f; #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, "FlatTestFiles/colored3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderTextured2D() { auto&& data = RenderTexturedData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL2D shader{data.flags|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { if(data.textureTransformation != Matrix3{}) shader.setTextureMatrix(data.textureTransformation); shader.setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) /* Colorized. Case without a color (where it should be white) is tested in renderSinglePixelTextured2D() */ .setColor(0x9999ff_rgbf) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, { TextureTransformationUniform{} .setTextureMatrix(data.textureTransformation) }}; if(data.flags & FlatGL2D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform); shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); Image2D rendered = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}); /* Dropping the alpha channel, as it's always 1.0 */ Containers::StridedArrayView2D pixels = Containers::arrayCast(rendered.pixels()); if(data.flip) pixels = pixels.flipped<0>().flipped<1>(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has minor rounding errors, Apple A8 & llvmpipe a bit more */ const Float maxThreshold = 2.334f, meanThreshold = 0.032f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 15.667f, meanThreshold = 3.254f; #endif CORRADE_COMPARE_WITH(pixels, Utility::Directory::join(_testDir, "FlatTestFiles/textured2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderTextured3D() { auto&& data = RenderTexturedData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates)); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL3D shader{data.flags|flag}; shader.bindTexture(texture); if(flag == FlatGL3D::Flag{}) { if(data.textureTransformation != Matrix3{}) shader.setTextureMatrix(data.textureTransformation); shader .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(data.flip ? 15.0_degf : -15.0_degf)* Matrix4::rotationX(data.flip ? -15.0_degf : 15.0_degf)) /* Colorized. Case without a color (where it should be white) is tested in renderSinglePixelTextured3D() */ .setColor(0x9999ff_rgbf) .draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(data.flip ? 15.0_degf : -15.0_degf)* Matrix4::rotationX(data.flip ? -15.0_degf : 15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, { TextureTransformationUniform{} .setTextureMatrix(data.textureTransformation) }}; if(data.flags & FlatGL3D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform); shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); Image2D rendered = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}); /* Dropping the alpha channel, as it's always 1.0 */ Containers::StridedArrayView2D pixels = Containers::arrayCast(rendered.pixels()); if(data.flip) pixels = pixels.flipped<0>().flipped<1>(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has 5 different pixels on the edges */ const Float maxThreshold = 139.0f, meanThreshold = 0.087f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 139.0f, meanThreshold = 2.896f; #endif CORRADE_COMPARE_WITH(pixels, Utility::Directory::join(_testDir, "FlatTestFiles/textured3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderVertexColor2D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName({T::Size == 3 ? "Color3" : "Color4", "Flag::UniformBuffers"}); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } else #endif { setTestCaseTemplateName(T::Size == 3 ? "Color3" : "Color4"); } if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Trade::MeshData circleData = Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates); /* Highlight a quarter */ Containers::Array colorData{DirectInit, circleData.vertexCount(), 0x999999_rgbf}; for(std::size_t i = 8; i != 16; ++i) colorData[i + 1] = 0xffff99_rgbf*1.5f; GL::Buffer colors; colors.setData(colorData); GL::Mesh circle = MeshTools::compile(circleData); circle.addVertexBuffer(colors, 0, GL::Attribute{}); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL2D shader{FlatGL2D::Flag::Textured|FlatGL2D::Flag::VertexColor|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { shader.setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) .setColor(0x9999ff_rgbf) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has minor rounding errors. ARM Mali / Apple A8 a bit more */ const Float maxThreshold = 3.334f, meanThreshold = 0.064f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's worse */ const Float maxThreshold = 15.334f, meanThreshold = 4.355f; #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, "FlatTestFiles/vertexColor2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderVertexColor3D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName({T::Size == 3 ? "Color3" : "Color4", "Flag::UniformBuffers"}); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } else #endif { setTestCaseTemplateName(T::Size == 3 ? "Color3" : "Color4"); } if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Trade::MeshData sphereData = Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates); /* Highlight the middle rings */ Containers::Array colorData{DirectInit, sphereData.vertexCount(), 0x999999_rgbf}; for(std::size_t i = 6*33; i != 9*33; ++i) colorData[i + 1] = 0xffff99_rgbf*1.5f; GL::Buffer colors; colors.setData(colorData); GL::Mesh sphere = MeshTools::compile(sphereData); sphere.addVertexBuffer(colors, 0, GL::Attribute{}); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL3D shader{FlatGL3D::Flag::Textured|FlatGL3D::Flag::VertexColor|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { shader.setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf)) .setColor(0x9999ff_rgbf) .draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has some minor differences on the edges, Apple A8 more */ const Float maxThreshold = 76.67f, meanThreshold = 0.138f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's worse */ const Float maxThreshold = 76.67f, meanThreshold = 3.908f; #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, "FlatTestFiles/vertexColor3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } void FlatGLTest::renderAlphaSetup() { renderSetup(); if(RenderAlphaData[testCaseInstanceId()].blending) GL::Renderer::enable(GL::Renderer::Feature::Blending); GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha); GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add); } void FlatGLTest::renderAlphaTeardown() { if(RenderAlphaData[testCaseInstanceId()].blending) GL::Renderer::disable(GL::Renderer::Feature::Blending); renderTeardown(); } template void FlatGLTest::renderAlpha2D() { auto&& data = RenderAlphaData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Containers::Optional image; Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; CORRADE_VERIFY(importer->openFile(Utility::Directory::join({_testDir, "TestFiles", "diffuse-alpha-texture.tga"})) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGBA, image->size()) .setSubImage(0, {}, *image); MAGNUM_VERIFY_NO_GL_ERROR(); GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates)); FlatGL2D shader{data.flags|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { /* Test that the default is correct by not setting the threshold if it's equal to the default */ if(data.flags & FlatGL2D::Flag::AlphaMask && data.threshold != 0.5f) shader.setAlphaMask(data.threshold); shader.setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) .setColor(0x9999ff_rgbf) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) .setAlphaMask(data.threshold) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* Minor differences between opaque and diffuse, not sure why */ const Float maxThreshold = 24.34f, meanThreshold = 0.305f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 31.34f, meanThreshold = 3.945f; #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, data.expected2D), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderAlpha3D() { auto&& data = RenderAlphaData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Containers::Optional image; Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; CORRADE_VERIFY(importer->openFile(Utility::Directory::join({_testDir, "TestFiles", "diffuse-alpha-texture.tga"})) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGBA, image->size()) .setSubImage(0, {}, *image); MAGNUM_VERIFY_NO_GL_ERROR(); GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates)); FlatGL3D shader{data.flags|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { shader.setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf)) .setColor(0x9999ff_rgbf); /* Test that the default is correct by not setting the threshold if it's equal to the default */ if(data.flags & FlatGL3D::Flag::AlphaMask && data.threshold != 0.5f) shader.setAlphaMask(data.threshold); /* For proper Z order draw back faces first and then front faces */ GL::Renderer::setFaceCullingMode(GL::Renderer::PolygonFacing::Front); shader.draw(sphere); GL::Renderer::setFaceCullingMode(GL::Renderer::PolygonFacing::Back); shader.draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) .setAlphaMask(data.threshold) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform); /* For proper Z order draw back faces first and then front faces */ GL::Renderer::setFaceCullingMode(GL::Renderer::PolygonFacing::Front); shader.draw(sphere); GL::Renderer::setFaceCullingMode(GL::Renderer::PolygonFacing::Back); shader.draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* Minor differences between opaque and diffuse, not sure why. SwiftShader has 5 different pixels on the edges, llvmpipe some off-by-one errors */ const Float maxThreshold = 139.0f, meanThreshold = 0.421f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 139.0f, meanThreshold = 4.587f; #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, data.expected3D}), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } #ifndef MAGNUM_TARGET_GLES2 void FlatGLTest::renderObjectIdSetup() { GL::Renderer::enable(GL::Renderer::Feature::FaceCulling); _color = GL::Renderbuffer{}; _color.setStorage(GL::RenderbufferFormat::RGBA8, RenderSize); _framebuffer = GL::Framebuffer{{{}, RenderSize}}; _framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, _color) /* Pick a color that's directly representable on RGBA4 as well to reduce artifacts (well, and this needs to be consistent with other tests that *need* to run on WebGL 1) */ .clearColor(0, 0x111111_rgbf) .bind(); /* If we don't have EXT_gpu_shader4, we likely don't have integer framebuffers either (Mesa's Zink), so skip setting up integer attachments to avoid GL errors */ #ifndef MAGNUM_TARGET_GLES if(GL::Context::current().isExtensionSupported()) #endif { _objectId = GL::Renderbuffer{}; _objectId.setStorage(GL::RenderbufferFormat::R32UI, RenderSize); _framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{1}, _objectId) .mapForDraw({ {FlatGL2D::ColorOutput, GL::Framebuffer::ColorAttachment{0}}, {FlatGL2D::ObjectIdOutput, GL::Framebuffer::ColorAttachment{1}} }) .clearColor(1, Vector4ui{27}); } } void FlatGLTest::renderObjectIdTeardown() { _color = GL::Renderbuffer{NoCreate}; _objectId = GL::Renderbuffer{NoCreate}; _framebuffer = GL::Framebuffer{NoCreate}; } template void FlatGLTest::renderObjectId2D() { auto&& data = RenderObjectIdData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::EXT::gpu_shader4::string() << "is not supported."); #endif CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Draw), GL::Framebuffer::Status::Complete); GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32)); if(data.instanceCount) circle .setInstanceCount(data.instanceCount) .addVertexBufferInstanced( GL::Buffer{Containers::arrayView({11002u, 48823u})}, 1, 0, FlatGL2D::ObjectId{}); FlatGL2D shader{data.flags|flag}; if(flag == FlatGL2D::Flag{}) { shader.setColor(0x9999ff_rgbf) .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) .setObjectId(data.uniformId) .draw(circle); } else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f})) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) .setObjectId(data.uniformId) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(circle); } else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); /* Color output should have no difference -- same as in colored2D() */ #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) const Float maxThreshold = 0.0f, meanThreshold = 0.0f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 11.34f, meanThreshold = 0.51f; #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, "FlatTestFiles/colored2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); /* Object ID -- no need to verify the whole image, just check that pixels on known places have expected values. SwiftShader insists that the read format has to be 32bit, so the renderbuffer format is that too to make it the same (ES3 Mesa complains if these don't match). */ _framebuffer.mapForRead(GL::Framebuffer::ColorAttachment{1}); CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Read), GL::Framebuffer::Status::Complete); Image2D image = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::R32UI}); MAGNUM_VERIFY_NO_GL_ERROR(); /* Outside of the object, cleared to 27 */ CORRADE_COMPARE(image.pixels()[10][10], 27); /* Inside of the object */ CORRADE_COMPARE(image.pixels()[40][46], data.expected); } template void FlatGLTest::renderObjectId3D() { auto&& data = RenderObjectIdData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL3D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::EXT::gpu_shader4::string() << "is not supported."); #endif CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Draw), GL::Framebuffer::Status::Complete); GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32)); if(data.instanceCount) sphere .setInstanceCount(data.instanceCount) .addVertexBufferInstanced( GL::Buffer{Containers::arrayView({11002u, 48823u})}, 1, 0, FlatGL2D::ObjectId{}); FlatGL3D shader{data.flags|flag}; if(flag == FlatGL3D::Flag{}) { shader.setColor(0x9999ff_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::rotationX(15.0_degf)) .setObjectId(data.uniformId) .draw(sphere); } else if(flag == FlatGL3D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::rotationY(-15.0_degf)* Matrix4::rotationX(15.0_degf) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0x9999ff_rgbf) .setObjectId(data.uniformId) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .draw(sphere); } else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); /* Color output should have no difference -- same as in colored3D() */ #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* SwiftShader has 5 different pixels on the edges */ const Float maxThreshold = 170.0f, meanThreshold = 0.133f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */ const Float maxThreshold = 170.0f, meanThreshold = 0.456f; #endif _framebuffer.mapForRead(GL::Framebuffer::ColorAttachment{0}); CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Read), GL::Framebuffer::Status::Complete); 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, "FlatTestFiles/colored3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); /* Object ID -- no need to verify the whole image, just check that pixels on known places have expected values. SwiftShader insists that the read format has to be 32bit, so the renderbuffer format is that too to make it the same (ES3 Mesa complains if these don't match). */ _framebuffer.mapForRead(GL::Framebuffer::ColorAttachment{1}); CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Read), GL::Framebuffer::Status::Complete); Image2D image = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::R32UI}); MAGNUM_VERIFY_NO_GL_ERROR(); /* Outside of the object, cleared to 27 */ CORRADE_COMPARE(image.pixels()[10][10], 27); /* Inside of the object */ CORRADE_COMPARE(image.pixels()[40][46], data.expected); } #endif template void FlatGLTest::renderInstanced2D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::instanced_arrays::string() << "is not supported."); #elif defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_WEBGL if(!GL::Context::current().isExtensionSupported() && !GL::Context::current().isExtensionSupported() && !GL::Context::current().isExtensionSupported()) CORRADE_SKIP("GL_{ANGLE,EXT,NV}_instanced_arrays is not supported"); #else if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ANGLE::instanced_arrays::string() << "is not supported."); #endif #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); GL::Mesh circle = MeshTools::compile(Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates)); /* Three circles, each in a different location */ struct { Matrix3 transformation; Color3 color; Vector2 textureOffset; } instanceData[] { {Matrix3::translation({-1.25f, -1.25f}), 0xff3333_rgbf, {0.0f, 0.0f}}, {Matrix3::translation({ 1.25f, -1.25f}), 0x33ff33_rgbf, {1.0f, 0.0f}}, {Matrix3::translation({ 0.00f, 1.25f}), 0x9999ff_rgbf, {0.5f, 1.0f}} }; circle .addVertexBufferInstanced(GL::Buffer{instanceData}, 1, 0, FlatGL2D::TransformationMatrix{}, FlatGL2D::Color3{}, FlatGL2D::TextureOffset{}) .setInstanceCount(3); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL2D shader{FlatGL2D::Flag::Textured| FlatGL2D::Flag::VertexColor| FlatGL2D::Flag::InstancedTransformation| FlatGL2D::Flag::InstancedTextureOffset|flag}; shader.bindTexture(texture); if(flag == FlatGL2D::Flag{}) { shader.setColor(0xffff99_rgbf) .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::scaling(Vector2{0.4f})) .setTextureMatrix(Matrix3::scaling(Vector2{0.5f})) .draw(circle); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform2D{} .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::scaling(Vector2{0.4f}) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0xffff99_rgbf) }}; GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, { TextureTransformationUniform{} .setTextureMatrix(Matrix3::scaling(Vector2{0.5f})) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .bindTextureTransformationBuffer(textureTransformationUniform) .draw(circle); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* Minor differences on AMD, SwiftShader a bit more */ const Float maxThreshold = 3.0f, meanThreshold = 0.018f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage */ const Float maxThreshold = 3.0f, meanThreshold = 0.018f; #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, "FlatTestFiles/instanced2D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } template void FlatGLTest::renderInstanced3D() { #ifndef MAGNUM_TARGET_GLES2 if(flag == FlatGL2D::Flag::UniformBuffers) { setTestCaseTemplateName("Flag::UniformBuffers"); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif } #endif #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::instanced_arrays::string() << "is not supported."); #elif defined(MAGNUM_TARGET_GLES2) #ifndef MAGNUM_TARGET_WEBGL if(!GL::Context::current().isExtensionSupported() && !GL::Context::current().isExtensionSupported() && !GL::Context::current().isExtensionSupported()) CORRADE_SKIP("GL_{ANGLE,EXT,NV}_instanced_arrays is not supported"); #else if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ANGLE::instanced_arrays::string() << "is not supported."); #endif #endif if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); GL::Mesh sphere = MeshTools::compile(Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates)); /* Three spheres, each in a different location */ struct { Matrix4 transformation; Color3 color; Vector2 textureOffset; } instanceData[] { {Matrix4::translation({-1.25f, -1.25f, 0.0f}), 0xff3333_rgbf, {0.0f, 0.0f}}, {Matrix4::translation({ 1.25f, -1.25f, 0.0f}), 0x33ff33_rgbf, {1.0f, 0.0f}}, {Matrix4::translation({ 0.0f, 1.0f, 1.0f}), 0x9999ff_rgbf, {0.5f, 1.0f}} }; sphere .addVertexBufferInstanced(GL::Buffer{instanceData}, 1, 0, FlatGL3D::TransformationMatrix{}, FlatGL3D::Color3{}, FlatGL3D::TextureOffset{}) .setInstanceCount(3); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); GL::Texture2D texture; Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, TextureFormatRGB, image->size()) .setSubImage(0, {}, *image); FlatGL3D shader{FlatGL3D::Flag::Textured| FlatGL3D::Flag::VertexColor| FlatGL3D::Flag::InstancedTransformation| FlatGL3D::Flag::InstancedTextureOffset|flag}; shader.bindTexture(texture); if(flag == FlatGL3D::Flag{}) { shader.setColor(0xffff99_rgbf) .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::scaling(Vector3{0.4f})) .setTextureMatrix(Matrix3::scaling(Vector2{0.5f})) .draw(sphere); } #ifndef MAGNUM_TARGET_GLES2 else if(flag == FlatGL2D::Flag::UniformBuffers) { GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, { TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::scaling(Vector3{0.4f}) ) }}; GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, { FlatDrawUniform{} .setColor(0xffff99_rgbf) }}; GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, { TextureTransformationUniform{} .setTextureMatrix(Matrix3::scaling(Vector2{0.5f})) }}; shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform) .bindTextureTransformationBuffer(textureTransformationUniform) .draw(sphere); } #endif else CORRADE_INTERNAL_ASSERT_UNREACHABLE(); MAGNUM_VERIFY_NO_GL_ERROR(); #if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL)) /* Minor differences on AMD, SwiftShader a bit more */ const Float maxThreshold = 67.67f, meanThreshold = 0.062f; #else /* WebGL 1 doesn't have 8bit renderbuffer storage */ const Float maxThreshold = 67.67f, meanThreshold = 0.062f; #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, "FlatTestFiles/instanced3D.tga"), (DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold})); } #ifndef MAGNUM_TARGET_GLES2 void FlatGLTest::renderMulti2D() { auto&& data = RenderMultiData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif GL::Texture2D texture; if(data.flags & FlatGL2D::Flag::Textured) { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, GL::TextureFormat::RGB8, image->size()) .setSubImage(0, {}, *image); } /* Circle is a fan, plane is a strip, make it indexed first */ Trade::MeshData circleData = MeshTools::generateIndices(Primitives::circle2DSolid(32, Primitives::Circle2DFlag::TextureCoordinates)); Trade::MeshData squareData = MeshTools::generateIndices(Primitives::squareSolid( Primitives::SquareFlag::TextureCoordinates)); Trade::MeshData triangleData = MeshTools::generateIndices(Primitives::circle2DSolid(3, Primitives::Circle2DFlag::TextureCoordinates)); GL::Mesh mesh = MeshTools::compile(MeshTools::concatenate({circleData, squareData, triangleData})); GL::MeshView circle{mesh}; circle.setCount(circleData.indexCount()); GL::MeshView square{mesh}; square.setCount(squareData.indexCount()) .setIndexRange(circleData.indexCount()); GL::MeshView triangle{mesh}; triangle.setCount(triangleData.indexCount()) .setIndexRange(circleData.indexCount() + squareData.indexCount()); /* Some drivers have uniform offset alignment as high as 256, which means the subsequent sets of uniforms have to be aligned to a multiply of it. The data.uniformIncrement is set high enough to ensure that, in the non-offset-bind case this value is 1. */ Containers::Array transformationProjectionData{2*data.uniformIncrement + 1}; transformationProjectionData[0*data.uniformIncrement] = TransformationProjectionUniform2D{} .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::scaling(Vector2{0.4f})* Matrix3::translation({-1.25f, -1.25f}) ); transformationProjectionData[1*data.uniformIncrement] = TransformationProjectionUniform2D{} .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::scaling(Vector2{0.4f})* Matrix3::translation({ 1.25f, -1.25f}) ); transformationProjectionData[2*data.uniformIncrement] = TransformationProjectionUniform2D{} .setTransformationProjectionMatrix( Matrix3::projection({2.1f, 2.1f})* Matrix3::scaling(Vector2{0.4f})* Matrix3::translation({ 0.00f, 1.25f}) ); GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, transformationProjectionData}; Containers::Array textureTransformationData{2*data.uniformIncrement + 1}; textureTransformationData[0*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({0.0f, 0.0f}) ); textureTransformationData[1*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({1.0f, 0.0f}) ); textureTransformationData[2*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({0.5f, 1.0f}) ); GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, textureTransformationData}; Containers::Array drawData{2*data.uniformIncrement + 1}; drawData[0*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL2D::Flag::Textured ? 0xffffff_rgbf : 0xff0000_rgbf) .setObjectId(1211); drawData[1*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL2D::Flag::Textured ? 0xffffff_rgbf : 0x0000ff_rgbf) .setObjectId(5627); drawData[2*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL2D::Flag::Textured ? 0xffffff_rgbf : 0xff0000_rgbf) .setObjectId(36363); GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, drawData}; FlatGL2D shader{FlatGL2D::Flag::UniformBuffers|FlatGL2D::Flag::ObjectId|data.flags, data.drawCount}; if(data.flags & FlatGL2D::Flag::Textured) shader.bindTexture(texture); /* Just one draw, rebinding UBOs each time */ if(data.drawCount == 1) { shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 0*data.uniformIncrement*sizeof(TransformationProjectionUniform2D), sizeof(TransformationProjectionUniform2D)); shader.bindDrawBuffer(drawUniform, 0*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL2D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 0*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(circle); shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 1*data.uniformIncrement*sizeof(TransformationProjectionUniform2D), sizeof(TransformationProjectionUniform2D)); shader.bindDrawBuffer(drawUniform, 1*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL2D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 1*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(square); shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 2*data.uniformIncrement*sizeof(TransformationProjectionUniform2D), sizeof(TransformationProjectionUniform2D)); shader.bindDrawBuffer(drawUniform, 2*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL2D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 2*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(triangle); /* Otherwise using the draw offset */ } else { shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform); if(data.flags & FlatGL2D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform); shader.setDrawOffset(0) .draw(circle); shader.setDrawOffset(1) .draw(square); shader.setDrawOffset(2) .draw(triangle); } MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); /* Colored case: - Circle should be lower left, red - Square lower right, blue - Triangle up center, red Textured case: - Circle should have bottom left numbers, so light 7881 - Square bottom right, 1223 - Triangle 6778 */ 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, "FlatTestFiles", data.expected2D}), (DebugTools::CompareImageToFile{_manager, data.maxThreshold, data.meanThreshold})); /* Object ID -- no need to verify the whole image, just check that pixels on known places have expected values. SwiftShader insists that the read format has to be 32bit, so the renderbuffer format is that too to make it the same (ES3 Mesa complains if these don't match). */ #ifndef MAGNUM_TARGET_GLES if(GL::Context::current().isExtensionSupported()) #endif { _framebuffer.mapForRead(GL::Framebuffer::ColorAttachment{1}); CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Read), GL::Framebuffer::Status::Complete); Image2D image = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::R32UI}); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(image.pixels()[5][5], 27); /* Outside */ CORRADE_COMPARE(image.pixels()[24][24], 1211); /* Circle */ CORRADE_COMPARE(image.pixels()[24][56], 5627); /* Square */ CORRADE_COMPARE(image.pixels()[56][40], 36363); /* Triangle */ } } void FlatGLTest::renderMulti3D() { auto&& data = RenderMultiData[testCaseInstanceId()]; setTestCaseDescription(data.name); #ifndef MAGNUM_TARGET_GLES if(!GL::Context::current().isExtensionSupported()) CORRADE_SKIP(GL::Extensions::ARB::uniform_buffer_object::string() << "is not supported."); #endif GL::Texture2D texture; if(data.flags & FlatGL3D::Flag::Textured) { if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); Containers::Pointer importer = _manager.loadAndInstantiate("AnyImageImporter"); CORRADE_VERIFY(importer); Containers::Optional image; CORRADE_VERIFY(importer->openFile(Utility::Directory::join(_testDir, "TestFiles/diffuse-texture.tga")) && (image = importer->image2D(0))); texture.setMinificationFilter(GL::SamplerFilter::Linear) .setMagnificationFilter(GL::SamplerFilter::Linear) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setStorage(1, GL::TextureFormat::RGB8, image->size()) .setSubImage(0, {}, *image); } Trade::MeshData sphereData = Primitives::uvSphereSolid(16, 32, Primitives::UVSphereFlag::TextureCoordinates); /* Plane is a strip, make it indexed first */ Trade::MeshData planeData = MeshTools::generateIndices(Primitives::planeSolid( Primitives::PlaneFlag::TextureCoordinates)); Trade::MeshData coneData = Primitives::coneSolid(1, 32, 1.0f, Primitives::ConeFlag::TextureCoordinates); GL::Mesh mesh = MeshTools::compile(MeshTools::concatenate({sphereData, planeData, coneData})); GL::MeshView sphere{mesh}; sphere.setCount(sphereData.indexCount()); GL::MeshView plane{mesh}; plane.setCount(planeData.indexCount()) .setIndexRange(sphereData.indexCount()); GL::MeshView cone{mesh}; cone.setCount(coneData.indexCount()) .setIndexRange(sphereData.indexCount() + planeData.indexCount()); /* Some drivers have uniform offset alignment as high as 256, which means the subsequent sets of uniforms have to be aligned to a multiply of it. The data.uniformIncrement is set high enough to ensure that, in the non-offset-bind case this value is 1. */ Containers::Array transformationProjectionData{2*data.uniformIncrement + 1}; transformationProjectionData[0*data.uniformIncrement] = TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::scaling(Vector3{0.4f})* Matrix4::translation({-1.25f, -1.25f, 0.0f})* /* To be consistent with Phong's output where it tests that the normal matrix is applied properly */ Matrix4::rotationX(90.0_degf) ); transformationProjectionData[1*data.uniformIncrement] = TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::scaling(Vector3{0.4f})* Matrix4::translation({ 1.25f, -1.25f, 0.0f}) ); transformationProjectionData[2*data.uniformIncrement] = TransformationProjectionUniform3D{} .setTransformationProjectionMatrix( Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)* Matrix4::translation(Vector3::zAxis(-2.15f))* Matrix4::scaling(Vector3{0.4f})* Matrix4::translation({ 0.0f, 1.0f, 1.0f}) ); GL::Buffer transformationProjectionUniform{GL::Buffer::TargetHint::Uniform, transformationProjectionData}; Containers::Array textureTransformationData{2*data.uniformIncrement + 1}; textureTransformationData[0*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({0.0f, 0.0f}) ); textureTransformationData[1*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({1.0f, 0.0f}) ); textureTransformationData[2*data.uniformIncrement] = TextureTransformationUniform{} .setTextureMatrix( Matrix3::scaling(Vector2{0.5f})* Matrix3::translation({0.5f, 1.0f}) ); GL::Buffer textureTransformationUniform{GL::Buffer::TargetHint::Uniform, textureTransformationData}; Containers::Array drawData{2*data.uniformIncrement + 1}; drawData[0*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL3D::Flag::Textured ? 0xffffff_rgbf : 0xff0000_rgbf) .setObjectId(1211); drawData[1*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL3D::Flag::Textured ? 0xffffff_rgbf : 0x0000ff_rgbf) .setObjectId(5627); drawData[2*data.uniformIncrement] = FlatDrawUniform{} .setColor(data.flags & FlatGL3D::Flag::Textured ? 0xffffff_rgbf : 0xff0000_rgbf) .setObjectId(36363); GL::Buffer drawUniform{GL::Buffer::TargetHint::Uniform, drawData}; FlatGL3D shader{FlatGL3D::Flag::UniformBuffers|FlatGL3D::Flag::ObjectId|data.flags, data.drawCount}; if(data.flags & FlatGL3D::Flag::Textured) shader.bindTexture(texture); /* Just one draw, rebinding UBOs each time */ if(data.drawCount == 1) { shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 0*data.uniformIncrement*sizeof(TransformationProjectionUniform3D), sizeof(TransformationProjectionUniform3D)); shader.bindDrawBuffer(drawUniform, 0*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL3D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 0*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(sphere); shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 1*data.uniformIncrement*sizeof(TransformationProjectionUniform3D), sizeof(TransformationProjectionUniform3D)); shader.bindDrawBuffer(drawUniform, 1*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL3D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 1*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(plane); shader.bindTransformationProjectionBuffer(transformationProjectionUniform, 2*data.uniformIncrement*sizeof(TransformationProjectionUniform3D), sizeof(TransformationProjectionUniform3D)); shader.bindDrawBuffer(drawUniform, 2*data.uniformIncrement*sizeof(FlatDrawUniform), sizeof(FlatDrawUniform)); if(data.flags & FlatGL3D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform, 2*data.uniformIncrement*sizeof(TextureTransformationUniform), sizeof(TextureTransformationUniform)); shader.draw(cone); /* Otherwise using the draw offset */ } else { shader.bindTransformationProjectionBuffer(transformationProjectionUniform) .bindDrawBuffer(drawUniform); if(data.flags & FlatGL3D::Flag::TextureTransformation) shader.bindTextureTransformationBuffer(textureTransformationUniform); shader.setDrawOffset(0) .draw(sphere); shader.setDrawOffset(1) .draw(plane); shader.setDrawOffset(2) .draw(cone); } MAGNUM_VERIFY_NO_GL_ERROR(); if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) || !(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded)) CORRADE_SKIP("AnyImageImporter / TgaImporter plugins not found."); /* Colored case: - Sphere should be lower left, red - Plane lower right, blue - Cone up center, red Textured case: - Sphere should have bottom left numbers, so light 7881, rotated (78 visible) - Plane bottom right, 1223 - Cone 6778 */ 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, "FlatTestFiles", data.expected3D}), (DebugTools::CompareImageToFile{_manager, data.maxThreshold, data.meanThreshold})); /* Object ID -- no need to verify the whole image, just check that pixels on known places have expected values. SwiftShader insists that the read format has to be 32bit, so the renderbuffer format is that too to make it the same (ES3 Mesa complains if these don't match). */ #ifndef MAGNUM_TARGET_GLES if(GL::Context::current().isExtensionSupported()) #endif { _framebuffer.mapForRead(GL::Framebuffer::ColorAttachment{1}); CORRADE_COMPARE(_framebuffer.checkStatus(GL::FramebufferTarget::Read), GL::Framebuffer::Status::Complete); Image2D image = _framebuffer.read(_framebuffer.viewport(), {PixelFormat::R32UI}); MAGNUM_VERIFY_NO_GL_ERROR(); CORRADE_COMPARE(image.pixels()[5][5], 27); /* Outside */ CORRADE_COMPARE(image.pixels()[24][24], 1211); /* Sphere */ CORRADE_COMPARE(image.pixels()[24][56], 5627); /* Plane */ CORRADE_COMPARE(image.pixels()[56][40], 36363); /* Circle */ } } #endif }}}} CORRADE_TEST_MAIN(Magnum::Shaders::Test::FlatGLTest)