This commit is contained in:
2026-05-20 17:04:03 +10:00
parent e43ac21b2f
commit a9eeed30cf
8 changed files with 58 additions and 23 deletions

View File

@@ -88,6 +88,7 @@ bool RuntimeTextTextureCache::Configure(const RuntimeShaderArtifact& artifact, s
TextTexture texture;
texture.parameterId = definition.id;
texture.fontId = definition.fontId;
texture.maxLength = definition.maxLength == 0 ? 64 : definition.maxLength;
mTextTextures.push_back(std::move(texture));
}
@@ -101,14 +102,21 @@ void RuntimeTextTextureCache::UpdateArtifactState(const RuntimeShaderArtifact& a
mArtifact.parameterValues = artifact.parameterValues;
}
void RuntimeTextTextureCache::RefreshTextTextures()
{
for (TextTexture& textTexture : mTextTextures)
{
EnsureTextTexture(textTexture);
}
}
void RuntimeTextTextureCache::BindTextTextures(GLuint program)
{
for (std::size_t index = 0; index < mTextTextures.size(); ++index)
{
TextTexture& textTexture = mTextTextures[index];
if (!EnsureTextTexture(textTexture))
const TextTexture& textTexture = mTextTextures[index];
if (textTexture.texture == 0)
continue;
glActiveTexture(GL_TEXTURE0 + kFirstTextTextureUnit + static_cast<GLuint>(index));
glBindTexture(GL_TEXTURE_2D, textTexture.texture);
}
@@ -318,21 +326,32 @@ bool RuntimeTextTextureCache::EnsureTextTexture(TextTexture& texture)
unsigned width = 0;
unsigned height = 0;
std::vector<unsigned char> pixels = ComposeTextMask(*atlas, text, width, height);
std::vector<unsigned char> pixels = ComposeTextMask(*atlas, texture, text, width, height);
if (pixels.empty() || width == 0 || height == 0)
return false;
if (texture.texture == 0)
glGenTextures(1, &texture.texture);
GLint previousUnpackAlignment = 4;
glGetIntegerv(GL_UNPACK_ALIGNMENT, &previousUnpackAlignment);
glBindTexture(GL_TEXTURE_2D, texture.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RED, GL_UNSIGNED_BYTE, pixels.data());
if (texture.width != width || texture.height != height)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RED, GL_UNSIGNED_BYTE, pixels.data());
}
else
{
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height), GL_RED, GL_UNSIGNED_BYTE, pixels.data());
}
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, previousUnpackAlignment);
glActiveTexture(GL_TEXTURE0);
texture.cachedText = text;
texture.width = width;
@@ -340,7 +359,7 @@ bool RuntimeTextTextureCache::EnsureTextTexture(TextTexture& texture)
return texture.texture != 0;
}
std::vector<unsigned char> RuntimeTextTextureCache::ComposeTextMask(const Atlas& atlas, const std::string& text, unsigned& width, unsigned& height) const
std::vector<unsigned char> RuntimeTextTextureCache::ComposeTextMask(const Atlas& atlas, const TextTexture& texture, const std::string& text, unsigned& width, unsigned& height) const
{
double advance = 0.0;
for (unsigned char character : text)
@@ -350,7 +369,8 @@ std::vector<unsigned char> RuntimeTextTextureCache::ComposeTextMask(const Atlas&
advance += glyphIt->second.advance;
}
width = (std::max)(1u, static_cast<unsigned>(std::ceil(advance * kFontPixelsPerEm)) + kTextTexturePadding * 2u);
const unsigned fixedWidth = static_cast<unsigned>(std::ceil(static_cast<double>(texture.maxLength) * kFontPixelsPerEm * 0.9)) + kTextTexturePadding * 2u;
width = (std::max)(fixedWidth, static_cast<unsigned>(std::ceil(advance * kFontPixelsPerEm)) + kTextTexturePadding * 2u);
height = kTextTextureHeight;
std::vector<unsigned char> mask(static_cast<std::size_t>(width) * height, 0);