You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
17 KiB

/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
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 <Corrade/Containers/Optional.h>
#include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Directory.h>
#include "Magnum/DebugTools/CompareImage.h"
#include "Magnum/GL/OpenGLTester.h"
#include "Magnum/GL/Framebuffer.h"
#include "Magnum/GL/Mesh.h"
#include "Magnum/GL/Renderbuffer.h"
#include "Magnum/GL/RenderbufferFormat.h"
#include "Magnum/GL/Texture.h"
#include "Magnum/GL/TextureFormat.h"
#include "Magnum/Image.h"
#include "Magnum/ImageView.h"
#include "Magnum/PixelFormat.h"
#include "Magnum/MeshTools/Compile.h"
#include "Magnum/Primitives/Plane.h"
#include "Magnum/Primitives/Square.h"
#include "Magnum/Shaders/DistanceFieldVector.h"
#include "Magnum/Trade/AbstractImporter.h"
#include "Magnum/Trade/ImageData.h"
#include "Magnum/Trade/MeshData2D.h"
#include "Magnum/Trade/MeshData3D.h"
#include "configure.h"
namespace Magnum { namespace Shaders { namespace Test { namespace {
struct DistanceFieldVectorGLTest: GL::OpenGLTester {
explicit DistanceFieldVectorGLTest();
template<UnsignedInt dimensions> void construct();
template<UnsignedInt dimensions> void constructMove();
void renderSetup();
void renderTeardown();
void renderDefaults2D();
void renderDefaults3D();
void render2D();
void render3D();
private:
PluginManager::Manager<Trade::AbstractImporter> _manager{"nonexistent"};
GL::Renderbuffer _color{NoCreate};
#ifndef MAGNUM_TARGET_GLES2
GL::Renderbuffer _objectId{NoCreate};
#endif
GL::Framebuffer _framebuffer{NoCreate};
};
/*
Rendering tests done on:
- Mesa Intel
- Mesa AMD
- SwiftShader ES2/ES3
- ARM Mali (Huawei P10) ES2/ES3
- WebGL 1 / 2 (on Mesa Intel)
*/
using namespace Math::Literals;
constexpr struct {
const char* name;
Float outlineRangeStart, outlineRangeEnd, smoothness;
const char* file2D;
const char* file3D;
} RenderData[] {
{"smooth0.1", 0.5f, 1.0f, 0.1f, "smooth0.1-2D.tga", "smooth0.1-3D.tga"},
{"smooth0.2", 0.5f, 1.0f, 0.2f, "smooth0.2-2D.tga", "smooth0.2-3D.tga"},
{"outline", 0.6f, 0.45f, 0.05f, "outline2D.tga", "outline3D.tga"}
};
DistanceFieldVectorGLTest::DistanceFieldVectorGLTest() {
addTests<DistanceFieldVectorGLTest>({
&DistanceFieldVectorGLTest::construct<2>,
&DistanceFieldVectorGLTest::construct<3>,
&DistanceFieldVectorGLTest::constructMove<2>,
&DistanceFieldVectorGLTest::constructMove<3>});
addTests({&DistanceFieldVectorGLTest::renderDefaults2D,
&DistanceFieldVectorGLTest::renderDefaults3D},
&DistanceFieldVectorGLTest::renderSetup,
&DistanceFieldVectorGLTest::renderTeardown);
addInstancedTests({&DistanceFieldVectorGLTest::render2D,
&DistanceFieldVectorGLTest::render3D},
Containers::arraySize(RenderData),
&DistanceFieldVectorGLTest::renderSetup,
&DistanceFieldVectorGLTest::renderTeardown);
/* Load the plugins directly from the build tree. Otherwise they're either
static and already loaded or not present in the build tree */
#ifdef ANYIMAGEIMPORTER_PLUGIN_FILENAME
CORRADE_INTERNAL_ASSERT(_manager.load(ANYIMAGEIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded);
#endif
#ifdef TGAIMPORTER_PLUGIN_FILENAME
CORRADE_INTERNAL_ASSERT(_manager.load(TGAIMPORTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded);
#endif
}
template<UnsignedInt dimensions> void DistanceFieldVectorGLTest::construct() {
setTestCaseTemplateName(std::to_string(dimensions));
DistanceFieldVector<dimensions> shader;
CORRADE_VERIFY(shader.id());
{
#ifdef CORRADE_TARGET_APPLE
CORRADE_EXPECT_FAIL("macOS drivers need insane amount of state to validate properly.");
#endif
CORRADE_VERIFY(shader.validate().first);
}
MAGNUM_VERIFY_NO_GL_ERROR();
}
template<UnsignedInt dimensions> void DistanceFieldVectorGLTest::constructMove() {
setTestCaseTemplateName(std::to_string(dimensions));
DistanceFieldVector<dimensions> a;
const GLuint id = a.id();
CORRADE_VERIFY(id);
MAGNUM_VERIFY_NO_GL_ERROR();
DistanceFieldVector<dimensions> b{std::move(a)};
CORRADE_COMPARE(b.id(), id);
CORRADE_VERIFY(!a.id());
DistanceFieldVector<dimensions> c{NoCreate};
c = std::move(b);
CORRADE_COMPARE(c.id(), id);
CORRADE_VERIFY(!b.id());
}
constexpr Vector2i RenderSize{80, 80};
void DistanceFieldVectorGLTest::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 DistanceFieldVectorGLTest::renderTeardown() {
_framebuffer = GL::Framebuffer{NoCreate};
_color = GL::Renderbuffer{NoCreate};
}
constexpr GL::TextureFormat TextureFormatR =
#ifndef MAGNUM_TARGET_GLES2
GL::TextureFormat::R8
#else
GL::TextureFormat::Luminance
#endif
;
void DistanceFieldVectorGLTest::renderDefaults2D() {
if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) ||
!(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded))
CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found.");
GL::Mesh square = MeshTools::compile(Primitives::squareSolid(Primitives::SquareTextureCoords::Generate));
Containers::Pointer<Trade::AbstractImporter> importer = _manager.loadAndInstantiate("AnyImageImporter");
CORRADE_VERIFY(importer);
GL::Texture2D texture;
Containers::Optional<Trade::ImageData2D> image;
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(SHADERS_TEST_DIR, "TestFiles/vector-distancefield.tga")) && (image = importer->image2D(0)));
texture.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setWrapping(GL::SamplerWrapping::ClampToEdge);
#ifdef MAGNUM_TARGET_GLES2
/* Don't want to bother with the fiasco of single-channel formats and
texture storage extensions on ES2 */
texture.setImage(0, TextureFormatR, *image);
#else
texture.setStorage(1, TextureFormatR, image->size())
.setSubImage(0, {}, *image);
#endif
DistanceFieldVector2D shader;
shader.bindVectorTexture(texture);
square.draw(shader);
MAGNUM_VERIFY_NO_GL_ERROR();
/* Should be almost the same as Shaders::Vector output, but due to various
differences in the SDF output and too sharp default shininess it can't
be exact */
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join(SHADERS_TEST_DIR, "VectorTestFiles/defaults.tga"),
(DebugTools::CompareImageToFile{_manager, 189.0f, 6.1f}));
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
/* SwiftShader has off-by-one differences on edges, ARM Mali off-by-one in
all channels. */
const Float maxThreshold = 3.0f, meanThreshold = 0.076f;
#else
/* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */
const Float maxThreshold = 17.0f, meanThreshold = 0.480f;
#endif
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join(SHADERS_TEST_DIR, "VectorTestFiles/defaults-distancefield.tga"),
(DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold}));
}
void DistanceFieldVectorGLTest::renderDefaults3D() {
if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) ||
!(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded))
CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found.");
GL::Mesh plane = MeshTools::compile(Primitives::planeSolid(Primitives::PlaneTextureCoords::Generate));
Containers::Pointer<Trade::AbstractImporter> importer = _manager.loadAndInstantiate("AnyImageImporter");
CORRADE_VERIFY(importer);
GL::Texture2D texture;
Containers::Optional<Trade::ImageData2D> image;
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(SHADERS_TEST_DIR, "TestFiles/vector-distancefield.tga")) && (image = importer->image2D(0)));
texture.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setWrapping(GL::SamplerWrapping::ClampToEdge);
#ifdef MAGNUM_TARGET_GLES2
/* Don't want to bother with the fiasco of single-channel formats and
texture storage extensions on ES2 */
texture.setImage(0, TextureFormatR, *image);
#else
texture.setStorage(1, TextureFormatR, image->size())
.setSubImage(0, {}, *image);
#endif
DistanceFieldVector2D shader;
shader.bindVectorTexture(texture);
plane.draw(shader);
MAGNUM_VERIFY_NO_GL_ERROR();
/* Should be almost the same as Shaders::Vector output, but due to various
differences in the SDF output and too sharp default shininess it can't
be exact */
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join(SHADERS_TEST_DIR, "VectorTestFiles/defaults.tga"),
(DebugTools::CompareImageToFile{_manager, 189.0f, 6.1f}));
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
/* SwiftShader has off-by-one differences on edges, ARM Mali off-by-one in
all channels. */
const Float maxThreshold = 3.0f, meanThreshold = 0.076f;
#else
/* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */
const Float maxThreshold = 17.0f, meanThreshold = 0.480f;
#endif
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join(SHADERS_TEST_DIR, "VectorTestFiles/defaults-distancefield.tga"),
(DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold}));
}
void DistanceFieldVectorGLTest::render2D() {
auto&& data = RenderData[testCaseInstanceId()];
setTestCaseDescription(data.name);
if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) ||
!(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded))
CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found.");
GL::Mesh square = MeshTools::compile(Primitives::squareSolid(Primitives::SquareTextureCoords::Generate));
Containers::Pointer<Trade::AbstractImporter> importer = _manager.loadAndInstantiate("AnyImageImporter");
CORRADE_VERIFY(importer);
GL::Texture2D texture;
Containers::Optional<Trade::ImageData2D> image;
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(SHADERS_TEST_DIR, "TestFiles/vector-distancefield.tga")) && (image = importer->image2D(0)));
texture.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setWrapping(GL::SamplerWrapping::ClampToEdge);
#ifdef MAGNUM_TARGET_GLES2
/* Don't want to bother with the fiasco of single-channel formats and
texture storage extensions on ES2 */
texture.setImage(0, TextureFormatR, *image);
#else
texture.setStorage(1, TextureFormatR, image->size())
.setSubImage(0, {}, *image);
#endif
DistanceFieldVector2D shader;
shader
/** @todo implement background color */
.setColor(0xffff99_rgbf)
.setOutlineColor(0x9999ff_rgbf)
.setOutlineRange(data.outlineRangeStart, data.outlineRangeEnd)
.setSmoothness(data.smoothness)
.setTransformationProjectionMatrix(Matrix3::projection({2.1f, 2.1f}))
.bindVectorTexture(texture);
square.draw(shader);
MAGNUM_VERIFY_NO_GL_ERROR();
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
/* SwiftShader has off-by-one differences when smoothing */
const Float maxThreshold = 5.667f, meanThreshold = 0.268f;
#else
/* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */
const Float maxThreshold = 17.0f, meanThreshold = 2.386f;
#endif
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join({SHADERS_TEST_DIR, "VectorTestFiles", data.file2D}),
(DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold}));
}
void DistanceFieldVectorGLTest::render3D() {
auto&& data = RenderData[testCaseInstanceId()];
setTestCaseDescription(data.name);
if(!(_manager.loadState("AnyImageImporter") & PluginManager::LoadState::Loaded) ||
!(_manager.loadState("TgaImporter") & PluginManager::LoadState::Loaded))
CORRADE_SKIP("AnyImageImporter / TgaImageImporter plugins not found.");
GL::Mesh plane = MeshTools::compile(Primitives::planeSolid(Primitives::PlaneTextureCoords::Generate));
Containers::Pointer<Trade::AbstractImporter> importer = _manager.loadAndInstantiate("AnyImageImporter");
CORRADE_VERIFY(importer);
GL::Texture2D texture;
Containers::Optional<Trade::ImageData2D> image;
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(SHADERS_TEST_DIR, "TestFiles/vector-distancefield.tga")) && (image = importer->image2D(0)));
texture.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setWrapping(GL::SamplerWrapping::ClampToEdge);
#ifdef MAGNUM_TARGET_GLES2
/* Don't want to bother with the fiasco of single-channel formats and
texture storage extensions on ES2 */
texture.setImage(0, TextureFormatR, *image);
#else
texture.setStorage(1, TextureFormatR, image->size())
.setSubImage(0, {}, *image);
#endif
DistanceFieldVector3D shader;
shader
/** @todo implement background color */
.setColor(0xffff99_rgbf)
.setOutlineColor(0x9999ff_rgbf)
.setOutlineRange(data.outlineRangeStart, data.outlineRangeEnd)
.setSmoothness(data.smoothness)
.setTransformationProjectionMatrix(
Matrix4::perspectiveProjection(60.0_degf, 1.0f, 0.1f, 10.0f)*
Matrix4::translation(Vector3::zAxis(-2.15f))*
Matrix4::rotationY(-15.0_degf)*
Matrix4::rotationZ(15.0_degf))
.bindVectorTexture(texture);
plane.draw(shader);
MAGNUM_VERIFY_NO_GL_ERROR();
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
/* SwiftShader has off-by-one differences when smoothing plus a bunch of
different pixels on primitive edges */
const Float maxThreshold = 17.0f, meanThreshold = 0.192f;
#else
/* WebGL 1 doesn't have 8bit renderbuffer storage, so it's way worse */
const Float maxThreshold = 17.0f, meanThreshold = 1.613f;
#endif
CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Directory::join({SHADERS_TEST_DIR, "VectorTestFiles", data.file3D}),
(DebugTools::CompareImageToFile{_manager, maxThreshold, meanThreshold}));
}
}}}}
CORRADE_TEST_MAIN(Magnum::Shaders::Test::DistanceFieldVectorGLTest)