mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
yes
This commit is contained in:
@@ -49,8 +49,6 @@ namespace mlx
|
|||||||
int m_id;
|
int m_id;
|
||||||
|
|
||||||
bool m_has_window;
|
bool m_has_window;
|
||||||
|
|
||||||
bool m_insert_new_pixel_put_texture = false;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,21 +8,18 @@ namespace mlx
|
|||||||
MLX_PROFILE_FUNCTION();
|
MLX_PROFILE_FUNCTION();
|
||||||
p_scene->ResetSprites();
|
p_scene->ResetSprites();
|
||||||
m_put_pixel_manager.ResetRenderData();
|
m_put_pixel_manager.ResetRenderData();
|
||||||
m_insert_new_pixel_put_texture = true;
|
|
||||||
m_draw_layer = 0;
|
m_draw_layer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
|
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
|
||||||
{
|
{
|
||||||
MLX_PROFILE_FUNCTION();
|
MLX_PROFILE_FUNCTION();
|
||||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_insert_new_pixel_put_texture, color);
|
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
|
||||||
if(texture)
|
if(texture)
|
||||||
{
|
{
|
||||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||||
m_draw_layer++;
|
|
||||||
}
|
}
|
||||||
m_insert_new_pixel_put_texture = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
|
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
|
||||||
@@ -42,14 +39,10 @@ namespace mlx
|
|||||||
{
|
{
|
||||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||||
m_insert_new_pixel_put_texture = true;
|
m_draw_layer++;
|
||||||
}
|
}
|
||||||
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
||||||
{
|
|
||||||
p_scene->BringToFront(std::move(sprite));
|
p_scene->BringToFront(std::move(sprite));
|
||||||
m_insert_new_pixel_put_texture = true;
|
|
||||||
}
|
|
||||||
m_draw_layer++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
|
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace mlx
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static MemManager* s_instance;
|
static MemManager* s_instance;
|
||||||
inline static std::list<void*> s_blocks;
|
inline static std::vector<void*> s_blocks;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
37
runtime/Includes/Graphics/Font.h
git.filemode.normal_file
37
runtime/Includes/Graphics/Font.h
git.filemode.normal_file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef __MLX_FONT__
|
||||||
|
#define __MLX_FONT__
|
||||||
|
|
||||||
|
#include <Renderer/Image.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class Font
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Font(const std::filesystem::path& path, float scale) : m_build_data(path), m_name(path.string()), m_scale(scale) {}
|
||||||
|
Font(const std::string& name, const std::vector<std::uint8_t>& ttf_data, float scale) : m_build_data(ttf_data), m_name(name), m_scale(scale) {}
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
|
|
||||||
|
inline const std::string& GetName() const { return m_name; }
|
||||||
|
inline float GetScale() const noexcept { return m_scale; }
|
||||||
|
inline const std::array<stbtt_packedchar, 96>& GetCharData() const { return m_cdata; }
|
||||||
|
inline const Texture& GetTexture() const noexcept { return m_atlas; }
|
||||||
|
inline bool operator==(const Font& rhs) const { return rhs.m_name == m_name && rhs.m_scale == m_scale; }
|
||||||
|
inline bool operator!=(const Font& rhs) const { return rhs.m_name != m_name || rhs.m_scale != m_scale; }
|
||||||
|
|
||||||
|
inline ~Font() { Destroy(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void BuildFont();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<stbtt_packedchar, 96> m_cdata;
|
||||||
|
Texture m_atlas;
|
||||||
|
std::variant<std::filesystem::path, std::vector<std::uint8_t>> m_build_data;
|
||||||
|
std::string m_name;
|
||||||
|
float m_scale;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -16,20 +16,7 @@ namespace mlx
|
|||||||
IndexBuffer ibo;
|
IndexBuffer ibo;
|
||||||
std::size_t triangle_count = 0;
|
std::size_t triangle_count = 0;
|
||||||
|
|
||||||
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
|
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices);
|
||||||
{
|
|
||||||
CPUBuffer vb(vertices.size() * sizeof(Vertex));
|
|
||||||
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
|
|
||||||
vbo.Init(vb.GetSize(), 0, "mlx_mesh");
|
|
||||||
vbo.SetData(std::move(vb));
|
|
||||||
|
|
||||||
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
|
|
||||||
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
|
|
||||||
ibo.Init(ib.GetSize(), 0, "mlx_mesh");
|
|
||||||
ibo.SetData(std::move(ib));
|
|
||||||
|
|
||||||
triangle_count = vertices.size() / 3;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -48,21 +35,8 @@ namespace mlx
|
|||||||
private:
|
private:
|
||||||
std::vector<SubMesh> m_sub_meshes;
|
std::vector<SubMesh> m_sub_meshes;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MeshRegistry
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
MeshRegistry() = default;
|
|
||||||
|
|
||||||
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
|
|
||||||
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
|
|
||||||
inline bool IsMeshKnown(std::shared_ptr<Mesh> mesh);
|
|
||||||
|
|
||||||
~MeshRegistry() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::unordered_set<std::shared_ptr<Mesh>> m_mesh_registry;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Graphics/Mesh.inl>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
20
runtime/Includes/Graphics/Mesh.inl
git.filemode.normal_file
20
runtime/Includes/Graphics/Mesh.inl
git.filemode.normal_file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Graphics/Mesh.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
|
||||||
|
{
|
||||||
|
CPUBuffer vb(vertices.size() * sizeof(Vertex));
|
||||||
|
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
|
||||||
|
vbo.Init(vb.GetSize(), 0, "mlx_mesh");
|
||||||
|
vbo.SetData(std::move(vb));
|
||||||
|
|
||||||
|
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
|
||||||
|
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
|
||||||
|
ibo.Init(ib.GetSize(), 0, "mlx_mesh");
|
||||||
|
ibo.SetData(std::move(ib));
|
||||||
|
|
||||||
|
triangle_count = vertices.size() / 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,13 +11,13 @@ namespace mlx
|
|||||||
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
|
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
|
||||||
|
|
||||||
// Return a valid pointer when a new texture has been created
|
// Return a valid pointer when a new texture has been created
|
||||||
NonOwningPtr<Texture> DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color);
|
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, std::uint32_t color);
|
||||||
void ResetRenderData();
|
void ResetRenderData();
|
||||||
|
|
||||||
~PutPixelManager();
|
~PutPixelManager();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<Texture> m_textures;
|
std::unordered_map<std::uint64_t, Texture> m_textures;
|
||||||
NonOwningPtr<class Renderer> p_renderer;
|
NonOwningPtr<class Renderer> p_renderer;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ namespace mlx
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Sprite(NonOwningPtr<Texture> texture);
|
Sprite(NonOwningPtr<Texture> texture);
|
||||||
|
Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture);
|
||||||
|
|
||||||
inline void SetColor(Vec4f color) noexcept { m_color = color; }
|
inline void SetColor(Vec4f color) noexcept { m_color = color; }
|
||||||
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
|
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
|
||||||
|
|||||||
@@ -54,7 +54,7 @@
|
|||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MLX_PLAT_LINUX)
|
#ifdef MLX_PLAT_LINUX
|
||||||
#include <math.h> // sincos
|
#include <math.h> // sincos
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ namespace mlx
|
|||||||
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
|
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
|
||||||
[[nodiscard]] MLX_FORCEINLINE ImageType GetType() const noexcept { return m_type; }
|
[[nodiscard]] MLX_FORCEINLINE ImageType GetType() const noexcept { return m_type; }
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE const std::string& GetDebugName() const { return m_debug_name; }
|
||||||
|
#endif
|
||||||
|
|
||||||
virtual ~Image() = default;
|
virtual ~Image() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace mlx
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Render2DPass() = default;
|
Render2DPass() = default;
|
||||||
void Init(class Renderer& renderer);
|
void Init();
|
||||||
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
~Render2DPass() = default;
|
~Render2DPass() = default;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace mlx
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FinalPass() = default;
|
FinalPass() = default;
|
||||||
void Init(class Renderer& renderer);
|
void Init();
|
||||||
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
~FinalPass() = default;
|
~FinalPass() = default;
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ namespace mlx
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RenderPasses() = default;
|
RenderPasses() = default;
|
||||||
|
|
||||||
void Init(class Renderer& renderer);
|
void Init(class Renderer& renderer);
|
||||||
void Pass(class Scene& scene, class Renderer& renderer);
|
void Pass(class Scene& scene, class Renderer& renderer);
|
||||||
void Destroy();
|
void Destroy();
|
||||||
|
|
||||||
~RenderPasses() = default;
|
~RenderPasses() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
7
runtime/Sources/Graphics/Font.cpp
git.filemode.normal_file
7
runtime/Sources/Graphics/Font.cpp
git.filemode.normal_file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <PreCompiled.h>
|
||||||
|
|
||||||
|
#include <Graphics/Font.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -5,26 +5,20 @@
|
|||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color)
|
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, std::uint32_t color)
|
||||||
{
|
{
|
||||||
Verify((bool)p_renderer, "invalid renderer pointer");
|
Verify((bool)p_renderer, "invalid renderer pointer");
|
||||||
|
|
||||||
VkExtent2D swapchain_extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain());
|
VkExtent2D swapchain_extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain());
|
||||||
if(insert_new_texture)
|
|
||||||
{
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(m_textures.size()));
|
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer));
|
||||||
#else
|
#else
|
||||||
Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
|
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
|
||||||
#endif
|
#endif
|
||||||
texture.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
|
if(res.second)
|
||||||
}
|
res.first->second.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
|
||||||
if(!m_textures.empty())
|
res.first->second.SetPixel(x, y, color);
|
||||||
{
|
return (res.second ? &res.first->second : nullptr);
|
||||||
m_textures.back().SetPixel(x, y, color);
|
|
||||||
return (insert_new_texture ? &m_textures.back() : nullptr);
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PutPixelManager::ResetRenderData()
|
void PutPixelManager::ResetRenderData()
|
||||||
|
|||||||
@@ -8,6 +8,19 @@ namespace mlx
|
|||||||
Sprite& Scene::CreateSprite(NonOwningPtr<Texture> texture) noexcept
|
Sprite& Scene::CreateSprite(NonOwningPtr<Texture> texture) noexcept
|
||||||
{
|
{
|
||||||
MLX_PROFILE_FUNCTION();
|
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)
|
||||||
|
{
|
||||||
|
if(texture->GetWidth() == sprite->GetTexture()->GetWidth() && texture->GetHeight() == sprite->GetTexture()->GetHeight())
|
||||||
|
{
|
||||||
|
std::shared_ptr<Sprite> new_sprite = std::make_shared<Sprite>(sprite->GetMesh(), texture);
|
||||||
|
m_sprites.push_back(new_sprite);
|
||||||
|
return *new_sprite;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>(texture);
|
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>(texture);
|
||||||
m_sprites.push_back(sprite);
|
m_sprites.push_back(sprite);
|
||||||
return *sprite;
|
return *sprite;
|
||||||
@@ -52,6 +65,7 @@ namespace mlx
|
|||||||
|
|
||||||
bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const
|
bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const
|
||||||
{
|
{
|
||||||
|
MLX_PROFILE_FUNCTION();
|
||||||
if(draw_layer >= m_sprites.size())
|
if(draw_layer >= m_sprites.size())
|
||||||
return false;
|
return false;
|
||||||
return m_sprites[draw_layer]->GetTexture() == texture;
|
return m_sprites[draw_layer]->GetTexture() == texture;
|
||||||
|
|||||||
@@ -44,4 +44,13 @@ namespace mlx
|
|||||||
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
|
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
|
||||||
p_texture = texture;
|
p_texture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sprite::Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture)
|
||||||
|
{
|
||||||
|
MLX_PROFILE_FUNCTION();
|
||||||
|
Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)");
|
||||||
|
Verify((bool)mesh, "Sprite: invalid mesh (internal mlx issue, please report to devs)");
|
||||||
|
p_mesh = mesh;
|
||||||
|
p_texture = texture;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
void GraphicPipeline::Init(const GraphicPipelineDescriptor& descriptor, std::string_view debug_name)
|
void GraphicPipeline::Init(const GraphicPipelineDescriptor& descriptor, [[maybe_unused]] std::string_view debug_name)
|
||||||
{
|
{
|
||||||
MLX_PROFILE_FUNCTION();
|
MLX_PROFILE_FUNCTION();
|
||||||
if(!descriptor.vertex_shader || !descriptor.fragment_shader)
|
if(!descriptor.vertex_shader || !descriptor.fragment_shader)
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace mlx
|
|||||||
Vec4f position;
|
Vec4f position;
|
||||||
};
|
};
|
||||||
|
|
||||||
void Render2DPass::Init(Renderer& renderer)
|
void Render2DPass::Init()
|
||||||
{
|
{
|
||||||
MLX_PROFILE_FUNCTION();
|
MLX_PROFILE_FUNCTION();
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
void FinalPass::Init(Renderer& renderer)
|
void FinalPass::Init()
|
||||||
{
|
{
|
||||||
MLX_PROFILE_FUNCTION();
|
MLX_PROFILE_FUNCTION();
|
||||||
ShaderLayout vertex_shader_layout(
|
ShaderLayout vertex_shader_layout(
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ namespace mlx
|
|||||||
{
|
{
|
||||||
void RenderPasses::Init(Renderer& renderer)
|
void RenderPasses::Init(Renderer& renderer)
|
||||||
{
|
{
|
||||||
m_2Dpass.Init(renderer);
|
m_2Dpass.Init();
|
||||||
m_final.Init(renderer);
|
m_final.Init();
|
||||||
func::function<void(const EventBase&)> functor = [this, renderer](const EventBase& event)
|
func::function<void(const EventBase&)> functor = [this, renderer](const EventBase& event)
|
||||||
{
|
{
|
||||||
if(event.What() == Event::ResizeEventCode)
|
if(event.What() == Event::ResizeEventCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user