From a70bec474002700bad8fa1855795670c97b948b7 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Wed, 2 Aug 2023 12:50:50 +0200 Subject: [PATCH 1/3] fixing performance issues --- Makefile | 6 +- includes/mlx.h | 243 +++++++++++++++++- src/core/application.cpp | 30 +-- src/core/application.h | 15 +- src/core/application.inl | 27 +- src/core/bridge.cpp | 126 ++++----- src/core/errors.cpp | 10 +- src/core/graphics.h | 6 +- src/core/graphics.inl | 4 +- src/platform/inputs.cpp | 5 +- src/platform/inputs.h | 6 +- src/renderer/buffers/vk_buffer.cpp | 4 +- src/renderer/buffers/vk_buffer.h | 2 +- src/renderer/buffers/vk_ibo.h | 2 +- src/renderer/buffers/vk_ubo.cpp | 2 +- src/renderer/buffers/vk_ubo.h | 2 +- src/renderer/buffers/vk_vbo.h | 2 +- src/renderer/command/vk_cmd_buffer.cpp | 33 ++- src/renderer/command/vk_cmd_buffer.h | 8 +- src/renderer/core/render_core.cpp | 22 +- src/renderer/core/render_core.h | 7 +- .../descriptors/vk_descriptor_set.cpp | 2 +- src/renderer/images/texture.cpp | 90 ++++--- src/renderer/images/texture.h | 20 +- src/renderer/images/texture_atlas.cpp | 4 +- src/renderer/images/vk_image.cpp | 126 ++++----- src/renderer/images/vk_image.h | 10 +- src/renderer/pixel_put.cpp | 25 +- src/renderer/pixel_put.h | 11 +- src/renderer/renderer.cpp | 2 +- src/renderer/text_library.cpp | 3 +- src/renderer/text_pipeline.cpp | 4 +- src/renderer/texture_library.cpp | 54 ---- src/renderer/texture_library.h | 46 ---- test/.gdb_history | 59 +++++ test/main.c | 42 +-- 36 files changed, 629 insertions(+), 431 deletions(-) delete mode 100644 src/renderer/texture_library.cpp delete mode 100644 src/renderer/texture_library.h create mode 100644 test/.gdb_history diff --git a/Makefile b/Makefile index a0bd7d7..369b703 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2023/04/04 16:21:17 by maldavid ### ########.fr # +# Updated: 2023/06/06 15:59:27 by maldavid ### ########.fr # # # # **************************************************************************** # @@ -29,10 +29,10 @@ ifeq ($(TOOLCHAIN), gcc) endif CXXFLAGS = -std=c++17 -O3 -fPIC -INCLUDES = -I./includes -I./src -I./third_party +INCLUDES = -I./includes -I./src -I./third_party -I ~/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/include ifeq ($(DEBUG), true) - CXXFLAGS += -g + CXXFLAGS += -g -D DEBUG endif RM = rm -f diff --git a/includes/mlx.h b/includes/mlx.h index 3fa5114..58faa17 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2023/04/12 19:33:39 by maldavid ### ########.fr */ +/* Updated: 2023/04/25 15:09:04 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,41 +26,276 @@ typedef enum MLX_WINDOW_EVENT = 4 } mlx_event_type; +/** + * @brief Initializes the MLX internal application + * + * @return (void*) An opaque pointer to the internal MLX application or NULL (0x0) in case of error + */ void* mlx_init(); + +/** + * @brief Creates a new window + * + * @param mlx Internal MLX application + * @param w Width of the window + * @param h Height of the window + * @param title Title of the window + * + * @return (void*) An opaque pointer to the internal MLX window or NULL (0x0) in case of error + */ void* mlx_new_window(void* mlx, int w, int h, const char* title); +/** + * @brief Gives a function to be executed at each loop turn + * + * @param mlx Internal MLX application + * @param f The function + * @param param Param to give to the function passed + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ + int mlx_loop_hook(void* mlx, int (*f)(), void* param); + +/** + * @brief Starts the internal main loop + * + * @param mlx Internal MLX application + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_loop(void* mlx); + +/** + * @brief Ends the internal main loop + * + * @param mlx Internal MLX application + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_loop_end(void* mlx); +/** + * @brief Shows mouse cursor + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_mouse_show(); + +/** + * @brief Hides mouse cursor + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_mouse_hide(); + +/** + * @brief Moves cursor to givent position + * + * @param mlx Internal MLX application + * @param win Internal window from which cursor moves + * @param x X coordinate + * @param y Y coordinate + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_mouse_move(void* mlx, void* win, int x, int y); + +/** + * @brief Get cursor's position + * + * @param mlx Internal MLX application + * @param x Get x coordinate + * @param y Get y coordinate + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_mouse_get_pos(void* mlx, int* x, int* y); + +/** + * @brief Gives a function to be executed on event type + * + * @param mlx Internal MLX application + * @param win Internal window + * @param event Event type (see union on top of this file) + * @param f Function to be executed + * @param param Parameter given to the function + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*f)(), void* param); -int mlx_do_key_autorepeaton(void* mlx); -int mlx_do_key_autorepeatoff(void* mlx); +/** + * @brief Put a pixel in the window + * + * @param mlx Internal MLX application + * @param win Internal window + * @param x X coordinate + * @param y Y coordinate + * @param color Color of the pixel (coded on 3 bytes in an int, 0x00RRGGBB) + * + * Note : If your're reading pixel colors from an image, don't forget to shift them + * one byte to the right as image pixels are encoded as 0xRRGGBBAA and pixel put takes 0x00RRGGBB. + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_pixel_put(void* mlx, void* win, int x, int y, int color); + +/** + * @brief Create a new empty image + * + * @param mlx Internal MLX application + * @param width Width of the image + * @param height Height of the image + * + * @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error + */ void* mlx_new_image(void* mlx, int width, int height); -char* mlx_get_data_addr(void* mlx, void* img, int* bits_per_pixel, int* size_line, int* endian); + +/** + * @brief Get image pixel data + * + * @param mlx Internal MLX application + * @param img Internal image + * @param x X coordinate in the image + * @param y Y coordinate in the image + * + * @return (int) Return the pixel data + */ +int mlx_get_image_pixel(void* mlx, void* img, int x, int y); + +/** + * @brief Set image pixel data + * + * @param mlx Internal MLX application + * @param img Internal image + * @param x X coordinate in the image + * @param y Y coordinate in the image + * @param color Color of the pixel to set + * + * @return (void) + */ +void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color); + +/** + * @brief Put image to the given window + * + * @param mlx Internal MLX application + * @param win Internal window + * @param img Internal image + * @param x X coordinate + * @param y Y coordinate + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y); + +/** + * @brief Destroys internal image + * + * @param mlx Internal MLX application + * @param img Internal image + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_destroy_image(void* mlx, void* img); + +/** + * @brief Create a new image from a png file + * + * @param mlx Internal MLX application + * @param filename Path to the png file + * @param width Get the width of the image + * @param heigth Get the height of the image + * + * @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error + */ void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height); + +/** + * @brief Create a new image from a jpg file + * + * @param mlx Internal MLX application + * @param filename Path to the jpg file + * @param width Get the width of the image + * @param heigth Get the height of the image + * + * @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error + */ void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height); + +/** + * @brief Create a new image from a bmp file + * + * @param mlx Internal MLX application + * @param filename Path to the bmp file + * @param width Get the width of the image + * @param heigth Get the height of the image + * + * @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error + */ void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height); + +/** + * @brief Put text in given window + * + * @param mlx Internal MLX application + * @param win Internal window + * @param x X coordinate + * @param y Y coordinate + * @param color Color of the pixel (coded on 3 bytes in an int, 0x00RRGGBB) + * @param str Text to put + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str); + +/** + * @brief Clears the given window (resets all rendered data) + * + * @param mlx Internal MLX application + * @param win Internal window + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_clear_window(void* mlx, void* win); + +/** + * @brief Destroys internal window + * + * @param mlx Internal MLX application + * @param win Internal window + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_destroy_window(void* mlx, void* win); + +/** + * @brief Destroy internal MLX application + * + * @param mlx Internal MLX application + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_destroy_display(void* mlx); + +/** + * @brief Get screen size + * + * @param mlx Internal MLX application + * @param x Get X size + * @param y Get Y size + * + * @return (int) Always return 0, made this to copy the behaviour of the original MLX + */ int mlx_get_screens_size(void* mlx, int* w, int* h); #ifdef __cplusplus diff --git a/src/core/application.cpp b/src/core/application.cpp index ae8ca9e..0ed073c 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/04/12 11:03:57 by maldavid ### ########.fr */ +/* Updated: 2023/04/25 15:12:57 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,36 +36,20 @@ namespace mlx::core void* Application::newTexture(int w, int h) { - std::shared_ptr texture = std::make_shared(); - texture->create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM); - TextureID id = _texture_lib.addTextureToLibrary(texture); - _texture_ids.push_back(id); - return &_texture_ids.back(); + _textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM); + return &_textures.front(); } void* Application::newStbTexture(char* file, int* w, int* h) { - std::shared_ptr texture = std::make_shared(stbTextureLoad(file, w, h)); - TextureID id = _texture_lib.addTextureToLibrary(texture); - _texture_ids.push_back(id); - return &_texture_ids.back(); - } - - char* Application::mapTexture(void* img, int* bits_per_pixel, int* size_line, int* endian) - { - TextureID id = *static_cast(img); - std::shared_ptr texture = _texture_lib.getTexture(id); - char* map = static_cast(texture->openCPUmap()); - *bits_per_pixel = sizeof(uint32_t) * 8; - *size_line = texture->getWidth(); - *endian = isSystemBigEndian(); - return map; + _textures.emplace_front(stbTextureLoad(file, w, h)); + return &_textures.front(); } void Application::destroyTexture(void* ptr) { vkDeviceWaitIdle(Render_Core::get().getDevice().get()); - TextureID id = *static_cast(ptr); - _texture_lib.removeTextureFromLibrary(id); + Texture* texture = static_cast(ptr); + texture->destroy(); } } diff --git a/src/core/application.h b/src/core/application.h index fb46936..e287f70 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2023/04/13 10:56:19 by maldavid ### ########.fr */ +/* Updated: 2023/04/25 15:23:31 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,6 @@ #include #include -#include - namespace mlx::core { class Application @@ -39,22 +37,20 @@ namespace mlx::core inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept; - inline void enableAutoRepeat() noexcept; - inline void disableAutoRepeat() noexcept; - inline void getScreenSize(int* w, int* h) noexcept; inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title); inline void clearGraphicsSupport(void* win); inline void destroyGraphicsSupport(void* win); - inline void pixelPut(void* win, int x, int y, int color) const noexcept; + inline void pixelPut(void* win, int x, int y, uint32_t color) const noexcept; inline void stringPut(void* win, int x, int y, int 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, ...) - char* mapTexture(void* img, int* bits_per_pixel, int* size_line, int* endian); 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, uint32_t color); void destroyTexture(void* ptr); inline void loopHook(int (*f)(void*), void* param); @@ -65,8 +61,7 @@ namespace mlx::core ~Application() = default; private: - TextureLibrary _texture_lib; - std::list _texture_ids; + std::list _textures; std::vector> _graphics; std::function _loop_hook; std::unique_ptr _in; diff --git a/src/core/application.inl b/src/core/application.inl index 5111fb2..ba7a2fe 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -32,16 +32,6 @@ namespace mlx::core _in->onEvent(_graphics[*static_cast(win)]->getWindow()->getID(), event, funct_ptr, param); } - void Application::enableAutoRepeat() noexcept - { - _in->enableAutoRepeat(); - } - - void Application::disableAutoRepeat() noexcept - { - _in->disableAutoRepeat(); - } - void Application::getScreenSize(int* w, int* h) noexcept { SDL_DisplayMode DM; @@ -67,7 +57,7 @@ namespace mlx::core _graphics[*static_cast(win)].reset(); } - void Application::pixelPut(void* win, int x, int y, int color) const noexcept + void Application::pixelPut(void* win, int x, int y, uint32_t color) const noexcept { _graphics[*static_cast(win)]->pixelPut(x, y, color); } @@ -79,11 +69,22 @@ namespace mlx::core void Application::texturePut(void* win, void* img, int x, int y) { - TextureID id = *static_cast(img); - std::shared_ptr texture = _texture_lib.getTexture(id); + Texture* texture = static_cast(img); _graphics[*static_cast(win)]->texturePut(texture, x, y); } + int Application::getTexturePixel(void* img, int x, int y) + { + Texture* texture = static_cast(img); + return texture->getPixel(x, y); + } + + void Application::setTexturePixel(void* img, int x, int y, uint32_t color) + { + Texture* texture = static_cast(img); + texture->setPixel(x, y, color); + } + void Application::loopHook(int (*f)(void*), void* param) { _loop_hook = f; diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index b26e490..3f7854d 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2023/04/12 19:34:22 by maldavid ### ########.fr */ +/* Updated: 2023/04/25 15:23:05 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,26 +24,26 @@ extern "C" return new mlx::core::Application(); } - void* mlx_new_window(void* mlx, int w, int h, const char* title) + void* mlx_new_window(mlx::core::Application* mlx, int w, int h, const char* title) { - return static_cast(mlx)->newGraphicsSuport(w, h, title); + return mlx->newGraphicsSuport(w, h, title); } - int mlx_loop_hook(void* mlx, int (*f)(void*), void* param) + int mlx_loop_hook(mlx::core::Application* mlx, int (*f)(void*), void* param) { - static_cast(mlx)->loopHook(f, param); + mlx->loopHook(f, param); return 0; } - int mlx_loop(void* mlx) + int mlx_loop(mlx::core::Application* mlx) { - static_cast(mlx)->run(); + mlx->run(); return 0; } - int mlx_loop_end(void* mlx) + int mlx_loop_end(mlx::core::Application* mlx) { - static_cast(mlx)->loopEnd(); + mlx->loopEnd(); return 0; } @@ -57,59 +57,57 @@ extern "C" return SDL_ShowCursor(SDL_DISABLE); } - int mlx_mouse_move(void* mlx, void* win, int x, int y) + int mlx_mouse_move(mlx::core::Application* mlx, void* win, int x, int y) { - static_cast(mlx)->mouseMove(win, x, y); + mlx->mouseMove(win, x, y); return 0; } - int mlx_mouse_get_pos(void* mlx, int* x, int* y) + int mlx_mouse_get_pos(mlx::core::Application* mlx, int* x, int* y) { - static_cast(mlx)->getMousePos(x, y); + mlx->getMousePos(x, y); return 0; } - int mlx_on_event(void* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param) + int mlx_on_event(mlx::core::Application* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param) { - static_cast(mlx)->onEvent(win, event, funct_ptr, param); + mlx->onEvent(win, event, funct_ptr, param); return 0; } - int mlx_do_key_autorepeaton(void* mlx) + void* mlx_new_image(mlx::core::Application* mlx, int width, int height) { - static_cast(mlx)->enableAutoRepeat(); + return mlx->newTexture(width, height); + } + + int mlx_get_image_pixel(mlx::core::Application* mlx, void* img, int x, int y) + { + return mlx->getTexturePixel(img, x, y); + } + + void mlx_set_image_pixel(mlx::core::Application* mlx, void* img, int x, int y, int color) + { + unsigned char color_bits[4]; + color_bits[0] = (color & 0x00FF0000) >> 16; + color_bits[1] = (color & 0x0000FF00) >> 8; + color_bits[2] = color & 0x000000FF; + color_bits[3] = 0xFF; + mlx->setTexturePixel(img, x, y, *reinterpret_cast(color_bits)); + } + + int mlx_put_image_to_window(mlx::core::Application* mlx, void* win, void* img, int x, int y) + { + mlx->texturePut(win, img, x, y); return 0; } - int mlx_do_key_autorepeatoff(void* mlx) + int mlx_destroy_image(mlx::core::Application* mlx, void* img) { - static_cast(mlx)->disableAutoRepeat(); + mlx->destroyTexture(img); return 0; } - void* mlx_new_image(void* mlx, int width, int height) - { - return static_cast(mlx)->newTexture(width, height); - } - - char* mlx_get_data_addr(void* mlx, void* img, int* bits_per_pixel, int* size_line, int* endian) - { - return static_cast(mlx)->mapTexture(img, bits_per_pixel, size_line, endian); - } - - int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y) - { - static_cast(mlx)->texturePut(win, img, x, y); - return 0; - } - - int mlx_destroy_image(void* mlx, void* img) - { - static_cast(mlx)->destroyTexture(img); - return 0; - } - - void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height) + void* mlx_png_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height) { std::filesystem::path file(filename); if(file.extension() != ".png") @@ -117,10 +115,10 @@ extern "C" mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename); return nullptr; } - return static_cast(mlx)->newStbTexture(filename, width, height); + return mlx->newStbTexture(filename, width, height); } - void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height) + void* mlx_jpg_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height) { std::filesystem::path file(filename); if(file.extension() != ".jpg" && file.extension() != ".jpeg") @@ -128,10 +126,10 @@ extern "C" mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename); return nullptr; } - return static_cast(mlx)->newStbTexture(filename, width, height); + return mlx->newStbTexture(filename, width, height); } - void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height) + void* mlx_bmp_file_to_image(mlx::core::Application* mlx, char* filename, int* width, int* height) { std::filesystem::path file(filename); if(file.extension() != ".bmp" && file.extension() != ".dib") @@ -139,43 +137,53 @@ extern "C" mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename); return nullptr; } - return static_cast(mlx)->newStbTexture(filename, width, height); + return mlx->newStbTexture(filename, width, height); } - int mlx_pixel_put(void* mlx, void* win, int x, int y, int color) + int mlx_pixel_put(mlx::core::Application* mlx, void* win, int x, int y, int color) { - static_cast(mlx)->pixelPut(win, x, y, color); + unsigned char color_bits[4]; + color_bits[0] = (color & 0x00FF0000) >> 16; + color_bits[1] = (color & 0x0000FF00) >> 8; + color_bits[2] = color & 0x000000FF; + color_bits[3] = 0xFF; + mlx->pixelPut(win, x, y, *reinterpret_cast(color_bits)); return 0; } - int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str) + int mlx_string_put(mlx::core::Application* mlx, void* win, int x, int y, int color, char* str) { - static_cast(mlx)->stringPut(win, x, y, color, str); + unsigned char color_bits[4]; + color_bits[0] = (color & 0x00FF0000) >> 16; + color_bits[1] = (color & 0x0000FF00) >> 8; + color_bits[2] = color & 0x000000FF; + color_bits[3] = 0xFF; + mlx->stringPut(win, x, y, *reinterpret_cast(color_bits), str); return 0; } - int mlx_clear_window(void* mlx, void* win) + int mlx_clear_window(mlx::core::Application* mlx, void* win) { - static_cast(mlx)->clearGraphicsSupport(win); + mlx->clearGraphicsSupport(win); return 0; } - int mlx_destroy_window(void* mlx, void* win) + int mlx_destroy_window(mlx::core::Application* mlx, void* win) { - static_cast(mlx)->destroyGraphicsSupport(win); + mlx->destroyGraphicsSupport(win); return 0; } - int mlx_destroy_display(void* mlx) + int mlx_destroy_display(mlx::core::Application* mlx) { - delete static_cast(mlx); + delete mlx; mlx::Render_Core::get().destroy(); return 0; } - int mlx_get_screens_size(void* mlx, int* w, int* h) + int mlx_get_screens_size(mlx::core::Application* mlx, int* w, int* h) { - static_cast(mlx)->getScreenSize(w, h); + mlx->getScreenSize(w, h); return 0; } } diff --git a/src/core/errors.cpp b/src/core/errors.cpp index c78ad4c..d2b6155 100644 --- a/src/core/errors.cpp +++ b/src/core/errors.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:48:06 by maldavid #+# #+# */ -/* Updated: 2022/12/18 17:47:51 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 13:07:56 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,11 +30,11 @@ namespace mlx::core::error switch(kind) { - case e_kind::message: std::cout << "[MacroLibX] Message : " << buffer << std::endl; break; - case e_kind::warning: std::cout << "[MacroLibX] Warning : " << buffer << std::endl; break; - case e_kind::error: std::cerr << "[MacroLibX] Error : " << buffer << std::endl; break; + 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 << "[MacroLibX] Fatal Error : " << buffer << std::endl; + 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 index bac82f5..66606d8 100644 --- a/src/core/graphics.h +++ b/src/core/graphics.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ -/* Updated: 2023/04/11 18:37:11 by maldavid ### ########.fr */ +/* Updated: 2023/04/21 18:43:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,9 +40,9 @@ namespace mlx inline void beginRender() noexcept; inline void clearRenderData() noexcept; - inline void pixelPut(int x, int y, int color) noexcept; + inline void pixelPut(int x, int y, uint32_t color) noexcept; inline void stringPut(int x, int y, int color, std::string str); - inline void texturePut(std::shared_ptr texture, int x, int y); + inline void texturePut(Texture* texture, int x, int y); void endRender() noexcept; diff --git a/src/core/graphics.inl b/src/core/graphics.inl index 9bfa1f6..d7bc2aa 100644 --- a/src/core/graphics.inl +++ b/src/core/graphics.inl @@ -32,7 +32,7 @@ namespace mlx _text_put_pipeline->clear(); } - void GraphicsSupport::pixelPut(int x, int y, int color) noexcept + void GraphicsSupport::pixelPut(int x, int y, uint32_t color) noexcept { _pixel_put_pipeline.setPixel(x, y, color); } @@ -42,7 +42,7 @@ namespace mlx _text_put_pipeline->put(x, y, color, str); } - void GraphicsSupport::texturePut(std::shared_ptr texture, int x, int y) + void GraphicsSupport::texturePut(Texture* texture, int x, int y) { _textures_to_render.emplace(texture, x, y); } diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index bfb27f6..27e81a2 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ -/* Updated: 2023/04/12 19:54:20 by maldavid ### ########.fr */ +/* Updated: 2023/04/19 12:14:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,9 +26,6 @@ namespace mlx _xRel = 0; _yRel = 0; - if(!_auto_repeat) - std::memset(_keys.data(), 0, SDL_NUM_SCANCODES); - while(SDL_PollEvent(&_event)) { if(_event.type == SDL_MOUSEMOTION) diff --git a/src/platform/inputs.h b/src/platform/inputs.h index af3296b..60c98d1 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2023/04/13 10:55:46 by maldavid ### ########.fr */ +/* Updated: 2023/04/19 12:14:43 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,9 +51,6 @@ namespace mlx inline bool is_running() const noexcept { return !_end; } inline constexpr void finish() noexcept { _end = true; } - inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; } - inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; } - inline void addWindow(std::shared_ptr window) { _windows[window->getID()] = window; @@ -81,6 +78,5 @@ namespace mlx int _yRel = 0; bool _end = false; - bool _auto_repeat = true; }; } diff --git a/src/renderer/buffers/vk_buffer.cpp b/src/renderer/buffers/vk_buffer.cpp index 2853190..f3c2dae 100644 --- a/src/renderer/buffers/vk_buffer.cpp +++ b/src/renderer/buffers/vk_buffer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */ -/* Updated: 2023/01/25 15:24:40 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 12:37:32 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,7 +81,7 @@ namespace mlx VkMemoryAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocInfo.allocationSize = memRequirements.size; - allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties); + allocInfo.memoryTypeIndex = *RCore::findMemoryType(memRequirements.memoryTypeBits, properties); if(vkAllocateMemory(device, &allocInfo, nullptr, &_memory) != VK_SUCCESS) core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate buffer memory"); diff --git a/src/renderer/buffers/vk_buffer.h b/src/renderer/buffers/vk_buffer.h index afce6a4..08b87b5 100644 --- a/src/renderer/buffers/vk_buffer.h +++ b/src/renderer/buffers/vk_buffer.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */ -/* Updated: 2023/03/31 15:29:01 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:51:47 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/buffers/vk_ibo.h b/src/renderer/buffers/vk_ibo.h index 5f16ecb..7312060 100644 --- a/src/renderer/buffers/vk_ibo.h +++ b/src/renderer/buffers/vk_ibo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */ -/* Updated: 2023/01/25 15:36:41 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:51:54 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/buffers/vk_ubo.cpp b/src/renderer/buffers/vk_ubo.cpp index fd96343..f8d0a55 100644 --- a/src/renderer/buffers/vk_ubo.cpp +++ b/src/renderer/buffers/vk_ubo.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:45:52 by maldavid #+# #+# */ -/* Updated: 2023/01/23 18:54:12 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:51:56 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/buffers/vk_ubo.h b/src/renderer/buffers/vk_ubo.h index d639418..b31e63d 100644 --- a/src/renderer/buffers/vk_ubo.h +++ b/src/renderer/buffers/vk_ubo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */ -/* Updated: 2023/01/25 15:37:09 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:51:59 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/buffers/vk_vbo.h b/src/renderer/buffers/vk_vbo.h index 2a5b740..ebadecf 100644 --- a/src/renderer/buffers/vk_vbo.h +++ b/src/renderer/buffers/vk_vbo.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */ -/* Updated: 2023/01/25 15:37:00 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:52:05 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/command/vk_cmd_buffer.cpp b/src/renderer/command/vk_cmd_buffer.cpp index f9572ad..2d30b6c 100644 --- a/src/renderer/command/vk_cmd_buffer.cpp +++ b/src/renderer/command/vk_cmd_buffer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:26:06 by maldavid #+# #+# */ -/* Updated: 2023/04/02 18:13:38 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 15:19:08 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,16 @@ namespace mlx { void CmdBuffer::init(CmdManager* manager) { - _manager = manager; + init(&manager->getCmdPool()); + } + + void CmdBuffer::init(CmdPool* pool) + { + _pool = pool; VkCommandBufferAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - allocInfo.commandPool = manager->getCmdPool().get(); + allocInfo.commandPool = pool->get(); allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandBufferCount = 1; @@ -57,6 +62,26 @@ namespace mlx _is_recording = false; } + void CmdBuffer::submitIdle() noexcept + { + auto device = Render_Core::get().getDevice().get(); + + VkSubmitInfo submitInfo = {}; + submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = &_cmd_buffer; + + VkFenceCreateInfo fenceCreateInfo = {}; + fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + + VkFence fence; + vkCreateFence(device, &fenceCreateInfo, nullptr, &fence); + vkResetFences(device, 1, &fence); + vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, fence); + vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX); + vkDestroyFence(device, fence, nullptr); + } + void CmdBuffer::submit(Semaphore& semaphores) noexcept { VkSemaphore signalSemaphores[] = { semaphores.getRenderImageSemaphore() }; @@ -79,7 +104,7 @@ namespace mlx void CmdBuffer::destroy() noexcept { - vkFreeCommandBuffers(Render_Core::get().getDevice().get(), _manager->getCmdPool().get(), 1, &_cmd_buffer); _fence.destroy(); + _cmd_buffer = VK_NULL_HANDLE; } } diff --git a/src/renderer/command/vk_cmd_buffer.h b/src/renderer/command/vk_cmd_buffer.h index b94db94..ee6d8df 100644 --- a/src/renderer/command/vk_cmd_buffer.h +++ b/src/renderer/command/vk_cmd_buffer.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */ -/* Updated: 2023/04/02 17:57:26 by maldavid ### ########.fr */ +/* Updated: 2023/04/21 13:20:49 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,23 +22,27 @@ namespace mlx { public: void init(class CmdManager* manager); + void init(class CmdPool* pool); void destroy() noexcept; void beginRecord(VkCommandBufferUsageFlags usage = 0); void submit(class Semaphore& semaphores) noexcept; + void submitIdle() noexcept; inline void waitForExecution() noexcept { _fence.waitAndReset(); } inline void reset() noexcept { vkResetCommandBuffer(_cmd_buffer, 0); } void endRecord(); inline bool isRecording() const noexcept { return _is_recording; } + inline bool isInit() const noexcept { return _cmd_buffer != VK_NULL_HANDLE; } inline VkCommandBuffer& operator()() noexcept { return _cmd_buffer; } inline VkCommandBuffer& get() noexcept { return _cmd_buffer; } + inline Fence& getFence() noexcept { return _fence; } private: Fence _fence; VkCommandBuffer _cmd_buffer = VK_NULL_HANDLE; - class CmdManager* _manager = nullptr; + class CmdPool* _pool = nullptr; bool _is_recording = false; }; } diff --git a/src/renderer/core/render_core.cpp b/src/renderer/core/render_core.cpp index 6a0e941..2388186 100644 --- a/src/renderer/core/render_core.cpp +++ b/src/renderer/core/render_core.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */ -/* Updated: 2023/03/31 12:22:34 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 19:09:21 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,13 +23,15 @@ #include "render_core.h" #include +#ifdef DEBUG + #warning "MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages and may impact rendering performances" +#endif + namespace mlx { - std::mutex mutex; - namespace RCore { - uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) + std::optional findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error) { VkPhysicalDeviceMemoryProperties memProperties; vkGetPhysicalDeviceMemoryProperties(Render_Core::get().getDevice().getPhysicalDevice(), &memProperties); @@ -37,11 +39,11 @@ namespace mlx for(uint32_t i = 0; i < memProperties.memoryTypeCount; i++) { if((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) - return i; + return i; } - - core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type"); - return -1; // just to avoid warning + if(error) + core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type"); + return std::nullopt; } } @@ -60,12 +62,10 @@ namespace mlx void Render_Core::destroy() { - std::unique_lock watchdog(mutex, std::try_to_lock); - if(!_is_init) return; - vkDeviceWaitIdle(_device()); + vkDeviceWaitIdle(_device()); _device.destroy(); _layers.destroy(); diff --git a/src/renderer/core/render_core.h b/src/renderer/core/render_core.h index e28e0ff..afc9d3c 100644 --- a/src/renderer/core/render_core.h +++ b/src/renderer/core/render_core.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */ -/* Updated: 2023/01/25 15:23:19 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 12:31:42 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #define __MLX_RENDER_CORE__ #include +#include #include "vk_queues.h" #include "vk_device.h" @@ -27,10 +28,10 @@ namespace mlx { namespace RCore { - uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); + std::optional findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error = true); } - #ifndef NDEBUG + #ifdef DEBUG constexpr const bool enableValidationLayers = true; #else constexpr const bool enableValidationLayers = false; diff --git a/src/renderer/descriptors/vk_descriptor_set.cpp b/src/renderer/descriptors/vk_descriptor_set.cpp index b24009b..c3a1cd3 100644 --- a/src/renderer/descriptors/vk_descriptor_set.cpp +++ b/src/renderer/descriptors/vk_descriptor_set.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */ -/* Updated: 2023/03/31 17:54:38 by maldavid ### ########.fr */ +/* Updated: 2023/04/22 19:52:08 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/images/texture.cpp b/src/renderer/images/texture.cpp index 856502a..62e9d2f 100644 --- a/src/renderer/images/texture.cpp +++ b/src/renderer/images/texture.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */ -/* Updated: 2023/04/08 18:41:54 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 12:34:25 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,19 +14,20 @@ #include #include #include -#include +#include #define STB_IMAGE_IMPLEMENTATION #include +#include + namespace mlx { void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format) { - Image::create(width, height, format, - VK_IMAGE_TILING_OPTIMAL, + Image::create(width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT + { VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT } ); Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); @@ -52,29 +53,58 @@ namespace mlx Image::copyFromBuffer(staging_buffer); staging_buffer.destroy(); } - _cpu_map = nullptr; - _cpu_map_adress = nullptr; } - void* Texture::openCPUmap() + void Texture::setPixel(int x, int y, uint32_t color) noexcept { - if(_cpu_map == nullptr) - { - _cpu_map = std::make_shared(); - _cpu_map->create(Buffer::kind::dynamic, sizeof(uint32_t) * (getWidth() * getHeight()), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); - Image::copyToBuffer(*_cpu_map); - } - if(!_cpu_map->isMapped()) - _cpu_map->mapMem(&_cpu_map_adress); - if(_cpu_map_adress == nullptr) - core::error::report(e_kind::fatal_error, "Texture : CPU memory mapping failed"); - return _cpu_map_adress; + if(x < 0 || y < 0 || x > getWidth() || y > getHeight()) + return; + if(_map == nullptr) + openCPUmap(); + _cpu_map[(y * getWidth()) + x] = color; + _has_been_modified = true; + } + + int Texture::getPixel(int x, int y) noexcept + { + if(x < 0 || y < 0 || x > getWidth() || y > getHeight()) + return 0; + if(_map == nullptr) + openCPUmap(); + uint32_t color = _cpu_map[(y * getWidth()) + x]; + color >>= 8; + return (color); + } + + void Texture::openCPUmap() + { + if(_map != nullptr) + return; + + #ifdef DEBUG + core::error::report(e_kind::message, "Texture : enabling CPU mapping"); + #endif + + std::size_t size = getWidth() * getHeight() * formatSize(getFormat()); + _buf_map.emplace(); + _buf_map->create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); + Image::copyToBuffer(*_buf_map); + _buf_map->mapMem(&_map); + _cpu_map = std::vector(getWidth() * getHeight(), 0); + std::memcpy(_cpu_map.data(), _map, size); + #ifdef DEBUG + core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer"); + #endif } void Texture::render(Renderer& renderer, int x, int y) { - if(_cpu_map_adress != nullptr) - Image::copyFromBuffer(*_cpu_map); + if(_has_been_modified) + { + std::memcpy(_map, _cpu_map.data(), _cpu_map.size() * formatSize(getFormat())); + Image::copyFromBuffer(*_buf_map); + _has_been_modified = false; + } auto cmd = renderer.getActiveCmdBuffer().get(); _vbo.bind(renderer); _ibo.bind(renderer); @@ -86,8 +116,8 @@ namespace mlx void Texture::destroy() noexcept { Image::destroy(); - if(_cpu_map != nullptr) - _cpu_map->destroy(); + if(_buf_map.has_value()) + _buf_map->destroy(); _vbo.destroy(); _ibo.destroy(); } @@ -96,23 +126,15 @@ namespace mlx { Texture texture; int channels; - VkFormat format; uint8_t* data = nullptr; std::string filename = file.string(); if(!std::filesystem::exists(std::move(file))) core::error::report(e_kind::fatal_error, "Image : file not found '%s'", filename.c_str()); if(stbi_is_hdr(filename.c_str())) - { - data = (uint8_t*)stbi_loadf(filename.c_str(), w, h, &channels, 4); - format = VK_FORMAT_R32G32B32A32_SFLOAT; - } - else - { - data = stbi_load(filename.c_str(), w, h, &channels, 4); - format = VK_FORMAT_R8G8B8A8_UNORM; - } - texture.create(data, *w, *h, format); + core::error::report(e_kind::fatal_error, "Texture : unsupported image format '%s'", filename.c_str()); + data = stbi_load(filename.c_str(), w, h, &channels, 4); + texture.create(data, *w, *h, VK_FORMAT_R8G8B8A8_UNORM); stbi_image_free(data); return texture; } diff --git a/src/renderer/images/texture.h b/src/renderer/images/texture.h index 81f96c5..199d6a9 100644 --- a/src/renderer/images/texture.h +++ b/src/renderer/images/texture.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */ -/* Updated: 2023/04/07 16:39:35 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 12:32:27 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,8 @@ namespace mlx void render(class Renderer& renderer, int x, int y); void destroy() noexcept override; - void* openCPUmap(); + void setPixel(int x, int y, uint32_t color) noexcept; + int getPixel(int x, int y) noexcept; inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); } inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } @@ -43,12 +44,17 @@ namespace mlx ~Texture() = default; + private: + void openCPUmap(); + private: C_VBO _vbo; C_IBO _ibo; DescriptorSet _set; - std::shared_ptr _cpu_map; - void* _cpu_map_adress; + std::vector _cpu_map; + std::optional _buf_map = std::nullopt; + void* _map = nullptr; + bool _has_been_modified = false; bool _has_been_updated = false; }; @@ -56,11 +62,11 @@ namespace mlx struct TextureRenderData { - std::shared_ptr texture; + Texture* texture; int x; int y; - TextureRenderData(std::shared_ptr _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {} + TextureRenderData(Texture* _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {} bool operator==(const TextureRenderData& rhs) const { return texture == rhs.texture && x == rhs.x && y == rhs.y; } }; } @@ -72,7 +78,7 @@ namespace std { size_t operator()(const mlx::TextureRenderData& td) const noexcept { - return std::hash>()(td.texture) + std::hash()(td.x) + std::hash()(td.y); + return std::hash()(td.texture) + std::hash()(td.x) + std::hash()(td.y); } }; } diff --git a/src/renderer/images/texture_atlas.cpp b/src/renderer/images/texture_atlas.cpp index 3e57926..1a414c4 100644 --- a/src/renderer/images/texture_atlas.cpp +++ b/src/renderer/images/texture_atlas.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */ -/* Updated: 2023/04/11 12:30:28 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 12:55:22 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ namespace mlx Image::create(width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT + { VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT } ); Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); diff --git a/src/renderer/images/vk_image.cpp b/src/renderer/images/vk_image.cpp index 10f32f4..1e3bedc 100644 --- a/src/renderer/images/vk_image.cpp +++ b/src/renderer/images/vk_image.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */ -/* Updated: 2023/04/13 15:07:01 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 14:59:41 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,14 +14,16 @@ #include #include #include +#include namespace mlx { - void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties) + void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector properties) { _width = width; _height = height; _format = format; + _tiling = tiling; VkImageCreateInfo imageInfo{}; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -43,16 +45,27 @@ namespace mlx VkMemoryRequirements memRequirements; vkGetImageMemoryRequirements(Render_Core::get().getDevice().get(), _image, &memRequirements); - + + std::optional memTypeIndex; + for(auto prop : properties) + { + memTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, prop, false); + if(memTypeIndex.has_value()) + break; + } + if(!memTypeIndex.has_value()) + core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type for an image"); VkMemoryAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocInfo.allocationSize = memRequirements.size; - allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties); + allocInfo.memoryTypeIndex = *memTypeIndex; if(vkAllocateMemory(Render_Core::get().getDevice().get(), &allocInfo, nullptr, &_memory) != VK_SUCCESS) core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate memory for an image"); vkBindImageMemory(Render_Core::get().getDevice().get(), _image, _memory, 0); + + _pool.init(); } void Image::createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept @@ -93,24 +106,11 @@ namespace mlx void Image::copyFromBuffer(Buffer& buffer) { - CmdPool cmdpool; - cmdpool.init(); - auto device = Render_Core::get().getDevice().get(); + if(!_transfer_cmd.isInit()) + _transfer_cmd.init(&_pool); - VkCommandBufferAllocateInfo allocInfo{}; - allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocInfo.commandPool = cmdpool.get(); - allocInfo.commandBufferCount = 1; - - VkCommandBuffer cmdBuffer; - vkAllocateCommandBuffers(device, &allocInfo, &cmdBuffer); - - VkCommandBufferBeginInfo beginInfo{}; - beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - - vkBeginCommandBuffer(cmdBuffer, &beginInfo); + _transfer_cmd.reset(); + _transfer_cmd.beginRecord(); VkImageMemoryBarrier copy_barrier = {}; copy_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -123,7 +123,7 @@ namespace mlx copy_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copy_barrier.subresourceRange.levelCount = 1; copy_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, ©_barrier); + vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, ©_barrier); VkBufferImageCopy region = {}; region.bufferOffset = 0; @@ -133,13 +133,10 @@ namespace mlx region.imageSubresource.mipLevel = 0; region.imageSubresource.baseArrayLayer = 0; region.imageSubresource.layerCount = 1; - region.imageOffset = {0, 0, 0}; - region.imageExtent = { - _width, - _height, - 1 - }; - vkCmdCopyBufferToImage(cmdBuffer, buffer.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + region.imageOffset = { 0, 0, 0 }; + region.imageExtent = { _width, _height, 1 }; + + vkCmdCopyBufferToImage(_transfer_cmd.get(), buffer.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); VkImageMemoryBarrier use_barrier = {}; use_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -153,43 +150,19 @@ namespace mlx use_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; use_barrier.subresourceRange.levelCount = 1; use_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier); + vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier); - vkEndCommandBuffer(cmdBuffer); - - VkSubmitInfo submitInfo{}; - submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &cmdBuffer; - - auto graphicsQueue = Render_Core::get().getQueue().getGraphic(); - - vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); - vkQueueWaitIdle(graphicsQueue); - - cmdpool.destroy(); + _transfer_cmd.endRecord(); + _transfer_cmd.submitIdle(); } void Image::copyToBuffer(Buffer& buffer) { - CmdPool cmdpool; - cmdpool.init(); - auto device = Render_Core::get().getDevice().get(); + if(!_transfer_cmd.isInit()) + _transfer_cmd.init(&_pool); - VkCommandBufferAllocateInfo allocInfo{}; - allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocInfo.commandPool = cmdpool.get(); - allocInfo.commandBufferCount = 1; - - VkCommandBuffer cmdBuffer; - vkAllocateCommandBuffers(device, &allocInfo, &cmdBuffer); - - VkCommandBufferBeginInfo beginInfo{}; - beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - - vkBeginCommandBuffer(cmdBuffer, &beginInfo); + _transfer_cmd.reset(); + _transfer_cmd.beginRecord(); VkImageMemoryBarrier copy_barrier = {}; copy_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -202,7 +175,7 @@ namespace mlx copy_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copy_barrier.subresourceRange.levelCount = 1; copy_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, ©_barrier); + vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, ©_barrier); VkBufferImageCopy region = {}; region.bufferOffset = 0; @@ -212,13 +185,10 @@ namespace mlx region.imageSubresource.mipLevel = 0; region.imageSubresource.baseArrayLayer = 0; region.imageSubresource.layerCount = 1; - region.imageOffset = {0, 0, 0}; - region.imageExtent = { - _width, - _height, - 1 - }; - vkCmdCopyImageToBuffer(cmdBuffer, _image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.get(), 1, ®ion); + region.imageOffset = { 0, 0, 0 }; + region.imageExtent = { _width, _height, 1 }; + + vkCmdCopyImageToBuffer(_transfer_cmd.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.get(), 1, ®ion); VkImageMemoryBarrier use_barrier = {}; use_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -232,21 +202,10 @@ namespace mlx use_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; use_barrier.subresourceRange.levelCount = 1; use_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier); + vkCmdPipelineBarrier(_transfer_cmd.get(), VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &use_barrier); - vkEndCommandBuffer(cmdBuffer); - - VkSubmitInfo submitInfo{}; - submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &cmdBuffer; - - auto graphicsQueue = Render_Core::get().getQueue().getGraphic(); - - vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); - vkQueueWaitIdle(graphicsQueue); - - cmdpool.destroy(); + _transfer_cmd.endRecord(); + _transfer_cmd.submitIdle(); } void Image::destroy() noexcept @@ -258,6 +217,9 @@ namespace mlx vkFreeMemory(Render_Core::get().getDevice().get(), _memory, nullptr); vkDestroyImage(Render_Core::get().getDevice().get(), _image, nullptr); + if(_transfer_cmd.isInit()) + _transfer_cmd.destroy(); + _pool.destroy(); } uint32_t formatSize(VkFormat format) diff --git a/src/renderer/images/vk_image.h b/src/renderer/images/vk_image.h index 6afa88c..7a68b5a 100644 --- a/src/renderer/images/vk_image.h +++ b/src/renderer/images/vk_image.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */ -/* Updated: 2023/04/13 10:59:04 by maldavid ### ########.fr */ +/* Updated: 2023/04/23 14:17:11 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ #include #include #include +#include +#include namespace mlx { @@ -26,7 +28,7 @@ namespace mlx public: Image() = default; - void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties); + void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector properties); void createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept; void createSampler() noexcept; void copyFromBuffer(class Buffer& buffer); @@ -38,6 +40,7 @@ namespace mlx inline VkDeviceMemory getDeviceMemory() noexcept { return _memory; } inline VkImageView getImageView() noexcept { return _image_view; } inline VkFormat getFormat() noexcept { return _format; } + inline VkImageTiling getTiling() noexcept { return _tiling; } inline VkSampler getSampler() noexcept { return _sampler; } inline uint32_t getWidth() const noexcept { return _width; } inline uint32_t getHeight() const noexcept { return _height; } @@ -45,11 +48,14 @@ namespace mlx virtual ~Image() = default; private: + CmdBuffer _transfer_cmd; + CmdPool _pool; VkImage _image = VK_NULL_HANDLE; VkDeviceMemory _memory = VK_NULL_HANDLE; VkImageView _image_view = VK_NULL_HANDLE; VkSampler _sampler = VK_NULL_HANDLE; VkFormat _format; + VkImageTiling _tiling; uint32_t _width = 0; uint32_t _height = 0; }; diff --git a/src/renderer/pixel_put.cpp b/src/renderer/pixel_put.cpp index 0902329..ce66b92 100644 --- a/src/renderer/pixel_put.cpp +++ b/src/renderer/pixel_put.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */ -/* Updated: 2023/04/13 14:51:37 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 05:28:49 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,35 +21,23 @@ namespace mlx _texture.setDescriptor(renderer.getFragDescriptorSet().duplicate()); _buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT); + _buffer.mapMem(&_buffer_map); + _cpu_map = std::vector(height * width, 0); _width = width; _height = height; } - VkDescriptorSet PixelPutPipeline::getDescriptorSet() noexcept - { - return _texture.getSet(); - } - - void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, int color) noexcept + void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept { if(x < 0 || y < 0 || x > _width || y > _height) return; - if(!_buffer.isMapped()) - _buffer.mapMem(&_map); - unsigned char* mem = static_cast(_map) + (y * _width * sizeof(uint32_t)) + (x * sizeof(uint32_t)); - uint32_t new_color = color & 0xFFFFFF00; - new_color >>= 8; - new_color |= (color << 24) & 0xFF000000; - *reinterpret_cast(mem) = new_color; + _cpu_map[(y * _width) + x] = color; _has_been_modified = true; } void PixelPutPipeline::clear() { - if(!_buffer.isMapped()) - _buffer.mapMem(&_map); - unsigned char* mem = static_cast(_map); - std::memset(mem, 0, sizeof(uint32_t) * (_width * _height)); + _cpu_map.assign(_width * _height, 0); _has_been_modified = true; } @@ -57,6 +45,7 @@ namespace mlx { if(_has_been_modified) { + std::memcpy(_buffer_map, _cpu_map.data(), sizeof(uint32_t) * _cpu_map.size()); _texture.copyFromBuffer(_buffer); _has_been_modified = false; } diff --git a/src/renderer/pixel_put.h b/src/renderer/pixel_put.h index d030b23..0742fa5 100644 --- a/src/renderer/pixel_put.h +++ b/src/renderer/pixel_put.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */ -/* Updated: 2023/04/03 14:22:35 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 05:27:27 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,13 +25,12 @@ namespace mlx void init(uint32_t width, uint32_t height, class Renderer& renderer) noexcept; - void setPixel(uint32_t x, uint32_t y, int color) noexcept; + void setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept; void present() noexcept; void render(class Renderer& renderer) noexcept; - VkDescriptorSet getDescriptorSet() noexcept; + inline VkDescriptorSet getDescriptorSet() noexcept { return _texture.getSet(); } void clear(); - void destroy() noexcept; ~PixelPutPipeline(); @@ -39,7 +38,9 @@ namespace mlx private: Texture _texture; Buffer _buffer; - void* _map = nullptr; + // using vector as CPU map and not directly writting to mapped buffer to improve performances + std::vector _cpu_map; + void* _buffer_map = nullptr; uint32_t _width = 0; uint32_t _height = 0; bool _has_been_modified = true; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 64dd62b..f007f6a 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/18 17:25:16 by maldavid #+# #+# */ -/* Updated: 2023/04/02 18:10:01 by maldavid ### ########.fr */ +/* Updated: 2023/04/21 20:56:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/text_library.cpp b/src/renderer/text_library.cpp index 9c94610..ae3c527 100644 --- a/src/renderer/text_library.cpp +++ b/src/renderer/text_library.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */ -/* Updated: 2023/04/12 13:24:19 by maldavid ### ########.fr */ +/* Updated: 2023/04/19 13:43:43 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #include #include #include -#include namespace mlx { diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index 7b2801c..588bb9a 100644 --- a/src/renderer/text_pipeline.cpp +++ b/src/renderer/text_pipeline.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ -/* Updated: 2023/04/13 10:49:04 by maldavid ### ########.fr */ +/* Updated: 2023/04/19 13:43:49 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,6 @@ #define STB_TRUETYPE_IMPLEMENTATION #include -#include - namespace mlx { TextDrawData::TextDrawData(std::string _text, int _color, int _x, int _y) : diff --git a/src/renderer/texture_library.cpp b/src/renderer/texture_library.cpp deleted file mode 100644 index 751d135..0000000 --- a/src/renderer/texture_library.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* texture_library.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/01 14:24:00 by maldavid #+# #+# */ -/* Updated: 2023/04/12 13:26:59 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -namespace mlx -{ - std::shared_ptr TextureLibrary::getTexture(TextureID id) - { - if(!_cache.count(id)) - core::error::report(e_kind::fatal_error, "Texture Library : wrong texture ID '%d'", id); - return _cache[id]; - } - - TextureID TextureLibrary::addTextureToLibrary(std::shared_ptr texture) - { - auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair>& v) - { - return v.second.get() == texture.get(); - }); - if(it != _cache.end()) - return it->first; - _cache[_current_id] = texture; - _current_id++; - return _current_id - 1; - } - - void TextureLibrary::removeTextureFromLibrary(TextureID id) - { - if(_cache.count(id)) - { - _cache[id]->destroy(); - _cache.erase(id); - } - } - - void TextureLibrary::clearLibrary() - { - for(auto [id, texture] : _cache) - texture->destroy(); - _cache.clear(); - } -} diff --git a/src/renderer/texture_library.h b/src/renderer/texture_library.h deleted file mode 100644 index c486333..0000000 --- a/src/renderer/texture_library.h +++ /dev/null @@ -1,46 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* texture_library.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/01 14:16:13 by maldavid #+# #+# */ -/* Updated: 2023/04/01 14:42:39 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_TEXTURE_LIBRARY__ -#define __MLX_TEXTURE_LIBRARY__ - -#include -#include -#include -#include -#include - -namespace mlx -{ - using TextureID = uint32_t; - constexpr TextureID nulltexture = 0; - - class TextureLibrary - { - public: - TextureLibrary() = default; - - std::shared_ptr getTexture(TextureID id); - TextureID addTextureToLibrary(std::shared_ptr texture); - void removeTextureFromLibrary(TextureID id); - - void clearLibrary(); - - ~TextureLibrary() = default; - - private: - std::unordered_map> _cache; - TextureID _current_id = 1; - }; -} - -#endif diff --git a/test/.gdb_history b/test/.gdb_history new file mode 100644 index 0000000..6eef341 --- /dev/null +++ b/test/.gdb_history @@ -0,0 +1,59 @@ +run +a +q +run +bt +q +run +a +q +run +bt +a +y +q +run +p (i / 400) * 100 +q +run +bt +q +run +bt +q +b create_image +run +tui n +n +n +n +n +n +n +n +n +s +n +n +s +s +n +n +n +n +n +n +n +n +n +n +n +n +n +n +n +n +n +s +s +q diff --git a/test/main.c b/test/main.c index ab12830..ba6cf24 100644 --- a/test/main.c +++ b/test/main.c @@ -6,11 +6,12 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/04/13 14:59:32 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 12:36:11 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include "../includes/mlx.h" typedef struct s_mlx @@ -24,14 +25,18 @@ int update(t_mlx *mlx) { static int i = 0; int j; + int k; mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100); mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); j = 0; + k = 0; while (j < 400) { - mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFF00FF01); + mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + k); mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); + if (k < 255) + k++; j++; } i++; @@ -42,24 +47,29 @@ int update(t_mlx *mlx) void *create_image(t_mlx *mlx) { - int ignore[3]; - void *img; - char *addr; - int i; + unsigned char pixel[4]; + int i[3]; + void *img; + memset(i, 0, sizeof(int) * 3); img = mlx_new_image(mlx->mlx, 100, 100); - addr = mlx_get_data_addr(mlx->mlx, img, &ignore[0], &ignore[1], &ignore[2]); - i = 0; - while (i < (100 * 100) * 4) + while (i[0] < (100 * 100) * 4) { - if (i < 10000 || i > 20000) + if (i[0] < 10000 || i[0] > 20000) { - addr[i + 0] = 0xFF; - addr[i + 1] = i; - addr[i + 2] = 0x00; - addr[i + 3] = 0xFF; + pixel[0] = i[0]; + pixel[1] = i[1]; + pixel[2] = i[2]; + pixel[3] = 0xFF; + mlx_set_image_pixel(mlx->mlx, img, i[1], i[2], *((int *)pixel)); + } + i[0] += 4; + i[1]++; + if (i[1] >= 100) + { + i[1] = 0; + i[2]++; } - i += 4; } return (img); } @@ -81,9 +91,9 @@ int window_hook(int event, t_mlx *param) int main(void) { t_mlx mlx; + void *img; int w; int h; - void *img; mlx.mlx = mlx_init(); mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); From 4e1832e59d3b7502e0d9825e364dc5df52aeab92 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Wed, 2 Aug 2023 12:51:04 +0200 Subject: [PATCH 2/3] fixing performance issues --- test/.gdb_history | 59 ----------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 test/.gdb_history diff --git a/test/.gdb_history b/test/.gdb_history deleted file mode 100644 index 6eef341..0000000 --- a/test/.gdb_history +++ /dev/null @@ -1,59 +0,0 @@ -run -a -q -run -bt -q -run -a -q -run -bt -a -y -q -run -p (i / 400) * 100 -q -run -bt -q -run -bt -q -b create_image -run -tui n -n -n -n -n -n -n -n -n -s -n -n -s -s -n -n -n -n -n -n -n -n -n -n -n -n -n -n -n -n -n -s -s -q From bc84808333c264a40464498896c6ae57a971a081 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Wed, 2 Aug 2023 12:52:38 +0200 Subject: [PATCH 3/3] fixing get pixel image issue --- src/renderer/images/texture.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/renderer/images/texture.cpp b/src/renderer/images/texture.cpp index 62e9d2f..6f38b23 100644 --- a/src/renderer/images/texture.cpp +++ b/src/renderer/images/texture.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */ -/* Updated: 2023/08/02 12:34:25 by maldavid ### ########.fr */ +/* Updated: 2023/08/02 12:52:26 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,7 +72,6 @@ namespace mlx if(_map == nullptr) openCPUmap(); uint32_t color = _cpu_map[(y * getWidth()) + x]; - color >>= 8; return (color); }