From d4bd8b62c5dd15dc6b6821fbc09faec6ce03b95f Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sun, 27 Oct 2024 01:14:07 +0200 Subject: [PATCH] working on texts and fonts --- runtime/Includes/Core/Application.inl | 8 +- runtime/Includes/Core/Graphics.h | 1 + runtime/Includes/Core/Graphics.inl | 32 ++++- runtime/Includes/Graphics/Drawable.h | 47 ++++++++ runtime/Includes/Graphics/Enums.h | 13 ++ runtime/Includes/Graphics/Font.inl | 5 +- runtime/Includes/Graphics/Scene.h | 19 ++- runtime/Includes/Graphics/Sprite.h | 29 ++--- runtime/Includes/Graphics/Text.h | 22 +++- runtime/Includes/PreCompiled.h | 2 + runtime/Includes/Renderer/Image.h | 2 +- runtime/Sources/Core/Application.cpp | 1 + runtime/Sources/Core/Bridge.cpp | 20 ++-- runtime/Sources/Core/Memory.cpp | 2 +- runtime/Sources/Core/Profiler.cpp | 2 +- runtime/Sources/Core/SDLManager.cpp | 14 +-- runtime/Sources/Graphics/Font.cpp | 6 +- runtime/Sources/Graphics/Scene.cpp | 112 +++++++++++++----- runtime/Sources/Graphics/Sprite.cpp | 4 +- runtime/Sources/Graphics/Text.cpp | 61 ++++++++++ runtime/Sources/Renderer/Buffer.cpp | 20 ++-- runtime/Sources/Renderer/Descriptor.cpp | 16 +-- runtime/Sources/Renderer/Image.cpp | 14 ++- runtime/Sources/Renderer/Memory.cpp | 24 ++-- .../Sources/Renderer/Pipelines/Graphics.cpp | 18 +-- runtime/Sources/Renderer/Pipelines/Shader.cpp | 8 +- runtime/Sources/Renderer/RenderCore.cpp | 10 +- .../Sources/Renderer/RenderPasses/2DPass.cpp | 27 ++--- runtime/Sources/Renderer/Renderer.cpp | 24 ++-- .../Sources/Renderer/Vulkan/VulkanLoader.cpp | 20 ++-- third_party/kvf.h | 12 +- 31 files changed, 411 insertions(+), 184 deletions(-) create mode 100644 runtime/Includes/Graphics/Drawable.h create mode 100644 runtime/Includes/Graphics/Enums.h create mode 100644 runtime/Sources/Graphics/Text.cpp diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index 51b3afe..5e96302 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -153,8 +153,14 @@ namespace mlx font = std::make_shared("default", dogica_ttf, scale); else font = std::make_shared(filepath, scale); - if(!m_font_registry.IsFontKnown(font)) + for(auto& gs : m_graphics) + { + if(gs) + gs->GetScene().BindFont(font); + } + if(m_font_registry.IsFontKnown(font)) return; + font->BuildFont(); m_font_registry.RegisterFont(font); } diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 8593523..e631349 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -32,6 +32,7 @@ namespace mlx [[nodiscard]] MLX_FORCEINLINE bool HasWindow() const noexcept { return m_has_window; } [[nodiscard]] MLX_FORCEINLINE Renderer& GetRenderer() { return m_renderer; } + [[nodiscard]] MLX_FORCEINLINE Scene& GetScene() { return *p_scene; } ~GraphicsSupport(); diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index f4386a0..2b44238 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -6,7 +6,7 @@ namespace mlx void GraphicsSupport::ResetRenderData() noexcept { MLX_PROFILE_FUNCTION(); - p_scene->ResetSprites(); + p_scene->ResetScene(); m_put_pixel_manager.ResetRenderData(); m_draw_layer = 0; m_pixelput_called = false; @@ -27,10 +27,30 @@ namespace mlx void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) { MLX_PROFILE_FUNCTION(); - (void)x; - (void)y; - (void)color; - (void)str; + Vec4f vec_color = { + static_cast((color & 0x000000FF)) / 255.f, + static_cast((color & 0x0000FF00) >> 8) / 255.f, + static_cast((color & 0x00FF0000) >> 16) / 255.f, + static_cast((color & 0xFF000000) >> 24) / 255.f + }; + + NonOwningPtr text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast(x), static_cast(y) }, vec_color); + if(!text) + { + Text& new_text = p_scene->CreateText(str); + new_text.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); + new_text.SetColor(std::move(vec_color)); + if(m_pixelput_called) + { + m_draw_layer++; + m_pixelput_called = false; + } + } + else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer)) + { + p_scene->BringToFront(text.Get()); + m_draw_layer++; + } } void GraphicsSupport::TexturePut(NonOwningPtr texture, int x, int y) @@ -50,7 +70,7 @@ namespace mlx } else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer)) { - p_scene->BringToFront(std::move(sprite)); + p_scene->BringToFront(sprite.Get()); m_draw_layer++; } } diff --git a/runtime/Includes/Graphics/Drawable.h b/runtime/Includes/Graphics/Drawable.h new file mode 100644 index 0000000..0fa4091 --- /dev/null +++ b/runtime/Includes/Graphics/Drawable.h @@ -0,0 +1,47 @@ +#ifndef __MLX_DRAWABLE__ +#define __MLX_DRAWABLE__ + +#include + +namespace mlx +{ + class Drawable + { + friend class Render2DPass; + + public: + inline Drawable(DrawableType type) : m_type(type) {} + + inline void SetColor(Vec4f color) noexcept { m_color = color; } + inline void SetPosition(Vec2f position) noexcept { m_position = position; } + + inline virtual void Update([[maybe_unused]] VkCommandBuffer cmd) {} + + [[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; } + [[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; } + [[nodiscard]] MLX_FORCEINLINE std::shared_ptr GetMesh() const { return p_mesh; } + [[nodiscard]] MLX_FORCEINLINE DrawableType GetType() const noexcept { return m_type; } + + inline virtual ~Drawable() { if(p_set) p_set->ReturnDescriptorSetToPool(); } + + protected: + [[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); } + [[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; } + + inline void UpdateDescriptorSet(std::shared_ptr set) + { + p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType()); + } + + virtual void Bind(std::size_t frame_index, VkCommandBuffer cmd) = 0; + + protected: + std::shared_ptr p_set; + std::shared_ptr p_mesh; + Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; + Vec2f m_position = Vec2f{ 0.0f, 0.0f }; + DrawableType m_type; + }; +} + +#endif diff --git a/runtime/Includes/Graphics/Enums.h b/runtime/Includes/Graphics/Enums.h new file mode 100644 index 0000000..8347ab0 --- /dev/null +++ b/runtime/Includes/Graphics/Enums.h @@ -0,0 +1,13 @@ +#ifndef __MLX_GRAPHICS_ENUMS__ +#define __MLX_GRAPHICS_ENUMS__ + +namespace mlx +{ + enum class DrawableType + { + Sprite, + Text + }; +} + +#endif diff --git a/runtime/Includes/Graphics/Font.inl b/runtime/Includes/Graphics/Font.inl index deef911..6a3482c 100644 --- a/runtime/Includes/Graphics/Font.inl +++ b/runtime/Includes/Graphics/Font.inl @@ -15,6 +15,9 @@ namespace mlx bool FontRegistry::IsFontKnown(std::shared_ptr font) { - return m_fonts_registry.find(font) != m_fonts_registry.end(); + return std::find_if(m_fonts_registry.begin(), m_fonts_registry.end(), [&font](std::shared_ptr rhs) + { + return font->GetName() == rhs->GetName() && font->GetScale() == rhs->GetScale(); + }) != m_fonts_registry.end(); } } diff --git a/runtime/Includes/Graphics/Scene.h b/runtime/Includes/Graphics/Scene.h index 05a0bc0..d7f2e2a 100644 --- a/runtime/Includes/Graphics/Scene.h +++ b/runtime/Includes/Graphics/Scene.h @@ -2,7 +2,10 @@ #define __MLX_SCENE__ #include +#include +#include #include +#include #include namespace mlx @@ -14,20 +17,28 @@ namespace mlx Sprite& CreateSprite(NonOwningPtr texture) noexcept; NonOwningPtr GetSpriteFromTextureAndPosition(NonOwningPtr texture, const Vec2f& position) const; - void BringToFront(NonOwningPtr sprite); void TryEraseSpriteFromTexture(NonOwningPtr texture); bool IsTextureAtGivenDrawLayer(NonOwningPtr texture, std::uint64_t draw_layer) const; - inline void ResetSprites() { m_sprites.clear(); } + Text& CreateText(const std::string& text) noexcept; + NonOwningPtr GetTextFromPositionAndColor(const std::string& text, const Vec2f& position, const Vec4f& color) const; + bool IsTextAtGivenDrawLayer(const std::string& text, std::uint64_t draw_layer) const; - [[nodiscard]] MLX_FORCEINLINE const std::vector>& GetSprites() const noexcept { return m_sprites; } + inline void BindFont(std::shared_ptr font) { Verify((bool)font, "invalid fond pointer"); p_bound_font = font; } + + void BringToFront(NonOwningPtr drawable); + + inline void ResetScene() { m_drawables.clear(); } + + [[nodiscard]] MLX_FORCEINLINE const std::vector>& GetDrawables() const noexcept { return m_drawables; } [[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; } ~Scene() = default; private: - std::vector> m_sprites; + std::vector> m_drawables; ViewerData m_viewer_data; + std::shared_ptr p_bound_font; }; } diff --git a/runtime/Includes/Graphics/Sprite.h b/runtime/Includes/Graphics/Sprite.h index 49bde83..b82f4fc 100644 --- a/runtime/Includes/Graphics/Sprite.h +++ b/runtime/Includes/Graphics/Sprite.h @@ -6,10 +6,11 @@ #include #include #include +#include namespace mlx { - class Sprite + class Sprite : public Drawable { friend class Render2DPass; @@ -17,26 +18,18 @@ namespace mlx Sprite(NonOwningPtr texture); Sprite(std::shared_ptr mesh, NonOwningPtr texture); - inline void SetColor(Vec4f color) noexcept { m_color = color; } - inline void SetPosition(Vec2f position) noexcept { m_position = position; } + MLX_FORCEINLINE void Update(VkCommandBuffer cmd) override + { + Verify((bool)p_texture, "a sprite has no texture attached (internal mlx issue, please report to the devs)"); + p_texture->Update(cmd); + } - [[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; } - [[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; } - [[nodiscard]] MLX_FORCEINLINE std::shared_ptr GetMesh() const { return p_mesh; } [[nodiscard]] MLX_FORCEINLINE NonOwningPtr GetTexture() const { return p_texture; } - inline ~Sprite() { if(p_set) p_set->ReturnDescriptorSetToPool(); } + inline ~Sprite() = default; private: - [[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); } - [[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; } - - inline void UpdateDescriptorSet(std::shared_ptr set) - { - p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType()); - } - - inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) + inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) override { if(!p_set) return; @@ -45,11 +38,7 @@ namespace mlx } private: - std::shared_ptr p_set; NonOwningPtr p_texture; - std::shared_ptr p_mesh; - Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; - Vec2f m_position = Vec2f{ 0.0f, 0.0f }; }; } diff --git a/runtime/Includes/Graphics/Text.h b/runtime/Includes/Graphics/Text.h index 6c17c60..457bc21 100644 --- a/runtime/Includes/Graphics/Text.h +++ b/runtime/Includes/Graphics/Text.h @@ -2,24 +2,38 @@ #define __MLX_TEXT__ #include +#include +#include namespace mlx { - class Text + class Text : public Drawable { + friend class Render2DPass; + public: Text(const std::string& text, std::shared_ptr font); + inline Text(const std::string& text, std::shared_ptr font, std::shared_ptr mesh) : Drawable(DrawableType::Text) { Init(text, font, mesh); } [[nodiscard]] inline const std::string& GetText() const { return m_text; } [[nodiscard]] inline std::shared_ptr GetFont() const { return p_font; } - [[nodiscard]] MLX_FORCEINLINE std::uint32_t GetColor() const noexcept { return m_color; } - ~Text(); + virtual ~Text() = default; + + private: + void Init(const std::string& text, std::shared_ptr font, std::shared_ptr mesh); + + inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) override + { + if(!p_set) + return; + p_set->SetImage(frame_index, 0, const_cast(p_font->GetTexture())); + p_set->Update(frame_index, cmd); + } private: std::shared_ptr p_font; std::string m_text; - std::uint32_t m_color; }; } diff --git a/runtime/Includes/PreCompiled.h b/runtime/Includes/PreCompiled.h index 11b2a78..0330c84 100644 --- a/runtime/Includes/PreCompiled.h +++ b/runtime/Includes/PreCompiled.h @@ -96,6 +96,8 @@ #include #include +constexpr const int RANGE = 1024; + using Handle = void*; #endif diff --git a/runtime/Includes/Renderer/Image.h b/runtime/Includes/Renderer/Image.h index b262224..c9fd54e 100644 --- a/runtime/Includes/Renderer/Image.h +++ b/runtime/Includes/Renderer/Image.h @@ -71,7 +71,7 @@ namespace mlx bool m_is_multisampled = false; }; - class Texture : public Image + class Texture: public Image { public: Texture() = default; diff --git a/runtime/Sources/Core/Application.cpp b/runtime/Sources/Core/Application.cpp index 9f894c2..fd2cd36 100644 --- a/runtime/Sources/Core/Application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -24,6 +24,7 @@ namespace mlx m_fps.Init(); p_render_core = std::make_unique(); + LoadFont("default", 6.0f); } void Application::Run() noexcept diff --git a/runtime/Sources/Core/Bridge.cpp b/runtime/Sources/Core/Bridge.cpp index 945d5a3..8905390 100644 --- a/runtime/Sources/Core/Bridge.cpp +++ b/runtime/Sources/Core/Bridge.cpp @@ -155,13 +155,13 @@ extern "C" MLX_CHECK_APPLICATION_POINTER(mlx); if (filename == nullptr) { - mlx::Error("PNG loader : filename is NULL"); + mlx::Error("PNG loader: filename is NULL"); return nullptr; } std::filesystem::path file(filename); if(file.extension() != ".png") { - mlx::Error("PNG loader : not a png file '%'", filename); + mlx::Error("PNG loader: not a png file '%'", filename); return nullptr; } return static_cast(mlx)->NewStbTexture(filename, width, height); @@ -172,13 +172,13 @@ extern "C" MLX_CHECK_APPLICATION_POINTER(mlx); if (filename == nullptr) { - mlx::Error("JPG loader : filename is NULL"); + mlx::Error("JPG loader: filename is NULL"); return nullptr; } std::filesystem::path file(filename); if(file.extension() != ".jpg" && file.extension() != ".jpeg") { - mlx::Error("JPG loader : not a jpg file '%'", filename); + mlx::Error("JPG loader: not a jpg file '%'", filename); return nullptr; } return static_cast(mlx)->NewStbTexture(filename, width, height); @@ -189,13 +189,13 @@ extern "C" MLX_CHECK_APPLICATION_POINTER(mlx); if (filename == nullptr) { - mlx::Error("BMP loader : filename is NULL"); + mlx::Error("BMP loader: filename is NULL"); return nullptr; } std::filesystem::path file(filename); if(file.extension() != ".bmp" && file.extension() != ".dib") { - mlx::Error("BMP loader : not a bmp file '%'", filename); + mlx::Error("BMP loader: not a bmp file '%'", filename); return nullptr; } return static_cast(mlx)->NewStbTexture(filename, width, height); @@ -230,13 +230,13 @@ extern "C" MLX_CHECK_APPLICATION_POINTER(mlx); if (filepath == nullptr) { - mlx::Error("Font loader : filepath is NULL"); + mlx::Error("Font loader: filepath is NULL"); return; } std::filesystem::path file(filepath); if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") { - mlx::Error("TTF loader : not a truetype font file '%'", filepath); + mlx::Error("TTF loader: not a truetype font file '%'", filepath); return; } if(std::strcmp(filepath, "default") == 0) @@ -250,13 +250,13 @@ extern "C" MLX_CHECK_APPLICATION_POINTER(mlx); if (filepath == nullptr) { - mlx::Error("Font loader : filepath is NULL"); + mlx::Error("Font loader: filepath is NULL"); return; } std::filesystem::path file(filepath); if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") { - mlx::Error("TTF loader : not a truetype font file '%'", filepath); + mlx::Error("TTF loader: not a truetype font file '%'", filepath); return; } static_cast(mlx)->LoadFont(file, scale); diff --git a/runtime/Sources/Core/Memory.cpp b/runtime/Sources/Core/Memory.cpp index f5f8ce6..197a458 100644 --- a/runtime/Sources/Core/Memory.cpp +++ b/runtime/Sources/Core/Memory.cpp @@ -43,7 +43,7 @@ namespace mlx auto it = std::find(s_blocks.begin(), s_blocks.end(), ptr); if(it == s_blocks.end()) { - Error("Memory Manager : trying to free a pointer not allocated by the memory manager"); + Error("Memory Manager: trying to free a pointer not allocated by the memory manager"); return; } std::free(*it); diff --git a/runtime/Sources/Core/Profiler.cpp b/runtime/Sources/Core/Profiler.cpp index a4bb9d6..b7c106b 100644 --- a/runtime/Sources/Core/Profiler.cpp +++ b/runtime/Sources/Core/Profiler.cpp @@ -16,7 +16,7 @@ namespace mlx if(m_output_stream.is_open()) WriteHeader(); else - Error("Profiler : cannot open runtime profile file"); + Error("Profiler: cannot open runtime profile file"); m_runtime_session_began = true; } diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 4d91a8c..d734cb7 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -43,21 +43,21 @@ namespace mlx #endif if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) - FatalError("SDL : unable to init all subsystems; %", SDL_GetError()); + FatalError("SDL: unable to init all subsystems; %", SDL_GetError()); DebugLog("SDL Manager initialized"); } Handle SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id) { Internal::WindowInfos* infos = new Internal::WindowInfos; - Verify(infos != nullptr, "SDL : window allocation failed"); + Verify(infos != nullptr, "SDL: window allocation failed"); if(title == "让我们在月光下做爱吧") infos->window = SDL_CreateWindow(title.c_str(), std::rand() % 512, std::rand() % 512, w, h, SDL_WINDOW_VULKAN | (hidden ? SDL_WINDOW_HIDDEN : SDL_WINDOW_SHOWN)); else infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | (hidden ? SDL_WINDOW_HIDDEN : SDL_WINDOW_SHOWN)); if(!infos->window) - FatalError("SDL : unable to open a new window; %", SDL_GetError()); + FatalError("SDL: unable to open a new window; %", SDL_GetError()); infos->icon = SDL_CreateRGBSurfaceFrom(static_cast(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask); SDL_SetWindowIcon(infos->window, infos->icon); @@ -70,7 +70,7 @@ namespace mlx void SDLManager::DestroyWindow(Handle window) noexcept { - Verify(m_windows_registry.find(window) != m_windows_registry.end(), "SDL : cannot destroy window; unknown window pointer"); + Verify(m_windows_registry.find(window) != m_windows_registry.end(), "SDL: cannot destroy window; unknown window pointer"); Internal::WindowInfos* infos = static_cast(window); if(infos->window != nullptr) @@ -86,7 +86,7 @@ namespace mlx { VkSurfaceKHR surface; if(!SDL_Vulkan_CreateSurface(static_cast(window)->window, instance, &surface)) - FatalError("SDL : could not create a Vulkan surface; %", SDL_GetError()); + FatalError("SDL: could not create a Vulkan surface; %", SDL_GetError()); return surface; } @@ -94,10 +94,10 @@ namespace mlx { std::uint32_t count; if(!SDL_Vulkan_GetInstanceExtensions(static_cast(window)->window, &count, nullptr)) - FatalError("SDL Manager : could not retrieve Vulkan instance extensions"); + FatalError("SDL Manager: could not retrieve Vulkan instance extensions"); std::vector extensions(count); if(!SDL_Vulkan_GetInstanceExtensions(static_cast(window)->window, &count, extensions.data())) - FatalError("SDL Manager : could not retrieve Vulkan instance extensions"); + FatalError("SDL Manager: could not retrieve Vulkan instance extensions"); extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); return extensions; } diff --git a/runtime/Sources/Graphics/Font.cpp b/runtime/Sources/Graphics/Font.cpp index 15d59db..d74afa9 100644 --- a/runtime/Sources/Graphics/Font.cpp +++ b/runtime/Sources/Graphics/Font.cpp @@ -10,8 +10,6 @@ namespace mlx { - constexpr const int RANGE = 1024; - void Font::BuildFont() { MLX_PROFILE_FUNCTION(); @@ -21,7 +19,7 @@ namespace mlx std::ifstream file(std::get(m_build_data), std::ios::binary); if(!file.is_open()) { - Error("Font : cannot open font file, %", m_name); + Error("Font: cannot open font file, %", m_name); return; } std::ifstream::pos_type file_size = std::filesystem::file_size(std::get(m_build_data)); @@ -56,6 +54,8 @@ namespace mlx #else m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, {}); #endif + + DebugLog("Font: loaded %", m_name); } void Font::Destroy() diff --git a/runtime/Sources/Graphics/Scene.cpp b/runtime/Sources/Graphics/Scene.cpp index 6335974..f7752e6 100644 --- a/runtime/Sources/Graphics/Scene.cpp +++ b/runtime/Sources/Graphics/Scene.cpp @@ -1,3 +1,4 @@ +#include "Graphics/Enums.h" #include #include #include @@ -10,64 +11,117 @@ namespace mlx MLX_PROFILE_FUNCTION(); Verify((bool)texture, "Scene: invalid texture (internal mlx issue, please report to devs)"); - #pragma omp parallel for - for(auto& sprite : m_sprites) + for(auto& drawable : m_drawables) { - if(texture->GetWidth() == sprite->GetTexture()->GetWidth() && texture->GetHeight() == sprite->GetTexture()->GetHeight()) + if(!drawable || drawable->GetType() != DrawableType::Sprite) + continue; + if(texture->GetWidth() == static_cast(drawable.get())->GetTexture()->GetWidth() && texture->GetHeight() == static_cast(drawable.get())->GetTexture()->GetHeight()) { - std::shared_ptr new_sprite = std::make_shared(sprite->GetMesh(), texture); - m_sprites.push_back(new_sprite); + std::shared_ptr new_sprite = std::make_shared(drawable->GetMesh(), texture); + m_drawables.push_back(new_sprite); return *new_sprite; } } std::shared_ptr sprite = std::make_shared(texture); - m_sprites.push_back(sprite); + m_drawables.push_back(sprite); return *sprite; } NonOwningPtr Scene::GetSpriteFromTextureAndPosition(NonOwningPtr texture, const Vec2f& position) const { MLX_PROFILE_FUNCTION(); - auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&texture, &position](std::shared_ptr sprite) + auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position](std::shared_ptr drawable) { - return sprite->GetTexture() == texture && sprite->GetPosition().x == position.x && sprite->GetPosition().y == position.y; + if(!drawable || drawable->GetType() != DrawableType::Sprite) + return false; + return static_cast(drawable.get())->GetTexture() == texture && drawable->GetPosition() == position; }); - return (it != m_sprites.end() ? it->get() : nullptr); + return static_cast(it != m_drawables.end() ? it->get() : nullptr); } - void Scene::BringToFront(NonOwningPtr sprite) + void Scene::TryEraseSpriteFromTexture(NonOwningPtr texture) { MLX_PROFILE_FUNCTION(); - auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&sprite](std::shared_ptr sprite_ptr) + auto it = m_drawables.begin(); + do { - return sprite_ptr.get() == sprite.Get(); - }); - if(it == m_sprites.end()) - return; - std::rotate(it, it + 1, m_sprites.end()); + it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture](std::shared_ptr drawable) + { + if(!drawable || drawable->GetType() != DrawableType::Sprite) + return false; + return static_cast(drawable.get())->GetTexture() == texture; + }); + if(it != m_drawables.end()) + m_drawables.erase(it); + } while(it != m_drawables.end()); } - void Scene::TryEraseSpriteFromTexture(NonOwningPtr texture) + bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr texture, std::uint64_t draw_layer) const { MLX_PROFILE_FUNCTION(); - auto it = m_sprites.begin(); - do + if(draw_layer >= m_drawables.size()) + return false; + if(!m_drawables[draw_layer] || m_drawables[draw_layer]->GetType() != DrawableType::Sprite) + return false; + return static_cast(m_drawables[draw_layer].get())->GetTexture() == texture; + } + + Text& Scene::CreateText(const std::string& text) noexcept + { + MLX_PROFILE_FUNCTION(); + + Assert((bool)p_bound_font, "no font bound"); + + for(auto& drawable : m_drawables) { - it = std::find_if(m_sprites.begin(), m_sprites.end(), [&texture](std::shared_ptr sprite) + if(!drawable || drawable->GetType() != DrawableType::Text) + continue; + if(text == static_cast(drawable.get())->GetText() && p_bound_font == static_cast(drawable.get())->GetFont()) { - return sprite->GetTexture() == texture; - }); - if(it != m_sprites.end()) - m_sprites.erase(it); - } while(it != m_sprites.end()); + std::shared_ptr new_text = std::make_shared(text, p_bound_font, drawable->GetMesh()); + m_drawables.push_back(new_text); + return *new_text; + } + } + + std::shared_ptr new_text = std::make_shared(text, p_bound_font); + m_drawables.push_back(new_text); + return *new_text; } - - bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr texture, std::uint64_t draw_layer) const + + NonOwningPtr Scene::GetTextFromPositionAndColor(const std::string& text, const Vec2f& position, const Vec4f& color) const + { + MLX_PROFILE_FUNCTION(); + auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&text, &position, &color](std::shared_ptr drawable) + { + if(!drawable || drawable->GetType() != DrawableType::Text) + return false; + return static_cast(drawable.get())->GetText() == text && drawable->GetPosition() == position && drawable->GetColor() == color; + }); + return static_cast(it != m_drawables.end() ? it->get() : nullptr); + } + + bool Scene::IsTextAtGivenDrawLayer(const std::string& text, std::uint64_t draw_layer) const { MLX_PROFILE_FUNCTION(); - if(draw_layer >= m_sprites.size()) + if(draw_layer >= m_drawables.size()) + return false; + if(!m_drawables[draw_layer] || m_drawables[draw_layer]->GetType() != DrawableType::Text) return false; - return m_sprites[draw_layer]->GetTexture() == texture; + Text* ptr = static_cast(m_drawables[draw_layer].get()); + return ptr->GetText() == text && ptr->GetFont() == p_bound_font; + } + + void Scene::BringToFront(NonOwningPtr drawable) + { + MLX_PROFILE_FUNCTION(); + auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&drawable](std::shared_ptr drawable_ptr) + { + return drawable_ptr.get() == drawable.Get(); + }); + if(it == m_drawables.end()) + return; + std::rotate(it, it + 1, m_drawables.end()); } } diff --git a/runtime/Sources/Graphics/Sprite.cpp b/runtime/Sources/Graphics/Sprite.cpp index caa0c64..3fd9ae0 100644 --- a/runtime/Sources/Graphics/Sprite.cpp +++ b/runtime/Sources/Graphics/Sprite.cpp @@ -37,7 +37,7 @@ namespace mlx return mesh; } - Sprite::Sprite(NonOwningPtr texture) + Sprite::Sprite(NonOwningPtr texture) : Drawable(DrawableType::Sprite) { MLX_PROFILE_FUNCTION(); Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)"); @@ -45,7 +45,7 @@ namespace mlx p_texture = texture; } - Sprite::Sprite(std::shared_ptr mesh, NonOwningPtr texture) + Sprite::Sprite(std::shared_ptr mesh, NonOwningPtr texture) : Drawable(DrawableType::Sprite) { MLX_PROFILE_FUNCTION(); Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)"); diff --git a/runtime/Sources/Graphics/Text.cpp b/runtime/Sources/Graphics/Text.cpp new file mode 100644 index 0000000..475541c --- /dev/null +++ b/runtime/Sources/Graphics/Text.cpp @@ -0,0 +1,61 @@ +#include + +#include + +#define STB_RECT_PACK_IMPLEMENTATION +#include + +namespace mlx +{ + Text::Text(const std::string& text, std::shared_ptr font) : Drawable(DrawableType::Text) + { + MLX_PROFILE_FUNCTION(); + + Assert(font != nullptr, "invalid font"); + + std::vector vertex_data; + std::vector index_data; + + float stb_x = 0.0f; + float stb_y = 0.0f; + + for(char c : text) + { + if(c < 32) + continue; + + stbtt_aligned_quad q; + stbtt_GetPackedQuad(font->GetCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1); + + std::size_t index = vertex_data.size(); + + vertex_data.emplace_back(Vec4f{ q.x0, q.y0, 0.0f, 0.0f }, Vec2f{ q.s0, q.t0 }); + vertex_data.emplace_back(Vec4f{ q.x1, q.y0, 0.0f, 0.0f }, Vec2f{ q.s1, q.t0 }); + vertex_data.emplace_back(Vec4f{ q.x1, q.y1, 0.0f, 0.0f }, Vec2f{ q.s1, q.t1 }); + vertex_data.emplace_back(Vec4f{ q.x0, q.y1, 0.0f, 0.0f }, Vec2f{ q.s0, q.t1 }); + + 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 mesh = std::make_shared(); + mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) }); + Init(text, font, mesh); + } + + void Text::Init(const std::string& text, std::shared_ptr font, std::shared_ptr mesh) + { + MLX_PROFILE_FUNCTION(); + + Assert(font != nullptr, "invalid font"); + Assert(mesh != nullptr, "invalid mesh"); + + p_mesh = mesh; + p_font = font; + m_text = text; + } +} diff --git a/runtime/Sources/Renderer/Buffer.cpp b/runtime/Sources/Renderer/Buffer.cpp index 3e64d99..a0dd737 100644 --- a/runtime/Sources/Renderer/Buffer.cpp +++ b/runtime/Sources/Renderer/Buffer.cpp @@ -15,7 +15,7 @@ namespace mlx { if(data.Empty()) { - Warning("Vulkan : trying to create constant buffer without data (constant buffers cannot be modified after creation)"); + Warning("Vulkan: trying to create constant buffer without data (constant buffers cannot be modified after creation)"); return; } m_usage = usage | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; @@ -70,12 +70,12 @@ namespace mlx MLX_PROFILE_FUNCTION(); if(!(m_usage & VK_BUFFER_USAGE_TRANSFER_DST_BIT)) { - Error("Vulkan : buffer cannot be the destination of a copy because it does not have the correct usage flag"); + Error("Vulkan: buffer cannot be the destination of a copy because it does not have the correct usage flag"); return false; } if(!(buffer.m_usage & VK_BUFFER_USAGE_TRANSFER_SRC_BIT)) { - Error("Vulkan : buffer cannot be the source of a copy because it does not have the correct usage flag"); + Error("Vulkan: buffer cannot be the source of a copy because it does not have the correct usage flag"); return false; } @@ -108,7 +108,7 @@ namespace mlx if(new_buffer.CopyFrom(*this)) Swap(new_buffer); new_buffer.Destroy(); - DebugLog("Vulkan : pushed buffer to GPU memory"); + DebugLog("Vulkan: pushed buffer to GPU memory"); } void GPUBuffer::Destroy() noexcept @@ -141,12 +141,12 @@ namespace mlx MLX_PROFILE_FUNCTION(); if(data.GetSize() > m_size) { - Error("Vulkan : trying to store to much data in a vertex buffer (% bytes in % bytes)", data.GetSize(), m_size); + Error("Vulkan: trying to store to much data in a vertex buffer (% bytes in % bytes)", data.GetSize(), m_size); return; } if(data.Empty()) { - Warning("Vulkan : cannot set empty data in a vertex buffer"); + Warning("Vulkan: cannot set empty data in a vertex buffer"); return; } GPUBuffer staging; @@ -164,12 +164,12 @@ namespace mlx MLX_PROFILE_FUNCTION(); if(data.GetSize() > m_size) { - Error("Vulkan : trying to store to much data in an index buffer (% bytes in % bytes)", data.GetSize(), m_size); + Error("Vulkan: trying to store to much data in an index buffer (% bytes in % bytes)", data.GetSize(), m_size); return; } if(data.Empty()) { - Warning("Vulkan : cannot set empty data in an index buffer"); + Warning("Vulkan: cannot set empty data in an index buffer"); return; } GPUBuffer staging; @@ -194,7 +194,7 @@ namespace mlx #endif m_maps[i] = m_buffers[i].GetMap(); if(m_maps[i] == nullptr) - FatalError("Vulkan : unable to map a uniform buffer"); + FatalError("Vulkan: unable to map a uniform buffer"); } } @@ -203,7 +203,7 @@ namespace mlx MLX_PROFILE_FUNCTION(); if(data.GetSize() != m_buffers[frame_index].GetSize()) { - Error("Vulkan : invalid data size to update to a uniform buffer, % != %", data.GetSize(), m_buffers[frame_index].GetSize()); + Error("Vulkan: invalid data size to update to a uniform buffer, % != %", data.GetSize(), m_buffers[frame_index].GetSize()); return; } if(m_maps[frame_index] != nullptr) diff --git a/runtime/Sources/Renderer/Descriptor.cpp b/runtime/Sources/Renderer/Descriptor.cpp index 4349fd9..b0240c1 100644 --- a/runtime/Sources/Renderer/Descriptor.cpp +++ b/runtime/Sources/Renderer/Descriptor.cpp @@ -19,7 +19,7 @@ namespace mlx if(image.GetType() == ImageType::Color) image.TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, cmd); else - Error("Vulkan : cannot transition descriptor image layout, unkown image type"); + Error("Vulkan: cannot transition descriptor image layout, unkown image type"); } void DescriptorPool::Init() noexcept @@ -128,7 +128,7 @@ namespace mlx }); if(it == m_used_sets.end()) { - Error("Vulkan : cannot return descriptor set to pool, invalid pool"); + Error("Vulkan: cannot return descriptor set to pool, invalid pool"); return; } m_used_sets.erase(it); @@ -182,12 +182,12 @@ namespace mlx }); if(it == m_descriptors.end()) { - Warning("Vulkan : cannot update descriptor set image; invalid binding"); + Warning("Vulkan: cannot update descriptor set image; invalid binding"); return; } if(it->type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { - Error("Vulkan : trying to bind an image to the wrong descriptor"); + Error("Vulkan: trying to bind an image to the wrong descriptor"); return; } it->image_ptr = ℑ @@ -203,12 +203,12 @@ namespace mlx }); if(it == m_descriptors.end()) { - Warning("Vulkan : cannot update descriptor set buffer; invalid binding"); + Warning("Vulkan: cannot update descriptor set buffer; invalid binding"); return; } if(it->type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) { - Error("Vulkan : trying to bind a buffer to the wrong descriptor"); + Error("Vulkan: trying to bind a buffer to the wrong descriptor"); return; } it->storage_buffer_ptr = &buffer; @@ -224,12 +224,12 @@ namespace mlx }); if(it == m_descriptors.end()) { - Warning("Vulkan : cannot update descriptor set buffer; invalid binding"); + Warning("Vulkan: cannot update descriptor set buffer; invalid binding"); return; } if(it->type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) { - Error("Vulkan : trying to bind a buffer to the wrong descriptor"); + Error("Vulkan: trying to bind a buffer to the wrong descriptor"); return; } it->uniform_buffer_ptr = &buffer; diff --git a/runtime/Sources/Renderer/Image.cpp b/runtime/Sources/Renderer/Image.cpp index ab4c58b..53ea351 100644 --- a/runtime/Sources/Renderer/Image.cpp +++ b/runtime/Sources/Renderer/Image.cpp @@ -14,6 +14,12 @@ #include #endif +#ifdef IMAGE_OPTIMIZED + #define TILING VK_IMAGE_TILING_OPTIMAL +#else + #define TILING VK_IMAGE_TILING_LINEAR +#endif + namespace mlx { void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name) @@ -153,7 +159,7 @@ namespace mlx void Texture::Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name) { MLX_PROFILE_FUNCTION(); - Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled, std::move(debug_name)); + Image::Init(ImageType::Color, width, height, format, TILING, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled, std::move(debug_name)); Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); Image::CreateSampler(); if(pixels) @@ -228,7 +234,7 @@ namespace mlx if(m_staging_buffer.has_value()) return; #ifdef DEBUG - DebugLog("Texture : enabling CPU mapping for '%'", m_debug_name); + DebugLog("Texture: enabling CPU mapping for '%'", m_debug_name); #endif m_staging_buffer.emplace(); std::size_t size = m_width * m_height * kvfFormatSize(m_format); @@ -263,12 +269,12 @@ namespace mlx if(!std::filesystem::exists(file)) { - Error("Image : file not found %", file); + Error("Image: file not found %", file); return nullptr; } if(stbi_is_hdr(filename.c_str())) { - Error("Texture : unsupported image format % (HDR image)", file); + Error("Texture: unsupported image format % (HDR image)", file); return nullptr; } int dummy_w; diff --git a/runtime/Sources/Renderer/Memory.cpp b/runtime/Sources/Renderer/Memory.cpp index 92dcb55..b4b041f 100644 --- a/runtime/Sources/Renderer/Memory.cpp +++ b/runtime/Sources/Renderer/Memory.cpp @@ -54,7 +54,7 @@ namespace mlx allocator_create_info.pVulkanFunctions = &vma_vulkan_func; kvfCheckVk(vmaCreateAllocator(&allocator_create_info, &m_allocator)); - DebugLog("Graphics allocator : created new allocator"); + DebugLog("Graphics Allocator: created new allocator"); } VmaAllocation GPUAllocator::CreateBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name) noexcept @@ -74,7 +74,7 @@ namespace mlx #endif vmaSetAllocationName(m_allocator, allocation, name); } - DebugLog("Graphics Allocator : created new buffer '%'", name); + DebugLog("Graphics Allocator: created new buffer '%'", name); m_active_buffers_allocations++; return allocation; } @@ -85,9 +85,9 @@ namespace mlx RenderCore::Get().WaitDeviceIdle(); vmaDestroyBuffer(m_allocator, buffer, allocation); if(name != nullptr) - DebugLog("Graphics Allocator : destroyed buffer '%'", name); + DebugLog("Graphics Allocator: destroyed buffer '%'", name); else - DebugLog("Graphics Allocator : destroyed buffer"); + DebugLog("Graphics Allocator: destroyed buffer"); m_active_buffers_allocations--; } @@ -108,7 +108,7 @@ namespace mlx #endif vmaSetAllocationName(m_allocator, allocation, name); } - DebugLog("Graphics Allocator : created new image '%'", name); + DebugLog("Graphics Allocator: created new image '%'", name); m_active_images_allocations++; return allocation; } @@ -119,9 +119,9 @@ namespace mlx RenderCore::Get().WaitDeviceIdle(); vmaDestroyImage(m_allocator, image, allocation); if(name != nullptr) - DebugLog("Graphics Allocator : destroyed image '%'", name); + DebugLog("Graphics Allocator: destroyed image '%'", name); else - DebugLog("Graphics Allocator : destroyed image"); + DebugLog("Graphics Allocator: destroyed image"); m_active_images_allocations--; } @@ -145,7 +145,7 @@ namespace mlx std::ofstream file(name); if(!file.is_open()) { - Error("Graphics allocator : unable to dump memory to a json file"); + Error("Graphics Allocator: unable to dump memory to a json file"); return; } char* str = nullptr; @@ -166,14 +166,14 @@ namespace mlx { MLX_PROFILE_FUNCTION(); if(m_active_images_allocations != 0) - Error("Graphics allocator : some user-dependant allocations were not freed before destroying the display (% active allocations). You may have not destroyed all the MLX resources you've created", m_active_images_allocations); + Error("Graphics Allocator: some user-dependant allocations were not freed before destroying the display (% active allocations). You may have not destroyed all the MLX resources you've created", m_active_images_allocations); else if(m_active_buffers_allocations != 0) - Error("Graphics allocator : some MLX-dependant allocations were not freed before destroying the display (% active allocations). This is an error in the MLX, please report this should not happen", m_active_buffers_allocations); + Error("Graphics Allocator: some MLX-dependant allocations were not freed before destroying the display (% active allocations). This is an error in the MLX, please report this should not happen", m_active_buffers_allocations); if(m_active_images_allocations < 0 || m_active_buffers_allocations < 0) - Warning("Graphics allocator : the impossible happened, the MLX has freed more allocations than it has made (wtf)"); + Warning("Graphics Allocator: the impossible happened, the MLX has freed more allocations than it has made (wtf)"); vmaDestroyAllocator(m_allocator); m_active_buffers_allocations = 0; m_active_images_allocations = 0; - DebugLog("Vulkan : destroyed a graphics allocator"); + DebugLog("Vulkan: destroyed a graphics allocator"); } } diff --git a/runtime/Sources/Renderer/Pipelines/Graphics.cpp b/runtime/Sources/Renderer/Pipelines/Graphics.cpp index 401a877..7d9b902 100644 --- a/runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -11,7 +11,7 @@ namespace mlx { MLX_PROFILE_FUNCTION(); if(!descriptor.vertex_shader || !descriptor.fragment_shader) - FatalError("Vulkan : invalid shaders"); + FatalError("Vulkan: invalid shaders"); m_attachments = descriptor.color_attachments; p_vertex_shader = descriptor.vertex_shader; @@ -51,7 +51,7 @@ namespace mlx } m_pipeline = kvfCreateGraphicsPipeline(RenderCore::Get().GetDevice(), m_pipeline_layout, builder, m_renderpass); - DebugLog("Vulkan : graphics pipeline created"); + DebugLog("Vulkan: graphics pipeline created"); kvfDestroyGPipelineBuilder(builder); #ifdef MLX_HAS_DEBUG_UTILS_FUNCTIONS @@ -132,18 +132,18 @@ namespace mlx for(auto& fb : m_framebuffers) { kvfDestroyFramebuffer(RenderCore::Get().GetDevice(), fb); - DebugLog("Vulkan : framebuffer destroyed"); + DebugLog("Vulkan: framebuffer destroyed"); } m_framebuffers.clear(); kvfDestroyPipelineLayout(RenderCore::Get().GetDevice(), m_pipeline_layout); m_pipeline_layout = VK_NULL_HANDLE; - DebugLog("Vulkan : graphics pipeline layout destroyed"); + DebugLog("Vulkan: graphics pipeline layout destroyed"); kvfDestroyRenderPass(RenderCore::Get().GetDevice(), m_renderpass); m_renderpass = VK_NULL_HANDLE; - DebugLog("Vulkan : renderpass destroyed"); + DebugLog("Vulkan: renderpass destroyed"); kvfDestroyPipeline(RenderCore::Get().GetDevice(), m_pipeline); m_pipeline = VK_NULL_HANDLE; - DebugLog("Vulkan : graphics pipeline destroyed"); + DebugLog("Vulkan: graphics pipeline destroyed"); } void GraphicPipeline::CreateFramebuffers(const std::vector>& render_targets, bool clear_attachments) @@ -167,7 +167,7 @@ namespace mlx m_renderpass = kvfCreateRenderPass(RenderCore::Get().GetDevice(), attachments.data(), attachments.size(), GetPipelineBindPoint()); m_clears.clear(); m_clears.resize(attachments.size()); - DebugLog("Vulkan : renderpass created"); + DebugLog("Vulkan: renderpass created"); if(p_renderer) { @@ -175,14 +175,14 @@ namespace mlx { attachment_views[0] = image.GetImageView(); m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image.GetWidth(), .height = image.GetHeight() })); - DebugLog("Vulkan : framebuffer created"); + DebugLog("Vulkan: framebuffer created"); } } #pragma omp parallel for for(NonOwningPtr image : render_targets) { m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image->GetWidth(), .height = image->GetHeight() })); - DebugLog("Vulkan : framebuffer created"); + DebugLog("Vulkan: framebuffer created"); } } diff --git a/runtime/Sources/Renderer/Pipelines/Shader.cpp b/runtime/Sources/Renderer/Pipelines/Shader.cpp index 26c7d91..5a92e35 100644 --- a/runtime/Sources/Renderer/Pipelines/Shader.cpp +++ b/runtime/Sources/Renderer/Pipelines/Shader.cpp @@ -15,7 +15,7 @@ namespace mlx default : FatalError("wtf"); break; } m_module = kvfCreateShaderModule(RenderCore::Get().GetDevice(), reinterpret_cast(m_bytecode.data()), m_bytecode.size() / 4); - DebugLog("Vulkan : shader module created"); + DebugLog("Vulkan: shader module created"); GeneratePipelineLayout(m_layout); } @@ -35,7 +35,7 @@ namespace mlx bindings[i].stageFlags = m_stage; } m_set_layouts.emplace_back(kvfCreateDescriptorSetLayout(RenderCore::Get().GetDevice(), bindings.data(), bindings.size())); - DebugLog("Vulkan : descriptor set layout created"); + DebugLog("Vulkan: descriptor set layout created"); m_pipeline_layout_part.set_layouts.push_back(m_set_layouts.back()); } @@ -56,11 +56,11 @@ namespace mlx { MLX_PROFILE_FUNCTION(); kvfDestroyShaderModule(RenderCore::Get().GetDevice(), m_module); - DebugLog("Vulkan : shader module destroyed"); + DebugLog("Vulkan: shader module destroyed"); for(auto& layout : m_set_layouts) { kvfDestroyDescriptorSetLayout(RenderCore::Get().GetDevice(), layout); - DebugLog("Vulkan : descriptor set layout destroyed"); + DebugLog("Vulkan: descriptor set layout destroyed"); } } } diff --git a/runtime/Sources/Renderer/RenderCore.cpp b/runtime/Sources/Renderer/RenderCore.cpp index 58d3d54..402f893 100644 --- a/runtime/Sources/Renderer/RenderCore.cpp +++ b/runtime/Sources/Renderer/RenderCore.cpp @@ -69,7 +69,7 @@ namespace mlx #endif m_instance = kvfCreateInstance(instance_extensions.data(), instance_extensions.size()); - DebugLog("Vulkan : instance created"); + DebugLog("Vulkan: instance created"); loader->LoadInstance(m_instance); LoadKVFInstanceVulkanFunctionPointers(); @@ -81,13 +81,13 @@ namespace mlx // just for style VkPhysicalDeviceProperties props; vkGetPhysicalDeviceProperties(m_physical_device, &props); - DebugLog("Vulkan : physical device picked '%'", props.deviceName); + DebugLog("Vulkan: physical device picked '%'", props.deviceName); const char* device_extensions[] = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; VkPhysicalDeviceFeatures features{}; vkGetPhysicalDeviceFeatures(m_physical_device, &features); m_device = kvfCreateDevice(m_physical_device, device_extensions, sizeof(device_extensions) / sizeof(device_extensions[0]), &features); - DebugLog("Vulkan : logical device created"); + DebugLog("Vulkan: logical device created"); loader->LoadDevice(m_device); LoadKVFDeviceVulkanFunctionPointers(); @@ -201,9 +201,9 @@ namespace mlx m_descriptor_pool_manager.Destroy(); m_allocator.Destroy(); kvfDestroyDevice(m_device); - DebugLog("Vulkan : logical device destroyed"); + DebugLog("Vulkan: logical device destroyed"); kvfDestroyInstance(m_instance); - DebugLog("Vulkan : instance destroyed"); + DebugLog("Vulkan: instance destroyed"); loader.reset(); s_instance = nullptr; diff --git a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp index 1cd2e25..827a056 100644 --- a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -92,32 +92,31 @@ namespace mlx VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); - const auto& sprites = scene.GetSprites(); + const auto& drawables = scene.GetDrawables(); - for(auto sprite : sprites) + for(auto drawable : drawables) { // Check every textures and update modified ones to GPU before starting the render pass - if(!sprite->IsSetInit()) - sprite->UpdateDescriptorSet(p_texture_set); - Verify((bool)sprite->GetTexture(), "a sprite has no texture attached (internal mlx issue, please report to the devs)"); - sprite->GetTexture()->Update(cmd); + if(!drawable->IsSetInit()) + drawable->UpdateDescriptorSet(p_texture_set); + drawable->Update(cmd); } m_pipeline.BindPipeline(cmd, 0, {}); - for(auto sprite : sprites) + for(auto drawable : drawables) { - SpriteData sprite_data; - sprite_data.position = Vec4f{ sprite->GetPosition(), 0.0f, 1.0f }; - sprite_data.color = sprite->GetColor(); + SpriteData drawable_data; + drawable_data.position = Vec4f{ drawable->GetPosition(), 0.0f, 1.0f }; + drawable_data.color = drawable->GetColor(); - sprite->Bind(frame_index, cmd); + drawable->Bind(frame_index, cmd); - std::array sets = { p_viewer_data_set->GetSet(frame_index), sprite->GetSet(frame_index) }; + std::array sets = { p_viewer_data_set->GetSet(frame_index), drawable->GetSet(frame_index) }; - RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data); + RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &drawable_data); RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); - sprite->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); + drawable->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); } m_pipeline.EndPipeline(cmd); } diff --git a/runtime/Sources/Renderer/Renderer.cpp b/runtime/Sources/Renderer/Renderer.cpp index 31b12a9..823db60 100644 --- a/runtime/Sources/Renderer/Renderer.cpp +++ b/runtime/Sources/Renderer/Renderer.cpp @@ -33,20 +33,20 @@ namespace mlx p_window = window; m_surface = p_window->CreateVulkanSurface(RenderCore::Get().GetInstance()); - DebugLog("Vulkan : surface created"); + DebugLog("Vulkan: surface created"); CreateSwapchain(); for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { m_image_available_semaphores[i] = kvfCreateSemaphore(RenderCore::Get().GetDevice()); - DebugLog("Vulkan : image available semaphore created"); + DebugLog("Vulkan: image available semaphore created"); m_render_finished_semaphores[i] = kvfCreateSemaphore(RenderCore::Get().GetDevice()); - DebugLog("Vulkan : render finished semaphore created"); + DebugLog("Vulkan: render finished semaphore created"); m_cmd_buffers[i] = kvfCreateCommandBuffer(RenderCore::Get().GetDevice()); - DebugLog("Vulkan : command buffer created"); + DebugLog("Vulkan: command buffer created"); m_cmd_fences[i] = kvfCreateFence(RenderCore::Get().GetDevice()); - DebugLog("Vulkan : fence created"); + DebugLog("Vulkan: fence created"); } } @@ -63,7 +63,7 @@ namespace mlx //return false; } else if(result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) - FatalError("Vulkan error : failed to acquire swapchain image, %", kvfVerbaliseVkResult(result)); + FatalError("Vulkan error: failed to acquire swapchain image, %", kvfVerbaliseVkResult(result)); RenderCore::Get().vkResetCommandBuffer(m_cmd_buffers[m_current_frame_index], 0); kvfBeginCommandBuffer(m_cmd_buffers[m_current_frame_index], 0); @@ -110,7 +110,7 @@ namespace mlx m_swapchain_images[i].TransitionLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); m_swapchain_images[i].CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); } - DebugLog("Vulkan : swapchain created"); + DebugLog("Vulkan: swapchain created"); } void Renderer::DestroySwapchain() @@ -120,7 +120,7 @@ namespace mlx for(Image& img : m_swapchain_images) img.DestroyImageView(); kvfDestroySwapchainKHR(RenderCore::Get().GetDevice(), m_swapchain); - DebugLog("Vulkan : swapchain destroyed"); + DebugLog("Vulkan: swapchain destroyed"); } void Renderer::Destroy() noexcept @@ -131,16 +131,16 @@ namespace mlx for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { kvfDestroySemaphore(RenderCore::Get().GetDevice(), m_image_available_semaphores[i]); - DebugLog("Vulkan : image available semaphore destroyed"); + DebugLog("Vulkan: image available semaphore destroyed"); kvfDestroySemaphore(RenderCore::Get().GetDevice(), m_render_finished_semaphores[i]); - DebugLog("Vulkan : render finished semaphore destroyed"); + DebugLog("Vulkan: render finished semaphore destroyed"); kvfDestroyFence(RenderCore::Get().GetDevice(), m_cmd_fences[i]); - DebugLog("Vulkan : fence destroyed"); + DebugLog("Vulkan: fence destroyed"); } DestroySwapchain(); RenderCore::Get().vkDestroySurfaceKHR(RenderCore::Get().GetInstance(), m_surface, nullptr); - DebugLog("Vulkan : surface destroyed"); + DebugLog("Vulkan: surface destroyed"); m_surface = VK_NULL_HANDLE; } } diff --git a/runtime/Sources/Renderer/Vulkan/VulkanLoader.cpp b/runtime/Sources/Renderer/Vulkan/VulkanLoader.cpp index cba9310..4f63d54 100644 --- a/runtime/Sources/Renderer/Vulkan/VulkanLoader.cpp +++ b/runtime/Sources/Renderer/Vulkan/VulkanLoader.cpp @@ -30,8 +30,8 @@ namespace mlx { PFN_vkVoidFunction function = RenderCore::Get().vkGetInstanceProcAddr(static_cast(context), name); if(!function) - FatalError("Vulkan loader : could not load '%'", name); - //DebugLog("Vulkan loader : loaded %", name); + FatalError("Vulkan Loader: could not load '%'", name); + //DebugLog("Vulkan Loader: loaded %", name); return function; } @@ -39,8 +39,8 @@ namespace mlx { PFN_vkVoidFunction function = RenderCore::Get().vkGetDeviceProcAddr(static_cast(context), name); if(!function) - FatalError("Vulkan loader : could not load '%'", name); - //DebugLog("Vulkan loader : loaded %", name); + FatalError("Vulkan Loader: could not load '%'", name); + //DebugLog("Vulkan Loader: loaded %", name); return function; } @@ -95,13 +95,13 @@ namespace mlx RESTORE_GCC_PEDANTIC_WARNINGS if(RenderCore::Get().vkGetInstanceProcAddr) { - DebugLog("Vulkan loader : libvulkan loaded using '%'", libname); + DebugLog("Vulkan Loader: libvulkan loaded using '%'", libname); break; } } } if(!p_module || !RenderCore::Get().vkGetInstanceProcAddr) - FatalError("Vulkan loader : failed to load libvulkan"); + FatalError("Vulkan Loader: failed to load libvulkan"); LoadGlobalFunctions(nullptr, Internal::vkGetInstanceProcAddrStub); } @@ -120,7 +120,7 @@ namespace mlx #define MLX_VULKAN_GLOBAL_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast(load(context, #fn)); #include #undef MLX_VULKAN_GLOBAL_FUNCTION - DebugLog("Vulkan loader : global functions loaded"); + DebugLog("Vulkan Loader: global functions loaded"); } void VulkanLoader::LoadInstanceFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*)) noexcept @@ -128,7 +128,7 @@ namespace mlx #define MLX_VULKAN_INSTANCE_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast(load(context, #fn)); #include #undef MLX_VULKAN_INSTANCE_FUNCTION - DebugLog("Vulkan loader : instance functions loaded"); + DebugLog("Vulkan Loader: instance functions loaded"); } void VulkanLoader::LoadDeviceFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*)) noexcept @@ -136,7 +136,7 @@ namespace mlx #define MLX_VULKAN_DEVICE_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast(load(context, #fn)); #include #undef MLX_VULKAN_DEVICE_FUNCTION - DebugLog("Vulkan loader : device functions loaded"); + DebugLog("Vulkan Loader: device functions loaded"); } VulkanLoader::~VulkanLoader() @@ -147,6 +147,6 @@ namespace mlx dlclose(p_module); #endif p_module = nullptr; - DebugLog("Vulkan loader : libvulkan unloaded"); + DebugLog("Vulkan Loader: libvulkan unloaded"); } } diff --git a/third_party/kvf.h b/third_party/kvf.h index 6bc5e75..7e4602f 100755 --- a/third_party/kvf.h +++ b/third_party/kvf.h @@ -894,7 +894,7 @@ VkPipelineStageFlags kvfLayoutToAccessMask(VkImageLayout layout, bool is_destina { case VK_IMAGE_LAYOUT_UNDEFINED: if(is_destination) - KVF_ASSERT(false && "Vulkan : the new layout used in a transition must not be VK_IMAGE_LAYOUT_UNDEFINED"); + KVF_ASSERT(false && "Vulkan: the new layout used in a transition must not be VK_IMAGE_LAYOUT_UNDEFINED"); break; case VK_IMAGE_LAYOUT_GENERAL: access_mask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; break; case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: access_mask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; break; @@ -909,13 +909,13 @@ VkPipelineStageFlags kvfLayoutToAccessMask(VkImageLayout layout, bool is_destina if(!is_destination) access_mask = VK_ACCESS_HOST_WRITE_BIT; else - KVF_ASSERT(false && "Vulkan : the new layout used in a transition must not be VK_IMAGE_LAYOUT_PREINITIALIZED"); + KVF_ASSERT(false && "Vulkan: the new layout used in a transition must not be VK_IMAGE_LAYOUT_PREINITIALIZED"); break; case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL: access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; break; case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL: access_mask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; break; case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: access_mask = VK_ACCESS_MEMORY_READ_BIT; break; - default: KVF_ASSERT(false && "Vulkan : unexpected image layout"); break; + default: KVF_ASSERT(false && "Vulkan: unexpected image layout"); break; } return access_mask; @@ -929,7 +929,7 @@ VkPipelineStageFlags kvfAccessFlagsToPipelineStage(VkAccessFlags access_flags, V { VkAccessFlagBits _access_flag = (VkAccessFlagBits)(access_flags & (~(access_flags - 1))); if(_access_flag == 0 || (_access_flag & (_access_flag - 1)) != 0) - KVF_ASSERT(false && "Vulkan : an error has been caught during access flag to pipeline stage operation"); + KVF_ASSERT(false && "Vulkan: an error has been caught during access flag to pipeline stage operation"); access_flags &= ~_access_flag; switch(_access_flag) @@ -952,7 +952,7 @@ VkPipelineStageFlags kvfAccessFlagsToPipelineStage(VkAccessFlags access_flags, V case VK_ACCESS_MEMORY_READ_BIT: break; case VK_ACCESS_MEMORY_WRITE_BIT: break; - default: KVF_ASSERT(false && "Vulkan : unknown access flag"); break; + default: KVF_ASSERT(false && "Vulkan: unknown access flag"); break; } } return stages; @@ -973,7 +973,7 @@ VkFormat kvfFindSupportFormatInCandidates(VkDevice device, VkFormat* candidates, return candidates[i]; } - KVF_ASSERT(false && "Vulkan : failed to find image format"); + KVF_ASSERT(false && "Vulkan: failed to find image format"); return VK_FORMAT_R8G8B8A8_SRGB; // just to avoir warning }