From 7187ccb48b69075a868f924ac617608428f0db36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 12 Jun 2013 14:44:48 +0200 Subject: [PATCH] Text: ported GlyphCache to older OpenGL and OpenGL ES. * Not requiring ARB_texture_storage, as Texture::setStorage() has already implemented fallback. * Not requiring EXT_texture_rg for single-channel texture on ES2, as this extension might not be available everywhere (unlike in ES3 desktop OpenGL, where Mesa 8/9 with OpenGL 2.1 supports it), fallback to Luminance in GlyphCache and RGB in DistanceFieldGlyphCache, because Luminance might not be renderable everywhere. * Re-enabled building of Text library in all ES PKGBUILDs. --- PKGBUILD-es2 | 1 - PKGBUILD-es2desktop | 1 - PKGBUILD-es3 | 1 - src/Text/DistanceFieldGlyphCache.cpp | 40 ++++++++++++++++++++++------ src/Text/DistanceFieldGlyphCache.h | 5 +++- src/Text/GlyphCache.cpp | 22 +++++---------- src/Text/GlyphCache.h | 7 ++--- 7 files changed, 47 insertions(+), 30 deletions(-) diff --git a/PKGBUILD-es2 b/PKGBUILD-es2 index 8daa7c23c..5372804da 100644 --- a/PKGBUILD-es2 +++ b/PKGBUILD-es2 @@ -26,7 +26,6 @@ build() { -DBUILD_TESTS=ON \ -DTARGET_GLES=ON \ -DTARGET_GLES2=ON \ - -DWITH_TEXT=OFF \ -DWITH_MAGNUMINFO=OFF \ -DWITH_XEGLAPPLICATION=ON make diff --git a/PKGBUILD-es2desktop b/PKGBUILD-es2desktop index 3f58adf14..821dcf6e3 100644 --- a/PKGBUILD-es2desktop +++ b/PKGBUILD-es2desktop @@ -27,7 +27,6 @@ build() { -DTARGET_GLES=ON \ -DTARGET_GLES2=ON \ -DTARGET_DESKTOP_GLES=ON \ - -DWITH_TEXT=OFF \ -DWITH_MAGNUMINFO=OFF \ -DWITH_XEGLAPPLICATION=ON make diff --git a/PKGBUILD-es3 b/PKGBUILD-es3 index 4ff7500c4..6c8a8aef6 100644 --- a/PKGBUILD-es3 +++ b/PKGBUILD-es3 @@ -26,7 +26,6 @@ build() { -DBUILD_TESTS=ON \ -DTARGET_GLES=ON \ -DTARGET_GLES2=OFF \ - -DWITH_TEXT=OFF \ -DWITH_MAGNUMINFO=OFF \ -DWITH_XEGLAPPLICATION=ON make diff --git a/src/Text/DistanceFieldGlyphCache.cpp b/src/Text/DistanceFieldGlyphCache.cpp index 0bda02670..83daad155 100644 --- a/src/Text/DistanceFieldGlyphCache.cpp +++ b/src/Text/DistanceFieldGlyphCache.cpp @@ -26,30 +26,54 @@ #include "Extensions.h" #include "Image.h" +#ifndef CORRADE_NO_ASSERT +#include "ImageFormat.h" +#endif #include "TextureFormat.h" #include "TextureTools/DistanceField.h" namespace Magnum { namespace Text { -namespace { +DistanceFieldGlyphCache::DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& distanceFieldSize, UnsignedInt radius): GlyphCache(originalSize, Vector2i(radius)), scale(Vector2(distanceFieldSize)/originalSize), radius(radius) { + #ifndef MAGNUM_TARGET_GLES + MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); + #endif + #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) const TextureFormat internalFormat = TextureFormat::R8; #else - const TextureFormat internalFormat = TextureFormat::Red; + const TextureFormat internalFormat = + Context::current()->isExtensionSupported() ? + TextureFormat::Red : TextureFormat::RGB; + if(internalFormat == TextureFormat::RGB) + Warning() << "Text::DistanceFieldGlyphCache:" << Extensions::GL::EXT::texture_rg::string() << "not supported, using inefficient RGB format for glyph cache texture"; #endif + + initialize(internalFormat, distanceFieldSize); } -DistanceFieldGlyphCache::DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& distanceFieldSize, UnsignedInt radius): GlyphCache(originalSize, Vector2i(radius)), scale(Vector2(distanceFieldSize)/originalSize), radius(radius) { +void DistanceFieldGlyphCache::setImage(const Vector2i& offset, Image2D* const image) { #ifndef MAGNUM_TARGET_GLES MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); - #else - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); #endif - initialize(internalFormat, distanceFieldSize); -} + #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) + const TextureFormat internalFormat = TextureFormat::R8; + CORRADE_ASSERT(image->format() == ImageFormat::Red, + "Text::DistanceFieldGlyphCache::setImage(): expected" << ImageFormat::Red << "but got" << image->format(), ); + #else + TextureFormat internalFormat; + if(Context::current()->isExtensionSupported()) { + internalFormat = TextureFormat::Red; + CORRADE_ASSERT(image->format() == ImageFormat::Red, + "Text::DistanceFieldGlyphCache::setImage(): expected" << ImageFormat::Red << "but got" << image->format(), ); + } else { + internalFormat = TextureFormat::Luminance; + CORRADE_ASSERT(image->format() == ImageFormat::Luminance, + "Text::DistanceFieldGlyphCache::setImage(): expected" << ImageFormat::Luminance << "but got" << image->format(), ); + } + #endif -void DistanceFieldGlyphCache::setImage(const Vector2i& offset, Image2D* const image) { Texture2D input; input.setWrapping(Sampler::Wrapping::ClampToEdge) ->setMinificationFilter(Sampler::Filter::Linear) diff --git a/src/Text/DistanceFieldGlyphCache.h b/src/Text/DistanceFieldGlyphCache.h index e9161a95f..360d34844 100644 --- a/src/Text/DistanceFieldGlyphCache.h +++ b/src/Text/DistanceFieldGlyphCache.h @@ -62,7 +62,10 @@ class MAGNUM_TEXT_EXPORT DistanceFieldGlyphCache: public GlyphCache { * @param radius Distance field computation radius * * See TextureTools::distanceField() for more information about the - * parameters. + * parameters. Sets internal texture format to red channel only. On + * desktop OpenGL requires @extension{ARB,texture_rg} (also part of + * OpenGL ES 3.0), in ES2 uses @es_extension{EXT,texture_rg} if + * available or @ref TextureFormat "TextureFormat::RGB" as fallback. */ explicit DistanceFieldGlyphCache(const Vector2i& originalSize, const Vector2i& distanceFieldSize, UnsignedInt radius); diff --git a/src/Text/GlyphCache.cpp b/src/Text/GlyphCache.cpp index f3f072132..8d96ef2a3 100644 --- a/src/Text/GlyphCache.cpp +++ b/src/Text/GlyphCache.cpp @@ -31,19 +31,17 @@ namespace Magnum { namespace Text { -namespace { - #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) - const TextureFormat internalFormat = TextureFormat::R8; - #else - const TextureFormat internalFormat = TextureFormat::Red; - #endif -} - GlyphCache::GlyphCache(const Vector2i& size): _size(size) { #ifndef MAGNUM_TARGET_GLES MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_rg); + #endif + + #if !defined(MAGNUM_TARGET_GLES) || defined(MAGNUM_TARGET_GLES3) + const TextureFormat internalFormat = TextureFormat::R8; #else - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_rg); + const TextureFormat internalFormat = + Context::current()->isExtensionSupported() ? + TextureFormat::Red : TextureFormat::Luminance; #endif initialize(internalFormat, size); @@ -59,12 +57,6 @@ GlyphCache::~GlyphCache() = default; /** @todo Delegating constructor when support for GCC 4.6 is dropped */ void GlyphCache::initialize(const TextureFormat internalFormat, const Vector2i& size) { - #ifndef MAGNUM_TARGET_GLES - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::texture_storage); - #else - MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::EXT::texture_storage); - #endif - _texture.setWrapping(Sampler::Wrapping::ClampToEdge) ->setMinificationFilter(Sampler::Filter::Linear) ->setMagnificationFilter(Sampler::Filter::Linear) diff --git a/src/Text/GlyphCache.h b/src/Text/GlyphCache.h index b87504f7a..e2a27a33e 100644 --- a/src/Text/GlyphCache.h +++ b/src/Text/GlyphCache.h @@ -69,9 +69,10 @@ class MAGNUM_TEXT_EXPORT GlyphCache { * @brief Constructor * @param size Glyph cache texture size * - * Sets internal texture format to red channel only. Requires - * @extension{ARB,texture_rg} (also part of OpenGL ES 3.0 or available - * as @es_extension{EXT,texture_rg} in ES 2.0). + * Sets internal texture format to red channel only. On desktop OpenGL + * requires @extension{ARB,texture_rg} (also part of OpenGL ES 3.0), in + * ES2 uses @es_extension{EXT,texture_rg}, if available, or + * @ref TextureFormat "TextureFormat::Luminance" as fallback. */ explicit GlyphCache(const Vector2i& size);