mirror of https://github.com/mosra/magnum.git
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.
172 lines
7.9 KiB
172 lines
7.9 KiB
|
13 years ago
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
6 years ago
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
|
||
|
3 years ago
|
2020, 2021, 2022, 2023 Vladimír Vondruš <mosra@centrum.cz>
|
||
|
13 years ago
|
|
||
|
|
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.
|
||
|
|
*/
|
||
|
|
|
||
|
2 years ago
|
#include "DistanceFieldGlyphCacheGL.h"
|
||
|
13 years ago
|
|
||
|
11 years ago
|
#include "Magnum/ImageView.h"
|
||
|
3 years ago
|
#include "Magnum/PixelFormat.h"
|
||
|
8 years ago
|
#include "Magnum/GL/Context.h"
|
||
|
|
#include "Magnum/GL/Extensions.h"
|
||
|
11 years ago
|
#ifndef CORRADE_NO_ASSERT
|
||
|
8 years ago
|
#include "Magnum/GL/PixelFormat.h"
|
||
|
11 years ago
|
#endif
|
||
|
8 years ago
|
#include "Magnum/GL/TextureFormat.h"
|
||
|
3 years ago
|
#include "Magnum/Math/Range.h"
|
||
|
2 years ago
|
#include "Magnum/TextureTools/DistanceFieldGL.h"
|
||
|
13 years ago
|
|
||
|
|
namespace Magnum { namespace Text {
|
||
|
|
|
||
|
2 years ago
|
DistanceFieldGlyphCacheGL::DistanceFieldGlyphCacheGL(const Vector2i& size, const Vector2i& processedSize, const UnsignedInt radius):
|
||
|
|
GlyphCacheGL{PixelFormat::R8Unorm, size,
|
||
|
2 years ago
|
#if !defined(MAGNUM_TARGET_GLES) || !defined(MAGNUM_TARGET_GLES2)
|
||
|
|
PixelFormat::R8Unorm,
|
||
|
|
#else
|
||
|
|
#ifndef MAGNUM_TARGET_WEBGL
|
||
|
|
/* Without EXT_texture_rg, PixelFormat::R8Unorm maps to Luminance which
|
||
|
|
is not renderable in most cases. RGB is *theoretically* space-
|
||
|
|
efficient but practically the driver uses RGBA internally anyway, so
|
||
|
|
just use RGBA. */
|
||
|
|
GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>() ?
|
||
|
|
PixelFormat::R8Unorm :
|
||
|
|
#endif
|
||
|
|
PixelFormat::RGBA8Unorm,
|
||
|
|
#endif
|
||
|
|
processedSize, Vector2i(radius)},
|
||
|
2 years ago
|
_distanceField{radius}
|
||
|
13 years ago
|
{
|
||
|
2 years ago
|
/* Replicating the assertion from TextureTools::DistanceFieldGL so it gets
|
||
|
3 years ago
|
checked during construction already instead of only later during the
|
||
|
|
setImage() call */
|
||
|
2 years ago
|
CORRADE_ASSERT(size % processedSize == Vector2i{0} &&
|
||
|
|
(size/processedSize) % 2 == Vector2i{0},
|
||
|
2 years ago
|
"Text::DistanceFieldGlyphCacheGL: expected source and processed size ratio to be a multiple of 2, got" << Debug::packed << size << "and" << Debug::packed << processedSize, );
|
||
|
3 years ago
|
|
||
|
11 years ago
|
#if defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
|
||
|
2 years ago
|
/* On ES2 print a warning to make it known that EXT_texture_rg wasn't
|
||
|
|
available. On WebGL 1 this is the case always, so a warning would be
|
||
|
|
just a noise. */
|
||
|
8 years ago
|
if(!GL::Context::current().isExtensionSupported<GL::Extensions::EXT::texture_rg>())
|
||
|
2 years ago
|
Warning() << "Text::DistanceFieldGlyphCacheGL:" << GL::Extensions::EXT::texture_rg::string() << "not supported, using a full RGBA format for the distance field texture";
|
||
|
13 years ago
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
2 years ago
|
DistanceFieldGlyphCacheGL::DistanceFieldGlyphCacheGL(NoCreateT) noexcept: GlyphCacheGL{NoCreate}, _distanceField{NoCreate} {}
|
||
|
2 years ago
|
|
||
|
2 years ago
|
GlyphCacheFeatures DistanceFieldGlyphCacheGL::doFeatures() const {
|
||
|
3 years ago
|
return GlyphCacheFeature::ImageProcessing
|
||
|
|
#ifndef MAGNUM_TARGET_GLES
|
||
|
|
|GlyphCacheFeature::ProcessedImageDownload
|
||
|
|
#endif
|
||
|
|
;
|
||
|
|
}
|
||
|
|
|
||
|
2 years ago
|
void DistanceFieldGlyphCacheGL::doSetImage(const Vector2i& offset, const ImageView2D& image) {
|
||
|
8 years ago
|
GL::Texture2D input;
|
||
|
|
input.setWrapping(GL::SamplerWrapping::ClampToEdge)
|
||
|
|
.setMinificationFilter(GL::SamplerFilter::Linear)
|
||
|
|
.setMagnificationFilter(GL::SamplerFilter::Linear);
|
||
|
|
|
||
|
3 years ago
|
/* The constructor already checked that the ratio is an integer multiple,
|
||
|
|
so this division should lead to no information loss */
|
||
|
2 years ago
|
CORRADE_INTERNAL_ASSERT(size().xy() % processedSize().xy() == Vector2i{0});
|
||
|
|
const Vector2i ratio = size().xy()/processedSize().xy();
|
||
|
3 years ago
|
|
||
|
|
/* Upload the input texture and create a distance field from it. 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
|
||
|
3 years ago
|
ignoring the PixelStorage properties of the input and also process it as
|
||
|
|
a whole. */
|
||
|
|
#ifdef MAGNUM_TARGET_GLES2
|
||
|
|
#ifndef MAGNUM_TARGET_WEBGL
|
||
|
|
if(!GL::Context::current().isExtensionSupported<GL::Extensions::EXT::unpack_subimage>())
|
||
|
6 years ago
|
#endif
|
||
|
3 years ago
|
{
|
||
|
|
input.setImage(0, GL::textureFormat(image.format()), ImageView2D{image.format(), size().xy(), image.data()});
|
||
|
3 years ago
|
_distanceField(input, texture(), {{}, size().xy()/ratio}, size().xy());
|
||
|
3 years ago
|
#ifdef MAGNUM_TARGET_WEBGL
|
||
|
|
static_cast<void>(offset);
|
||
|
|
#endif
|
||
|
|
}
|
||
|
11 years ago
|
#ifndef MAGNUM_TARGET_WEBGL
|
||
|
3 years ago
|
else
|
||
|
11 years ago
|
#endif
|
||
|
3 years ago
|
#endif
|
||
|
|
#if !(defined(MAGNUM_TARGET_GLES2) && defined(MAGNUM_TARGET_WEBGL))
|
||
|
11 years ago
|
{
|
||
|
2 years ago
|
/* The image range was already expanded to include the padding in
|
||
|
|
flushImage() */
|
||
|
3 years ago
|
CORRADE_INTERNAL_ASSERT(image.storage().skip().xy() == offset);
|
||
|
2 years ago
|
#ifdef CORRADE_NO_ASSERT
|
||
|
|
static_cast<void>(offset);
|
||
|
|
#endif
|
||
|
2 years ago
|
const Vector2i paddedMin = image.storage().skip().xy();
|
||
|
|
const Vector2i paddedMax = image.size() + image.storage().skip().xy();
|
||
|
3 years ago
|
|
||
|
2 years ago
|
/* TextureTools::DistanceFieldGL expects the input size and output
|
||
|
3 years ago
|
rectangle size ratio to be a multiple of 2 in order for the shader
|
||
|
|
to perform pixel addressing correctly. That might not always be the
|
||
|
|
case with the rectangle passed to flushImage(), so round the
|
||
|
|
paddedMin *down* to a multiple of the ratio and paddedMax *up* to a
|
||
|
|
multiple of the ratio. */
|
||
|
|
const Vector2i paddedMinRounded = ratio*(paddedMin/ratio);
|
||
|
|
const Vector2i paddedMaxRounded = ratio*((paddedMax + ratio - Vector2i{1})/ratio);
|
||
|
|
/* As the size is also a multiple of ratio, the resulting size should
|
||
|
|
not get larger. */
|
||
|
|
CORRADE_INTERNAL_ASSERT(paddedMaxRounded <= size().xy());
|
||
|
|
|
||
|
3 years ago
|
const ImageView2D paddedImage{
|
||
|
|
PixelStorage{image.storage()}
|
||
|
3 years ago
|
.setSkip({paddedMinRounded, image.storage().skip().z()}),
|
||
|
3 years ago
|
image.format(),
|
||
|
3 years ago
|
paddedMaxRounded - paddedMinRounded,
|
||
|
3 years ago
|
image.data()};
|
||
|
|
|
||
|
|
input.setImage(0, GL::textureFormat(paddedImage.format()), paddedImage);
|
||
|
3 years ago
|
_distanceField(input, texture(), Range2Di::fromSize(paddedMinRounded/ratio, paddedImage.size()/ratio), paddedImage.size());
|
||
|
13 years ago
|
}
|
||
|
|
#endif
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
2 years ago
|
#ifdef MAGNUM_BUILD_DEPRECATED
|
||
|
2 years ago
|
void DistanceFieldGlyphCacheGL::setDistanceFieldImage(const Vector2i& offset, const ImageView2D& image) {
|
||
|
2 years ago
|
/* The original function accepted GL pixel formats as well, try to
|
||
|
|
translate them back to the generic format. If that fails, pass the image
|
||
|
|
as-is let the base implementation deal with that instead.
|
||
|
|
|
||
|
|
Replacing the whole view instead of just the format so we don't need to
|
||
|
|
do any special-casing for when the format stays implementation-specific
|
||
|
|
and requires a pixel size to be specified externally. */
|
||
|
|
ImageView2D imageToUse = image;
|
||
|
|
if(isPixelFormatImplementationSpecific(image.format())) {
|
||
|
|
if(const Containers::Optional<PixelFormat> candidateFormat = GL::genericPixelFormat(pixelFormatUnwrap<GL::PixelFormat>(image.format()), GL::PixelType(image.formatExtra())))
|
||
|
|
imageToUse = ImageView2D{image.storage(), *candidateFormat, image.size(), image.data()};
|
||
|
11 years ago
|
}
|
||
|
13 years ago
|
|
||
|
2 years ago
|
setProcessedImage(offset, imageToUse);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
13 years ago
|
}}
|