begenning the refactor

This commit is contained in:
2024-03-27 23:03:54 +01:00
parent e5ff232065
commit 6bbf1e196d
131 changed files with 2135 additions and 1192 deletions

116
runtime/Sources/Core/Application.cpp git.filemode.normal_file
View File

@@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Application.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2024/03/27 17:43:18 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <PreCompiled.h>
#include <Core/Application.h>
#include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <core/errors.h>
#include <mlx_profile.h>
#include <core/memory.h>
#include <Core/EventBus.h>
namespace mlx::core
{
Application::Application() : _fps(), _in(std::make_unique<Input>())
{
EventBus::RegisterListener({[](const EventBase& event)
{
}, "__internal_application" });
_fps.init();
glfwSetErrorCallback([]([[maybe_unused]] int code, const char* desc)
{
error::report(e_kind::fatal_error, "GLFW error : %s", desc);
});
glfwInit();
}
void Application::run() noexcept
{
while(_in->isRunning())
{
if(!_fps.update())
continue;
_in->update();
if(_loop_hook)
_loop_hook(_param);
for(auto& gs : _graphics)
gs->render();
}
Render_Core::get().getSingleTimeCmdManager().updateSingleTimesCmdBuffersSubmitState();
for(auto& gs : _graphics)
{
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
gs->getRenderer().getCmdBuffer(i).waitForExecution();
}
}
void* Application::newTexture(int w, int h)
{
MLX_PROFILE_FUNCTION();
#ifdef DEBUG
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_unamed_user_texture");
#else
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, nullptr);
#endif
return &_textures.front();
}
void* Application::newStbTexture(char* file, int* w, int* h)
{
MLX_PROFILE_FUNCTION();
_textures.emplace_front(stbTextureLoad(file, w, h));
return &_textures.front();
}
void Application::destroyTexture(void* ptr)
{
MLX_PROFILE_FUNCTION();
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); // TODO : synchronize with another method than waiting for GPU to be idle
if(ptr == nullptr)
{
core::error::report(e_kind::error, "invalid image ptr (NULL)");
return;
}
auto it = std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) { return &texture == ptr; });
if(it == _textures.end())
{
core::error::report(e_kind::error, "invalid image ptr");
return;
}
Texture* texture = static_cast<Texture*>(ptr);
if(!texture->isInit())
core::error::report(e_kind::error, "trying to destroy a texture that has already been destroyed");
else
texture->destroy();
for(auto& gs : _graphics)
gs->tryEraseTextureFromManager(texture);
_textures.erase(it);
}
Application::~Application()
{
TextLibrary::get().clearLibrary();
FontLibrary::get().clearLibrary();
glfwTerminate();
}
}

304
runtime/Sources/Core/Bridge.cpp git.filemode.normal_file
View File

