Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Oct 21, 2024
1 parent c7484b7 commit 8f757ca
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 68 deletions.
2 changes: 1 addition & 1 deletion example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void* create_image(mlx_t* mlx)
pixel[1] = j;
pixel[2] = k;
pixel[3] = 0x99;
mlx_set_image_pixel(mlx->mlx, img, j, k, *((int *)pixel));
mlx_set_image_pixel(mlx->mlx, img, j, k, *((int*)pixel));
}
}
return img;
Expand Down
2 changes: 0 additions & 2 deletions runtime/Includes/Core/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ namespace mlx
int m_id;

bool m_has_window;

bool m_insert_new_pixel_put_texture = false;
};
}

Expand Down
11 changes: 2 additions & 9 deletions runtime/Includes/Core/Graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ namespace mlx
MLX_PROFILE_FUNCTION();
p_scene->ResetSprites();
m_put_pixel_manager.ResetRenderData();
m_insert_new_pixel_put_texture = true;
m_draw_layer = 0;
}

void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
{
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)
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
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)
Expand All @@ -42,14 +39,10 @@ namespace mlx
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
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))
{
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)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Includes/Core/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace mlx

private:
static MemManager* s_instance;
inline static std::list<void*> s_blocks;
inline static std::vector<void*> s_blocks;
};
}

Expand Down
37 changes: 37 additions & 0 deletions runtime/Includes/Graphics/Font.h
Original file line number Diff line number Diff line change
@@ -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
32 changes: 3 additions & 29 deletions runtime/Includes/Graphics/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,7 @@ namespace mlx
IndexBuffer ibo;
std::size_t triangle_count = 0;

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;
}
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices);
};

public:
Expand All @@ -48,21 +35,8 @@ namespace mlx
private:
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
20 changes: 20 additions & 0 deletions runtime/Includes/Graphics/Mesh.inl
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions runtime/Includes/Graphics/PutPixelManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace mlx
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}

// 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();

~PutPixelManager();

private:
std::list<Texture> m_textures;
std::unordered_map<std::uint64_t, Texture> m_textures;
NonOwningPtr<class Renderer> p_renderer;
};
}
Expand Down
1 change: 1 addition & 0 deletions runtime/Includes/Graphics/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace mlx

public:
Sprite(NonOwningPtr<Texture> texture);
Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture);

inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
Expand Down
2 changes: 1 addition & 1 deletion runtime/Includes/PreCompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <dlfcn.h>
#endif

#if defined(MLX_PLAT_LINUX)
#ifdef MLX_PLAT_LINUX
#include <math.h> // sincos
#endif

Expand Down
4 changes: 4 additions & 0 deletions runtime/Includes/Renderer/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ namespace mlx
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
[[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;

protected:
Expand Down
2 changes: 1 addition & 1 deletion runtime/Includes/Renderer/RenderPasses/2DPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace mlx
{
public:
Render2DPass() = default;
void Init(class Renderer& renderer);
void Init();
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
void Destroy();
~Render2DPass() = default;
Expand Down
2 changes: 1 addition & 1 deletion runtime/Includes/Renderer/RenderPasses/FinalPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace mlx
{
public:
FinalPass() = default;
void Init(class Renderer& renderer);
void Init();
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
void Destroy();
~FinalPass() = default;
Expand Down
2 changes: 2 additions & 0 deletions runtime/Includes/Renderer/RenderPasses/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ namespace mlx
{
public:
RenderPasses() = default;

void Init(class Renderer& renderer);
void Pass(class Scene& scene, class Renderer& renderer);
void Destroy();

~RenderPasses() = default;

private:
Expand Down
7 changes: 7 additions & 0 deletions runtime/Sources/Graphics/Font.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <PreCompiled.h>

#include <Graphics/Font.h>

namespace mlx
{
}
26 changes: 10 additions & 16 deletions runtime/Sources/Graphics/PutPixelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@

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");

VkExtent2D swapchain_extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain());
if(insert_new_texture)
{
#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()));
#else
Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
#endif
texture.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
}
if(!m_textures.empty())
{
m_textures.back().SetPixel(x, y, color);
return (insert_new_texture ? &m_textures.back() : nullptr);
}
return nullptr;
#ifdef DEBUG
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
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
#endif
if(res.second)
res.first->second.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
res.first->second.SetPixel(x, y, color);
return (res.second ? &res.first->second : nullptr);
}

void PutPixelManager::ResetRenderData()
Expand Down
14 changes: 14 additions & 0 deletions runtime/Sources/Graphics/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ namespace mlx
Sprite& Scene::CreateSprite(NonOwningPtr<Texture> texture) noexcept
{
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);
m_sprites.push_back(sprite);
return *sprite;
Expand Down Expand Up @@ -52,6 +65,7 @@ namespace mlx

bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const
{
MLX_PROFILE_FUNCTION();
if(draw_layer >= m_sprites.size())
return false;
return m_sprites[draw_layer]->GetTexture() == texture;
Expand Down
9 changes: 9 additions & 0 deletions runtime/Sources/Graphics/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ namespace mlx
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
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;
}
}
2 changes: 1 addition & 1 deletion runtime/Sources/Renderer/Pipelines/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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();
if(!descriptor.vertex_shader || !descriptor.fragment_shader)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Sources/Renderer/RenderPasses/2DPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace mlx
Vec4f position;
};

void Render2DPass::Init(Renderer& renderer)
void Render2DPass::Init()
{
MLX_PROFILE_FUNCTION();

Expand Down
2 changes: 1 addition & 1 deletion runtime/Sources/Renderer/RenderPasses/FinalPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace mlx
{
void FinalPass::Init(Renderer& renderer)
void FinalPass::Init()
{
MLX_PROFILE_FUNCTION();
ShaderLayout vertex_shader_layout(
Expand Down
4 changes: 2 additions & 2 deletions runtime/Sources/Renderer/RenderPasses/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace mlx
{
void RenderPasses::Init(Renderer& renderer)
{
m_2Dpass.Init(renderer);
m_final.Init(renderer);
m_2Dpass.Init();
m_final.Init();
func::function<void(const EventBase&)> functor = [this, renderer](const EventBase& event)
{
if(event.What() == Event::ResizeEventCode)
Expand Down

0 comments on commit 8f757ca

Please sign in to comment.