diff --git a/src/Magnum/MeshTools/Test/CMakeLists.txt b/src/Magnum/MeshTools/Test/CMakeLists.txt index cee809552..82e45b6ce 100644 --- a/src/Magnum/MeshTools/Test/CMakeLists.txt +++ b/src/Magnum/MeshTools/Test/CMakeLists.txt @@ -59,3 +59,12 @@ if(WITH_PRIMITIVES) set_target_properties(MeshToolsSubdivideRemov___Benchmark PROPERTIES FOLDER "Magnum/MeshTools/Test") endif() + +if(BUILD_GL_TESTS) + corrade_add_resource(FullScreenTriangleGLTest_RESOURCES resources.conf) + set_target_properties(FullScreenTriangleGLTest_RESOURCES-dependencies PROPERTIES FOLDER "Magnum/MeshTools/Test") + + corrade_add_test(MeshToolsFullScreenTriangleGLTest + FullScreenTriangleGLTest.cpp ${FullScreenTriangleGLTest_RESOURCES} + LIBRARIES MagnumMeshTools MagnumGL MagnumOpenGLTester) +endif() diff --git a/src/Magnum/MeshTools/Test/FullScreenTriangleGLTest.cpp b/src/Magnum/MeshTools/Test/FullScreenTriangleGLTest.cpp new file mode 100644 index 000000000..cc8e7b02a --- /dev/null +++ b/src/Magnum/MeshTools/Test/FullScreenTriangleGLTest.cpp @@ -0,0 +1,144 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#include "Magnum/Image.h" +#include "Magnum/PixelFormat.h" +#include "Magnum/GL/AbstractShaderProgram.h" +#include "Magnum/GL/Framebuffer.h" +#include "Magnum/GL/Mesh.h" +#include "Magnum/GL/OpenGLTester.h" +#include "Magnum/GL/Renderbuffer.h" +#include "Magnum/GL/RenderbufferFormat.h" +#include "Magnum/GL/Shader.h" +#include "Magnum/GL/Version.h" +#include "Magnum/Math/Color.h" +#include "Magnum/MeshTools/FullScreenTriangle.h" +#include "Magnum/Shaders/Implementation/CreateCompatibilityShader.h" + +namespace Magnum { namespace MeshTools { namespace Test { namespace { + +struct FullScreenTriangleGLTest: GL::OpenGLTester { + explicit FullScreenTriangleGLTest(); + + void test(); +}; + +constexpr struct { + const char* name; + GL::Version version; +} VersionData[] { + #ifndef MAGNUM_TARGET_GLES + {"GL 2.1", GL::Version::GL210}, + {"GL 3.0", GL::Version::GL300} + #else + {"GLES 2.0", GL::Version::GLES200}, + #ifndef MAGNUM_TARGET_GLES2 + {"GLES 3.0", GL::Version::GLES300} + #endif + #endif +}; + +FullScreenTriangleGLTest::FullScreenTriangleGLTest() { + addInstancedTests({&FullScreenTriangleGLTest::test}, + Containers::arraySize(VersionData)); +} + +void FullScreenTriangleGLTest::test() { + auto&& data = VersionData[testCaseInstanceId()]; + setTestCaseDescription(data.name); + + struct FullscreenFlatShader: GL::AbstractShaderProgram { + FullscreenFlatShader(GL::Version version) { + Utility::Resource rs{"FullScreenTriangleTest"}; + + GL::Shader vert = Shaders::Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Vertex); + vert.addSource(rs.get("FullScreenTriangle.glsl")) + .addSource(R"( +void main() { + fullScreenTriangle(); +} + )"); + + GL::Shader frag = Shaders::Implementation::createCompatibilityShader(rs, version, GL::Shader::Type::Fragment); + frag.addSource(R"( +#ifdef NEW_GLSL +out lowp vec4 fragmentColor; +#else +#define fragmentColor gl_FragColor +#endif + +void main() { + fragmentColor = vec4(1.0, 0.5, 1.0, 0.5); +} + )"); + + CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag})); + + attachShaders({vert, frag}); + + CORRADE_INTERNAL_ASSERT_OUTPUT(link()); + } + } shader{data.version}; + + MAGNUM_VERIFY_NO_GL_ERROR(); + + GL::Renderbuffer color; + color.setStorage( + #ifndef MAGNUM_TARGET_GLES2 + GL::RenderbufferFormat::RGBA8, + #else + GL::RenderbufferFormat::RGBA4, + #endif + Vector2i{4}); + GL::Framebuffer framebuffer{{{}, Vector2i{4}}}; + framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, color) + .bind(); + + MAGNUM_VERIFY_NO_GL_ERROR(); + + GL::Mesh mesh = fullScreenTriangle(data.version); + mesh.draw(shader); + + MAGNUM_VERIFY_NO_GL_ERROR(); + + using namespace Math::Literals; + + Image2D image = framebuffer.read({{}, Vector2i{4}}, PixelFormat::RGBA8Unorm); + CORRADE_COMPARE_AS(Containers::arrayCast(image.data()), Containers::arrayView(Containers::Array{Containers::InPlaceInit, { + 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, + 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, + 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, + 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba, 0xff80ff80_rgba + }}), TestSuite::Compare::Container); +} + +}}}} + +CORRADE_TEST_MAIN(Magnum::MeshTools::Test::FullScreenTriangleGLTest) diff --git a/src/Magnum/MeshTools/Test/resources.conf b/src/Magnum/MeshTools/Test/resources.conf new file mode 100644 index 000000000..d8c8d72df --- /dev/null +++ b/src/Magnum/MeshTools/Test/resources.conf @@ -0,0 +1,9 @@ +group=FullScreenTriangleTest + +[file] +filename=../../Shaders/FullScreenTriangle.glsl +alias=FullScreenTriangle.glsl + +[file] +filename=../../Shaders/compatibility.glsl +alias=compatibility.glsl