still refactor

This commit is contained in:
2024-04-24 13:47:58 +02:00
parent 1d9a51e4f7
commit afee09d1ea
10 changed files with 288 additions and 303 deletions

View File

@@ -6,64 +6,62 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 09:28:14 by maldavid #+# #+# */
/* Updated: 2024/04/23 22:59:16 by maldavid ### ########.fr */
/* Updated: 2024/04/24 01:28:40 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <PreCompiled.h>
#include <renderer/texts/font_library.h>
#include <renderer/texts/font.h>
#include <core/errors.h>
#include <renderer/renderer.h>
#include <core/profiler.h>
#include <Renderer/Texts/FontLibrary.h>
#include <Renderer/Texts/Font.h>
#include <Renderer/Renderer.h>
namespace mlx
{
std::shared_ptr<Font> FontLibrary::getFontData(FontID id)
std::shared_ptr<Font> FontLibrary::GetFontData(FontID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
core::error::report(e_kind::fatal_error, "Font Library : wrong font ID '%d'", id);
return _cache[id];
if(!m_cache.count(id) || std::find(m_invalid_ids.begin(), m_invalid_ids.end(), id) != m_invalid_ids.end())
FatalError("Font Library : wrong font ID '%'", id);
return m_cache[id];
}
FontID FontLibrary::addFontToLibrary(std::shared_ptr<Font> font)
FontID FontLibrary::AddFontToLibrary(std::shared_ptr<Font> font)
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(_cache.begin(), _cache.end(), [&](const std::pair<FontID, std::shared_ptr<Font>>& v)
auto it = std::find_if(m_cache.begin(), m_cache.end(), [&](const std::pair<FontID, std::shared_ptr<Font>>& v)
{
return v.second->getScale() == font->getScale() &&
v.second->getName() == font->getName() &&
std::find(_invalid_ids.begin(), _invalid_ids.end(), v.first) == _invalid_ids.end();
return v.second->GetScale() == font->GetScale() &&
v.second->GetName() == font->GetName() &&
std::find(m_invalid_ids.begin(), m_invalid_ids.end(), v.first) == m_invalid_ids.end();
});
if(it != _cache.end())
if(it != m_cache.end())
return it->first;
font->buildFont();
_cache[_current_id] = font;
_current_id++;
return _current_id - 1;
font->BuildFont();
m_cache[m_current_id] = font;
m_current_id++;
return m_current_id - 1;
}
void FontLibrary::removeFontFromLibrary(FontID id)
void FontLibrary::RemoveFontFromLibrary(FontID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
if(!m_cache.count(id) || std::find(m_invalid_ids.begin(), m_invalid_ids.end(), id) != m_invalid_ids.end())
{
core::error::report(e_kind::warning, "Font Library : trying to remove a font with an unkown or invalid ID '%d'", id);
Warning("Font Library : trying to remove a font with an unkown or invalid ID '%'", id);
return;
}
_cache[id]->destroy();
_invalid_ids.push_back(id);
m_cache[id]->Destroy();
m_invalid_ids.push_back(id);
}
void FontLibrary::clearLibrary()
void FontLibrary::ClearLibrary()
{
MLX_PROFILE_FUNCTION();
for(auto& [id, font] : _cache)
for(auto& [id, font] : m_cache)
{
font->destroy();
_invalid_ids.push_back(id);
font->Destroy();
m_invalid_ids.push_back(id);
}
// do not `_cache.clear();` as it releases the fonts and may not destroy the texture atlas that is in use by command buffers
}

View File

@@ -1,79 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text.cpp :+: :+: :+: */
/* Text.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:11:56 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:04:01 by maldavid ### ########.fr */
/* Updated: 2024/04/24 01:33:58 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <PreCompiled.h>
#include <core/profiler.h>
#include <renderer/texts/text.h>
#include <renderer/renderer.h>
#include <Renderer/Texts/Text.h>
#include <Renderer/Renderer.h>
namespace mlx
{
void Text::init(std::string text, FontID font, std::uint32_t color, std::vector<Vertex> vbo_data, std::vector<std::uint16_t> ibo_data)
void Text::Init(std::string text, FontID font, std::uint32_t color, std::vector<Vertex> vbo_data, std::vector<std::uint16_t> ibo_data)
{
MLX_PROFILE_FUNCTION();
if(_is_init)
if(m_is_init)
return;
_text = std::move(text);
_color = color;
_font = font;
m_text = std::move(text);
m_color = color;
m_font = font;
#ifdef DEBUG
std::string debug_name = _text;
std::string debug_name = m_text;
for(char& c : debug_name)
{
if(c == ' ' || c == '"' || c == '\'')
c = '_';
}
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), debug_name.c_str());
_ibo.create(sizeof(std::uint16_t) * ibo_data.size(), ibo_data.data(), debug_name.c_str());
m_vbo[i].Create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), debug_name.c_str());
m_ibo.Create(sizeof(std::uint16_t) * ibo_data.size(), ibo_data.data(), debug_name.c_str());
#else
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), nullptr);
_ibo.create(sizeof(std::uint16_t) * ibo_data.size(), ibo_data.data(), nullptr);
m_vbo[i].Create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), nullptr);
m_ibo.Create(sizeof(std::uint16_t) * ibo_data.size(), ibo_data.data(), nullptr);
#endif
_is_init = true;
m_is_init = true;
}
void Text::bind(Renderer& renderer) noexcept
void Text::Bind(Renderer& renderer) noexcept
{
MLX_PROFILE_FUNCTION();
if(!_is_init)
if(!m_is_init)
return;
_vbo[renderer.getActiveImageIndex()].bind(renderer);
_ibo.bind(renderer);
m_vbo[renderer.GetActiveImageIndex()].Bind(renderer);
m_ibo.Bind(renderer);
}
void Text::updateVertexData(int frame, std::vector<Vertex> vbo_data)
{
MLX_PROFILE_FUNCTION();
if(!_is_init)
if(!m_is_init)
return;
_vbo[frame].setData(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()));
m_vbo[frame].SetData(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()));
}
void Text::destroy() noexcept
{
MLX_PROFILE_FUNCTION();
if(!_is_init)
if(!m_is_init)
return;
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_vbo[i].destroy();
_ibo.destroy();
_is_init = false;
m_vbo[i].Destroy();
m_ibo.Destroy();
m_is_init = false;
}
Text::~Text()
{
destroy();
Destroy();
}
}

View File

@@ -1,61 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_descriptor.cpp :+: :+: :+: */
/* TextDescriptor.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:04:52 by maldavid ### ########.fr */
/* Updated: 2024/04/24 01:38:40 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <Preompiled.h>
#include <core/profiler.h>
#include <renderer/texts/text_descriptor.h>
#include <renderer/images/texture_atlas.h>
#include <renderer/texts/font.h>
#include <renderer/texts/text.h>
#include <Renderer/Images/TextureAtlas.h>
#include <Renderer/Texts/TextDescriptor.h>
#include <Renderer/Texts/Font.h>
#include <Renderer/Texts/Text.h>
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb_rect_pack.h>
#include <core/memory.h>
#include <Core/Memory.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STB_malloc(x, u) ((void)(u), MemManager::malloc(x))
#define STB_free(x, u) ((void)(u), MemManager::free(x))
#define STB_malloc(x, u) ((void)(u), MemManager::Malloc(x))
#define STB_free(x, u) ((void)(u), MemManager::Free(x))
#include <stb_truetype.h>
constexpr const int RANGE = 1024;
namespace mlx
{
TextDrawDescriptor::TextDrawDescriptor(std::string text, std::uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), _text(std::move(text))
TextDrawDescriptor::TextDrawDescriptor(std::string text, std::uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), m_text(std::move(text))
{}
void TextDrawDescriptor::init(FontID font) noexcept
void TextDrawDescriptor::Init(FontID font) noexcept
{
MLX_PROFILE_FUNCTION();
std::vector<Vertex> vertexData;
std::vector<std::uint16_t> indexData;
std::vector<Vertex> vertex_data;
std::vector<std::uint16_t> index_data;
float stb_x = 0.0f;
float stb_y = 0.0f;
{
std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(font);
std::shared_ptr<Font> font_data = FontLibrary::Get().GetFontData(font);
for(char c : _text)
for(char c : m_text)
{
if(c < 32)
continue;
stbtt_aligned_quad q;
stbtt_GetPackedQuad(font_data->getCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
stbtt_GetPackedQuad(font_data->GetCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
std::size_t index = vertexData.size();
std::size_t index = vertex_data.size();
glm::vec4 vertex_color = {
static_cast<float>((color & 0x000000FF)) / 255.f,
@@ -64,49 +63,45 @@ namespace mlx
static_cast<float>((color & 0xFF000000) >> 24) / 255.f
};
vertexData.emplace_back(glm::vec2{q.x0, q.y0}, vertex_color, glm::vec2{q.s0, q.t0});
vertexData.emplace_back(glm::vec2{q.x1, q.y0}, vertex_color, glm::vec2{q.s1, q.t0});
vertexData.emplace_back(glm::vec2{q.x1, q.y1}, vertex_color, glm::vec2{q.s1, q.t1});
vertexData.emplace_back(glm::vec2{q.x0, q.y1}, vertex_color, glm::vec2{q.s0, q.t1});
vertex_data.emplace_back(glm::vec2{q.x0, q.y0}, vertex_color, glm::vec2{q.s0, q.t0});
vertex_data.emplace_back(glm::vec2{q.x1, q.y0}, vertex_color, glm::vec2{q.s1, q.t0});
vertex_data.emplace_back(glm::vec2{q.x1, q.y1}, vertex_color, glm::vec2{q.s1, q.t1});
vertex_data.emplace_back(glm::vec2{q.x0, q.y1}, vertex_color, glm::vec2{q.s0, q.t1});
indexData.emplace_back(index + 0);
indexData.emplace_back(index + 1);
indexData.emplace_back(index + 2);
indexData.emplace_back(index + 2);
indexData.emplace_back(index + 3);
indexData.emplace_back(index + 0);
index_data.emplace_back(index + 0);
index_data.emplace_back(index + 1);
index_data.emplace_back(index + 2);
index_data.emplace_back(index + 2);
index_data.emplace_back(index + 3);
index_data.emplace_back(index + 0);
}
}
std::shared_ptr<Text> text_data = std::make_shared<Text>();
text_data->init(_text, font, color, std::move(vertexData), std::move(indexData));
id = TextLibrary::get().addTextToLibrary(text_data);
#ifdef DEBUG
core::error::report(e_kind::message, "Text put : registered new text to render");
#endif
text_data->Init(m_text, font, color, std::move(vertex_data), std::move(index_data));
id = TextLibrary::Get().AddTextToLibrary(text_data);
DebugLog("Text put : registered new text to render");
}
void TextDrawDescriptor::render(std::array<VkDescriptorSet, 2>& sets, Renderer& renderer)
void TextDrawDescriptor::Render(Renderer& renderer)
{
MLX_PROFILE_FUNCTION();
std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id);
std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(draw_data->getFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->getAtlas());
draw_data->bind(renderer);
if(!atlas.getSet().isInit())
atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
if(!atlas.hasBeenUpdated())
atlas.updateSet(0);
sets[1] = const_cast<TextureAtlas&>(atlas).getVkSet();
vkCmdBindDescriptorSets(renderer.getActiveCmdBuffer().get(), VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
atlas.render(renderer, x, y, draw_data->getIBOsize());
std::shared_ptr<Text> draw_data = TextLibrary::Get().GetTextData(id);
std::shared_ptr<Font> font_data = FontLibrary::Get().GetFontData(draw_data->GetFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->GetAtlas());
draw_data->Bind(renderer);
if(!atlas.GetSet().IsInit())
atlas.SetDescriptor(renderer.GetFragDescriptorSet().Duplicate());
if(!atlas.HasBeenUpdated())
atlas.UpdateSet(0);
atlas.GetSet().Bind();
atlas.Render(renderer, x, y, draw_data->GetIBOsize());
}
void TextDrawDescriptor::resetUpdate()
void TextDrawDescriptor::ResetUpdate()
{
std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id);
std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(draw_data->getFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->getAtlas());
atlas.resetUpdate();
std::shared_ptr<Text> draw_data = TextLibrary::Get().GetTextData(id);
std::shared_ptr<Font> font_data = FontLibrary::Get().GetFontData(draw_data->GetFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->GetAtlas());
atlas.ResetUpdate();
}
}

View File

@@ -1,64 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_library.cpp :+: :+: :+: */
/* TextLibrary.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:05:09 by maldavid ### ########.fr */
/* Updated: 2024/04/24 01:40:28 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <PreCompiled.h>
#include <renderer/texts/text_library.h>
#include <renderer/texts/text.h>
#include <core/errors.h>
#include <renderer/renderer.h>
#include <core/profiler.h>
#include <Renderer/Texts/TextLibrary.h>
#include <Renderer/Texts/Text.h>
#include <Renderer/Renderer.h>
namespace mlx
{
std::shared_ptr<Text> TextLibrary::getTextData(TextID id)
std::shared_ptr<Text> TextLibrary::GetTextData(TextID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id))
core::error::report(e_kind::fatal_error, "Text Library : wrong text ID '%d'", id);
return _cache[id];
if(!m_cache.count(id))
FatalError("Text Library : wrong text ID '%d'", id);
return m_cache[id];
}
TextID TextLibrary::addTextToLibrary(std::shared_ptr<Text> text)
TextID TextLibrary::AddTextToLibrary(std::shared_ptr<Text> text)
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(_cache.begin(), _cache.end(), [&](const std::pair<TextID, std::shared_ptr<Text>>& v)
auto it = std::find_if(m_cache.begin(), m_cache.end(), [&](const std::pair<TextID, std::shared_ptr<Text>>& v)
{
return v.second->getText() == text->getText() && v.second->getColor() == text->getColor();
return v.second->GetText() == text->GetText() && v.second->GetColor() == text->GetColor();
});
if(it != _cache.end())
if(it != m_cache.end())
return it->first;
_cache[_current_id] = text;
_current_id++;
return _current_id - 1;
m_cache[m_current_id] = text;
m_current_id++;
return m_current_id - 1;
}
void TextLibrary::removeTextFromLibrary(TextID id)
void TextLibrary::RemoveTextFromLibrary(TextID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id))
if(!m_cache.count(id))
{
core::error::report(e_kind::warning, "Text Library : trying to remove a text with an unkown or invalid ID '%d'", id);
Warning("Text Library : trying to remove a text with an unkown or invalid ID '%d'", id);
return;
}
_cache[id]->destroy();
_cache.erase(id);
m_cache[id]->Destroy();
m_cache.erase(id);
}
void TextLibrary::clearLibrary()
void TextLibrary::ClearLibrary()
{
MLX_PROFILE_FUNCTION();
for(auto& [id, text] : _cache)
text->destroy();
_cache.clear();
for(auto& [id, text] : m_cache)
text->Destroy();
m_cache.clear();
}
}

View File

@@ -1,34 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_manager.cpp :+: :+: :+: */
/* TextManager.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:05:13 by maldavid ### ########.fr */
/* Updated: 2024/04/24 01:42:19 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <PreCompiled.h>
#include <renderer/texts/text_descriptor.h>
#include <renderer/texts/text_library.h>
#include <renderer/texts/text.h>
#include <renderer/texts/text_manager.h>
#include <core/profiler.h>
#include <utils/dogica_ttf.h>
#include <Renderer/Texts/TextDescriptor.h>
#include <Renderer/Texts/TextLibrary.h>
#include <Renderer/Texts/Text.h>
#include <Renderer/Texts/TextManager.h>
#include <Utils/DogicaTTF.h>
namespace mlx
{
void TextManager::init(Renderer& renderer) noexcept
void TextManager::Init(Renderer& renderer) noexcept
{
MLX_PROFILE_FUNCTION();
loadFont(renderer, "default", 6.f);
LoadFont(renderer, "default", 6.f);
}
void TextManager::loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale)
void TextManager::LoadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale)
{
MLX_PROFILE_FUNCTION();
std::shared_ptr<Font> font;
@@ -36,33 +34,32 @@ namespace mlx
font = std::make_shared<Font>(renderer, "default", dogica_ttf, scale);
else
font = std::make_shared<Font>(renderer, filepath, scale);
_font_in_use = FontLibrary::get().addFontToLibrary(font);
m_font_in_use = FontLibrary::Get().AddFontToLibrary(font);
}
std::pair<DrawableResource*, bool> TextManager::registerText(int x, int y, std::uint32_t color, std::string str)
std::pair<DrawableResource*, bool> TextManager::RegisterText(int x, int y, std::uint32_t color, std::string str)
{
MLX_PROFILE_FUNCTION();
auto res = _text_descriptors.emplace(std::move(str), color, x, y);
auto res = m_text_descriptors.emplace(std::move(str), color, x, y);
if(res.second)
{
const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use);
const_cast<TextDrawDescriptor&>(*res.first).Init(m_font_in_use);
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextDrawDescriptor&>(*res.first)), true);
}
auto text_ptr = TextLibrary::get().getTextData(res.first->id);
if(_font_in_use != text_ptr->getFontInUse())
auto text_ptr = TextLibrary::Get().GetTextData(res.first->id);
if(_font_in_use != text_ptr->GetFontInUse())
{
// TODO : update text vertex buffers rather than destroying it and recreating it
TextLibrary::get().removeTextFromLibrary(res.first->id);
const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use);
TextLibrary::Get().RemoveTextFromLibrary(res.first->id);
const_cast<TextDrawDescriptor&>(*res.first).Init(_font_in_use);
}
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextDrawDescriptor&>(*res.first)), false);
}
void TextManager::destroy() noexcept
void TextManager::Destroy() noexcept
{
MLX_PROFILE_FUNCTION();
_text_descriptors.clear();
m_text_descriptors.clear();
}
}