@@ -0,0 +1,304 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bridge.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2024/03/25 23:05:46 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <pre_compiled.h>
#include "errors.h"
#include "application.h"
#include <renderer/core/render_core.h>
#include <mlx.h>
#include <core/memory.h>
static void* __mlx_ptr = nullptr;
#define MLX_CHECK_APPLICATION_POINTER(ptr) \
if(ptr != __mlx_ptr || ptr == NULL) \
mlx::core::error::report(e_kind::fatal_error, "invalid mlx pointer passed to '%s'", MLX_FUNC_SIG); \
else {} // just to avoid issues with possible if-else statements outside this macro
extern "C"
{
void* mlx_init()
{
if(__mlx_ptr != nullptr)
{
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
return NULL; // not nullptr for the C compatibility
}
mlx::MemManager::get(); // just to initialize the C garbage collector
mlx::core::Application* app = new mlx::core::Application;
mlx::Render_Core::get().init();
if(app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "Tout a pété");
__mlx_ptr = static_cast<void*>(app);
return __mlx_ptr;
}
void* mlx_new_window(void* mlx, int w, int h, const char* title)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0)
{
mlx::core::error::report(e_kind::fatal_error, "invalid window size (%d x %d)", w, h);
return NULL; // not nullptr for the C compatibility
}
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
}
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
return 0;
}
int mlx_loop(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->run();
return 0;
}
int mlx_loop_end(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->loopEnd();
return 0;
}
int mlx_mouse_show()
{
return 0;
}
int mlx_mouse_hide()
{
return 0;
}
int mlx_mouse_move(void* mlx, void* win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
return 0;
}
int mlx_mouse_get_pos(void* mlx, int* x, int* y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
return 0;
}
int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->onEvent(win, static_cast<int>(event), funct_ptr, param);
return 0;
}
void* mlx_new_image(void* mlx, int width, int height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (width <= 0 || height <= 0)
mlx::core::error::report(e_kind::fatal_error, "invalid image size (%d x %d)", width, height);
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
}
int mlx_get_image_pixel(void* mlx, void* img, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
int color = static_cast<mlx::core::Application*>(mlx)->getTexturePixel(img, x, y);
unsigned char color_bits[4];
color_bits[0] = (color & 0x000000FF);
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x00FF0000) >> 16;
color_bits[3] = (color & 0xFF000000) >> 24;
return *reinterpret_cast<int*>(color_bits);
}
void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::core::Application*>(mlx)->setTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
}
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
return 0;
}
int mlx_destroy_image(void* mlx, void* img)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
return 0;
}
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "PNG loader : filename is NULL");
std::filesystem::path file(filename);
if(file.extension() != ".png")
{
mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "JPG loader : filename is NULL");
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{
mlx::core::error::report(e_kind::error, "JPG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "BMP loader : filename is NULL");
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
{
mlx::core::error::report(e_kind::error, "BMP loader : not a bmp file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::core::Application*>(mlx)->pixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
return 0;
}
int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::core::Application*>(mlx)->stringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str);
return 0;
}
void mlx_set_font(void* mlx, void* win, char* filepath)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::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::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
return;
}
if(std::strcmp(filepath, "default") == 0)
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 6.f);
else
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 16.f);
}
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::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::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
return;
}
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, scale);
}
int mlx_clear_window(void* mlx, void* win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
return 0;
}
int mlx_destroy_window(void* mlx, void* win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
return 0;
}
int mlx_destroy_display(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
delete static_cast<mlx::core::Application*>(mlx);
mlx::Render_Core::get().destroy();
__mlx_ptr = nullptr;
return 0;
}
int mlx_get_screens_size(void* mlx, void* win, int* w, int* h)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->getScreenSize(win, w, h);
return 0;
}
int mlx_set_fps_goal(void* mlx, int fps)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(fps < 0)
{
mlx::core::error::report(e_kind::error, "You cannot set a negative FPS cap (nice try)");
fps = -fps;
}
if(fps == 0)
{
mlx::core::error::report(e_kind::error, "You cannot set a FPS cap to 0 (nice try)");
return 0;
}
static_cast<mlx::core::Application*>(mlx)->setFPSCap(static_cast<std::uint32_t>(fps));
return 0;
}
}

