Browse Source

Text: assert size ratio expectations in DistanceFieldGlyphCache already.

Otherwise the assertion from TextureTools::DistanceField fires only
later during image upload, which can cause great confusion, not being
sure what's to blame, etc.
pull/168/head
Vladimír Vondruš 3 years ago
parent
commit
4363568b86
  1. 7
      src/Magnum/Text/DistanceFieldGlyphCache.cpp
  2. 14
      src/Magnum/Text/DistanceFieldGlyphCache.h
  3. 30
      src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp

7
src/Magnum/Text/DistanceFieldGlyphCache.cpp

@ -55,6 +55,13 @@ DistanceFieldGlyphCache::DistanceFieldGlyphCache(const Vector2i& sourceSize, con
MAGNUM_ASSERT_GL_EXTENSION_SUPPORTED(GL::Extensions::ARB::texture_rg);
#endif
/* Replicating the assertion from TextureTools::DistanceField so it gets
checked during construction already instead of only later during the
setImage() call */
CORRADE_ASSERT(sourceSize % size == Vector2i{0} &&
(sourceSize/size) % 2 == Vector2i{0},
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got" << Debug::packed << sourceSize << "and" << Debug::packed << size, );
#if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
/* Luminance is not renderable in most cases */
if(!GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>())

14
src/Magnum/Text/DistanceFieldGlyphCache.h

@ -83,11 +83,15 @@ class MAGNUM_TEXT_EXPORT DistanceFieldGlyphCache: public GlyphCache {
* @param radius Distance field computation radius
*
* See @ref TextureTools::DistanceField for more information about the
* parameters. Sets the internal texture format to single-channel.
* On OpenGL ES 3.0+ and WebGL 2 uses @ref GL::TextureFormat::R8. On
* desktop OpenGL requires @gl_extension{ARB,texture_rg} (part of
* OpenGL 3.0), on ES2 uses @gl_extension{EXT,texture_rg} if available
* or @ref GL::TextureFormat::RGB as fallback, on WebGL 1 uses
* parameters. Size restrictions from it apply here as well, in
* particular the ratio of @p sourceSize and @p size is expected to be
* a multiple of 2.
*
* Sets the internal texture format to single-channel. On OpenGL ES
* 3.0+ and WebGL 2 uses @ref GL::TextureFormat::R8. On desktop OpenGL
* requires @gl_extension{ARB,texture_rg} (part of OpenGL 3.0), on ES2
* uses @gl_extension{EXT,texture_rg} if available or
* @ref GL::TextureFormat::RGB as fallback, on WebGL 1 uses
* @ref GL::TextureFormat::RGB always.
*/
explicit DistanceFieldGlyphCache(const Vector2i& sourceSize, const Vector2i& size, UnsignedInt radius);

30
src/Magnum/Text/Test/DistanceFieldGlyphCacheGLTest.cpp

@ -55,6 +55,7 @@ struct DistanceFieldGlyphCacheGLTest: GL::OpenGLTester {
explicit DistanceFieldGlyphCacheGLTest();
void construct();
void constructSizeRatioNotMultipleOfTwo();
void constructCopy();
void constructMove();
@ -93,6 +94,7 @@ const struct {
DistanceFieldGlyphCacheGLTest::DistanceFieldGlyphCacheGLTest() {
addTests({&DistanceFieldGlyphCacheGLTest::construct,
&DistanceFieldGlyphCacheGLTest::constructSizeRatioNotMultipleOfTwo,
&DistanceFieldGlyphCacheGLTest::constructCopy,
&DistanceFieldGlyphCacheGLTest::constructMove});
@ -124,6 +126,30 @@ void DistanceFieldGlyphCacheGLTest::construct() {
#endif
}
void DistanceFieldGlyphCacheGLTest::constructSizeRatioNotMultipleOfTwo() {
CORRADE_SKIP_IF_NO_ASSERT();
/* This should be fine */
DistanceFieldGlyphCache{Vector2i{23*14}, Vector2i{23}, 4};
/* It's the same assert as in TextureTools::DistanceField */
std::ostringstream out;
Error redirectError{&out};
DistanceFieldGlyphCache{Vector2i{23*14}, Vector2i{23*2}, 4};
/* Verify also just one axis wrong */
DistanceFieldGlyphCache{Vector2i{23*14}, {23*2, 23}, 4};
DistanceFieldGlyphCache{Vector2i{23*14}, {23, 23*2}, 4};
/* Almost correct except that it's not an integer multiply */
DistanceFieldGlyphCache{Vector2i{23*14}, {22, 23}, 4};
DistanceFieldGlyphCache{Vector2i{23*14}, {23, 22}, 4};
CORRADE_COMPARE(out.str(),
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got {322, 322} and {46, 46}\n"
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got {322, 322} and {46, 23}\n"
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got {322, 322} and {23, 46}\n"
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got {322, 322} and {22, 23}\n"
"Text::DistanceFieldGlyphCache: expected source and destination size ratio to be a multiple of 2, got {322, 322} and {23, 22}\n");
}
void DistanceFieldGlyphCacheGLTest::constructCopy() {
CORRADE_VERIFY(!std::is_copy_constructible<DistanceFieldGlyphCache>{});
CORRADE_VERIFY(!std::is_copy_assignable<DistanceFieldGlyphCache>{});
@ -135,7 +161,7 @@ void DistanceFieldGlyphCacheGLTest::constructMove() {
DistanceFieldGlyphCache b = Utility::move(a);
CORRADE_COMPARE(b.size(), (Vector3i{1024, 512, 1}));
DistanceFieldGlyphCache c{{2, 3}, {1, 1}, 1};
DistanceFieldGlyphCache c{{2, 4}, {1, 2}, 1};
c = Utility::move(b);
CORRADE_COMPARE(c.size(), (Vector3i{1024, 512, 1}));
@ -276,7 +302,7 @@ void DistanceFieldGlyphCacheGLTest::setDistanceFieldImage() {
void DistanceFieldGlyphCacheGLTest::setDistanceFieldImageOutOfRange() {
CORRADE_SKIP_IF_NO_ASSERT();
DistanceFieldGlyphCache cache{{200, 300}, {100, 200}, 4};
DistanceFieldGlyphCache cache{{200, 400}, {100, 200}, 4};
/* This is fine. Not testing on ES2 as there it would need the complicated
format logic from above. */

Loading…
Cancel
Save