Switch to more compact TextGlyph vertex format

This commit is contained in:
Georg Hagen
2025-01-05 00:02:01 +01:00
parent 47a904f572
commit e9a1c629d9
13 changed files with 249 additions and 248 deletions

View File

@@ -6,9 +6,7 @@
#include "TextDrawable.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/UniformBuffer.hpp"
#include "Shader/Shader.hpp"
#include "Scene/IFontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include "Host/ResourceLoader.hpp"
@@ -24,32 +22,39 @@ namespace OpenVulkano::Scene
{
constexpr uint32_t MISSING_GLYPH_SYMBOL = '?';
Shader MakeDefaultShader(FontAtlasType type)
{
Shader shader;
shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/text");
shader.AddShaderProgram(ShaderProgramType::FRAGMENT, std::string(type.GetDefaultFragmentShader()));
shader.AddVertexInputDescription(Vertex::GetVertexInputDescription());
shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
DescriptorSetLayoutBinding desc = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
desc.stageFlags = ShaderProgramType::FRAGMENT;
shader.AddDescriptorSetLayoutBinding(desc);
shader.alphaBlend = true;
shader.cullMode = CullMode::NONE;
return shader;
}
Shader DEFAULT_SHADER_BITMAP = TextDrawable::MakeDefaultShader(FontAtlasType::BITMAP);
Shader DEFAULT_SHADER_SDF = TextDrawable::MakeDefaultShader(FontAtlasType::SDF);
Shader DEFAULT_SHADER_MSDF = TextDrawable::MakeDefaultShader(FontAtlasType::MSDF);
}
Shader DEFAULT_SHADER_BITMAP = MakeDefaultShader(FontAtlasType::BITMAP);
Shader DEFAULT_SHADER_SDF = MakeDefaultShader(FontAtlasType::SDF);
Shader DEFAULT_SHADER_MSDF = MakeDefaultShader(FontAtlasType::MSDF);
Shader TextDrawable::MakeDefaultShader(const FontAtlasType type)
{
Shader shader;
shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/text");
shader.AddShaderProgram(ShaderProgramType::FRAGMENT, std::string(type.GetDefaultFragmentShader()));
VertexInputDescription inputDesc(0, sizeof(TextGlyphVertex));
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, position));
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, position)+8);
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, position)+16);
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, position)+24);
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, uv));
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, uv)+8);
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, uv)+16);
inputDesc.AddInputParameter(DataFormat::R32G32_SFLOAT, offsetof(TextGlyphVertex, uv)+24);
inputDesc.AddInputParameter(DataFormat::R8G8B8A8_UNORM, offsetof(TextGlyphVertex, color));
inputDesc.AddInputParameter(DataFormat::R8G8B8A8_UNORM, offsetof(TextGlyphVertex, background));
inputDesc.stepMode = VertexStepMode::INSTANCE;
shader.AddVertexInputDescription(inputDesc);
shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
shader.alphaBlend = true;
shader.cullMode = CullMode::NONE;
shader.topology = Topology::TRIANGLE_FAN;
return shader;
}
TextDrawable::TextDrawable(const TextConfig& config)
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_cfg(config)
{
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
}
{}
TextDrawable::TextDrawable(const Array<char>& atlasMetadata, const TextConfig& config)
: TextDrawable(atlasMetadata, nullptr, config)
@@ -106,8 +111,6 @@ namespace OpenVulkano::Scene
read_bytes += sizeof(GlyphInfo);
readMetadataBytes += sizeof(GlyphInfo);
}
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
if (m_atlasData->meta.atlasType == FontAtlasType::BITMAP)
{
m_atlasData->texture.m_samplerConfig = &SamplerConfig::NEAREST;
@@ -118,8 +121,6 @@ namespace OpenVulkano::Scene
: Drawable(DrawEncoder::GetDrawEncoder<TextDrawable>()), m_atlasData(atlasData), m_cfg(config)
{
if (!atlasData || !*atlasData) throw std::runtime_error("Cannot initialize text drawable with empty atlas data");
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
m_uniBuffer.binding.stageFlags = ShaderProgramType::FRAGMENT;
}
uint32_t TextDrawable::GetFallbackGlyph() const
@@ -132,25 +133,24 @@ namespace OpenVulkano::Scene
return m_atlasData->glyphs.begin()->first;
}
void TextDrawable::GenerateText(const std::string& text, const Math::Vector3f& pos)
void TextDrawable::GenerateText(const std::string& text, const Math::Vector2f& pos)
{
if (text.empty()) return;
if (m_vertexBuffer.data) throw std::runtime_error("Text has already been initialized");
const uint32_t fallbackGlyph = GetFallbackGlyph();
m_text = text;
m_symbolCount = 0;
const size_t len = utf8::distance(text.begin(), text.end());
m_geometry.Close();
m_geometry.Init(len * 4, len * 6);
m_vertexBuffer.Close();
TextGlyphVertex* vertices = m_vertexBuffer.Init<TextGlyphVertex>(len);
std::map<uint32_t, GlyphInfo>* symbols = &m_atlasData->glyphs;
AtlasMetadata* meta = &m_atlasData->meta;
double cursorX = pos.x;
const double lineHeight = meta->lineHeight;
double posY = pos.y;
Math::Vector3f bmin(pos), bmax(pos);
int i = 0;
Math::Vector3f bmin(pos, 0), bmax(pos, 0);
for (auto begin = text.begin(), end = text.end(); begin != end;)
{
uint32_t c = utf8::next(begin, end);
@@ -160,6 +160,7 @@ namespace OpenVulkano::Scene
cursorX = pos.x;
continue;
}
// TODO handle special chars
if (!symbols->contains(c))
{
@@ -167,44 +168,29 @@ namespace OpenVulkano::Scene
c = fallbackGlyph;
}
uint32_t vIdx = i * 4;
uint32_t indices[] = { 1 + vIdx, 2 + vIdx, 3 + vIdx, 1 + vIdx, 3 + vIdx, 0 + vIdx };
GlyphInfo& info = symbols->at(c);
// left bottom
m_geometry.vertices[vIdx].position.x = info.xyz[0].x + cursorX;
m_geometry.vertices[vIdx].position.y = posY - info.xyz[0].y;
m_geometry.vertices[vIdx].position.z = info.xyz[0].z;
m_geometry.vertices[vIdx].textureCoordinates = Math::Vector3f(info.uv[0], 0);
for (int i = 0; i < 4; i++)
{
vertices->position[i].x = info.xyz[i].x + cursorX;
vertices->uv[i] = info.uv[i];
if (i < 2) vertices->position[i].y = posY - info.xyz[i].y;
else vertices->position[i].y = posY + info.xyz[i].y;
vertices->color = m_cfg.textColor;
vertices->background = m_cfg.backgroundColor;
}
// right bottom
m_geometry.vertices[vIdx + 1].position.x = info.xyz[1].x + cursorX;
m_geometry.vertices[vIdx + 1].position.y = posY - info.xyz[1].y;
m_geometry.vertices[vIdx + 1].position.z = info.xyz[1].z;
m_geometry.vertices[vIdx + 1].textureCoordinates = Math::Vector3f(info.uv[1], 0);
// top right
m_geometry.vertices[vIdx + 2].position.x = info.xyz[2].x + cursorX;
m_geometry.vertices[vIdx + 2].position.y = posY + info.xyz[2].y;
m_geometry.vertices[vIdx + 2].position.z = info.xyz[2].z;
m_geometry.vertices[vIdx + 2].textureCoordinates = Math::Vector3f(info.uv[2], 0);
// top left
m_geometry.vertices[vIdx + 3].position.x = info.xyz[3].x + cursorX;
m_geometry.vertices[vIdx + 3].position.y = posY + info.xyz[3].y;
m_geometry.vertices[vIdx + 3].position.z = info.xyz[3].z;
m_geometry.vertices[vIdx + 3].textureCoordinates = Math::Vector3f(info.uv[3], 0);
m_geometry.SetIndices(indices, 6, 6 * i);
// TODO: change to lower value(or ideally remove completely) to avoid overlapping and make less space between symbols
// when setting for depth comparison operator will be available( <= )
cursorX += info.advance + 0.08;
bmax.x = std::max(bmax.x, m_geometry.vertices[vIdx + 1].position.x);
bmax.y = std::max(bmax.y, m_geometry.vertices[vIdx + 2].position.y);
bmin.y = std::min(bmin.y, m_geometry.vertices[vIdx + 1].position.y);
++i;
bmax.x = std::max(bmax.x, vertices->position[1].x);
bmax.y = std::max(bmax.y, vertices->position[2].y);
bmin.y = std::min(bmin.y, vertices->position[1].y);
vertices++;
m_symbolCount++;
}
bmin.x = m_geometry.vertices[0].position.x;
bmin.x = vertices->position[0].x;
m_bbox.Init(bmin, bmax);
if (!GetShader()) SetShader(GetDefaultShader(m_atlasData->meta.atlasType));