37
runtime/Sources/Core/EventBus.cpp git.filemode.normal_file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* EventBus.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 17:36:05 by maldavid #+# #+# */
/* Updated: 2024/03/27 17:37:01 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <PreCompiled.h>
#include <Core/EventBus.h>
#include <Core/Logs.h>
namespace mlx
{
void EventBus::Send(const std::string& listener_name, const EventBase& event)
{
for(const EventListener& listener : s_listeners)
{
if(listener.GetName() == listener_name)
{
listener.Call(event);
return;
}
}
Warning("Event Bus : listener not found, '%'", listener_name);
}
void EventBus::SendBroadcast(const EventBase& event)
{
for(const EventListener& listener : s_listeners)
listener.Call(event);
}
}

21
runtime/Sources/Core/EventListener.cpp git.filemode.normal_file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* EventListener.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 17:37:09 by maldavid #+# #+# */
/* Updated: 2024/03/27 17:37:38 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <PreCompiled.h>
#include <Core/EventListener.h>
namespace mlx
{
EventListener::EventListener(func::function<void(const EventBase&)> functor, std::string name)
: m_listen_functor(std::move(functor)), m_name(std::move(name))
{}
}

42
runtime/Sources/Core/Fps.cpp git.filemode.normal_file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fps.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 14:56:17 by maldavid #+# #+# */
/* Updated: 2024/03/27 20:53:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <PreCompiled.h>
#include <Core/Fps.h>
namespace mlx
{
void FpsManager::Init()
{
m_timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
m_fps_before = m_timer;
m_fps_now = m_timer;
}
bool FpsManager::Update()
{
using namespace std::chrono_literals;
m_fps_now = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(std::chrono::duration<std::uint64_t>{m_fps_now - m_timer} >= 1s)
m_timer += m_fps_now;
m_fps_elapsed_time = m_fps_now - m_fps_before;
if(m_fps_elapsed_time >= m_ns)
{
m_fps_before += m_ns;
return true;
}
std::this_thread::sleep_for(std::chrono::duration<double, std::nano>(m_ns - 1));
return false;
}
}

94
runtime/Sources/Core/Graphics.cpp git.filemode.normal_file
View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphics.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2024/03/27 00:32:34 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <core/graphics.h>
namespace mlx
{
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id) :
_window(nullptr),
_renderer(std::make_unique<Renderer>()),
_width(w),
_height(h),
_id(id),
_has_window(false)
{
MLX_PROFILE_FUNCTION();
_renderer->setWindow(nullptr);
_renderer->init(render_target);
_pixel_put_pipeline.init(w, h, *_renderer);
_text_manager.init(*_renderer);
}
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) :
_window(std::make_shared<Window>(w, h, title)),
_renderer(std::make_unique<Renderer>()),
_width(w),
_height(h),
_id(id),
_has_window(true)
{
MLX_PROFILE_FUNCTION();
_renderer->setWindow(_window.get());
_renderer->init(nullptr);
_pixel_put_pipeline.init(w, h, *_renderer);
_text_manager.init(*_renderer);
}
void GraphicsSupport::render() noexcept
{
MLX_PROFILE_FUNCTION();
if(!_renderer->beginFrame())
return;
_proj = glm::ortho<float>(0, _width, 0, _height);
_renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj);
static std::array<VkDescriptorSet, 2> sets = {
_renderer->getVertDescriptorSet().get(),
VK_NULL_HANDLE
};
for(auto& data : _drawlist)
data->render(sets, *_renderer);
_pixel_put_pipeline.render(sets, *_renderer);
_renderer->endFrame();
for(auto& data : _drawlist)
data->resetUpdate();
#ifdef GRAPHICS_MEMORY_DUMP
// dump memory to file every two seconds
using namespace std::chrono_literals;
static std::int64_t timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(std::chrono::duration<std::uint64_t>{static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer} >= 1s)
{
Render_Core::get().getAllocator().dumpMemoryToJson();
timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
}
#endif
}
GraphicsSupport::~GraphicsSupport()
{
MLX_PROFILE_FUNCTION();
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
_text_manager.destroy();
_pixel_put_pipeline.destroy();
_renderer->destroy();
if(_window)
_window->destroy();
}
}

68
runtime/Sources/Core/Logs.cpp git.filemode.normal_file
View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Logs.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 17:20:55 by maldavid #+# #+# */
/* Updated: 2024/03/27 17:26:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <Core/Logs.h>
#include <Utils/Ansi.h>
#include <Core/EventBase.h>
#include <Core/EventBus.h>
namespace mlx
{
namespace Internal
{
struct FatalErrorEvent : public EventBase
{
std::uint32_t What() const override { return 167; }
};
}
void Logs::Report(LogType type, std::string message)
{
Report(type, 0, {}, {}, std::move(message));
}
void Logs::Report(LogType type, unsigned int line, std::string_view file, std::string_view function, std::string message)
{
using namespace std::literals;
#ifndef DEBUG
if(type == LogType::Debug)
return;
#endif
std::string code_infos;
if((type == LogType::Error || type == LogType::FatalError) && !file.empty() && !function.empty())
{
code_infos += "{in file '"s;
code_infos += file;
code_infos += "', line "s + std::to_string(line) + ", in function '"s;
code_infos += function;
code_infos += "'} "s;
}
switch(type)
{
case LogType::Debug: std::cout << Ansi::blue << "[Akel Debug] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Message: std::cout << Ansi::blue << "[Akel Message] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Warning: std::cout << Ansi::magenta << "[Akel Warning] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::Error: std::cerr << Ansi::red << "[Akel Error] " << Ansi::def << code_infos << message << '\n'; break;
case LogType::FatalError: std::cerr << Ansi::red << "[Akel Fatal Error] " << Ansi::def << code_infos << message << '\n'; break;
default: break;
}
if(type == LogType::FatalError)
{
std::cout << Ansi::bg_red << "Fatal Error: emergency exit" << Ansi::bg_def << std::endl;
EventBus::Send("__internal_application", Internal::FatalErrorEvent{});
}
}
}

66
runtime/Sources/Core/Memory.cpp git.filemode.normal_file
View File

@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */
/* Updated: 2024/03/25 19:01:02 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <core/memory.h>
#include <core/errors.h>
namespace mlx
{
void* MemManager::malloc(std::size_t size)
{
void* ptr = std::malloc(size);
if(ptr != nullptr)
_blocks.push_back(ptr);
return ptr;
}
void* MemManager::calloc(std::size_t n, std::size_t size)
{
void* ptr = std::calloc(n, size);
if(ptr != nullptr)
_blocks.push_back(ptr);
return ptr;
}
void* MemManager::realloc(void* ptr, std::size_t size)
{
void* ptr2 = std::realloc(ptr, size);
if(ptr2 != nullptr)
_blocks.push_back(ptr2);
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
if(it != _blocks.end())
_blocks.erase(it);
return ptr2;
}
void MemManager::free(void* ptr)
{
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
if(it == _blocks.end())
{
core::error::report(e_kind::error, "Memory Manager : trying to free a pointer not allocated by the memory manager");
return;
}
std::free(*it);
_blocks.erase(it);
}
MemManager::~MemManager()
{
std::for_each(_blocks.begin(), _blocks.end(), [](void* ptr)
{
std::free(ptr);
});
}
}

81
runtime/Sources/Core/Profiler.cpp git.filemode.normal_file
View File

@@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* profiler.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 13:56:21 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:01:06 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <core/profiler.h>
#include <core/errors.h>
#include <iostream>
namespace mlx
{
void Profiler::beginRuntimeSession()
{
std::lock_guard lock(_mutex);
if(_runtime_session_began)
return;
_output_stream.open("./runtime_profile.mlx.json", std::ofstream::out | std::ofstream::trunc);
if(_output_stream.is_open())
writeHeader();
else
core::error::report(e_kind::error, "Profiler : cannot open runtime profile file");
_runtime_session_began = true;
}
void Profiler::appendProfileData(ProfileResult&& result)
{
std::lock_guard lock(_mutex);
auto it = _profile_data.find(result.name);
if(it != _profile_data.end())
{
result.elapsed_time = (result.elapsed_time + it->second.second.elapsed_time) / it->second.first;
_profile_data[result.name].first++;
_profile_data[result.name].second = result;
}
else
_profile_data[result.name] = std::make_pair(1, result);
}
void Profiler::writeProfile(const ProfileResult& result)
{
std::stringstream json;
json << std::setprecision(9) << std::fixed;
json << ",\n{\n";
json << "\t\"type\" : \"function\"," << '\n';
json << "\t\"name\" : \"" << result.name << "\"," << '\n';
json << "\t\"thread id\" : " << result.thread_id << "," << '\n';
json << "\t\"average duration\" : \"" << result.elapsed_time.count() << "ms\"\n";
json << "}";
_output_stream << json.str();
}
void Profiler::endRuntimeSession()
{
std::lock_guard lock(_mutex);
if(!_runtime_session_began)
return;
for(auto& [_, pair] : _profile_data)
writeProfile(pair.second);
writeFooter();
_output_stream.close();
_profile_data.clear();
_runtime_session_began = false;
}
Profiler::~Profiler()
{
if(!_runtime_session_began)
return;
endRuntimeSession();
}
}

25
runtime/Sources/Core/UUID.cpp git.filemode.normal_file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* UUID.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 11:26:37 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:00:36 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <pre_compiled.h>
#include <core/UUID.h>
namespace mlx
{
static std::random_device random_device;
static std::mt19937_64 engine(random_device());
static std::uniform_int_distribution<std::uint64_t> uniform_distribution;
UUID::UUID() : _uuid(uniform_distribution(engine)) {}
UUID::UUID(std::uint64_t uuid) : _uuid(uuid) {}
}