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.

239 lines
12 KiB

/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024, 2025
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 "GlyphCacheGL.h"
#ifdef MAGNUM_BUILD_DEPRECATED
#include <Corrade/Containers/Optional.h>
#endif
#include "Magnum/Image.h"
#include "Magnum/ImageView.h"
#if (!defined(MAGNUM_TARGET_GLES) || (defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL))) || defined(MAGNUM_BUILD_DEPRECATED)
#include "Magnum/PixelFormat.h"
#endif
#include "Magnum/Math/Vector2.h"
#if !defined(MAGNUM_TARGET_GLES) || (defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL))
#include "Magnum/GL/Context.h"
#include "Magnum/GL/Extensions.h"
#endif
#if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
#include "Magnum/GL/PixelFormat.h"
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
#endif
#include "Magnum/GL/TextureFormat.h"
#include "Magnum/Text/Implementation/glyphCacheGLState.h"
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
namespace Magnum { namespace Text {
GlyphCacheGL::State::State(const PixelFormat format, const Vector2i& size, const PixelFormat processedFormat, const Vector2i& processedSize, const Vector2i& padding): AbstractGlyphCache::State{format, {size, 1}, processedFormat, processedSize, padding} {
#ifndef MAGNUM_TARGET_GLES
if(processedFormat == PixelFormat::R8Unorm)
MAGNUM_ASSERT_GL_EXTENSION_SUPPORTED(GL::Extensions::ARB::texture_rg);
#endif
/* Initialize the texture */
texture.setWrapping(GL::SamplerWrapping::ClampToEdge)
.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear);
/* ES2 special-casing. WebGL 1 has neither EXT_texture_rg nor
EXT_texture_storage so it can use the common code path without
issues. */
#if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
/* Prefer to use Red instead of Luminance if available, as Luminance isn't
renderable */
GL::TextureFormat textureFormat = GL::textureFormat(processedFormat);
GL::PixelFormat pixelFormat = GL::pixelFormat(processedFormat);
if(textureFormat == GL::TextureFormat::Luminance && GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>()) {
textureFormat = GL::TextureFormat::Red;
pixelFormat = GL::PixelFormat::Red;
}
/* And use setImage() instead of setStorage() if the format is unsized, as
EXT_texture_storage doesn't allow those */
if(textureFormat == GL::TextureFormat::Red ||
textureFormat == GL::TextureFormat::Luminance ||
textureFormat == GL::TextureFormat::RG ||
textureFormat == GL::TextureFormat::LuminanceAlpha ||
textureFormat == GL::TextureFormat::RGB ||
textureFormat == GL::TextureFormat::SRGB ||
textureFormat == GL::TextureFormat::RGBA ||
textureFormat == GL::TextureFormat::SRGBAlpha)
texture.setImage(0, textureFormat, ImageView2D{pixelFormat, GL::PixelType::UnsignedByte, processedSize});
else
texture.setStorage(1, textureFormat, processedSize);
#else
texture.setStorage(1, GL::textureFormat(processedFormat), processedSize);
#endif
}
GlyphCacheGL::GlyphCacheGL(PixelFormat format, const Vector2i& size, PixelFormat processedFormat, const Vector2i& processedSize, const Vector2i& padding): AbstractGlyphCache{Containers::pointer<State>(format, size, processedFormat, processedSize, padding)} {}
GlyphCacheGL::GlyphCacheGL(PixelFormat format, const Vector2i& size, PixelFormat processedFormat, const Vector2i& processedSize): GlyphCacheGL{format, size, processedFormat, processedSize, Vector2i{1}} {}
GlyphCacheGL::GlyphCacheGL(const PixelFormat format, const Vector2i& size, const Vector2i& padding): GlyphCacheGL{format, size, format, size, padding} {}
GlyphCacheGL::GlyphCacheGL(const PixelFormat format, const Vector2i& size): GlyphCacheGL{format, size, Vector2i{1}} {}
#ifdef MAGNUM_BUILD_DEPRECATED
/* The unconditional Optional unwrap in these two may assert in rare cases.
Let's hope it doesn't in practice. */
GlyphCacheGL::GlyphCacheGL(const GL::TextureFormat internalFormat, const Vector2i& size, const Vector2i& padding): GlyphCacheGL{*GL::genericPixelFormat(internalFormat), size, padding} {}
GlyphCacheGL::GlyphCacheGL(const GL::TextureFormat internalFormat, const Vector2i& size, const Vector2i& processedSize, const Vector2i& padding): GlyphCacheGL{*GL::genericPixelFormat(internalFormat), size, *GL::genericPixelFormat(internalFormat), processedSize, padding} {}
GlyphCacheGL::GlyphCacheGL(const Vector2i& size, const Vector2i& padding): GlyphCacheGL{PixelFormat::R8Unorm, size, padding} {}
GlyphCacheGL::GlyphCacheGL(const Vector2i& size, const Vector2i& processedSize, const Vector2i& padding): GlyphCacheGL{PixelFormat::R8Unorm, size, PixelFormat::R8Unorm, processedSize, padding} {}
#endif
GlyphCacheGL::GlyphCacheGL(Containers::Pointer<State>&& state) noexcept: AbstractGlyphCache{Utility::move(state)} {}
GlyphCacheGL::GlyphCacheGL(NoCreateT) noexcept: AbstractGlyphCache{NoCreate} {}
GL::Texture2D& GlyphCacheGL::texture() {
return static_cast<State&>(*_state).texture;
}
GlyphCacheFeatures GlyphCacheGL::doFeatures() const { return {}; }
void GlyphCacheGL::doSetImage(const Vector2i& offset, const ImageView2D& image) {
auto& state = static_cast<State&>(*_state);
CORRADE_ASSERT(format() == processedFormat() && size() == processedSize(),
"Text::GlyphCacheGL::flushImage(): subclass expected to provide a doSetImage() implementation to handle different processed format or size", );
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
/* On ES2 without EXT_unpack_subimage and on WebGL 1 there's no possibility
to upload just a slice of the input, upload the whole image instead by
ignoring the PixelStorage properties of the input */
#ifdef MAGNUM_TARGET_GLES2
#ifndef MAGNUM_TARGET_WEBGL
if(!GL::Context::current().isExtensionSupported<GL::Extensions::EXT::unpack_subimage>())
#endif
{
/* On ES2 if EXT_texture_rg is present, the single-channel texture
format is Red instead of Luminance. Have to duplicate the logic here
in addition to below because it's easier than extracting
formatExtra() and everything else from the view afterwards. */
#ifndef MAGNUM_TARGET_WEBGL
if(image.format() == PixelFormat::R8Unorm && GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>()) {
state.texture.setSubImage(0, {}, ImageView2D{GL::PixelFormat::Red, GL::PixelType::UnsignedByte, size().xy(), image.data()});
} else
#endif
{
state.texture.setSubImage(0, {}, ImageView2D{image.format(), size().xy(), image.data()});
}
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
#ifdef MAGNUM_TARGET_WEBGL
static_cast<void>(offset);
#endif
}
#ifndef MAGNUM_TARGET_WEBGL
else
#endif
#endif
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
{
/* On ES2 if EXT_texture_rg is present, the single-channel texture
format is Red instead of Luminance */
#ifdef MAGNUM_TARGET_GLES2
if(image.format() == PixelFormat::R8Unorm && GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>()) {
state.texture.setSubImage(0, offset, ImageView2D{image.storage(), GL::PixelFormat::Red, GL::PixelType::UnsignedByte, image.size(), image.data()});
} else
#endif
{
state.texture.setSubImage(0, offset, image);
}
Text: rework AbstractGlyphCache for better flexibility and efficiency. The class now supports incremental filling, multiple fonts, texture arrays, removes all reliance on STL containers and is finally properly documented. To avoid complete breakage of every use, as much as possible was kept as deprecated APIs -- in particular the reserve() with the nasty std::vectors, the insert() that assumes a 2D cache and a single font and textureSize() that returns a 2D vector. Those behave the same as before, but will assert if the cache is an array or contains more than one font. On the other hand, begin() / end() access with std::unordered_map iterators (ew!) was removed as the internals simply aren't a hashmap anymore. The image() that returned an Image2D is now used to fill the glyph cache instead of querying its potentially processed contents, and returns a MutableImageView3D. I considered keeping it and adding sourceImage() instead, but such naming turned out to be too inconsistent. For querying processed image data (such as with the distance field cache) there's a new processedImage() query, guarded by new GlyphCacheFeature bits -- if both ImageProcessing and ProcessedImageDownload is set, it can be used to retrieve the processed image (so, similar as ImageDownload was before), and if neither is set, the cache contents are queryable directly through image(), without needing any special support from the GPU API. Existing code is updated only in the minimal way possible to ensure that no serious breakage was introduced by reimplementing the deprecated APIs on top of the new backend. Porting away from deprecated APIs will be done in next commits. The GlyphCache and DistanceFieldGlyphCache have their public API kept intact for now, as a similar rework will be needed for them as well. Additionally, the MagnumFont and MagnumFontConverter plugins aren't compiling yet as they require substantial changes to deal with the new glyph cache features. That is not the case with other plugins in the magnum-plugins repository tho, for those the backwards compatibility "just works". On the other hand, since layout of the AbstractGlyphChange changed, I'm bumping the AbstractFont plugin interface version to force-trigger a rebuild of dependent projects. Because I ran a stale magnum-player binary, it worked without crashing or GL errors but just didn't show ANY text whatsoever due to ABI differences, and I wasted some precious minutes before realizing that a simple rebuild would fix it.
3 years ago
}
#endif
}
void GlyphCacheGL::doSetProcessedImage(const Vector2i& offset, const ImageView2D& image) {
ImageView2D imageToUse = image;
/* On ES2, R8Unorm maps to Luminance, but here it's actually Red if
EXT_texture_rg is supported. Reinterpret the image format in that
case. If the format is something else (such as RGBA8Unorm), no
reinterpret is done. WebGL doesn't have the EXT_texture_rg extension so
there it isn't done either. */
#if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
if(processedFormat() == PixelFormat::R8Unorm && GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>()) {
/* This is checked inside setProcessedImage() already */
CORRADE_INTERNAL_ASSERT(image.format() == PixelFormat::R8Unorm);
imageToUse = ImageView2D{image.storage(), GL::PixelFormat::Red, GL::PixelType::UnsignedByte, image.size(), image.data()};
}
#endif
static_cast<State&>(*_state).texture.setSubImage(0, offset, imageToUse);
}
#ifndef MAGNUM_TARGET_GLES
Image3D GlyphCacheGL::doProcessedImage() {
Image2D out = static_cast<State&>(*_state).texture.image(0, processedFormat());
return Image3D{out.format(), {out.size(), 1}, out.release()};
}
#endif
#ifndef MAGNUM_TARGET_GLES2
GlyphCacheArrayGL::State::State(const PixelFormat format, const Vector3i& size, const PixelFormat processedFormat, const Vector2i& processedSize, const Vector2i& padding): AbstractGlyphCache::State{format, size, processedFormat, processedSize, padding} {
#ifndef MAGNUM_TARGET_GLES
if(processedFormat == PixelFormat::R8Unorm)
MAGNUM_ASSERT_GL_EXTENSION_SUPPORTED(GL::Extensions::ARB::texture_rg);
#endif
/* Initialize the texture */
texture.setWrapping(GL::SamplerWrapping::ClampToEdge)
.setMinificationFilter(GL::SamplerFilter::Linear)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setStorage(1, GL::textureFormat(processedFormat), {processedSize, size.z()});
}
GlyphCacheArrayGL::GlyphCacheArrayGL(PixelFormat format, const Vector3i& size, PixelFormat processedFormat, const Vector2i& processedSize, const Vector2i& padding): AbstractGlyphCache{Containers::pointer<State>(format, size, processedFormat, processedSize, padding)} {}
GlyphCacheArrayGL::GlyphCacheArrayGL(PixelFormat format, const Vector3i& size, PixelFormat processedFormat, const Vector2i& processedSize): GlyphCacheArrayGL{format, size, processedFormat, processedSize, Vector2i{1}} {}
GlyphCacheArrayGL::GlyphCacheArrayGL(const PixelFormat format, const Vector3i& size, const Vector2i& padding): GlyphCacheArrayGL{format, size, format, size.xy(), padding} {}
GlyphCacheArrayGL::GlyphCacheArrayGL(const PixelFormat format, const Vector3i& size): GlyphCacheArrayGL{format, size, Vector2i{1}} {}
GlyphCacheArrayGL::GlyphCacheArrayGL(NoCreateT) noexcept: AbstractGlyphCache{NoCreate} {}
GL::Texture2DArray& GlyphCacheArrayGL::texture() {
return static_cast<State&>(*_state).texture;
}
GlyphCacheFeatures GlyphCacheArrayGL::doFeatures() const { return {}; }
void GlyphCacheArrayGL::doSetImage(const Vector3i& offset, const ImageView3D& image) {
CORRADE_ASSERT(format() == processedFormat() && size() == processedSize(),
"Text::GlyphCacheArrayGL::flushImage(): subclass expected to provide a doSetImage() implementation to handle different processed format or size", );
static_cast<State&>(*_state).texture.setSubImage(0, offset, image);
}
#endif
}}