From 0e04356ea77dc4b68eba1e37febc6e7b1ebc55aa Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 27 Mar 2024 23:03:54 +0100 Subject: [PATCH] begenning the refactor --- Makefile | 22 +- includes/mlx_profile.h | 12 +- runtime/Includes/Core/Application.h | 71 +++++++ runtime/Includes/Core/Application.inl | 196 ++++++++++++++++++ runtime/Includes/Core/Enums.h | 33 +++ .../Includes/Core/EventBase.h | 27 +-- runtime/Includes/Core/EventBus.h | 35 ++++ runtime/Includes/Core/EventListener.h | 37 ++++ runtime/Includes/Core/Format.h | 33 +++ runtime/Includes/Core/Format.inl | 146 +++++++++++++ src/core/fps.h => runtime/Includes/Core/Fps.h | 22 +- runtime/Includes/Core/Graphics.h | 75 +++++++ runtime/Includes/Core/Graphics.inl | 79 +++++++ runtime/Includes/Core/Logs.h | 84 ++++++++ runtime/Includes/Core/Logs.inl | 135 ++++++++++++ .../Includes/Core/Memory.h | 16 +- .../Includes/Core/Profiler.h | 64 +++--- {src/core => runtime/Includes/Core}/UUID.h | 6 +- runtime/Includes/Drivers/GLFW/GLFWInputs.h | 44 ++++ runtime/Includes/Platform/Inputs.h | 62 ++++++ .../Includes/Platform/Window.h | 28 +-- .../Includes/PreCompiled.h | 18 +- runtime/Includes/Renderer/Buffers/Buffer.h | 66 ++++++ .../Includes/Renderer/Buffers/IndexBuffer.h | 12 +- .../Includes/Renderer/Buffers/UniformBuffer.h | 50 +++++ .../Includes/Renderer/Buffers/VertexBuffer.h | 46 ++++ .../Includes/Renderer/Command/CommandBuffer.h | 70 +++++++ .../Renderer/Command/CommandManager.h | 32 +-- .../Includes/Renderer/Command/CommandPool.h | 20 +- .../Renderer/Command/CommandResource.h | 29 ++- .../Renderer/Command/SingleTimeCmdManager.h | 28 +-- .../Includes/Renderer/Core/Device.h | 24 +-- .../Includes/Renderer/Core/DrawableResource.h | 8 +- .../Includes/Renderer/Core/Fence.h | 20 +- .../Includes/Renderer/Core/Instance.h | 18 +- .../Includes/Renderer/Core/Memory.h | 30 +-- .../Includes/Renderer/Core/Queues.h | 29 ++- runtime/Includes/Renderer/Core/RenderCore.h | 78 +++++++ .../Includes/Renderer/Core/Semaphore.h | 14 +- .../Includes/Renderer/Core/Surface.h | 16 +- .../Includes/Renderer/Core/ValidationLayers.h | 24 +-- .../Renderer/Descriptors/DescriptorPool.h | 22 +- .../Descriptors/DescriptorPoolManager.h | 12 +- .../Renderer/Descriptors/DescriptorSet.h | 30 +-- .../Descriptors/DescriptorSetLayout.h | 18 +- runtime/Includes/Renderer/Enums.h | 59 ++++++ .../Includes/Renderer/Images/Image.h | 0 .../Includes/Renderer/Images/Texture.h | 0 .../Includes/Renderer/Images/TextureAtlas.h | 0 .../Renderer/Images/TextureDescriptor.h | 0 .../Includes/Renderer/Images/TextureManager.h | 0 .../Includes/Renderer/Pipelines/Pipeline.h | 0 .../Includes/Renderer/PixelPut.h | 0 .../Includes/Renderer/Renderer.h | 0 .../Renderer/Renderpass/FrameBuffer.h | 0 .../Includes/Renderer/Renderpass/RenderPass.h | 0 .../Includes/Renderer/Renderpass/Swapchain.h | 0 .../Includes/Renderer/Texts/Font.h | 0 .../Includes/Renderer/Texts/FontLibrary.h | 0 .../Includes/Renderer/Texts/Text.h | 0 .../Includes/Renderer/Texts/TextDescriptor.h | 0 .../Includes/Renderer/Texts/TextLibrary.h | 0 .../Includes/Renderer/Texts/TextManager.h | 0 runtime/Includes/Utils/Ansi.h | 56 +++++ .../Includes/Utils/CombineHash.h | 13 +- runtime/Includes/Utils/ConstMap.h | 69 ++++++ .../Includes/Utils/DogicaTTF.h | 6 +- .../Includes/Utils/IconMlx.h | 6 +- .../Includes/Utils/NonCopyable.h | 0 runtime/Includes/Utils/NonOwningPtr.h | 45 ++++ runtime/Includes/Utils/NonOwningPtr.inl | 62 ++++++ .../Includes/Utils/Singleton.h | 8 +- .../Sources/Core/Application.cpp | 14 +- .../Sources/Core/Bridge.cpp | 0 runtime/Sources/Core/EventBus.cpp | 37 ++++ runtime/Sources/Core/EventListener.cpp | 21 ++ .../fps.cpp => runtime/Sources/Core/Fps.cpp | 33 ++- .../Sources/Core/Graphics.cpp | 0 runtime/Sources/Core/Logs.cpp | 68 ++++++ .../Sources/Core/Memory.cpp | 0 .../Sources/Core/Profiler.cpp | 0 {src/core => runtime/Sources/Core}/UUID.cpp | 0 runtime/Sources/Drivers/GLFW/GLFWInputs.cpp | 22 ++ .../Sources/Platform/Inputs.cpp | 6 +- .../Sources/Platform/Window.cpp | 0 .../Sources/Renderer/Buffers/Buffer.cpp | 0 .../Renderer/Buffers/UniformBuffer.cpp | 0 .../Sources/Renderer/Buffers/VertexBuffer.cpp | 0 .../Renderer/Command/CommandBuffer.cpp | 0 .../Renderer/Command/CommandManager.cpp | 0 .../Sources/Renderer/Command/CommandPool.cpp | 0 .../Command/SingleTimeCommandManager.cpp | 0 .../Sources/Renderer/Core/Device.cpp | 0 .../Sources/Renderer/Core/Fence.cpp | 0 .../Sources/Renderer/Core/Instance.cpp | 0 .../Sources/Renderer/Core/Memory.cpp | 0 .../Sources/Renderer/Core/Queues.cpp | 0 .../Sources/Renderer/Core/RenderCore.cpp | 0 .../Sources/Renderer/Core/Semaphore.cpp | 0 .../Sources/Renderer/Core/Surface.cpp | 0 .../Renderer/Core/ValidationLayers.cpp | 0 .../Descriptors}/descriptor_pool_manager.cpp | 0 .../Descriptors}/vk_descriptor_pool.cpp | 0 .../Descriptors}/vk_descriptor_set.cpp | 0 .../Descriptors}/vk_descriptor_set_layout.cpp | 0 .../Sources/Renderer/Images}/texture.cpp | 0 .../Renderer/Images}/texture_atlas.cpp | 0 .../Sources/Renderer/Images}/vk_image.cpp | 0 .../Sources/Renderer/Pipelines}/pipeline.cpp | 0 .../Sources/Renderer/PixelPut.cpp | 0 .../Sources/Renderer/Renderer.cpp | 0 .../Renderer/Renderpass/Framebuffer.cpp | 0 .../Renderer/Renderpass/Renderpass.cpp | 0 .../Sources/Renderer/Renderpass/Swapchain.cpp | 0 .../Sources/Renderer/Texts/Font.cpp | 0 .../Sources/Renderer/Texts/FontLibrary.cpp | 0 .../Sources/Renderer/Texts/Text.cpp | 0 .../Sources/Renderer/Texts/TextDescriptor.cpp | 0 .../Sources/Renderer/Texts/TextLibrary.cpp | 0 .../Sources/Renderer/Texts/TextManager.cpp | 0 src/core/application.h | 73 ------- src/core/application.inl | 196 ------------------ src/core/errors.cpp | 41 ---- src/core/graphics.h | 76 ------- src/core/graphics.inl | 79 ------- src/platform/inputs.h | 71 ------- src/renderer/buffers/vk_buffer.h | 63 ------ src/renderer/buffers/vk_ubo.h | 48 ----- src/renderer/buffers/vk_vbo.h | 46 ---- src/renderer/command/vk_cmd_buffer.h | 85 -------- src/renderer/core/render_core.h | 83 -------- 131 files changed, 2135 insertions(+), 1192 deletions(-) create mode 100644 runtime/Includes/Core/Application.h create mode 100644 runtime/Includes/Core/Application.inl create mode 100644 runtime/Includes/Core/Enums.h rename src/core/errors.h => runtime/Includes/Core/EventBase.h (63%) create mode 100644 runtime/Includes/Core/EventBus.h create mode 100644 runtime/Includes/Core/EventListener.h create mode 100644 runtime/Includes/Core/Format.h create mode 100644 runtime/Includes/Core/Format.inl rename src/core/fps.h => runtime/Includes/Core/Fps.h (65%) create mode 100644 runtime/Includes/Core/Graphics.h create mode 100644 runtime/Includes/Core/Graphics.inl create mode 100644 runtime/Includes/Core/Logs.h create mode 100644 runtime/Includes/Core/Logs.inl rename src/core/memory.h => runtime/Includes/Core/Memory.h (72%) rename src/core/profiler.h => runtime/Includes/Core/Profiler.h (64%) rename {src/core => runtime/Includes/Core}/UUID.h (86%) create mode 100644 runtime/Includes/Drivers/GLFW/GLFWInputs.h create mode 100644 runtime/Includes/Platform/Inputs.h rename src/platform/window.h => runtime/Includes/Platform/Window.h (62%) rename src/pre_compiled.h => runtime/Includes/PreCompiled.h (86%) create mode 100644 runtime/Includes/Renderer/Buffers/Buffer.h rename src/renderer/buffers/vk_ibo.h => runtime/Includes/Renderer/Buffers/IndexBuffer.h (64%) create mode 100644 runtime/Includes/Renderer/Buffers/UniformBuffer.h create mode 100644 runtime/Includes/Renderer/Buffers/VertexBuffer.h create mode 100644 runtime/Includes/Renderer/Command/CommandBuffer.h rename src/renderer/command/cmd_manager.h => runtime/Includes/Renderer/Command/CommandManager.h (55%) rename src/renderer/command/vk_cmd_pool.h => runtime/Includes/Renderer/Command/CommandPool.h (67%) rename src/renderer/core/cmd_resource.h => runtime/Includes/Renderer/Command/CommandResource.h (60%) rename src/renderer/command/single_time_cmd_manager.h => runtime/Includes/Renderer/Command/SingleTimeCmdManager.h (64%) rename src/renderer/core/vk_device.h => runtime/Includes/Renderer/Core/Device.h (60%) rename src/renderer/core/drawable_resource.h => runtime/Includes/Renderer/Core/DrawableResource.h (81%) rename src/renderer/core/vk_fence.h => runtime/Includes/Renderer/Core/Fence.h (69%) rename src/renderer/core/vk_instance.h => runtime/Includes/Renderer/Core/Instance.h (69%) rename src/renderer/core/memory.h => runtime/Includes/Renderer/Core/Memory.h (60%) rename src/renderer/core/vk_queues.h => runtime/Includes/Renderer/Core/Queues.h (64%) create mode 100644 runtime/Includes/Renderer/Core/RenderCore.h rename src/renderer/core/vk_semaphore.h => runtime/Includes/Renderer/Core/Semaphore.h (65%) rename src/renderer/core/vk_surface.h => runtime/Includes/Renderer/Core/Surface.h (65%) rename src/renderer/core/vk_validation_layers.h => runtime/Includes/Renderer/Core/ValidationLayers.h (57%) rename src/renderer/descriptors/vk_descriptor_pool.h => runtime/Includes/Renderer/Descriptors/DescriptorPool.h (59%) rename src/renderer/descriptors/descriptor_pool_manager.h => runtime/Includes/Renderer/Descriptors/DescriptorPoolManager.h (77%) rename src/renderer/descriptors/vk_descriptor_set.h => runtime/Includes/Renderer/Descriptors/DescriptorSet.h (57%) rename src/renderer/descriptors/vk_descriptor_set_layout.h => runtime/Includes/Renderer/Descriptors/DescriptorSetLayout.h (67%) create mode 100644 runtime/Includes/Renderer/Enums.h rename src/renderer/images/vk_image.h => runtime/Includes/Renderer/Images/Image.h (100%) rename src/renderer/images/texture.h => runtime/Includes/Renderer/Images/Texture.h (100%) rename src/renderer/images/texture_atlas.h => runtime/Includes/Renderer/Images/TextureAtlas.h (100%) rename src/renderer/images/texture_descriptor.h => runtime/Includes/Renderer/Images/TextureDescriptor.h (100%) rename src/renderer/images/texture_manager.h => runtime/Includes/Renderer/Images/TextureManager.h (100%) rename src/renderer/pipeline/pipeline.h => runtime/Includes/Renderer/Pipelines/Pipeline.h (100%) rename src/renderer/pixel_put.h => runtime/Includes/Renderer/PixelPut.h (100%) rename src/renderer/renderer.h => runtime/Includes/Renderer/Renderer.h (100%) rename src/renderer/renderpass/vk_framebuffer.h => runtime/Includes/Renderer/Renderpass/FrameBuffer.h (100%) rename src/renderer/renderpass/vk_render_pass.h => runtime/Includes/Renderer/Renderpass/RenderPass.h (100%) rename src/renderer/swapchain/vk_swapchain.h => runtime/Includes/Renderer/Renderpass/Swapchain.h (100%) rename src/renderer/texts/font.h => runtime/Includes/Renderer/Texts/Font.h (100%) rename src/renderer/texts/font_library.h => runtime/Includes/Renderer/Texts/FontLibrary.h (100%) rename src/renderer/texts/text.h => runtime/Includes/Renderer/Texts/Text.h (100%) rename src/renderer/texts/text_descriptor.h => runtime/Includes/Renderer/Texts/TextDescriptor.h (100%) rename src/renderer/texts/text_library.h => runtime/Includes/Renderer/Texts/TextLibrary.h (100%) rename src/renderer/texts/text_manager.h => runtime/Includes/Renderer/Texts/TextManager.h (100%) create mode 100644 runtime/Includes/Utils/Ansi.h rename src/utils/combine_hash.h => runtime/Includes/Utils/CombineHash.h (74%) create mode 100644 runtime/Includes/Utils/ConstMap.h rename src/utils/dogica_ttf.h => runtime/Includes/Utils/DogicaTTF.h (99%) rename src/utils/icon_mlx.h => runtime/Includes/Utils/IconMlx.h (99%) rename src/utils/non_copyable.h => runtime/Includes/Utils/NonCopyable.h (100%) create mode 100644 runtime/Includes/Utils/NonOwningPtr.h create mode 100644 runtime/Includes/Utils/NonOwningPtr.inl rename src/utils/singleton.h => runtime/Includes/Utils/Singleton.h (84%) rename src/core/application.cpp => runtime/Sources/Core/Application.cpp (91%) rename src/core/bridge.cpp => runtime/Sources/Core/Bridge.cpp (100%) create mode 100644 runtime/Sources/Core/EventBus.cpp create mode 100644 runtime/Sources/Core/EventListener.cpp rename src/core/fps.cpp => runtime/Sources/Core/Fps.cpp (53%) rename src/core/graphics.cpp => runtime/Sources/Core/Graphics.cpp (100%) create mode 100644 runtime/Sources/Core/Logs.cpp rename src/core/memory.cpp => runtime/Sources/Core/Memory.cpp (100%) rename src/core/profiler.cpp => runtime/Sources/Core/Profiler.cpp (100%) rename {src/core => runtime/Sources/Core}/UUID.cpp (100%) create mode 100644 runtime/Sources/Drivers/GLFW/GLFWInputs.cpp rename src/platform/inputs.cpp => runtime/Sources/Platform/Inputs.cpp (92%) rename src/platform/window.cpp => runtime/Sources/Platform/Window.cpp (100%) rename src/renderer/buffers/vk_buffer.cpp => runtime/Sources/Renderer/Buffers/Buffer.cpp (100%) rename src/renderer/buffers/vk_ubo.cpp => runtime/Sources/Renderer/Buffers/UniformBuffer.cpp (100%) rename src/renderer/buffers/vk_vbo.cpp => runtime/Sources/Renderer/Buffers/VertexBuffer.cpp (100%) rename src/renderer/command/vk_cmd_buffer.cpp => runtime/Sources/Renderer/Command/CommandBuffer.cpp (100%) rename src/renderer/command/cmd_manager.cpp => runtime/Sources/Renderer/Command/CommandManager.cpp (100%) rename src/renderer/command/vk_cmd_pool.cpp => runtime/Sources/Renderer/Command/CommandPool.cpp (100%) rename src/renderer/command/single_time_cmd_manager.cpp => runtime/Sources/Renderer/Command/SingleTimeCommandManager.cpp (100%) rename src/renderer/core/vk_device.cpp => runtime/Sources/Renderer/Core/Device.cpp (100%) rename src/renderer/core/vk_fence.cpp => runtime/Sources/Renderer/Core/Fence.cpp (100%) rename src/renderer/core/vk_instance.cpp => runtime/Sources/Renderer/Core/Instance.cpp (100%) rename src/renderer/core/memory.cpp => runtime/Sources/Renderer/Core/Memory.cpp (100%) rename src/renderer/core/vk_queues.cpp => runtime/Sources/Renderer/Core/Queues.cpp (100%) rename src/renderer/core/render_core.cpp => runtime/Sources/Renderer/Core/RenderCore.cpp (100%) rename src/renderer/core/vk_semaphore.cpp => runtime/Sources/Renderer/Core/Semaphore.cpp (100%) rename src/renderer/core/vk_surface.cpp => runtime/Sources/Renderer/Core/Surface.cpp (100%) rename src/renderer/core/vk_validation_layers.cpp => runtime/Sources/Renderer/Core/ValidationLayers.cpp (100%) rename {src/renderer/descriptors => runtime/Sources/Renderer/Descriptors}/descriptor_pool_manager.cpp (100%) rename {src/renderer/descriptors => runtime/Sources/Renderer/Descriptors}/vk_descriptor_pool.cpp (100%) rename {src/renderer/descriptors => runtime/Sources/Renderer/Descriptors}/vk_descriptor_set.cpp (100%) rename {src/renderer/descriptors => runtime/Sources/Renderer/Descriptors}/vk_descriptor_set_layout.cpp (100%) rename {src/renderer/images => runtime/Sources/Renderer/Images}/texture.cpp (100%) rename {src/renderer/images => runtime/Sources/Renderer/Images}/texture_atlas.cpp (100%) rename {src/renderer/images => runtime/Sources/Renderer/Images}/vk_image.cpp (100%) rename {src/renderer/pipeline => runtime/Sources/Renderer/Pipelines}/pipeline.cpp (100%) rename src/renderer/pixel_put.cpp => runtime/Sources/Renderer/PixelPut.cpp (100%) rename src/renderer/renderer.cpp => runtime/Sources/Renderer/Renderer.cpp (100%) rename src/renderer/renderpass/vk_framebuffer.cpp => runtime/Sources/Renderer/Renderpass/Framebuffer.cpp (100%) rename src/renderer/renderpass/vk_render_pass.cpp => runtime/Sources/Renderer/Renderpass/Renderpass.cpp (100%) rename src/renderer/swapchain/vk_swapchain.cpp => runtime/Sources/Renderer/Renderpass/Swapchain.cpp (100%) rename src/renderer/texts/font.cpp => runtime/Sources/Renderer/Texts/Font.cpp (100%) rename src/renderer/texts/font_library.cpp => runtime/Sources/Renderer/Texts/FontLibrary.cpp (100%) rename src/renderer/texts/text.cpp => runtime/Sources/Renderer/Texts/Text.cpp (100%) rename src/renderer/texts/text_descriptor.cpp => runtime/Sources/Renderer/Texts/TextDescriptor.cpp (100%) rename src/renderer/texts/text_library.cpp => runtime/Sources/Renderer/Texts/TextLibrary.cpp (100%) rename src/renderer/texts/text_manager.cpp => runtime/Sources/Renderer/Texts/TextManager.cpp (100%) delete mode 100644 src/core/application.h delete mode 100644 src/core/application.inl delete mode 100644 src/core/errors.cpp delete mode 100644 src/core/graphics.h delete mode 100644 src/core/graphics.inl delete mode 100644 src/platform/inputs.h delete mode 100644 src/renderer/buffers/vk_buffer.h delete mode 100644 src/renderer/buffers/vk_ubo.h delete mode 100644 src/renderer/buffers/vk_vbo.h delete mode 100644 src/renderer/command/vk_cmd_buffer.h delete mode 100644 src/renderer/core/render_core.h diff --git a/Makefile b/Makefile index cf1ebdd..e488de3 100644 --- a/Makefile +++ b/Makefile @@ -6,22 +6,23 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2024/03/25 18:57:44 by maldavid ### ########.fr # +# Updated: 2024/03/27 21:30:44 by maldavid ### ########.fr # # # # **************************************************************************** # NAME = libmlx.so -SRCS = $(wildcard $(addsuffix /*.cpp, ./src/core)) -SRCS += $(wildcard $(addsuffix /*.cpp, ./src/platform)) -SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer)) -SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer/**)) +SRCS = $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Core)) +SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Platform)) +SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer)) +SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/**)) +SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Drivers/**)) OBJ_DIR = objs/makefile OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) -PCH = ./src/pre_compiled.h -GCH = ./src/pre_compiled.h.gch +PCH = ./runtime/Includes/PreCompiled.h +GCH = ./runtime/Includes/PreCompiled.h.gch OS = $(shell uname -s) DEBUG ?= false @@ -30,13 +31,14 @@ IMAGES_OPTIMIZED ?= true FORCE_INTEGRATED_GPU ?= false GRAPHICS_MEMORY_DUMP ?= false PROFILER ?= false +LEGACY ?= false MODE = "release" CXX = clang++ CXXFLAGS = -std=c++17 -O3 -fPIC -Wall -Wextra -Wno-deprecated -DSDL_MAIN_HANDLED -INCLUDES = -I./includes -I./src -I./third_party +INCLUDES = -I./includes -I./runtime/Includes -I./third_party LDLIBS = @@ -71,6 +73,10 @@ ifeq ($(PROFILER), true) CXXFLAGS += -D PROFILER endif +ifeq ($(LEGACY), true) + CXXFLAGS += -D LEGACY +endif + RM = rm -rf $(OBJ_DIR)/%.o: %.cpp $(GCH) diff --git a/includes/mlx_profile.h b/includes/mlx_profile.h index 52cecfb..9dd8577 100644 --- a/includes/mlx_profile.h +++ b/includes/mlx_profile.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */ -/* Updated: 2024/01/03 15:33:35 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 18:25:59 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -138,6 +138,16 @@ #endif #endif +#if !defined(MLX_FORCEINLINE) + #if defined(MLX_COMPILER_CLANG) || defined(MLX_COMPILER_GCC) + #define MLX_FORCEINLINE __attribute__((always_inline)) inline + #elif defined(MLX_COMPILER_MSVC) + #define MLX_FORCEINLINE __forceinline + #else + #define MLX_FORCEINLINE inline + #endif +#endif + // Checking common assumptions #ifdef __cplusplus #include diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h new file mode 100644 index 0000000..cceb2db --- /dev/null +++ b/runtime/Includes/Core/Application.h @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Application.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ +/* Updated: 2024/03/27 21:00:53 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_APPLICATION__ +#define __MLX_APPLICATION__ + +#include +#include +#include + +namespace mlx +{ + class Application + { + public: + Application(); + + inline void GetMousePos(int* x, int* y) noexcept; + inline void MouseMove(void* win, int x, int y) noexcept; + + inline void OnEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept; + + inline void GetScreenSize(void* win, int* w, int* h) noexcept; + + inline void SetFPSCap(std::uint32_t fps) noexcept; + + inline void* NewGraphicsSuport(std::size_t w, std::size_t h, const char* title); + inline void ClearGraphicsSupport(void* win); + inline void DestroyGraphicsSupport(void* win); + + inline void PixelPut(void* win, int x, int y, std::uint32_t color) const noexcept; + inline void StringPut(void* win, int x, int y, std::uint32_t color, char* str); + + void* NewTexture(int w, int h); + void* NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...) + inline void TexturePut(void* win, void* img, int x, int y); + inline int GetTexturePixel(void* img, int x, int y); + inline void SetTexturePixel(void* img, int x, int y, std::uint32_t color); + void DestroyTexture(void* ptr); + + inline void LoopHook(int (*f)(void*), void* param); + inline void LoopEnd() noexcept; + + inline void LoadFont(void* win, const std::filesystem::path& filepath, float scale); + + void Run() noexcept; + + ~Application(); + + private: + FpsManager m_fps; + std::list m_textures; + std::vector> m_graphics; + std::function f_loop_hook; + std::unique_ptr p_in; + void* p_param = nullptr; + }; +} + +#include + +#endif // __MLX_APPLICATION__ diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl new file mode 100644 index 0000000..2457c5b --- /dev/null +++ b/runtime/Includes/Core/Application.inl @@ -0,0 +1,196 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* application.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ +/* Updated: 2023/04/02 14:56:27 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#define CHECK_WINDOW_PTR(win) \ + if(win == nullptr) \ + { \ + Error("invalid window ptr (NULL)"); \ + return; \ + } \ + else if(*static_cast(win) < 0 || *static_cast(win) > static_cast(_graphics.size()))\ + { \ + Error("invalid window ptr"); \ + return; \ + } else {} + +#define CHECK_IMAGE_PTR(img, retval) \ + if(img == nullptr) \ + { \ + Error("invalid image ptr (NULL)"); \ + retval; \ + } \ + else if(std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) \ + { \ + return &texture == img; \ + }) == _textures.end()) \ + { \ + Error(e_kind::error, "invalid image ptr"); \ + retval; \ + } else {} + +namespace mlx +{ + void Application::GetMousePos(int* x, int* y) noexcept + { + *x = p_in->GetX(); + *y = p_in->GetY(); + } + + void Application::MouseMove(void* win, int x, int y) noexcept + { + CHECK_WINDOW_PTR(win); + if(!m_graphics[*static_cast(win)]->HasWindow()) + { + Warning("trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)"); + return; + } + } + + void Application::OnEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept + { + CHECK_WINDOW_PTR(win); + if(!m_graphics[*static_cast(win)]->HasWindow()) + { + Warning("trying to add event hook for a window that is targeting an image and not a real window, this is not allowed (hook ignored)"); + return; + } + p_in->OnEvent(m_graphics[*static_cast(win)]->GetWindow()->GetID(), event, funct_ptr, param); + } + + void Application::GetScreenSize(void* win, int* w, int* h) noexcept + { + CHECK_WINDOW_PTR(win); + *w = 0; + *h = 0; + } + + void Application::SetFPSCap(std::uint32_t fps) noexcept + { + m_fps.SetMaxFPS(fps); + } + + void* Application::NewGraphicsSuport(std::size_t w, std::size_t h, const char* title) + { + MLX_PROFILE_FUNCTION(); + auto it = std::find_if(m_textures.begin(), m_textures.end(), [=](const Texture& texture) + { + return &texture == reinterpret_cast(const_cast(title)); + }); + if(it != _textures.end()) + m_graphics.emplace_back(std::make_unique(w, h, reinterpret_cast(const_cast(title)), m_graphics.size())); + else + { + if(title == NULL) + { + FatalError("invalid window title (NULL)"); + return nullptr; + } + m_graphics.emplace_back(std::make_unique(w, h, title, m_graphics.size())); + p_in->RegisterWindow(m_graphics.back()->GetWindow()); + } + return static_cast(&m_graphics.back()->GetID()); + } + + void Application::ClearGraphicsSupport(void* win) + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + m_graphics[*static_cast(win)]->ClearRenderData(); + } + + void Application::DestroyGraphicsSupport(void* win) + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + m_graphics[*static_cast(win)].reset(); + } + + void Application::PixelPut(void* win, int x, int y, std::uint32_t color) const noexcept + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + m_graphics[*static_cast(win)]->PixelPut(x, y, color); + } + + void Application::StringPut(void* win, int x, int y, std::uint32_t color, char* str) + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + if(str == nullptr) + { + Error("wrong text (NULL)"); + return; + } + if(std::strlen(str) == 0) + { + Warning("trying to put an empty text"); + return; + } + m_graphics[*static_cast(win)]->StringPut(x, y, color, str); + } + + void Application::LoadFont(void* win, const std::filesystem::path& filepath, float scale) + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + m_graphics[*static_cast(win)]->LoadFont(filepath, scale); + } + + void Application::TexturePut(void* win, void* img, int x, int y) + { + MLX_PROFILE_FUNCTION(); + CHECK_WINDOW_PTR(win); + CHECK_IMAGE_PTR(img, return); + NonOwningPtr texture = static_cast(img); + if(!texture->IsInit()) + Error("trying to put a texture that has been destroyed"); + else + m_graphics[*static_cast(win)]->TexturePut(texture, x, y); + } + + int Application::GetTexturePixel(void* img, int x, int y) + { + MLX_PROFILE_FUNCTION(); + CHECK_IMAGE_PTR(img, return 0); + NonOwningPtr texture = static_cast(img); + if(!texture->IsInit()) + { + Error("trying to get a pixel from texture that has been destroyed"); + return 0; + } + return texture->GetPixel(x, y); + } + + void Application::setTexturePixel(void* img, int x, int y, std::uint32_t color) + { + MLX_PROFILE_FUNCTION(); + CHECK_IMAGE_PTR(img, return); + NonOwningPtr texture = static_cast(img); + if(!texture->IsInit()) + Error("trying to set a pixel on texture that has been destroyed"); + else + texture->SetPixel(x, y, color); + } + + void Application::LoopHook(int (*f)(void*), void* param) + { + f_loop_hook = f; + p_param = param; + } + + void Application::LoopEnd() noexcept + { + p_in->Finish(); + } +} diff --git a/runtime/Includes/Core/Enums.h b/runtime/Includes/Core/Enums.h new file mode 100644 index 0000000..80ef797 --- /dev/null +++ b/runtime/Includes/Core/Enums.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Enums.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:15:24 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:16:03 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_CORE_ENUMS__ +#define __MLX_CORE_ENUMS__ + +#include + +namespace mlx +{ + enum class LogType + { + Message = 0, + Warning, + Error, + FatalError, + + EndEnum + }; + + constexpr std::size_t LogTypeCount = static_cast(LogType::EndEnum) + 1; +} + +#endif diff --git a/src/core/errors.h b/runtime/Includes/Core/EventBase.h similarity index 63% rename from src/core/errors.h rename to runtime/Includes/Core/EventBase.h index b740865..37b5944 100644 --- a/src/core/errors.h +++ b/runtime/Includes/Core/EventBase.h @@ -1,29 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* errors.h :+: :+: :+: */ +/* EventBase.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/04 17:42:32 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:13:19 by maldavid ### ########.fr */ +/* Created: 2024/03/27 17:27:22 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:31:16 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef __MLX_ERRORS__ -#define __MLX_ERRORS__ +#ifndef __MLX_BASE_EVENT__ +#define __MLX_BASE_EVENT__ -enum class e_kind +namespace mlx { - message, - warning, - error, - fatal_error -}; - -namespace mlx::core::error -{ - void report(e_kind kind, std::string msg, ...); + struct EventBase + { + virtual std::uint32_t What() const = 0; + }; } -#endif // __MLX_ERRORS__ +#endif diff --git a/runtime/Includes/Core/EventBus.h b/runtime/Includes/Core/EventBus.h new file mode 100644 index 0000000..db110d3 --- /dev/null +++ b/runtime/Includes/Core/EventBus.h @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* EventBus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:30:36 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:31:41 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_EVENT_BUS__ +#define __MLX_EVENT_BUS__ + +#include +#include + +namespace mlx +{ + class EventBus + { + public: + EventBus() = delete; + static void Send(const std::string& listener_name, const EventBase& event); + static void SendBroadcast(const EventBase& event); + inline static void RegisterListener(const EventListener& listener) { s_listeners.push_back(listener); } + ~EventBus() = delete; + + private: + inline static std::vector s_listeners; + }; +} + +#endif diff --git a/runtime/Includes/Core/EventListener.h b/runtime/Includes/Core/EventListener.h new file mode 100644 index 0000000..d6c002c --- /dev/null +++ b/runtime/Includes/Core/EventListener.h @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* EventListener.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:28:17 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:37:53 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_EVENT_LISTENER__ +#define __MLX_EVENT_LISTENER__ + +#include + +namespace mlx +{ + class EventListener + { + public: + EventListener() = delete; + EventListener(func::function functor, std::string name); + + inline const std::string& GetName() const { return m_name; } + inline void Call(const EventBase& event) const noexcept { m_listen_functor(event); } + + ~EventListener() = default; + + private: + func::function m_listen_functor; + std::string m_name; + }; +} + +#endif diff --git a/runtime/Includes/Core/Format.h b/runtime/Includes/Core/Format.h new file mode 100644 index 0000000..d55b824 --- /dev/null +++ b/runtime/Includes/Core/Format.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Format.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:11:09 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:12:03 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_FORMAT__ +#define __MLX_FORMAT__ + +#include +#include + +namespace mlx +{ + template + struct IsOstreamable : std::false_type {}; + + template + struct IsOstreamable() << std::declval())>> : std::true_type {}; + + template...>, int> = 0> + auto Format(std::string_view format, const Args&... args); +} + +#include + +#endif diff --git a/runtime/Includes/Core/Format.inl b/runtime/Includes/Core/Format.inl new file mode 100644 index 0000000..e6d94d2 --- /dev/null +++ b/runtime/Includes/Core/Format.inl @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Format.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:11:09 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:12:03 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +namespace mlx +{ + namespace Internal + { + template + void Format(std::stringstream& ss, It first, It last) + { + for(auto it = first; it != last; ++it) + { + switch(*it) + { + case '%': + throw std::invalid_argument{"too few arguments"}; + case '/': + ++it; + if(it == last) + throw std::invalid_argument{"stray '/'"}; + [[fallthrough]]; + + default: ss << *it; + } + } + } + + template + void Format(std::stringstream& ss, It first, It last, const T& arg, const Args&... args) + { + for(auto it = first; it != last; ++it) + { + switch(*it) + { + case '%': + ss << arg; + return Format(ss, ++it, last, args...); + case '/': + ++it; + if(it == last) + throw std::invalid_argument{"stray '/'"}; + [[fallthrough]]; + + default: ss << *it; + } + } + throw std::invalid_argument{"too many arguments"}; + } + + template + void Format(std::ostream& os, It first, It last) + { + for(auto it = first; it != last; ++it) + { + switch(*it) + { + case '%': + throw std::invalid_argument{"too few arguments"}; + case '/': + ++it; + if(it == last) + throw std::invalid_argument{"stray '/'"}; + [[fallthrough]]; + + default: os << *it; + } + } + } + + template + void Format(std::ostream& os, It first, It last, const T& arg, const Args&... args) + { + for(auto it = first; it != last; ++it) + { + switch(*it) + { + case '%': + os << arg; + return Format(os, ++it, last, args...); + case '/': + ++it; + if(it == last) + throw std::invalid_argument{"stray '/'"}; + [[fallthrough]]; + + default: os << *it; + } + } + throw std::invalid_argument{"too many arguments"}; + } + + template + struct Formatter + { + std::string_view format; + std::tuple args; + }; + + template + void FormatHelper(std::stringstream& ss, const Formatter& formatter, std::index_sequence) + { + Format(ss, formatter.format.begin(), formatter.format.end(), + std::get(formatter.args)...); + } + + template + std::stringstream& operator<<(std::stringstream& ss, const Formatter& printer) + { + FormatHelper(ss, printer, std::index_sequence_for{}); + return ss; + } + + template + void FormatHelper(std::ostream& os, const Formatter& formatter, std::index_sequence) + { + Format(os, formatter.format.begin(), formatter.format.end(), + std::get(formatter.args)...); + } + + template + std::ostream& operator<<(std::ostream& os, const Formatter& printer) + { + FormatHelper(os, printer, std::index_sequence_for{}); + return os; + } + } + + template...>, int>> + auto Format(std::string_view format, const Args&... args) + { + return Internal::Formatter{format, std::forward_as_tuple(args...)}; + } +} diff --git a/src/core/fps.h b/runtime/Includes/Core/Fps.h similarity index 65% rename from src/core/fps.h rename to runtime/Includes/Core/Fps.h index f0d6fbb..0803bed 100644 --- a/src/core/fps.h +++ b/runtime/Includes/Core/Fps.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* fps.h :+: :+: :+: */ +/* Fps.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/18 14:53:30 by maldavid #+# #+# */ -/* Updated: 2024/03/25 22:58:32 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 20:52:06 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,19 +20,19 @@ namespace mlx public: FpsManager() = default; - void init(); - bool update(); - inline void setMaxFPS(std::uint32_t fps) noexcept { _max_fps = fps; _ns = 1000000000.0 / fps; } + void Init(); + bool Update(); + inline void SetMaxFPS(std::uint32_t fps) noexcept { m_max_fps = fps; m_ns = 1000000000.0 / fps; } ~FpsManager() = default; private: - double _ns = 1000000000.0 / 1'337'000.0; - std::int64_t _fps_before = 0; - std::int64_t _fps_now = 0; - std::int64_t _timer = 0; - std::uint32_t _max_fps = 1'337'000; - std::uint32_t _fps_elapsed_time = 0; + double m_ns = 1000000000.0 / 1'337'000.0; + std::int64_t m_fps_before = 0; + std::int64_t m_fps_now = 0; + std::int64_t m_timer = 0; + std::uint32_t m_max_fps = 1'337'000; + std::uint32_t m_fps_elapsed_time = 0; }; } diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h new file mode 100644 index 0000000..9c60f82 --- /dev/null +++ b/runtime/Includes/Core/Graphics.h @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Graphics.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ +/* Updated: 2024/03/27 21:16:11 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_GRAPHICS__ +#define __MLX_GRAPHICS__ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mlx +{ + class GraphicsSupport : public NonCopyable + { + public: + GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr render_target, int id); + GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id); + + inline int& GetID() noexcept; + inline std::shared_ptr GetWindow(); + + void Render() noexcept; + + inline void ClearRenderData() noexcept; + inline void PixelPut(int x, int y, std::uint32_t color) noexcept; + inline void StringPut(int x, int y, std::uint32_t color, std::string str); + inline void TexturePut(NonOwningPtr texture, int x, int y); + inline void LoadFont(const std::filesystem::path& filepath, float scale); + inline void TryEraseTextureFromManager(NonOwningPtr texture) noexcept; + + inline bool HasWindow() const noexcept { return _has_window; } + + inline Renderer& GetRenderer() { return *_renderer; } + + ~GraphicsSupport(); + + private: + PixelPutPipeline m_pixel_put_pipeline; + + std::vector> m_drawlist; + + TextManager m_text_manager; + TextureManager m_texture_manager; + + glm::mat4 m_proj = glm::mat4(1.0); + + std::shared_ptr p_window; + std::unique_ptr p_renderer; + + std::size_t m_width = 0; + std::size_t m_height = 0; + + int m_id; + + bool m_has_window; + }; +} + +#include + +#endif diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl new file mode 100644 index 0000000..6e9771e --- /dev/null +++ b/runtime/Includes/Core/Graphics.inl @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ +/* Updated: 2023/04/02 15:26:16 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace mlx +{ + int& GraphicsSupport::GetID() noexcept { return m_id; } + std::shared_ptr GraphicsSupport::GetWindow() { return p_window; } + + void GraphicsSupport::ClearRenderData() noexcept + { + MLX_PROFILE_FUNCTION(); + m_drawlist.clear(); + m_pixel_put_pipeline.Clear(); + m_text_manager.Clear(); + m_texture_manager.Clear(); + } + + void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept + { + MLX_PROFILE_FUNCTION(); + m_pixel_put_pipeline.SetPixel(x, y, color); + } + + void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) + { + MLX_PROFILE_FUNCTION(); + std::pair, bool> res = m_text_manager.RegisterText(x, y, color, str); + if(!res.second) // if this is not a completly new text draw + { + auto it = std::find(m_drawlist.begin(), m_drawlist.end(), res.first); + if(it != m_drawlist.end()) + m_drawlist.erase(it); + } + m_drawlist.push_back(res.first); + } + + void GraphicsSupport::TexturePut(NonOwningPtr texture, int x, int y) + { + MLX_PROFILE_FUNCTION(); + auto res = m_texture_manager.RegisterTexture(texture, x, y); + if(!res.second) // if this is not a completly new texture draw + { + auto it = std::find(m_drawlist.begin(), m_drawlist.end(), res.first); + if(it != m_drawlist.end()) + m_drawlist.erase(it); + } + m_drawlist.push_back(res.first); + } + + void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale) + { + MLX_PROFILE_FUNCTION(); + m_text_manager.LoadFont(*_renderer, filepath, scale); + } + + void GraphicsSupport::TryEraseTextureFromManager(NonOwningPtr texture) noexcept + { + MLX_PROFILE_FUNCTION(); + for(auto it = m_drawlist.begin(); it != m_drawlist.end();) + { + if(m_texture_manager.IsTextureKnown(texture)) + it = m_drawlist.erase(it); + else + ++it; + } + m_texture_manager.EraseTextures(texture); + } +} diff --git a/runtime/Includes/Core/Logs.h b/runtime/Includes/Core/Logs.h new file mode 100644 index 0000000..edd816b --- /dev/null +++ b/runtime/Includes/Core/Logs.h @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Logs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:14:10 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:19:23 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_LOGS__ +#define __MLX_LOGS__ + +#include + +namespace mlx +{ + template + void DebugLog(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + template + void Error(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + template + void Warning(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + template + void Message(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + template + void FatalError(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + template + void Verify(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + + class Logs + { + public: + Logs() = delete; + + static void Report(LogType type, std::string message); + static void Report(LogType type, unsigned int line, std::string_view file, std::string_view function, std::string message); + + ~Logs() = delete; + }; + + #if defined(DEBUG) + template + void Assert(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); + #else + template + void Assert(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) {} + #endif +} + +#include + +namespace mlx +{ + #undef DebugLog + #define DebugLog(...) DebugLog(__LINE__, __FILE__, AK_FUNC_SIG, __VA_ARGS__) + + #undef Message + #define Message(...) Message(__LINE__, __FILE__, __func__, __VA_ARGS__) + + #undef Warning + #define Warning(...) Warning(__LINE__, __FILE__, __func__, __VA_ARGS__) + + #undef Error + #define Error(...) Error(__LINE__, __FILE__, __func__, __VA_ARGS__) + + #undef FatalError + #define FatalError(...) FatalError(__LINE__, __FILE__, __func__, __VA_ARGS__) + + #undef Verify + #define Verify(cond, ...) Verify(cond, __LINE__, __FILE__, __func__, __VA_ARGS__) + + #undef Assert + #define Assert(cond, ...) Assert(cond, __LINE__, __FILE__, __func__, __VA_ARGS__) +} + +#endif diff --git a/runtime/Includes/Core/Logs.inl b/runtime/Includes/Core/Logs.inl new file mode 100644 index 0000000..2c33f55 --- /dev/null +++ b/runtime/Includes/Core/Logs.inl @@ -0,0 +1,135 @@ +/* **************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Logs.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:19:47 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:19:47 by maldavid ### ########.fr */ +/* */ +/* **************************************************************************** */ + +#include +#include + +namespace mlx +{ + template + void DebugLog(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + try + { + std::stringstream ss; + ss << Format(message, args...); + Logs::Report(LogType::Debug, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + template + void Error(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + try + { + std::stringstream ss; + ss << Format(message, args...); + Logs::Report(LogType::Error, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + template + void Warning(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + try + { + std::stringstream ss; + ss << Format(message, args...); + Logs::Report(LogType::Warning, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + template + void Message(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + try + { + std::stringstream ss; + ss << Format(message, args...); + Logs::Report(LogType::Message, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + template + void FatalError(unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + try + { + std::stringstream ss; + ss << Format(message, args...); + Logs::Report(LogType::FatalError, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + template + void Verify(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + if(cond) + return; + try + { + std::stringstream ss; + ss << Format("Verification failed : %", message, args...); + Logs::Report(LogType::FatalError, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + + #if defined(DEBUG) + template + void Assert(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) + { + using namespace std::literals; + if(cond) + return; + try + { + std::stringstream ss; + ss << Format("Assertion failed : %", message, args...); + Logs::Report(LogType::FatalError, line, file, function, ss.str()); + } + catch(const std::exception& e) + { + Logs::Report(LogType::Error, "formatter exception catched in the log printer : "s + e.what()); + } + } + #endif +} diff --git a/src/core/memory.h b/runtime/Includes/Core/Memory.h similarity index 72% rename from src/core/memory.h rename to runtime/Includes/Core/Memory.h index 1e08e1b..db11105 100644 --- a/src/core/memory.h +++ b/runtime/Includes/Core/Memory.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* memory.h :+: :+: :+: */ +/* Memory.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */ -/* Updated: 2024/03/25 19:13:05 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:16:44 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_MEMORY__ #define __MLX_MEMORY__ -#include +#include namespace mlx { @@ -22,17 +22,17 @@ namespace mlx friend class Singleton; public: - static void* malloc(std::size_t size); - static void* calloc(std::size_t n, std::size_t size); - static void* realloc(void* ptr, std::size_t size); - static void free(void* ptr); + static void* Malloc(std::size_t size); + static void* Calloc(std::size_t n, std::size_t size); + static void* Realloc(void* ptr, std::size_t size); + static void Free(void* ptr); private: MemManager() = default; ~MemManager(); private: - inline static std::list _blocks; + inline static std::list s_blocks; }; } diff --git a/src/core/profiler.h b/runtime/Includes/Core/Profiler.h similarity index 64% rename from src/core/profiler.h rename to runtime/Includes/Core/Profiler.h index e5f5c41..dbd6b1f 100644 --- a/src/core/profiler.h +++ b/runtime/Includes/Core/Profiler.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* profiler.h :+: :+: :+: */ +/* Profiler.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 13:35:45 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:57 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:19:01 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_PROFILER__ #define __MLX_PROFILER__ -#include +#include namespace mlx { @@ -34,63 +34,63 @@ namespace mlx Profiler(const Profiler&) = delete; Profiler(Profiler&&) = delete; - void appendProfileData(ProfileResult&& result); + void AppendProfileData(ProfileResult&& result); private: - Profiler() { beginRuntimeSession(); } + Profiler() { BeginRuntimeSession(); } ~Profiler(); - void beginRuntimeSession(); - void writeProfile(const ProfileResult& result); - void endRuntimeSession(); - inline void writeHeader() + void BeginRuntimeSession(); + void WriteProfile(const ProfileResult& result); + void EndRuntimeSession(); + inline void WriteHeader() { - _output_stream << "{\"profileData\":[{}"; - _output_stream.flush(); + m_output_stream << "{\"profileData\":[{}"; + m_output_stream.flush(); } - inline void writeFooter() + inline void WriteFooter() { - _output_stream << "]}"; - _output_stream.flush(); + m_output_stream << "]}"; + m_output_stream.flush(); } private: - std::unordered_map> _profile_data; - std::ofstream _output_stream; - std::mutex _mutex; - bool _runtime_session_began = false; + std::unordered_map> m_profile_data; + std::ofstream m_output_stream; + std::mutex m_mutex; + bool m_runtime_session_began = false; }; class ProfilerTimer { public: - ProfilerTimer(const char* name) : _name(name) + ProfilerTimer(const char* name) : m_name(name) { - _start_timepoint = std::chrono::steady_clock::now(); + m_start_timepoint = std::chrono::steady_clock::now(); } - inline void stop() + inline void Stop() { auto end_timepoint = std::chrono::steady_clock::now(); - auto high_res_start = FloatingPointMilliseconds{ _start_timepoint.time_since_epoch() }; - auto elapsed_time = std::chrono::time_point_cast(end_timepoint).time_since_epoch() - std::chrono::time_point_cast(_start_timepoint).time_since_epoch(); + auto high_res_start = FloatingPointMilliseconds{ m_start_timepoint.time_since_epoch() }; + auto elapsed_time = std::chrono::time_point_cast(end_timepoint).time_since_epoch() - std::chrono::time_point_cast(m_start_timepoint).time_since_epoch(); - Profiler::get().appendProfileData({ _name, elapsed_time, std::this_thread::get_id() }); + Profiler::get().appendProfileData({ m_name, elapsed_time, std::this_thread::get_id() }); - _stopped = true; + m_stopped = true; } ~ProfilerTimer() { - if(!_stopped) + if(!m_stopped) stop(); } private: - std::chrono::time_point _start_timepoint; - const char* _name; - bool _stopped = false; + std::chrono::time_point m_start_timepoint; + const char* m_name; + bool m_stopped = false; }; namespace ProfilerUtils @@ -102,7 +102,7 @@ namespace mlx }; template - constexpr auto cleanupOutputString(const char(&expr)[N], const char(&remove)[K]) + constexpr auto CleanupOutputString(const char(&expr)[N], const char(&remove)[K]) { ChangeResult result = {}; @@ -124,8 +124,8 @@ namespace mlx } #ifdef PROFILER - #define MLX_PROFILE_SCOPE_LINE2(name, line) constexpr auto fixedName##line = ::mlx::ProfilerUtils::cleanupOutputString(name, "__cdecl ");\ - ::mlx::ProfilerTimer timer##line(fixedName##line.data) + #define MLX_PROFILE_SCOPE_LINE2(name, line) constexpr auto fixed_name_##line = ::mlx::ProfilerUtils::CleanupOutputString(name, "__cdecl ");\ + ::mlx::ProfilerTimer timer##line(fixed_name_##line.data) #define MLX_PROFILE_SCOPE_LINE(name, line) MLX_PROFILE_SCOPE_LINE2(name, line) #define MLX_PROFILE_SCOPE(name) MLX_PROFILE_SCOPE_LINE(name, __LINE__) #define MLX_PROFILE_FUNCTION() MLX_PROFILE_SCOPE(MLX_FUNC_SIG) diff --git a/src/core/UUID.h b/runtime/Includes/Core/UUID.h similarity index 86% rename from src/core/UUID.h rename to runtime/Includes/Core/UUID.h index ad0507e..0b4075f 100644 --- a/src/core/UUID.h +++ b/runtime/Includes/Core/UUID.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/06 11:13:23 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:53 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:19:18 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ namespace mlx UUID(); UUID(std::uint64_t uuid); - inline operator std::uint64_t() const { return _uuid; } + inline operator std::uint64_t() const { return m_uuid; } private: - std::uint64_t _uuid; + std::uint64_t m_uuid; }; } diff --git a/runtime/Includes/Drivers/GLFW/GLFWInputs.h b/runtime/Includes/Drivers/GLFW/GLFWInputs.h new file mode 100644 index 0000000..0901ec9 --- /dev/null +++ b/runtime/Includes/Drivers/GLFW/GLFWInputs.h @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* GLFWInputs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 18:32:29 by maldavid #+# #+# */ +/* Updated: 2024/03/27 18:37:58 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_GLFW_INPUTS__ +#define __MLX_GLFW_INPUTS__ + +#include + +namespace mlx +{ + class GLFWInputs : public Inputs + { + public: + GLFWInputs() = default; + + void Update() override noexcept; + + void RegisterWindow(std::shared_ptr window) override; + + inline std::int32_t GetX() override const noexcept { return m_x; } + inline std::int32_t GetY() override const noexcept { return m_y; } + inline std::int32_t GetXRel() override const noexcept { return m_x_rel; } + inline std::int32_t GetYRel() override const noexcept { return m_y_rel; } + + ~GLFWInputs() override = default; + + private: + std::int32_t m_x = 0; + std::int32_t m_y = 0; + std::int32_t m_x_rel = 0; + std::int32_t m_y_rel = 0; + }; +} + +#endif diff --git a/runtime/Includes/Platform/Inputs.h b/runtime/Includes/Platform/Inputs.h new file mode 100644 index 0000000..aa8ad0f --- /dev/null +++ b/runtime/Includes/Platform/Inputs.h @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Inputs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ +/* Updated: 2024/03/27 18:36:21 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_INPUTS__ +#define __MLX_INPUTS__ + +#include +#include + +namespace mlx +{ + class Inputs + { + public: + struct Hook + { + func::function hook; + void* param = nullptr; + }; + + public: + Inputs() = default; + + virtual void Update() noexcept = 0; + + virtual void RegisterWindow(std::shared_ptr window) = 0; + + virtual std::int32_t GetX() const noexcept = 0; + virtual std::int32_t GetY() const noexcept = 0; + virtual std::int32_t GetXRel() const noexcept = 0; + virtual std::int32_t GetYRel() const noexcept = 0; + + inline bool IsMouseMoving() const noexcept { return GetXRel() || GetYRel(); } + MLX_FORCEINLINE bool IsRunning() const noexcept { return m_run; } + MLX_FORCEINLINE constexpr void Finish() noexcept { m_run = false; } + MLX_FORCEINLINE constexpr void Run() noexcept { m_run = true; } + + inline void OnEvent(std::uint32_t id, int event, int (*funct_ptr)(int, void*), void* param) noexcept + { + m_events_hooks[id][event].hook = funct_ptr; + m_events_hooks[id][event].param = param; + } + + virtual ~Inputs() = default; + + protected: + std::unordered_map> m_windows; + std::unordered_map> m_events_hooks; + bool m_run = false; + }; +} + +#endif diff --git a/src/platform/window.h b/runtime/Includes/Platform/Window.h similarity index 62% rename from src/platform/window.h rename to runtime/Includes/Platform/Window.h index 09202a0..d9e15a1 100644 --- a/src/platform/window.h +++ b/runtime/Includes/Platform/Window.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* window.h :+: :+: :+: */ +/* Window.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ -/* Updated: 2024/03/26 23:03:50 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:58:18 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,26 +15,30 @@ namespace mlx { + #ifdef LEGACY + using WindowHandle = SDL_Window; + #else + using WindowHandle = GLFWwindow; + #endif + class Window { public: Window(std::size_t w, std::size_t h, const std::string& title); - inline GLFWwindow* getNativeWindow() const noexcept { return _win; } - inline int getWidth() const noexcept { return _width; } - inline int getHeight() const noexcept { return _height; } - inline std::uint32_t getID() const noexcept { return _id; } + inline NonOwningPtr GetWindowHandle() const noexcept = 0; + inline int GetWidth() const noexcept { return m_width; } + inline int GetHeight() const noexcept { return m_height; } + inline std::uint32_t GetID() const noexcept { return m_id; } - void destroy() noexcept; + void Destroy() noexcept; ~Window() = default; private: - GLFWimage _icon; - GLFWwindow* _win = nullptr; - int _width = 0; - int _height = 0; - std::uint32_t _id = -1; + std::uint32_t m_id = -1; + int m_width = 0; + int m_height = 0; }; } diff --git a/src/pre_compiled.h b/runtime/Includes/PreCompiled.h similarity index 86% rename from src/pre_compiled.h rename to runtime/Includes/PreCompiled.h index 313cd6f..dd0f386 100644 --- a/src/pre_compiled.h +++ b/runtime/Includes/PreCompiled.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* pre_compiled.h :+: :+: :+: */ +/* PreCompiled.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/25 17:37:23 by maldavid #+# #+# */ -/* Updated: 2024/03/27 00:39:13 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:33:47 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,14 @@ #include #include #include -#include + +#ifdef LEGACY + #include + #include +#else + #include +#endif + #include #include #include @@ -74,4 +81,9 @@ #undef Window +#include +#include +#include +#include + #endif diff --git a/runtime/Includes/Renderer/Buffers/Buffer.h b/runtime/Includes/Renderer/Buffers/Buffer.h new file mode 100644 index 0000000..5acf9de --- /dev/null +++ b/runtime/Includes/Renderer/Buffers/Buffer.h @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Buffer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:09:07 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_VK_BUFFER__ +#define __MLX_VK_BUFFER__ + +#include +#include +#include + +namespace mlx +{ + class Buffer : public CommandResource + { + public: + Buffer() = default; + + void Create(BufferType type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr); + void Destroy() noexcept; + + inline void MapMem(void** data) noexcept { Render_Core::get().getAllocator().mapMemory(m_allocation, data); m_is_mapped = true; } + inline bool IsMapped() const noexcept { return m_is_mapped; } + inline void UnmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(m_allocation); m_is_mapped = false; } + + void Flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0); + bool CopyFromBuffer(const Buffer& buffer) noexcept; + + inline VkBuffer& operator()() noexcept { return m_buffer; } + inline VkBuffer& Get() noexcept { return m_buffer; } + inline VkDeviceSize GetSize() const noexcept { return m_size; } + inline VkDeviceSize GetOffset() const noexcept { return m_offset; } + + ~Buffer() = default; + + protected: + void PushToGPU() noexcept; + void Swap(Buffer& buffer) noexcept; + + protected: + VmaAllocation m_allocation; + VkBuffer m_buffer = VK_NULL_HANDLE; + VkDeviceSize m_offset = 0; + VkDeviceSize m_size = 0; + + private: + void CreateBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name); + + private: + #ifdef DEBUG + std::string m_name; + #endif + VkBufferUsageFlags m_usage = 0; + bool m_is_mapped = false; + }; +} + +#endif diff --git a/src/renderer/buffers/vk_ibo.h b/runtime/Includes/Renderer/Buffers/IndexBuffer.h similarity index 64% rename from src/renderer/buffers/vk_ibo.h rename to runtime/Includes/Renderer/Buffers/IndexBuffer.h index 580fa2e..20b6132 100644 --- a/src/renderer/buffers/vk_ibo.h +++ b/runtime/Includes/Renderer/Buffers/IndexBuffer.h @@ -1,28 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_ibo.h :+: :+: :+: */ +/* IndexBuffer.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:35 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:11:57 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __VK_IBO__ #define __VK_IBO__ -#include "vk_buffer.h" -#include +#include +#include namespace mlx { class C_IBO : public Buffer { public: - inline void create(std::uint32_t size, const std::uint16_t* data, const char* name) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, name, data); } - inline void bind(Renderer& renderer) noexcept { renderer.getActiveCmdBuffer().bindIndexBuffer(*this); } + inline void Create(std::uint32_t size, const std::uint16_t* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, name, data); } + inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindIndexBuffer(*this); } }; } diff --git a/runtime/Includes/Renderer/Buffers/UniformBuffer.h b/runtime/Includes/Renderer/Buffers/UniformBuffer.h new file mode 100644 index 0000000..77b1f3a --- /dev/null +++ b/runtime/Includes/Renderer/Buffers/UniformBuffer.h @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* UniformBuffer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:15:23 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_VK_UBO__ +#define __MLX_VK_UBO__ + +#include + +namespace mlx +{ + class UniformBuffer + { + public: + UniformBuffer() = default; + + void Create(NonOwningPtr renderer, std::uint32_t size, const char* name); + void Destroy() noexcept; + + void SetData(std::uint32_t size, const void* data); + + VkDeviceSize GetSize() noexcept; + VkDeviceSize GetOffset() noexcept; + VkDeviceMemory GetDeviceMemory() noexcept; + VkBuffer& operator()() noexcept; + VkBuffer& Get() noexcept; + + inline VkDeviceSize GetSize(int i) noexcept { return m_buffers[i].getSize(); } + inline VkDeviceSize GetOffset(int i) noexcept { return m_buffers[i].getOffset(); } + inline VkBuffer& operator()(int i) noexcept { return m_buffers[i].get(); } + inline VkBuffer& Get(int i) noexcept { return m_buffers[i].get(); } + + ~UniformBuffer() = default; + + private: + std::array m_buffers; + std::array m_maps; + NonOwningPtr m_renderer; + }; +} + +#endif // __MLX_VK_UBO__ diff --git a/runtime/Includes/Renderer/Buffers/VertexBuffer.h b/runtime/Includes/Renderer/Buffers/VertexBuffer.h new file mode 100644 index 0000000..10d7c07 --- /dev/null +++ b/runtime/Includes/Renderer/Buffers/VertexBuffer.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* VertexBuffer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:18:23 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_VK_VBO__ +#define __MLX_VK_VBO__ + +#include +#include +#include + +namespace mlx +{ + class VertexBuffer : public Buffer + { + public: + inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::HighDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } + void SetData(std::uint32_t size, const void* data); + inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); } + }; + + class DeviceVertexBuffer : public Buffer + { + public: + inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::LowDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } + void SetData(std::uint32_t size, const void* data); + inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); } + }; + + class ConstantVertexBuffer : public Buffer + { + public: + inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } + inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); } + }; +} + +#endif // __MLX_VK_VBO__ diff --git a/runtime/Includes/Renderer/Command/CommandBuffer.h b/runtime/Includes/Renderer/Command/CommandBuffer.h new file mode 100644 index 0000000..68c068f --- /dev/null +++ b/runtime/Includes/Renderer/Command/CommandBuffer.h @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* CommandBuffer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:44:58 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_VK_CMD_BUFFER__ +#define __MLX_VK_CMD_BUFFER__ + +#include +#include + +namespace mlx +{ + class Buffer; + class Image; + + class CommandBuffer + { + public: + void Init(CommandBufferType type, NonOwningPtr manager); + void Init(CommandBufferType type, NonOwningPtr pool); + void Destroy() noexcept; + + void BeginRecord(VkCommandBufferUsageFlags usage = 0); + void Submit(class Semaphore* semaphores) noexcept; + void SubmitIdle(bool shouldWaitForExecution = true) noexcept; // TODO : handle `shouldWaitForExecution` as false by default (needs to modify CmdResources lifetimes to do so) + void UpdateSubmitState() noexcept; + inline void WaitForExecution() noexcept { m_fence.wait(); UpdateSubmitState(); m_state = CommandBufferState::Ready; } + inline void Reset() noexcept { vkResetCommandBuffer(m_cmd_buffer, 0); } + void EndRecord(); + + void BindVertexBuffer(Buffer& buffer) noexcept; + void BindIndexBuffer(Buffer& buffer) noexcept; + void CopyBuffer(Buffer& dst, Buffer& src) noexcept; + void CopyBufferToImage(Buffer& buffer, Image& image) noexcept; + void CopyImagetoBuffer(Image& image, Buffer& buffer) noexcept; + void TransitionImageLayout(Image& image, VkImageLayout new_layout) noexcept; + + inline bool IsInit() const noexcept { return m_state != CommandBufferState::Uninit; } + inline bool IsReadyToBeUsed() const noexcept { return m_state == CommandBufferState::Ready; } + inline bool IsRecording() const noexcept { return m_state == CommandBufferState::Recording; } + inline bool HasBeenSubmitted() const noexcept { return m_state == CommandBufferState::Submitted; } + inline CommandBufferState GetCurrentState() const noexcept { return m_state; } + + inline VkCommandBuffer& operator()() noexcept { return m_cmd_buffer; } + inline VkCommandBuffer& Get() noexcept { return m_cmd_buffer; } + inline Fence& GetFence() noexcept { return m_fence; } + + private: + void PreTransferBarrier() noexcept; + void PostTransferBarrier() noexcept; + + private: + std::vector m_cmd_resources; + Fence m_fence; + VkCommandBuffer m_cmd_buffer = VK_NULL_HANDLE; + NonOwningPtr m_pool; + CommandBufferState m_state = CommandBufferState::Uninit; + CommandBufferType m_type; + }; +} + +#endif // __MLX_VK_CMD_BUFFER__ diff --git a/src/renderer/command/cmd_manager.h b/runtime/Includes/Renderer/Command/CommandManager.h similarity index 55% rename from src/renderer/command/cmd_manager.h rename to runtime/Includes/Renderer/Command/CommandManager.h index abac258..c0dcedc 100644 --- a/src/renderer/command/cmd_manager.h +++ b/runtime/Includes/Renderer/Command/CommandManager.h @@ -1,42 +1,42 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* cmd_manager.h :+: :+: :+: */ +/* CommandManager.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 17:48:52 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:23 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:20:53 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_COMMAND_MANAGER__ #define __MLX_COMMAND_MANAGER__ -#include -#include -#include +#include +#include +#include namespace mlx { - class CmdManager + class CommandManager { public: - CmdManager() = default; + CommandManager() = default; - void init() noexcept; - void beginRecord(int active_image_index); - void endRecord(int active_image_index); - void destroy() noexcept; + void Init() noexcept; + void BeginRecord(int active_image_index); + void EndRecord(int active_image_index); + void Destroy() noexcept; - inline CmdPool& getCmdPool() noexcept { return _cmd_pool; } - inline CmdBuffer& getCmdBuffer(int i) noexcept { return _cmd_buffers[i]; } + inline CommandPool& GetCmdPool() noexcept { return m_cmd_pool; } + inline CommandBuffer& GetCmdBuffer(int i) noexcept { return m_cmd_buffers[i]; } - ~CmdManager() = default; + ~CommandManager() = default; private: - std::array _cmd_buffers; - CmdPool _cmd_pool; + std::array m_cmd_buffers; + CommandPool m_cmd_pool; }; } diff --git a/src/renderer/command/vk_cmd_pool.h b/runtime/Includes/Renderer/Command/CommandPool.h similarity index 67% rename from src/renderer/command/vk_cmd_pool.h rename to runtime/Includes/Renderer/Command/CommandPool.h index 3958739..edb8f22 100644 --- a/src/renderer/command/vk_cmd_pool.h +++ b/runtime/Includes/Renderer/Command/CommandPool.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_cmd_pool.h :+: :+: :+: */ +/* CommandPool.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:24:12 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:14 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:33:15 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,17 +15,21 @@ namespace mlx { - class CmdPool + class CommandPool { public: - void init(); - void destroy() noexcept; + CommandPool() = default; - inline VkCommandPool& operator()() noexcept { return _cmd_pool; } - inline VkCommandPool& get() noexcept { return _cmd_pool; } + void Init(); + void Destroy() noexcept; + + inline VkCommandPool& operator()() noexcept { return m_cmd_pool; } + inline VkCommandPool& Get() noexcept { return m_cmd_pool; } + + ~CommandPool() = default; private: - VkCommandPool _cmd_pool = VK_NULL_HANDLE; + VkCommandPool m_cmd_pool = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/cmd_resource.h b/runtime/Includes/Renderer/Command/CommandResource.h similarity index 60% rename from src/renderer/core/cmd_resource.h rename to runtime/Includes/Renderer/Command/CommandResource.h index 47ec17e..e9c367e 100644 --- a/src/renderer/core/cmd_resource.h +++ b/runtime/Includes/Renderer/Command/CommandResource.h @@ -1,42 +1,37 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* cmd_resource.h :+: :+: :+: */ +/* CommandResource.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 20:44:29 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:08 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:37:06 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_COMMAND_RESOURCE__ #define __MLX_COMMAND_RESOURCE__ -#include +#include +#include namespace mlx { - class CmdResource + class CommandResource { friend class SingleTimeCmdManager; - public: - enum class state - { - in_cmd_buffer = 0, - out_cmd_buffer, - }; public: - CmdResource() : _uuid() {} - inline void recordedInCmdBuffer() noexcept { _state = state::in_cmd_buffer; } - inline void removedFromCmdBuffer() noexcept { _state = state::out_cmd_buffer; } - inline UUID getUUID() const noexcept { return _uuid; } - virtual ~CmdResource() = default; + CommandResource() : m_uuid() {} + inline void RecordedInCmdBuffer() noexcept { m_state = CommandResourceState::Held; } + inline void RemovedFromCmdBuffer() noexcept { m_state = CommandResourceState::Free; } + inline UUID GetUUID() const noexcept { return m_uuid; } + virtual ~CommandResource() = default; private: - UUID _uuid; - state _state = state::out_cmd_buffer; + UUID m_uuid; + CommandResourceState m_state = CommandResourceState::Free; }; } diff --git a/src/renderer/command/single_time_cmd_manager.h b/runtime/Includes/Renderer/Command/SingleTimeCmdManager.h similarity index 64% rename from src/renderer/command/single_time_cmd_manager.h rename to runtime/Includes/Renderer/Command/SingleTimeCmdManager.h index 1432ce7..be7f896 100644 --- a/src/renderer/command/single_time_cmd_manager.h +++ b/runtime/Includes/Renderer/Command/SingleTimeCmdManager.h @@ -1,48 +1,48 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* single_time_cmd_manager.h :+: :+: :+: */ +/* SingleTimeCmdManager.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/15 18:25:57 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:20 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:46:48 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_SINGLE_TIME_CMD_MANAGER__ #define __MLX_SINGLE_TIME_CMD_MANAGER__ -#include -#include +#include +#include namespace mlx { - class CmdBuffer; + class CommandBuffer; class SingleTimeCmdManager { - friend class Render_Core; + friend class RenderCore; public: SingleTimeCmdManager() = default; - void init() noexcept; - void destroy() noexcept; + void Init() noexcept; + void Destroy() noexcept; - void updateSingleTimesCmdBuffersSubmitState() noexcept; - void waitForAllExecutions() noexcept; + void UpdateSingleTimesCmdBuffersSubmitState() noexcept; + void WaitForAllExecutions() noexcept; - inline CmdPool& getCmdPool() noexcept { return _pool; } - CmdBuffer& getCmdBuffer() noexcept; + inline CommandPool& GetCmdPool() noexcept { return m_pool; } + CommanddBuffer& GetCmdBuffer() noexcept; ~SingleTimeCmdManager() = default; inline static constexpr const std::uint8_t BASE_POOL_SIZE = 16; private: - std::vector _buffers; - CmdPool _pool; + std::vector m_buffers; + CommanddPool m_pool; }; } diff --git a/src/renderer/core/vk_device.h b/runtime/Includes/Renderer/Core/Device.h similarity index 60% rename from src/renderer/core/vk_device.h rename to runtime/Includes/Renderer/Core/Device.h index 564fba1..c46aaf6 100644 --- a/src/renderer/core/vk_device.h +++ b/runtime/Includes/Renderer/Core/Device.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_device.h :+: :+: :+: */ +/* Device.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */ -/* Updated: 2024/03/25 22:31:46 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:47:21 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,22 +18,22 @@ namespace mlx class Device { public: - void init(); - void destroy() noexcept; + void Init(); + void Destroy() noexcept; - inline VkDevice& operator()() noexcept { return _device; } - inline VkDevice& get() noexcept { return _device; } + inline VkDevice& operator()() noexcept { return m_device; } + inline VkDevice& Get() noexcept { return m_device; } - inline VkPhysicalDevice& getPhysicalDevice() noexcept { return _physical_device; } + inline VkPhysicalDevice& GetPhysicalDevice() noexcept { return m_physical_device; } private: - void pickPhysicalDevice(); - bool checkDeviceExtensionSupport(VkPhysicalDevice device); - int deviceScore(VkPhysicalDevice device); + void PickPhysicalDevice(); + bool CheckDeviceExtensionSupport(VkPhysicalDevice device); + int DeviceScore(VkPhysicalDevice device); private: - VkPhysicalDevice _physical_device = VK_NULL_HANDLE; - VkDevice _device = VK_NULL_HANDLE; + VkPhysicalDevice m_physical_device = VK_NULL_HANDLE; + VkDevice m_device = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/drawable_resource.h b/runtime/Includes/Renderer/Core/DrawableResource.h similarity index 81% rename from src/renderer/core/drawable_resource.h rename to runtime/Includes/Renderer/Core/DrawableResource.h index 6dbf67c..fb60df9 100644 --- a/src/renderer/core/drawable_resource.h +++ b/runtime/Includes/Renderer/Core/DrawableResource.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* drawable_resource.h :+: :+: :+: */ +/* DrawableResource.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 21:00:37 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:05 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:47:32 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ namespace mlx { public: DrawableResource() = default; - virtual void render(std::array& sets, class Renderer& renderer) = 0; - virtual void resetUpdate() {} + virtual void Render(std::array& sets, class Renderer& renderer) = 0; + virtual void ResetUpdate() {} virtual ~DrawableResource() = default; }; } diff --git a/src/renderer/core/vk_fence.h b/runtime/Includes/Renderer/Core/Fence.h similarity index 69% rename from src/renderer/core/vk_fence.h rename to runtime/Includes/Renderer/Core/Fence.h index dd4a5d0..ea46aae 100644 --- a/src/renderer/core/vk_fence.h +++ b/runtime/Includes/Renderer/Core/Fence.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_fence.h :+: :+: :+: */ +/* Fence.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 17:52:09 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:11:53 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:48:31 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,20 +20,20 @@ namespace mlx public: Fence() = default; - void init(); + void Init(); - inline VkFence& get() noexcept { return _fence; } - void wait() noexcept; - void reset() noexcept; - bool isReady() const noexcept; - inline void waitAndReset() noexcept { wait(); reset(); } + inline VkFence& Get() noexcept { return m_fence; } + void Wait() noexcept; + void Reset() noexcept; + bool IsReady() const noexcept; + MLX_FORCEINLINE void WaitAndReset() noexcept { Wait(); Reset(); } - void destroy() noexcept; + void Destroy() noexcept; ~Fence() = default; private: - VkFence _fence = VK_NULL_HANDLE; + VkFence m_fence = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/vk_instance.h b/runtime/Includes/Renderer/Core/Instance.h similarity index 69% rename from src/renderer/core/vk_instance.h rename to runtime/Includes/Renderer/Core/Instance.h index da52679..9b33f3e 100644 --- a/src/renderer/core/vk_instance.h +++ b/runtime/Includes/Renderer/Core/Instance.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_instance.h :+: :+: :+: */ +/* Instance.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 19:03:04 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:11:50 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:48:55 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,17 @@ namespace mlx class Instance { public: - void init(); - void destroy() noexcept; + void Init(); + void Destroy() noexcept; - inline VkInstance& operator()() noexcept { return _instance; } - inline VkInstance& get() noexcept { return _instance; } + inline VkInstance& operator()() noexcept { return m_instance; } + inline VkInstance& Get() noexcept { return m_instance; } private: - std::vector getRequiredExtensions(); - VkInstance _instance = VK_NULL_HANDLE; + std::vector GetRequiredExtensions(); + + private: + VkInstance m_instance = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/memory.h b/runtime/Includes/Renderer/Core/Memory.h similarity index 60% rename from src/renderer/core/memory.h rename to runtime/Includes/Renderer/Core/Memory.h index 9703213..a29be0b 100644 --- a/src/renderer/core/memory.h +++ b/runtime/Includes/Renderer/Core/Memory.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* memory.h :+: :+: :+: */ +/* Memory.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:02 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:49:57 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,28 +20,28 @@ namespace mlx public: GPUallocator() = default; - void init() noexcept; - void destroy() noexcept; + void Init() noexcept; + void Destroy() noexcept; - VmaAllocation createBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name = nullptr) noexcept; - void destroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept; + VmaAllocation CreateBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name = nullptr) noexcept; + void DestroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept; - VmaAllocation createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name = nullptr) noexcept; - void destroyImage(VmaAllocation allocation, VkImage image) noexcept; + VmaAllocation CreateImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name = nullptr) noexcept; + void DestroyImage(VmaAllocation allocation, VkImage image) noexcept; - void mapMemory(VmaAllocation allocation, void** data) noexcept; - void unmapMemory(VmaAllocation allocation) noexcept; + void MapMemory(VmaAllocation allocation, void** data) noexcept; + void UnmapMemory(VmaAllocation allocation) noexcept; - void dumpMemoryToJson(); + void DumpMemoryToJson(); - void flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept; + void Flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept; ~GPUallocator() = default; private: - VmaAllocator _allocator; - std::int32_t _active_buffers_allocations = 0; - std::int32_t _active_images_allocations = 0; + VmaAllocator m_allocator; + std::int32_t m_active_buffers_allocations = 0; + std::int32_t m_active_images_allocations = 0; }; } diff --git a/src/renderer/core/vk_queues.h b/runtime/Includes/Renderer/Core/Queues.h similarity index 64% rename from src/renderer/core/vk_queues.h rename to runtime/Includes/Renderer/Core/Queues.h index 4895c9c..4230e25 100644 --- a/src/renderer/core/vk_queues.h +++ b/runtime/Includes/Renderer/Core/Queues.h @@ -1,20 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_queues.h :+: :+: :+: */ +/* Queues.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */ -/* Updated: 2024/03/25 22:29:26 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:50:52 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_VK_QUEUES__ #define __MLX_VK_QUEUES__ -#include - namespace mlx { class Queues @@ -28,24 +26,25 @@ namespace mlx inline bool isComplete() { return graphics_family.has_value() && present_family.has_value(); } }; - QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); + public: + QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device); - void init(); + void Init(); - inline VkQueue& getGraphic() noexcept { return _graphics_queue; } - inline VkQueue& getPresent() noexcept { return _present_queue; } - inline QueueFamilyIndices getFamilies() noexcept + inline VkQueue& GetGraphic() noexcept { return _graphics_queue; } + inline VkQueue& GetPresent() noexcept { return _present_queue; } + inline QueueFamilyIndices GetFamilies() noexcept { - if(_families.has_value()) - return *_families; - core::error::report(e_kind::fatal_error, "Vulkan : cannot get queue families, not init"); + if(m_families.has_value()) + return *m_families; + FatalError("Vulkan : cannot get queue families, not init"); return {}; // just to avoid warnings } private: - VkQueue _graphics_queue; - VkQueue _present_queue; - std::optional _families; + VkQueue m_graphics_queue; + VkQueue m_present_queue; + std::optional m_families; }; } diff --git a/runtime/Includes/Renderer/Core/RenderCore.h b/runtime/Includes/Renderer/Core/RenderCore.h new file mode 100644 index 0000000..a976b92 --- /dev/null +++ b/runtime/Includes/Renderer/Core/RenderCore.h @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RenderCore.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:55:43 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_RENDER_CORE__ +#define __MLX_RENDER_CORE__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace mlx +{ + const char* VerbaliseVkResult(VkResult result); + VkPipelineStageFlags AccessFlagsToPipelineStage(VkAccessFlags access_flags, VkPipelineStageFlags stage_flags); + + #ifdef DEBUG + constexpr const bool enable_validation_layers = true; + #else + constexpr const bool enable_validation_layers = false; + #endif + + const std::vector validation_layers = { "VK_LAYER_KHRONOS_validation" }; + + constexpr const int MAX_FRAMES_IN_FLIGHT = 3; + constexpr const int MAX_SETS_PER_POOL = 512; + constexpr const int NUMBER_OF_UNIFORM_BUFFERS = 1; // change this if for wathever reason more than one uniform buffer is needed + + class RenderCore : public Singleton + { + friend class Singleton; + + public: + void Init(); + void Destroy(); + + inline bool IsInit() const noexcept { return m_is_init; } + inline Instance& GetInstance() noexcept { return m_instance; } + inline Device& GetDevice() noexcept { return m_device; } + inline Queues& GetQueue() noexcept { return m_queues; } + inline GPUallocator& GetAllocator() noexcept { return m_allocator; } + inline ValidationLayers& GetLayers() noexcept { return m_layers; } + inline CommandBuffer& GetSingleTimeCmdBuffer() noexcept { return m_cmd_manager.GetCmdBuffer(); } + inline SingleTimeCmdManager& GetSingleTimeCmdManager() noexcept { return m_cmd_manager; } + inline DescriptorPool& GetDescriptorPool() { return m_pool_manager.GetAvailablePool(); } + + private: + RenderCore() = default; + ~RenderCore() = default; + + private: + ValidationLayers m_layers; + SingleTimeCmdManager m_cmd_manager; + Queues m_queues; + DescriptorPoolManager m_pool_manager; + Device m_device; + Instance m_instance; + GPUallocator m_allocator; + bool m_is_init = false; + }; +} + +#endif // __MLX_RENDER_CORE__ diff --git a/src/renderer/core/vk_semaphore.h b/runtime/Includes/Renderer/Core/Semaphore.h similarity index 65% rename from src/renderer/core/vk_semaphore.h rename to runtime/Includes/Renderer/Core/Semaphore.h index c022e8b..8e071b8 100644 --- a/src/renderer/core/vk_semaphore.h +++ b/runtime/Includes/Renderer/Core/Semaphore.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_semaphore.h :+: :+: :+: */ +/* Semaphore.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 18:59:38 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:11:43 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:56:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,13 @@ namespace mlx class Semaphore { public: - void init(); - void destroy() noexcept; + void Init(); + void Destroy() noexcept; - inline VkSemaphore& getImageSemaphore() noexcept { return _image_available_semaphore; } - inline VkSemaphore& getRenderImageSemaphore() noexcept { return _render_finished_semaphore; } + inline VkSemaphore& Get() noexcept { return m_semaphore; } private: - VkSemaphore _image_available_semaphore = VK_NULL_HANDLE; - VkSemaphore _render_finished_semaphore = VK_NULL_HANDLE; + VkSemaphore m_semaphore = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/vk_surface.h b/runtime/Includes/Renderer/Core/Surface.h similarity index 65% rename from src/renderer/core/vk_surface.h rename to runtime/Includes/Renderer/Core/Surface.h index d5ff3de..01a2785 100644 --- a/src/renderer/core/vk_surface.h +++ b/runtime/Includes/Renderer/Core/Surface.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_surface.h :+: :+: :+: */ +/* Surface.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 18:57:55 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:10:59 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:58:15 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,16 @@ namespace mlx class Surface { public: - void create(class Renderer& renderer); - void destroy() noexcept; + void Create(class Renderer& renderer); + void Destroy() noexcept; - VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector& availableFormats); + VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector& available_formats); - inline VkSurfaceKHR& operator()() noexcept { return _surface; } - inline VkSurfaceKHR& get() noexcept { return _surface; } + inline VkSurfaceKHR& operator()() noexcept { return m_surface; } + inline VkSurfaceKHR& Get() noexcept { return m_surface; } private: - VkSurfaceKHR _surface = VK_NULL_HANDLE; + VkSurfaceKHR m_surface = VK_NULL_HANDLE; }; } diff --git a/src/renderer/core/vk_validation_layers.h b/runtime/Includes/Renderer/Core/ValidationLayers.h similarity index 57% rename from src/renderer/core/vk_validation_layers.h rename to runtime/Includes/Renderer/Core/ValidationLayers.h index 971542d..cee5999 100644 --- a/src/renderer/core/vk_validation_layers.h +++ b/runtime/Includes/Renderer/Core/ValidationLayers.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_validation_layers.h :+: :+: :+: */ +/* ValidationLayers.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/19 14:04:25 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:00:00 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 22:59:00 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,24 +20,24 @@ namespace mlx public: ValidationLayers() = default; - void init(); - void destroy(); + void Init(); + void Destroy(); - bool checkValidationLayerSupport(); - void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo); + bool CheckValidationLayerSupport(); + void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& create_info); - VkResult setDebugUtilsObjectNameEXT(VkObjectType object_type, std::uint64_t object_handle, const char* object_name); + VkResult SetDebugUtilsObjectNameEXT(VkObjectType object_type, std::uint64_t object_handle, const char* object_name); ~ValidationLayers() = default; private: - VkResult createDebugUtilsMessengerEXT(const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator); - static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData); - void destroyDebugUtilsMessengerEXT(const VkAllocationCallbacks* pAllocator); + VkResult CreateDebugUtilsMessengerEXT(const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator); + static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData); + void DestroyDebugUtilsMessengerEXT(const VkAllocationCallbacks* pAllocator); private: - VkDebugUtilsMessengerEXT _debug_messenger; - PFN_vkSetDebugUtilsObjectNameEXT _vkSetDebugUtilsObjectNameEXT = nullptr; + VkDebugUtilsMessengerEXT m_debug_messenger; + PFN_vkSetDebugUtilsObjectNameEXT f_vkSetDebugUtilsObjectNameEXT = nullptr; }; } diff --git a/src/renderer/descriptors/vk_descriptor_pool.h b/runtime/Includes/Renderer/Descriptors/DescriptorPool.h similarity index 59% rename from src/renderer/descriptors/vk_descriptor_pool.h rename to runtime/Includes/Renderer/Descriptors/DescriptorPool.h index 512668a..97cabe9 100644 --- a/src/renderer/descriptors/vk_descriptor_pool.h +++ b/runtime/Includes/Renderer/Descriptors/DescriptorPool.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_descriptor_pool.h :+: :+: :+: */ +/* DescriptorPool.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:32:43 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:10:09 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 23:00:29 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,21 +20,21 @@ namespace mlx public: DescriptorPool() = default; - void init(std::size_t n, VkDescriptorPoolSize* size); - void freeDescriptor(const class DescriptorSet& set); - void destroy() noexcept; + void Init(std::size_t n, NonOwningPtr size); + void FreeDescriptor(const class DescriptorSet& set); + void Destroy() noexcept; - inline VkDescriptorPool& operator()() noexcept { return _pool; } - inline VkDescriptorPool& get() noexcept { return _pool; } - inline std::size_t getNumberOfSetsAllocated() const noexcept { return _allocated_sets; } + inline VkDescriptorPool& operator()() noexcept { return m_pool; } + inline VkDescriptorPool& Get() noexcept { return m_pool; } + inline std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocated_sets; } - inline bool isInit() const noexcept { return _pool != VK_NULL_HANDLE; } + inline bool IsInit() const noexcept { return m_pool != VK_NULL_HANDLE; } ~DescriptorPool() = default; private: - VkDescriptorPool _pool = VK_NULL_HANDLE; - std::size_t _allocated_sets = 0; + VkDescriptorPool m_pool = VK_NULL_HANDLE; + std::size_t m_allocated_sets = 0; }; } diff --git a/src/renderer/descriptors/descriptor_pool_manager.h b/runtime/Includes/Renderer/Descriptors/DescriptorPoolManager.h similarity index 77% rename from src/renderer/descriptors/descriptor_pool_manager.h rename to runtime/Includes/Renderer/Descriptors/DescriptorPoolManager.h index aea5b0d..8f4cb01 100644 --- a/src/renderer/descriptors/descriptor_pool_manager.h +++ b/runtime/Includes/Renderer/Descriptors/DescriptorPoolManager.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* descriptor_pool_manager.h :+: :+: :+: */ +/* DescriptorPoolManager.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 06:26:26 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:10:12 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 23:00:56 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_DESCRIPTOR_POOL_MANAGER__ #define __MLX_DESCRIPTOR_POOL_MANAGER__ -#include +#include namespace mlx { @@ -22,13 +22,13 @@ namespace mlx public: DescriptorPoolManager() = default; - DescriptorPool& getAvailablePool(); // assumes the pool is for only one set allocation, may cause some issues if this is for more than one - void destroyAllPools(); + DescriptorPool& GetAvailablePool(); // assumes the pool is for only one set allocation, may cause some issues if this is for more than one + void DestroyAllPools(); ~DescriptorPoolManager() = default; private: - std::list _pools; + std::list m_pools; }; } diff --git a/src/renderer/descriptors/vk_descriptor_set.h b/runtime/Includes/Renderer/Descriptors/DescriptorSet.h similarity index 57% rename from src/renderer/descriptors/vk_descriptor_set.h rename to runtime/Includes/Renderer/Descriptors/DescriptorSet.h index 2ad52c4..56c8ec4 100644 --- a/src/renderer/descriptors/vk_descriptor_set.h +++ b/runtime/Includes/Renderer/Descriptors/DescriptorSet.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_descriptor_set.h :+: :+: :+: */ +/* DescriptorSet.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:10:06 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 23:02:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __VK_DESCRIPTOR_SET__ #define __VK_DESCRIPTOR_SET__ -#include +#include namespace mlx { @@ -22,29 +22,29 @@ namespace mlx public: DescriptorSet() = default; - void init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout); + void Init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout); - void writeDescriptor(int binding, class UBO* ubo) const noexcept; - void writeDescriptor(int binding, const class Image& image) const noexcept; + void WriteDescriptor(int binding, NonOwningPtr ubo) const noexcept; + void WriteDescriptor(int binding, const class Image& image) const noexcept; - inline bool isInit() const noexcept { return _pool != nullptr && _renderer != nullptr; } + inline bool IsInit() const noexcept { return m_pool != nullptr && m_renderer != nullptr; } - DescriptorSet duplicate(); + DescriptorSet Duplicate(); VkDescriptorSet& operator()() noexcept; - VkDescriptorSet& get() noexcept; + VkDescriptorSet& Get() noexcept; - inline const std::array& getAllFramesDescriptorSets() const { return _desc_set; } + inline const std::array& GetAllFramesDescriptorSets() const { return m_desc_set; } - void destroy() noexcept; + void Destroy() noexcept; ~DescriptorSet() = default; private: - std::array _desc_set; - class DescriptorPool* _pool = nullptr; - class DescriptorSetLayout* _layout = nullptr; - class Renderer* _renderer = nullptr; + std::array m_desc_set; + NonOwningPtr p_pool; + NonOwningPtr p_layout; + NonOwningPtr p_renderer; }; } diff --git a/src/renderer/descriptors/vk_descriptor_set_layout.h b/runtime/Includes/Renderer/Descriptors/DescriptorSetLayout.h similarity index 67% rename from src/renderer/descriptors/vk_descriptor_set_layout.h rename to runtime/Includes/Renderer/Descriptors/DescriptorSetLayout.h index c7d2d45..e06bd54 100644 --- a/src/renderer/descriptors/vk_descriptor_set_layout.h +++ b/runtime/Includes/Renderer/Descriptors/DescriptorSetLayout.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* vk_descriptor_set_layout.h :+: :+: :+: */ +/* DescriptorSetLayout.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:36:22 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:10:03 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 23:03:04 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,18 +20,18 @@ namespace mlx public: DescriptorSetLayout() = default; - void init(std::vector> binds, VkShaderStageFlagBits stage); - void destroy() noexcept; + void Init(std::vector> binds, VkShaderStageFlagBits stage); + void Destroy() noexcept; - inline VkDescriptorSetLayout& operator()() noexcept { return _layout; } - inline VkDescriptorSetLayout& get() noexcept { return _layout; } - inline const std::vector>& getBindings() const noexcept { return _bindings; } + inline VkDescriptorSetLayout& operator()() noexcept { return m_layout; } + inline VkDescriptorSetLayout& Get() noexcept { return m_layout; } + inline const std::vector>& GetBindings() const noexcept { return m_bindings; } ~DescriptorSetLayout() = default; private: - VkDescriptorSetLayout _layout = VK_NULL_HANDLE; - std::vector> _bindings; + std::vector> m_bindings; + VkDescriptorSetLayout m_layout = VK_NULL_HANDLE; }; } diff --git a/runtime/Includes/Renderer/Enums.h b/runtime/Includes/Renderer/Enums.h new file mode 100644 index 0000000..30083b4 --- /dev/null +++ b/runtime/Includes/Renderer/Enums.h @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Enums.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 22:02:58 by maldavid #+# #+# */ +/* Updated: 2024/03/27 22:39:31 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_RENDERER_ENUMS__ +#define __MLX_RENDERER_ENUMS__ + +namespace mlx +{ + enum class BufferType + { + Constant = 0, + HighDynamic, // typically stored in RAM + LowDynamic, // typically stored in VRAM + + EndEnum + }; + constexpr std::size_t BufferTypeCount = static_cast(BufferType::EndEnum); + + enum class CommandResourceState + { + Held = 0, + Free, + + EndEnum + }; + constexpr std::size_t CommandResourceStateCount = static_cast(CommandResourceState::EndEnum); + + enum class CommandBufferState + { + Uninit = 0, // buffer not initialized or destroyed + Ready, // buffer ready to be used after having been submitted + Idle, // buffer has recorded informations but has not been submitted + Recording, // buffer is currently recording + Submitted, // buffer has been submitted + + EndEnum + }; + constexpr std::size_t CommandBufferStateCount = static_cast(CommandBufferState::EndEnum); + + enum class CommandBufferType + { + SingleTime = 0, + LongTime, + + EndEnum + }; + constexpr std::size_t CommandBufferTypeCount = static_cast(CommandBufferType::EndEnum); +} + +#endif diff --git a/src/renderer/images/vk_image.h b/runtime/Includes/Renderer/Images/Image.h similarity index 100% rename from src/renderer/images/vk_image.h rename to runtime/Includes/Renderer/Images/Image.h diff --git a/src/renderer/images/texture.h b/runtime/Includes/Renderer/Images/Texture.h similarity index 100% rename from src/renderer/images/texture.h rename to runtime/Includes/Renderer/Images/Texture.h diff --git a/src/renderer/images/texture_atlas.h b/runtime/Includes/Renderer/Images/TextureAtlas.h similarity index 100% rename from src/renderer/images/texture_atlas.h rename to runtime/Includes/Renderer/Images/TextureAtlas.h diff --git a/src/renderer/images/texture_descriptor.h b/runtime/Includes/Renderer/Images/TextureDescriptor.h similarity index 100% rename from src/renderer/images/texture_descriptor.h rename to runtime/Includes/Renderer/Images/TextureDescriptor.h diff --git a/src/renderer/images/texture_manager.h b/runtime/Includes/Renderer/Images/TextureManager.h similarity index 100% rename from src/renderer/images/texture_manager.h rename to runtime/Includes/Renderer/Images/TextureManager.h diff --git a/src/renderer/pipeline/pipeline.h b/runtime/Includes/Renderer/Pipelines/Pipeline.h similarity index 100% rename from src/renderer/pipeline/pipeline.h rename to runtime/Includes/Renderer/Pipelines/Pipeline.h diff --git a/src/renderer/pixel_put.h b/runtime/Includes/Renderer/PixelPut.h similarity index 100% rename from src/renderer/pixel_put.h rename to runtime/Includes/Renderer/PixelPut.h diff --git a/src/renderer/renderer.h b/runtime/Includes/Renderer/Renderer.h similarity index 100% rename from src/renderer/renderer.h rename to runtime/Includes/Renderer/Renderer.h diff --git a/src/renderer/renderpass/vk_framebuffer.h b/runtime/Includes/Renderer/Renderpass/FrameBuffer.h similarity index 100% rename from src/renderer/renderpass/vk_framebuffer.h rename to runtime/Includes/Renderer/Renderpass/FrameBuffer.h diff --git a/src/renderer/renderpass/vk_render_pass.h b/runtime/Includes/Renderer/Renderpass/RenderPass.h similarity index 100% rename from src/renderer/renderpass/vk_render_pass.h rename to runtime/Includes/Renderer/Renderpass/RenderPass.h diff --git a/src/renderer/swapchain/vk_swapchain.h b/runtime/Includes/Renderer/Renderpass/Swapchain.h similarity index 100% rename from src/renderer/swapchain/vk_swapchain.h rename to runtime/Includes/Renderer/Renderpass/Swapchain.h diff --git a/src/renderer/texts/font.h b/runtime/Includes/Renderer/Texts/Font.h similarity index 100% rename from src/renderer/texts/font.h rename to runtime/Includes/Renderer/Texts/Font.h diff --git a/src/renderer/texts/font_library.h b/runtime/Includes/Renderer/Texts/FontLibrary.h similarity index 100% rename from src/renderer/texts/font_library.h rename to runtime/Includes/Renderer/Texts/FontLibrary.h diff --git a/src/renderer/texts/text.h b/runtime/Includes/Renderer/Texts/Text.h similarity index 100% rename from src/renderer/texts/text.h rename to runtime/Includes/Renderer/Texts/Text.h diff --git a/src/renderer/texts/text_descriptor.h b/runtime/Includes/Renderer/Texts/TextDescriptor.h similarity index 100% rename from src/renderer/texts/text_descriptor.h rename to runtime/Includes/Renderer/Texts/TextDescriptor.h diff --git a/src/renderer/texts/text_library.h b/runtime/Includes/Renderer/Texts/TextLibrary.h similarity index 100% rename from src/renderer/texts/text_library.h rename to runtime/Includes/Renderer/Texts/TextLibrary.h diff --git a/src/renderer/texts/text_manager.h b/runtime/Includes/Renderer/Texts/TextManager.h similarity index 100% rename from src/renderer/texts/text_manager.h rename to runtime/Includes/Renderer/Texts/TextManager.h diff --git a/runtime/Includes/Utils/Ansi.h b/runtime/Includes/Utils/Ansi.h new file mode 100644 index 0000000..42eedeb --- /dev/null +++ b/runtime/Includes/Utils/Ansi.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Ansi.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:22:41 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:23:34 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_ANSI__ +#define __MLX_ANSI__ + +#include +#include + +namespace mlx +{ + enum class Ansi : std::uint32_t + { + red = 31, + green = 32, + blue = 34, + def = 0, + black = 30, + yellow = 33, + magenta = 35, + cyan = 36, + white = 37, + bg_red = 41, + bg_green = 42, + bg_blue = 44, + bg_def = 0, + bg_black = 40, + bg_yellow = 43, + bg_magenta = 45, + bg_cyan = 46, + bg_white = 47, + reset = 0, + bold = 1, + underline = 4, + inverse = 7, + bold_off = 21, + underline_off = 24, + inverse_off = 27 + }; + + inline std::ostream& operator<<(std::ostream& os, Ansi ansi) + { + return os << "\033[1;" << std::to_string(static_cast(ansi)) << "m"; + } +} + +#endif diff --git a/src/utils/combine_hash.h b/runtime/Includes/Utils/CombineHash.h similarity index 74% rename from src/utils/combine_hash.h rename to runtime/Includes/Utils/CombineHash.h index d86ec79..a11a35c 100644 --- a/src/utils/combine_hash.h +++ b/runtime/Includes/Utils/CombineHash.h @@ -1,31 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* combine_hash.h :+: :+: :+: */ +/* CombineHash.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/14 16:16:06 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:06:37 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:59:30 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_HASH__ #define __MLX_HASH__ -#include -#include - namespace mlx { - inline void hashCombine([[maybe_unused]] std::size_t& seed) noexcept {} + inline void HashCombine([[maybe_unused]] std::size_t& seed) noexcept {} template - inline void hashCombine(std::size_t& seed, const T& v, Rest... rest) + inline void HashCombine(std::size_t& seed, const T& v, Rest... rest) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); - hashCombine(seed, rest...); + HashCombine(seed, rest...); } } diff --git a/runtime/Includes/Utils/ConstMap.h b/runtime/Includes/Utils/ConstMap.h new file mode 100644 index 0000000..a4e8c40 --- /dev/null +++ b/runtime/Includes/Utils/ConstMap.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ConstMap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 15:26:39 by maldavid #+# #+# */ +/* Updated: 2024/03/27 21:59:35 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_CONST_MAP__ +#define __MLX_CONST_MAP__ + +namespace mlx +{ + template + class ConstMap + { + public: + using ValueType = std::pair; + using ContainerType = std::vector; + + using iterator = typename ContainerType::const_iterator; + using const_iterator = iterator; + + public: + ConstMap(std::initializer_list init) : m_container(init) + { + std::sort(m_container.begin(), m_container.end()); + } + + ConstMap(ContainerType container) : m_container(std::move(container)) + { + std::sort(m_container.begin(), m_container.end()); + } + + inline const_iterator begin() const { return m_container.begin(); } + inline const_iterator end() const { return m_container.end(); } + + template + inline const_iterator Find(const K& key) const + { + const_iterator it = std::lower_bound(begin(), end(), key, + [](const ValueType& p, const K& key) + { + return p.first < key; + } + ); + return it != end() && it->first == key ? it : end(); + } + + template + inline bool Has(const K& key) const + { + return Find(key) != end(); + } + + inline std::size_t Size() const { return m_container.size(); } + + ~ConstMap() = default; + + private: + ContainerType m_container; + }; +} + +#endif diff --git a/src/utils/dogica_ttf.h b/runtime/Includes/Utils/DogicaTTF.h similarity index 99% rename from src/utils/dogica_ttf.h rename to runtime/Includes/Utils/DogicaTTF.h index 30c48e2..3d24ce9 100644 --- a/src/utils/dogica_ttf.h +++ b/runtime/Includes/Utils/DogicaTTF.h @@ -1,20 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* dogica_ttf.h :+: :+: :+: */ +/* DogicaTTF.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/11 16:20:25 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:05:36 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:59:40 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_DOGICA_TTF__ #define __MLX_DOGICA_TTF__ -#include - constexpr const unsigned int dogica_ttf_len = 33860; static const std::vector dogica_ttf = { diff --git a/src/utils/icon_mlx.h b/runtime/Includes/Utils/IconMlx.h similarity index 99% rename from src/utils/icon_mlx.h rename to runtime/Includes/Utils/IconMlx.h index ca05561..4a8e5a3 100644 --- a/src/utils/icon_mlx.h +++ b/runtime/Includes/Utils/IconMlx.h @@ -1,20 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* icon_mlx.h :+: :+: :+: */ +/* IconMlx.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/25 11:23:16 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:05:28 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 21:59:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __ICON_MLX__ #define __ICON_MLX__ -#include - constexpr const int logo_mlx_height = 125; constexpr const int logo_mlx_width = 125; constexpr const int logo_mlx_size = logo_mlx_height * logo_mlx_width * 4; diff --git a/src/utils/non_copyable.h b/runtime/Includes/Utils/NonCopyable.h similarity index 100% rename from src/utils/non_copyable.h rename to runtime/Includes/Utils/NonCopyable.h diff --git a/runtime/Includes/Utils/NonOwningPtr.h b/runtime/Includes/Utils/NonOwningPtr.h new file mode 100644 index 0000000..baef061 --- /dev/null +++ b/runtime/Includes/Utils/NonOwningPtr.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* NonOwningPtr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 21:03:37 by maldavid #+# #+# */ +/* Updated: 2024/03/27 21:05:05 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_NON_OWNING_PTR__ +#define __MLX_NON_OWNING_PTR__ + +namespace mlx +{ + template + class NonOwningPtr + { + public: + NonOwningPtr(T* ptr = nullptr); + NonOwningPtr(const NonOwningPtr&) = default; + NonOwningPtr(NonOwningPtr&& ptr) noexcept; + + NonOwningPtr& operator=(T* ptr); + NonOwningPtr& operator=(const NonOwningPtr&) = default; + NonOwningPtr& operator=(NonOwningPtr&& ptr) noexcept; + + inline operator bool() const noexcept; + + inline T* Get() const noexcept; + inline T* operator->() const noexcept; + inline T& operator*() const noexcept; + + ~NonOwningPtr() = default; + + private: + T* p_ptr = nullptr; + }; +} + +#include + +#endif diff --git a/runtime/Includes/Utils/NonOwningPtr.inl b/runtime/Includes/Utils/NonOwningPtr.inl new file mode 100644 index 0000000..0247092 --- /dev/null +++ b/runtime/Includes/Utils/NonOwningPtr.inl @@ -0,0 +1,62 @@ +/* **************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* NonOwningPtr.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 21:04:00 by maldavid #+# #+# */ +/* Updated: 2024/03/27 21:04:00 by maldavid ### ########.fr */ +/* */ +/* **************************************************************************** */ + +#include + +namespace mlx +{ + template + NonOwningPtr::NonOwningPtr(T* ptr) : p_ptr(ptr) {} + + template + NonOwningPtr::NonOwningPtr(NonOwningPtr&& ptr) noexcept : p_ptr(ptr.p_ptr) + { + ptr.p_ptr = nullptr; + } + + template + NonOwningPtr& NonOwningPtr::operator=(T* ptr) + { + p_ptr = ptr; + } + + template + NonOwningPtr& NonOwningPtr::operator=(NonOwningPtr&& ptr) noexcept + { + p_ptr = ptr.p_ptr; + ptr.p_ptr = nullptr; + } + + template + NonOwningPtr::operator bool() const noexcept + { + return p_ptr != nullptr; + } + + template + T* NonOwningPtr::Get() const noexcept + { + return p_ptr; + } + + template + T* NonOwningPtr::operator->() const noexcept + { + return p_ptr; + } + + template + T& NonOwningPtr::operator*() const noexcept + { + return *p_ptr; + } +} diff --git a/src/utils/singleton.h b/runtime/Includes/Utils/Singleton.h similarity index 84% rename from src/utils/singleton.h rename to runtime/Includes/Utils/Singleton.h index ae8246e..33cc136 100644 --- a/src/utils/singleton.h +++ b/runtime/Includes/Utils/Singleton.h @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* singleton.h :+: :+: :+: */ +/* Singleton.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 19:18:46 by maldavid #+# #+# */ -/* Updated: 2024/03/24 14:42:56 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 18:20:11 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __MLX_SINGLETON__ #define __MLX_SINGLETON__ -#include "non_copyable.h" +#include namespace mlx { @@ -21,7 +21,7 @@ namespace mlx class Singleton : public NonCopyable { public: - inline static T& get() + inline static T& Get() { static T instance; return instance; diff --git a/src/core/application.cpp b/runtime/Sources/Core/Application.cpp similarity index 91% rename from src/core/application.cpp rename to runtime/Sources/Core/Application.cpp index e14379d..8e22407 100644 --- a/src/core/application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* application.cpp :+: :+: :+: */ +/* Application.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2024/03/25 22:16:24 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 17:43:18 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include -#include "application.h" +#include #include #include #include @@ -20,11 +20,17 @@ #include #include #include +#include namespace mlx::core { Application::Application() : _fps(), _in(std::make_unique()) { + EventBus::RegisterListener({[](const EventBase& event) + { + + }, "__internal_application" }); + _fps.init(); glfwSetErrorCallback([]([[maybe_unused]] int code, const char* desc) { diff --git a/src/core/bridge.cpp b/runtime/Sources/Core/Bridge.cpp similarity index 100% rename from src/core/bridge.cpp rename to runtime/Sources/Core/Bridge.cpp diff --git a/runtime/Sources/Core/EventBus.cpp b/runtime/Sources/Core/EventBus.cpp new file mode 100644 index 0000000..550882b --- /dev/null +++ b/runtime/Sources/Core/EventBus.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* EventBus.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:36:05 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:37:01 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +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); + } +} diff --git a/runtime/Sources/Core/EventListener.cpp b/runtime/Sources/Core/EventListener.cpp new file mode 100644 index 0000000..cb979e1 --- /dev/null +++ b/runtime/Sources/Core/EventListener.cpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* EventListener.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:37:09 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:37:38 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +namespace mlx +{ + EventListener::EventListener(func::function functor, std::string name) + : m_listen_functor(std::move(functor)), m_name(std::move(name)) + {} +} diff --git a/src/core/fps.cpp b/runtime/Sources/Core/Fps.cpp similarity index 53% rename from src/core/fps.cpp rename to runtime/Sources/Core/Fps.cpp index e88b489..59a0926 100644 --- a/src/core/fps.cpp +++ b/runtime/Sources/Core/Fps.cpp @@ -1,43 +1,42 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* fps.cpp :+: :+: :+: */ +/* Fps.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/18 14:56:17 by maldavid #+# #+# */ -/* Updated: 2024/03/25 22:59:13 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 20:53:11 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include - -#include +#include +#include namespace mlx { - void FpsManager::init() + void FpsManager::Init() { - _timer = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); - _fps_before = _timer; - _fps_now = _timer; + m_timer = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); + m_fps_before = m_timer; + m_fps_now = m_timer; } - bool FpsManager::update() + bool FpsManager::Update() { using namespace std::chrono_literals; - _fps_now = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); + m_fps_now = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); - if(std::chrono::duration{_fps_now - _timer} >= 1s) - _timer += _fps_now; + if(std::chrono::duration{m_fps_now - m_timer} >= 1s) + m_timer += m_fps_now; - _fps_elapsed_time = _fps_now - _fps_before; - if(_fps_elapsed_time >= _ns) + m_fps_elapsed_time = m_fps_now - m_fps_before; + if(m_fps_elapsed_time >= m_ns) { - _fps_before += _ns; + m_fps_before += m_ns; return true; } - std::this_thread::sleep_for(std::chrono::duration(_ns - 1)); + std::this_thread::sleep_for(std::chrono::duration(m_ns - 1)); return false; } } diff --git a/src/core/graphics.cpp b/runtime/Sources/Core/Graphics.cpp similarity index 100% rename from src/core/graphics.cpp rename to runtime/Sources/Core/Graphics.cpp diff --git a/runtime/Sources/Core/Logs.cpp b/runtime/Sources/Core/Logs.cpp new file mode 100644 index 0000000..adc9a0c --- /dev/null +++ b/runtime/Sources/Core/Logs.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Logs.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:20:55 by maldavid #+# #+# */ +/* Updated: 2024/03/27 17:26:59 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +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{}); + } + } +} diff --git a/src/core/memory.cpp b/runtime/Sources/Core/Memory.cpp similarity index 100% rename from src/core/memory.cpp rename to runtime/Sources/Core/Memory.cpp diff --git a/src/core/profiler.cpp b/runtime/Sources/Core/Profiler.cpp similarity index 100% rename from src/core/profiler.cpp rename to runtime/Sources/Core/Profiler.cpp diff --git a/src/core/UUID.cpp b/runtime/Sources/Core/UUID.cpp similarity index 100% rename from src/core/UUID.cpp rename to runtime/Sources/Core/UUID.cpp diff --git a/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp b/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp new file mode 100644 index 0000000..9379cf1 --- /dev/null +++ b/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* GLFWInputs.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 18:39:32 by maldavid #+# #+# */ +/* Updated: 2024/03/27 18:42:18 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +namespace mlx +{ + void GLFWInputs::Update() noexcept + { + glfwPollEvents(); + } +} diff --git a/src/platform/inputs.cpp b/runtime/Sources/Platform/Inputs.cpp similarity index 92% rename from src/platform/inputs.cpp rename to runtime/Sources/Platform/Inputs.cpp index 790b59b..2cc6ca6 100644 --- a/src/platform/inputs.cpp +++ b/runtime/Sources/Platform/Inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ -/* Updated: 2024/03/25 23:13:16 by maldavid ### ########.fr */ +/* Updated: 2024/03/27 15:50:07 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,9 +24,11 @@ namespace mlx _xRel = 0; _yRel = 0; + glfwPollEvents(); + static int i = 0; i++; - if(i >= 150) + if(i >= 500) { auto& hooks = _events_hooks[0]; auto& win_hook = hooks[MLX_WINDOW_EVENT]; diff --git a/src/platform/window.cpp b/runtime/Sources/Platform/Window.cpp similarity index 100% rename from src/platform/window.cpp rename to runtime/Sources/Platform/Window.cpp diff --git a/src/renderer/buffers/vk_buffer.cpp b/runtime/Sources/Renderer/Buffers/Buffer.cpp similarity index 100% rename from src/renderer/buffers/vk_buffer.cpp rename to runtime/Sources/Renderer/Buffers/Buffer.cpp diff --git a/src/renderer/buffers/vk_ubo.cpp b/runtime/Sources/Renderer/Buffers/UniformBuffer.cpp similarity index 100% rename from src/renderer/buffers/vk_ubo.cpp rename to runtime/Sources/Renderer/Buffers/UniformBuffer.cpp diff --git a/src/renderer/buffers/vk_vbo.cpp b/runtime/Sources/Renderer/Buffers/VertexBuffer.cpp similarity index 100% rename from src/renderer/buffers/vk_vbo.cpp rename to runtime/Sources/Renderer/Buffers/VertexBuffer.cpp diff --git a/src/renderer/command/vk_cmd_buffer.cpp b/runtime/Sources/Renderer/Command/CommandBuffer.cpp similarity index 100% rename from src/renderer/command/vk_cmd_buffer.cpp rename to runtime/Sources/Renderer/Command/CommandBuffer.cpp diff --git a/src/renderer/command/cmd_manager.cpp b/runtime/Sources/Renderer/Command/CommandManager.cpp similarity index 100% rename from src/renderer/command/cmd_manager.cpp rename to runtime/Sources/Renderer/Command/CommandManager.cpp diff --git a/src/renderer/command/vk_cmd_pool.cpp b/runtime/Sources/Renderer/Command/CommandPool.cpp similarity index 100% rename from src/renderer/command/vk_cmd_pool.cpp rename to runtime/Sources/Renderer/Command/CommandPool.cpp diff --git a/src/renderer/command/single_time_cmd_manager.cpp b/runtime/Sources/Renderer/Command/SingleTimeCommandManager.cpp similarity index 100% rename from src/renderer/command/single_time_cmd_manager.cpp rename to runtime/Sources/Renderer/Command/SingleTimeCommandManager.cpp diff --git a/src/renderer/core/vk_device.cpp b/runtime/Sources/Renderer/Core/Device.cpp similarity index 100% rename from src/renderer/core/vk_device.cpp rename to runtime/Sources/Renderer/Core/Device.cpp diff --git a/src/renderer/core/vk_fence.cpp b/runtime/Sources/Renderer/Core/Fence.cpp similarity index 100% rename from src/renderer/core/vk_fence.cpp rename to runtime/Sources/Renderer/Core/Fence.cpp diff --git a/src/renderer/core/vk_instance.cpp b/runtime/Sources/Renderer/Core/Instance.cpp similarity index 100% rename from src/renderer/core/vk_instance.cpp rename to runtime/Sources/Renderer/Core/Instance.cpp diff --git a/src/renderer/core/memory.cpp b/runtime/Sources/Renderer/Core/Memory.cpp similarity index 100% rename from src/renderer/core/memory.cpp rename to runtime/Sources/Renderer/Core/Memory.cpp diff --git a/src/renderer/core/vk_queues.cpp b/runtime/Sources/Renderer/Core/Queues.cpp similarity index 100% rename from src/renderer/core/vk_queues.cpp rename to runtime/Sources/Renderer/Core/Queues.cpp diff --git a/src/renderer/core/render_core.cpp b/runtime/Sources/Renderer/Core/RenderCore.cpp similarity index 100% rename from src/renderer/core/render_core.cpp rename to runtime/Sources/Renderer/Core/RenderCore.cpp diff --git a/src/renderer/core/vk_semaphore.cpp b/runtime/Sources/Renderer/Core/Semaphore.cpp similarity index 100% rename from src/renderer/core/vk_semaphore.cpp rename to runtime/Sources/Renderer/Core/Semaphore.cpp diff --git a/src/renderer/core/vk_surface.cpp b/runtime/Sources/Renderer/Core/Surface.cpp similarity index 100% rename from src/renderer/core/vk_surface.cpp rename to runtime/Sources/Renderer/Core/Surface.cpp diff --git a/src/renderer/core/vk_validation_layers.cpp b/runtime/Sources/Renderer/Core/ValidationLayers.cpp similarity index 100% rename from src/renderer/core/vk_validation_layers.cpp rename to runtime/Sources/Renderer/Core/ValidationLayers.cpp diff --git a/src/renderer/descriptors/descriptor_pool_manager.cpp b/runtime/Sources/Renderer/Descriptors/descriptor_pool_manager.cpp similarity index 100% rename from src/renderer/descriptors/descriptor_pool_manager.cpp rename to runtime/Sources/Renderer/Descriptors/descriptor_pool_manager.cpp diff --git a/src/renderer/descriptors/vk_descriptor_pool.cpp b/runtime/Sources/Renderer/Descriptors/vk_descriptor_pool.cpp similarity index 100% rename from src/renderer/descriptors/vk_descriptor_pool.cpp rename to runtime/Sources/Renderer/Descriptors/vk_descriptor_pool.cpp diff --git a/src/renderer/descriptors/vk_descriptor_set.cpp b/runtime/Sources/Renderer/Descriptors/vk_descriptor_set.cpp similarity index 100% rename from src/renderer/descriptors/vk_descriptor_set.cpp rename to runtime/Sources/Renderer/Descriptors/vk_descriptor_set.cpp diff --git a/src/renderer/descriptors/vk_descriptor_set_layout.cpp b/runtime/Sources/Renderer/Descriptors/vk_descriptor_set_layout.cpp similarity index 100% rename from src/renderer/descriptors/vk_descriptor_set_layout.cpp rename to runtime/Sources/Renderer/Descriptors/vk_descriptor_set_layout.cpp diff --git a/src/renderer/images/texture.cpp b/runtime/Sources/Renderer/Images/texture.cpp similarity index 100% rename from src/renderer/images/texture.cpp rename to runtime/Sources/Renderer/Images/texture.cpp diff --git a/src/renderer/images/texture_atlas.cpp b/runtime/Sources/Renderer/Images/texture_atlas.cpp similarity index 100% rename from src/renderer/images/texture_atlas.cpp rename to runtime/Sources/Renderer/Images/texture_atlas.cpp diff --git a/src/renderer/images/vk_image.cpp b/runtime/Sources/Renderer/Images/vk_image.cpp similarity index 100% rename from src/renderer/images/vk_image.cpp rename to runtime/Sources/Renderer/Images/vk_image.cpp diff --git a/src/renderer/pipeline/pipeline.cpp b/runtime/Sources/Renderer/Pipelines/pipeline.cpp similarity index 100% rename from src/renderer/pipeline/pipeline.cpp rename to runtime/Sources/Renderer/Pipelines/pipeline.cpp diff --git a/src/renderer/pixel_put.cpp b/runtime/Sources/Renderer/PixelPut.cpp similarity index 100% rename from src/renderer/pixel_put.cpp rename to runtime/Sources/Renderer/PixelPut.cpp diff --git a/src/renderer/renderer.cpp b/runtime/Sources/Renderer/Renderer.cpp similarity index 100% rename from src/renderer/renderer.cpp rename to runtime/Sources/Renderer/Renderer.cpp diff --git a/src/renderer/renderpass/vk_framebuffer.cpp b/runtime/Sources/Renderer/Renderpass/Framebuffer.cpp similarity index 100% rename from src/renderer/renderpass/vk_framebuffer.cpp rename to runtime/Sources/Renderer/Renderpass/Framebuffer.cpp diff --git a/src/renderer/renderpass/vk_render_pass.cpp b/runtime/Sources/Renderer/Renderpass/Renderpass.cpp similarity index 100% rename from src/renderer/renderpass/vk_render_pass.cpp rename to runtime/Sources/Renderer/Renderpass/Renderpass.cpp diff --git a/src/renderer/swapchain/vk_swapchain.cpp b/runtime/Sources/Renderer/Renderpass/Swapchain.cpp similarity index 100% rename from src/renderer/swapchain/vk_swapchain.cpp rename to runtime/Sources/Renderer/Renderpass/Swapchain.cpp diff --git a/src/renderer/texts/font.cpp b/runtime/Sources/Renderer/Texts/Font.cpp similarity index 100% rename from src/renderer/texts/font.cpp rename to runtime/Sources/Renderer/Texts/Font.cpp diff --git a/src/renderer/texts/font_library.cpp b/runtime/Sources/Renderer/Texts/FontLibrary.cpp similarity index 100% rename from src/renderer/texts/font_library.cpp rename to runtime/Sources/Renderer/Texts/FontLibrary.cpp diff --git a/src/renderer/texts/text.cpp b/runtime/Sources/Renderer/Texts/Text.cpp similarity index 100% rename from src/renderer/texts/text.cpp rename to runtime/Sources/Renderer/Texts/Text.cpp diff --git a/src/renderer/texts/text_descriptor.cpp b/runtime/Sources/Renderer/Texts/TextDescriptor.cpp similarity index 100% rename from src/renderer/texts/text_descriptor.cpp rename to runtime/Sources/Renderer/Texts/TextDescriptor.cpp diff --git a/src/renderer/texts/text_library.cpp b/runtime/Sources/Renderer/Texts/TextLibrary.cpp similarity index 100% rename from src/renderer/texts/text_library.cpp rename to runtime/Sources/Renderer/Texts/TextLibrary.cpp diff --git a/src/renderer/texts/text_manager.cpp b/runtime/Sources/Renderer/Texts/TextManager.cpp similarity index 100% rename from src/renderer/texts/text_manager.cpp rename to runtime/Sources/Renderer/Texts/TextManager.cpp diff --git a/src/core/application.h b/src/core/application.h deleted file mode 100644 index ff66a0f..0000000 --- a/src/core/application.h +++ /dev/null @@ -1,73 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* application.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:13:27 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_APPLICATION__ -#define __MLX_APPLICATION__ - -#include -#include -#include -#include -#include - -namespace mlx::core -{ - class Application - { - public: - Application(); - - inline void getMousePos(int* x, int* y) noexcept; - inline void mouseMove(void* win, int x, int y) noexcept; - - inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept; - - inline void getScreenSize(void* win, int* w, int* h) noexcept; - - inline void setFPSCap(std::uint32_t fps) noexcept; - - inline void* newGraphicsSuport(std::size_t w, std::size_t h, const char* title); - inline void clearGraphicsSupport(void* win); - inline void destroyGraphicsSupport(void* win); - - inline void pixelPut(void* win, int x, int y, std::uint32_t color) const noexcept; - inline void stringPut(void* win, int x, int y, std::uint32_t color, char* str); - - void* newTexture(int w, int h); - void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...) - inline void texturePut(void* win, void* img, int x, int y); - inline int getTexturePixel(void* img, int x, int y); - inline void setTexturePixel(void* img, int x, int y, std::uint32_t color); - void destroyTexture(void* ptr); - - inline void loopHook(int (*f)(void*), void* param); - inline void loopEnd() noexcept; - - inline void loadFont(void* win, const std::filesystem::path& filepath, float scale); - - void run() noexcept; - - ~Application(); - - private: - FpsManager _fps; - std::list _textures; - std::vector> _graphics; - std::function _loop_hook; - std::unique_ptr _in; - void* _param = nullptr; - }; -} - -#include - -#endif // __MLX_APPLICATION__ diff --git a/src/core/application.inl b/src/core/application.inl deleted file mode 100644 index f98d510..0000000 --- a/src/core/application.inl +++ /dev/null @@ -1,196 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* application.inl :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2023/04/02 14:56:27 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -#define CHECK_WINDOW_PTR(win) \ - if(win == nullptr) \ - { \ - core::error::report(e_kind::error, "invalid window ptr (NULL)"); \ - return; \ - } \ - else if(*static_cast(win) < 0 || *static_cast(win) > static_cast(_graphics.size()))\ - { \ - core::error::report(e_kind::error, "invalid window ptr"); \ - return; \ - } else {} - -#define CHECK_IMAGE_PTR(img, retval) \ - if(img == nullptr) \ - { \ - core::error::report(e_kind::error, "invalid image ptr (NULL)"); \ - retval; \ - } \ - else if(std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) \ - { \ - return &texture == img; \ - }) == _textures.end()) \ - { \ - core::error::report(e_kind::error, "invalid image ptr"); \ - retval; \ - } else {} - -namespace mlx::core -{ - void Application::getMousePos(int* x, int* y) noexcept - { - *x = _in->getX(); - *y = _in->getY(); - } - - void Application::mouseMove(void* win, int x, int y) noexcept - { - CHECK_WINDOW_PTR(win); - if(!_graphics[*static_cast(win)]->hasWindow()) - { - error::report(e_kind::warning, "trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)"); - return; - } - } - - void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept - { - CHECK_WINDOW_PTR(win); - if(!_graphics[*static_cast(win)]->hasWindow()) - { - error::report(e_kind::warning, "trying to add event hook for a window that is targeting an image and not a real window, this is not allowed (hook ignored)"); - return; - } - _in->onEvent(_graphics[*static_cast(win)]->getWindow()->getID(), event, funct_ptr, param); - } - - void Application::getScreenSize(void* win, int* w, int* h) noexcept - { - CHECK_WINDOW_PTR(win); - *w = 0; - *h = 0; - } - - void Application::setFPSCap(std::uint32_t fps) noexcept - { - _fps.setMaxFPS(fps); - } - - void* Application::newGraphicsSuport(std::size_t w, std::size_t h, const char* title) - { - MLX_PROFILE_FUNCTION(); - auto it = std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) - { - return &texture == reinterpret_cast(const_cast(title)); - }); - if(it != _textures.end()) - _graphics.emplace_back(std::make_unique(w, h, reinterpret_cast(const_cast(title)), _graphics.size())); - else - { - if(title == NULL) - { - core::error::report(e_kind::fatal_error, "invalid window title (NULL)"); - return nullptr; - } - _graphics.emplace_back(std::make_unique(w, h, title, _graphics.size())); - _in->addWindow(_graphics.back()->getWindow()); - } - return static_cast(&_graphics.back()->getID()); - } - - void Application::clearGraphicsSupport(void* win) - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - _graphics[*static_cast(win)]->clearRenderData(); - } - - void Application::destroyGraphicsSupport(void* win) - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - _graphics[*static_cast(win)].reset(); - } - - void Application::pixelPut(void* win, int x, int y, std::uint32_t color) const noexcept - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - _graphics[*static_cast(win)]->pixelPut(x, y, color); - } - - void Application::stringPut(void* win, int x, int y, std::uint32_t color, char* str) - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - if(str == nullptr) - { - core::error::report(e_kind::error, "wrong text (NULL)"); - return; - } - if(std::strlen(str) == 0) - { - core::error::report(e_kind::warning, "trying to put an empty text"); - return; - } - _graphics[*static_cast(win)]->stringPut(x, y, color, str); - } - - void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale) - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - _graphics[*static_cast(win)]->loadFont(filepath, scale); - } - - void Application::texturePut(void* win, void* img, int x, int y) - { - MLX_PROFILE_FUNCTION(); - CHECK_WINDOW_PTR(win); - CHECK_IMAGE_PTR(img, return); - Texture* texture = static_cast(img); - if(!texture->isInit()) - core::error::report(e_kind::error, "trying to put a texture that has been destroyed"); - else - _graphics[*static_cast(win)]->texturePut(texture, x, y); - } - - int Application::getTexturePixel(void* img, int x, int y) - { - MLX_PROFILE_FUNCTION(); - CHECK_IMAGE_PTR(img, return 0); - Texture* texture = static_cast(img); - if(!texture->isInit()) - { - core::error::report(e_kind::error, "trying to get a pixel from texture that has been destroyed"); - return 0; - } - return texture->getPixel(x, y); - } - - void Application::setTexturePixel(void* img, int x, int y, std::uint32_t color) - { - MLX_PROFILE_FUNCTION(); - CHECK_IMAGE_PTR(img, return); - Texture* texture = static_cast(img); - if(!texture->isInit()) - core::error::report(e_kind::error, "trying to set a pixel on texture that has been destroyed"); - else - texture->setPixel(x, y, color); - } - - void Application::loopHook(int (*f)(void*), void* param) - { - _loop_hook = f; - _param = param; - } - - void Application::loopEnd() noexcept - { - _in->finish(); - } -} diff --git a/src/core/errors.cpp b/src/core/errors.cpp deleted file mode 100644 index 098abc8..0000000 --- a/src/core/errors.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* errors.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/04 17:48:06 by maldavid #+# #+# */ -/* Updated: 2024/03/25 17:45:12 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -#include "errors.h" - -constexpr const int BUFFER_SIZE = 4096; - -namespace mlx::core::error -{ - void report(e_kind kind, std::string msg, ...) - { - char buffer[BUFFER_SIZE]; - - va_list al; - va_start(al, msg); - std::vsnprintf(buffer, BUFFER_SIZE, msg.c_str(), al); - va_end(al); - - switch(kind) - { - case e_kind::message: std::cout << "\033[1;34m[MacroLibX] Message : \033[1;0m" << buffer << std::endl; break; - case e_kind::warning: std::cout << "\033[1;35m[MacroLibX] Warning : \033[1;0m" << buffer << std::endl; break; - case e_kind::error: std::cerr << "\033[1;31m[MacroLibX] Error : \033[1;0m" << buffer << std::endl; break; - case e_kind::fatal_error: - std::cerr << "\033[1;31m[MacroLibX] Fatal Error : \033[1;0m" << buffer << std::endl; - std::exit(EXIT_FAILURE); - break; - } - } -} diff --git a/src/core/graphics.h b/src/core/graphics.h deleted file mode 100644 index bb9935d..0000000 --- a/src/core/graphics.h +++ /dev/null @@ -1,76 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* graphics.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ -/* Updated: 2024/03/27 00:32:28 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_GRAPHICS__ -#define __MLX_GRAPHICS__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace mlx -{ - class GraphicsSupport : public NonCopyable - { - public: - GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id); - GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id); - - inline int& getID() noexcept; - inline std::shared_ptr getWindow(); - - void render() noexcept; - - inline void clearRenderData() noexcept; - inline void pixelPut(int x, int y, std::uint32_t color) noexcept; - inline void stringPut(int x, int y, std::uint32_t color, std::string str); - inline void texturePut(Texture* texture, int x, int y); - inline void loadFont(const std::filesystem::path& filepath, float scale); - inline void tryEraseTextureFromManager(Texture* texture) noexcept; - - inline bool hasWindow() const noexcept { return _has_window; } - - inline Renderer& getRenderer() { return *_renderer; } - - ~GraphicsSupport(); - - private: - PixelPutPipeline _pixel_put_pipeline; - - std::vector _drawlist; - - TextManager _text_manager; - TextureManager _texture_manager; - - glm::mat4 _proj = glm::mat4(1.0); - - std::shared_ptr _window; - std::unique_ptr _renderer; - - std::size_t _width = 0; - std::size_t _height = 0; - - int _id; - - bool _has_window; - }; -} - -#include - -#endif diff --git a/src/core/graphics.inl b/src/core/graphics.inl deleted file mode 100644 index c6f60f6..0000000 --- a/src/core/graphics.inl +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* graphics.inl :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ -/* Updated: 2023/04/02 15:26:16 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -namespace mlx -{ - int& GraphicsSupport::getID() noexcept { return _id; } - std::shared_ptr GraphicsSupport::getWindow() { return _window; } - - void GraphicsSupport::clearRenderData() noexcept - { - MLX_PROFILE_FUNCTION(); - _drawlist.clear(); - _pixel_put_pipeline.clear(); - _text_manager.clear(); - _texture_manager.clear(); - } - - void GraphicsSupport::pixelPut(int x, int y, std::uint32_t color) noexcept - { - MLX_PROFILE_FUNCTION(); - _pixel_put_pipeline.setPixel(x, y, color); - } - - void GraphicsSupport::stringPut(int x, int y, std::uint32_t color, std::string str) - { - MLX_PROFILE_FUNCTION(); - std::pair res = _text_manager.registerText(x, y, color, str); - if(!res.second) // if this is not a completly new text draw - { - auto it = std::find(_drawlist.begin(), _drawlist.end(), res.first); - if(it != _drawlist.end()) - _drawlist.erase(it); - } - _drawlist.push_back(res.first); - } - - void GraphicsSupport::texturePut(Texture* texture, int x, int y) - { - MLX_PROFILE_FUNCTION(); - auto res = _texture_manager.registerTexture(texture, x, y); - if(!res.second) // if this is not a completly new texture draw - { - auto it = std::find(_drawlist.begin(), _drawlist.end(), res.first); - if(it != _drawlist.end()) - _drawlist.erase(it); - } - _drawlist.push_back(res.first); - } - - void GraphicsSupport::loadFont(const std::filesystem::path& filepath, float scale) - { - MLX_PROFILE_FUNCTION(); - _text_manager.loadFont(*_renderer, filepath, scale); - } - - void GraphicsSupport::tryEraseTextureFromManager(Texture* texture) noexcept - { - MLX_PROFILE_FUNCTION(); - for(auto it = _drawlist.begin(); it != _drawlist.end();) - { - if(_texture_manager.isTextureKnown(texture)) - it = _drawlist.erase(it); - else - ++it; - } - _texture_manager.eraseTextures(texture); - } -} diff --git a/src/platform/inputs.h b/src/platform/inputs.h deleted file mode 100644 index 7cd9078..0000000 --- a/src/platform/inputs.h +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* inputs.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2024/03/27 00:31:56 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_INPUTS__ -#define __MLX_INPUTS__ - -#include "window.h" - -namespace mlx -{ - struct Hook - { - func::function hook; - void* param = nullptr; - }; - - class Input - { - public: - Input() = default; - - void update(); - - inline bool isMouseMoving() const noexcept { return _xRel || _yRel; } - - inline int getX() const noexcept { return _x; } - inline int getY() const noexcept { return _y; } - - inline int getXRel() const noexcept { return _xRel; } - inline int getYRel() const noexcept { return _yRel; } - - inline bool isRunning() const noexcept { return !_end; } - inline constexpr void finish() noexcept { _end = true; } - - inline void addWindow(std::shared_ptr window) - { - _windows[window->getID()] = window; - _events_hooks[window->getID()] = {}; - } - - inline void onEvent(std::uint32_t id, int event, int (*funct_ptr)(int, void*), void* param) noexcept - { - _events_hooks[id][event].hook = funct_ptr; - _events_hooks[id][event].param = param; - } - - ~Input() = default; - - private: - std::unordered_map> _windows; - std::unordered_map> _events_hooks; - - int _x = 0; - int _y = 0; - int _xRel = 0; - int _yRel = 0; - - bool _end = false; - }; -} - -#endif diff --git a/src/renderer/buffers/vk_buffer.h b/src/renderer/buffers/vk_buffer.h deleted file mode 100644 index 3d7d149..0000000 --- a/src/renderer/buffers/vk_buffer.h +++ /dev/null @@ -1,63 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* vk_buffer.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:39 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_VK_BUFFER__ -#define __MLX_VK_BUFFER__ - -#include -#include - -namespace mlx -{ - class Buffer : public CmdResource - { - public: - enum class kind { dynamic, dynamic_device_local, uniform, constant }; - - void create(kind type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr); - void destroy() noexcept; - - inline void mapMem(void** data) noexcept { Render_Core::get().getAllocator().mapMemory(_allocation, data); _is_mapped = true; } - inline bool isMapped() const noexcept { return _is_mapped; } - inline void unmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(_allocation); _is_mapped = false; } - - void flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0); - bool copyFromBuffer(const Buffer& buffer) noexcept; - - inline VkBuffer& operator()() noexcept { return _buffer; } - inline VkBuffer& get() noexcept { return _buffer; } - inline VkDeviceSize getSize() const noexcept { return _size; } - inline VkDeviceSize getOffset() const noexcept { return _offset; } - - protected: - void pushToGPU() noexcept; - void swap(Buffer& buffer) noexcept; - - protected: - VmaAllocation _allocation; - VkBuffer _buffer = VK_NULL_HANDLE; - VkDeviceSize _offset = 0; - VkDeviceSize _size = 0; - - private: - void createBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name); - - private: - #ifdef DEBUG - std::string _name; - #endif - VkBufferUsageFlags _usage = 0; - bool _is_mapped = false; - }; -} - -#endif diff --git a/src/renderer/buffers/vk_ubo.h b/src/renderer/buffers/vk_ubo.h deleted file mode 100644 index 0c2c091..0000000 --- a/src/renderer/buffers/vk_ubo.h +++ /dev/null @@ -1,48 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* vk_ubo.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:32 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_VK_UBO__ -#define __MLX_VK_UBO__ - -#include "vk_buffer.h" - -namespace mlx -{ - class UBO - { - public: - void create(class Renderer* renderer, std::uint32_t size, const char* name); - - void setData(std::uint32_t size, const void* data); - void setDynamicData(std::uint32_t size, const void* data); - - void destroy() noexcept; - - unsigned int getSize() noexcept; - unsigned int getOffset() noexcept; - VkDeviceMemory getDeviceMemory() noexcept; - VkBuffer& operator()() noexcept; - VkBuffer& get() noexcept; - - inline unsigned int getSize(int i) noexcept { return _buffers[i].getSize(); } - inline unsigned int getOffset(int i) noexcept { return _buffers[i].getOffset(); } - inline VkBuffer& operator()(int i) noexcept { return _buffers[i].get(); } - inline VkBuffer& get(int i) noexcept { return _buffers[i].get(); } - - private: - std::array _buffers; - std::array _maps; - class Renderer* _renderer = nullptr; - }; -} - -#endif // __MLX_VK_UBO__ diff --git a/src/renderer/buffers/vk_vbo.h b/src/renderer/buffers/vk_vbo.h deleted file mode 100644 index 88d0023..0000000 --- a/src/renderer/buffers/vk_vbo.h +++ /dev/null @@ -1,46 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* vk_vbo.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */ -/* Updated: 2024/01/10 23:04:40 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_VK_VBO__ -#define __MLX_VK_VBO__ - -#include "vk_buffer.h" -#include -#include - -namespace mlx -{ - class VBO : public Buffer - { - public: - inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } - void setData(std::uint32_t size, const void* data); - inline void bind(Renderer& renderer) noexcept { renderer.getActiveCmdBuffer().bindVertexBuffer(*this); } - }; - - class D_VBO : public Buffer - { - public: - inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::dynamic_device_local, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } - void setData(std::uint32_t size, const void* data); - inline void bind(Renderer& renderer) noexcept { renderer.getActiveCmdBuffer().bindVertexBuffer(*this); } - }; - - class C_VBO : public Buffer - { - public: - inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::create(Buffer::kind::constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); } - inline void bind(Renderer& renderer) noexcept { renderer.getActiveCmdBuffer().bindVertexBuffer(*this); } - }; -} - -#endif // __MLX_VK_VBO__ diff --git a/src/renderer/command/vk_cmd_buffer.h b/src/renderer/command/vk_cmd_buffer.h deleted file mode 100644 index 8af1f2e..0000000 --- a/src/renderer/command/vk_cmd_buffer.h +++ /dev/null @@ -1,85 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* vk_cmd_buffer.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:12:17 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_VK_CMD_BUFFER__ -#define __MLX_VK_CMD_BUFFER__ - -#include - -namespace mlx -{ - class Buffer; - class Image; - - class CmdBuffer - { - public: - enum class state - { - uninit = 0, // buffer not initialized or destroyed - ready, // buffer ready to be used after having been submitted - idle, // buffer has recorded informations but has not been submitted - recording, // buffer is currently recording - submitted, // buffer has been submitted - }; - - enum class kind - { - single_time = 0, - long_time - }; - - public: - void init(kind type, class CmdManager* manager); - void init(kind type, class CmdPool* pool); - void destroy() noexcept; - - void beginRecord(VkCommandBufferUsageFlags usage = 0); - void submit(class Semaphore* semaphores) noexcept; - void submitIdle(bool shouldWaitForExecution = true) noexcept; // TODO : handle `shouldWaitForExecution` as false by default (needs to modify CmdResources lifetimes to do so) - void updateSubmitState() noexcept; - inline void waitForExecution() noexcept { _fence.wait(); updateSubmitState(); _state = state::ready; } - inline void reset() noexcept { vkResetCommandBuffer(_cmd_buffer, 0); } - void endRecord(); - - void bindVertexBuffer(Buffer& buffer) noexcept; - void bindIndexBuffer(Buffer& buffer) noexcept; - void copyBuffer(Buffer& dst, Buffer& src) noexcept; - void copyBufferToImage(Buffer& buffer, Image& image) noexcept; - void copyImagetoBuffer(Image& image, Buffer& buffer) noexcept; - void transitionImageLayout(Image& image, VkImageLayout new_layout) noexcept; - - inline bool isInit() const noexcept { return _state != state::uninit; } - inline bool isReadyToBeUsed() const noexcept { return _state == state::ready; } - inline bool isRecording() const noexcept { return _state == state::recording; } - inline bool hasBeenSubmitted() const noexcept { return _state == state::submitted; } - inline state getCurrentState() const noexcept { return _state; } - - inline VkCommandBuffer& operator()() noexcept { return _cmd_buffer; } - inline VkCommandBuffer& get() noexcept { return _cmd_buffer; } - inline Fence& getFence() noexcept { return _fence; } - - private: - void preTransferBarrier() noexcept; - void postTransferBarrier() noexcept; - - private: - std::vector _cmd_resources; - Fence _fence; - VkCommandBuffer _cmd_buffer = VK_NULL_HANDLE; - class CmdPool* _pool = nullptr; - state _state = state::uninit; - kind _type; - }; -} - -#endif // __MLX_VK_CMD_BUFFER__ diff --git a/src/renderer/core/render_core.h b/src/renderer/core/render_core.h deleted file mode 100644 index f2154c8..0000000 --- a/src/renderer/core/render_core.h +++ /dev/null @@ -1,83 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* render_core.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */ -/* Updated: 2024/03/25 19:11:59 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_RENDER_CORE__ -#define __MLX_RENDER_CORE__ - -#include -#include -#include -#include "vk_queues.h" -#include "vk_device.h" -#include "vk_instance.h" -#include "vk_validation_layers.h" -#include "memory.h" - -#include -#include - -namespace mlx -{ - namespace RCore - { - std::optional findMemoryType(std::uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error = true); - const char* verbaliseResultVk(VkResult result); - VkPipelineStageFlags accessFlagsToPipelineStage(VkAccessFlags accessFlags, VkPipelineStageFlags stageFlags); - } - - #ifdef DEBUG - constexpr const bool enableValidationLayers = true; - #else - constexpr const bool enableValidationLayers = false; - #endif - - const std::vector validationLayers = { "VK_LAYER_KHRONOS_validation" }; - - constexpr const int MAX_FRAMES_IN_FLIGHT = 3; - constexpr const int MAX_SETS_PER_POOL = 512; - constexpr const int NUMBER_OF_UNIFORM_BUFFERS = 1; // change this if for wathever reason more than one uniform buffer is needed - - class Render_Core : public Singleton - { - friend class Singleton; - - public: - void init(); - void destroy(); - - inline bool isInit() const noexcept { return _is_init; } - inline Instance& getInstance() noexcept { return _instance; } - inline Device& getDevice() noexcept { return _device; } - inline Queues& getQueue() noexcept { return _queues; } - inline GPUallocator& getAllocator() noexcept { return _allocator; } - inline ValidationLayers& getLayers() noexcept { return _layers; } - inline CmdBuffer& getSingleTimeCmdBuffer() noexcept { return _cmd_manager.getCmdBuffer(); } - inline SingleTimeCmdManager& getSingleTimeCmdManager() noexcept { return _cmd_manager; } - inline DescriptorPool& getDescriptorPool() { return _pool_manager.getAvailablePool(); } - - private: - Render_Core() = default; - ~Render_Core() = default; - - private: - ValidationLayers _layers; - SingleTimeCmdManager _cmd_manager; - Queues _queues; - DescriptorPoolManager _pool_manager; - Device _device; - Instance _instance; - GPUallocator _allocator; - bool _is_init = false; - }; -} - -#endif // __MLX_RENDER_CORE__