From b6edf442a2fb4f22616647b6f5f95abf22e101d4 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Fri, 20 Dec 2024 01:19:15 +0100 Subject: [PATCH] re-adding render to texture --- experimental/RenderToTexture/build.sh | 4 +- experimental/RenderToTexture/main.c | 69 +++++++++++-------- includes/mlx.h | 11 ++- runtime/Includes/Core/Application.inl | 3 +- runtime/Includes/Core/Handles.h | 3 - runtime/Sources/Core/Graphics.cpp | 25 +++++-- .../Sources/Renderer/RenderPasses/Passes.cpp | 4 +- 7 files changed, 77 insertions(+), 42 deletions(-) diff --git a/experimental/RenderToTexture/build.sh b/experimental/RenderToTexture/build.sh index 3ae5775..5488aa8 100755 --- a/experimental/RenderToTexture/build.sh +++ b/experimental/RenderToTexture/build.sh @@ -5,7 +5,7 @@ if [ -e a.out ]; then fi if [ $(uname -s) = 'Darwin' ]; then - clang main.c ../../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -g; + clang main.c ../../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -lm -g; else - clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror; + clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror -lm; fi diff --git a/experimental/RenderToTexture/main.c b/experimental/RenderToTexture/main.c index fb91e81..5113a98 100644 --- a/experimental/RenderToTexture/main.c +++ b/experimental/RenderToTexture/main.c @@ -1,61 +1,62 @@ +#include +#include #include "../../includes/mlx.h" +#include "../../includes/mlx_extended.h" typedef struct { - void* mlx; - void* win; - void* render_target; - void* render_target_win; + mlx_context mlx; + mlx_window win; + mlx_image render_target; + mlx_window render_target_window; } mlx_t; -int update(void* param) +void update(void* param) { mlx_t* mlx = (mlx_t*)param; - mlx_clear_window(mlx->mlx, mlx->win, 0xFF334D4D); + mlx_clear_window(mlx->mlx, mlx->win, (mlx_color){ .rgba = 0x334D4DFF }); - mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "text"); - mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); + mlx_string_put(mlx->mlx, mlx->win, 160, 120, (mlx_color){ .rgba = 0xFF2066FF }, "text"); + mlx_string_put(mlx->mlx, mlx->win, 20, 50, (mlx_color){ .rgba = 0xFFFFFFFF }, "that's a text"); for(int j = 0, color = 0; j < 400; j++) { - mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + color); - mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); + mlx_pixel_put(mlx->mlx, mlx->win, j, j, (mlx_color){ .rgba = 0xFF0000FF + (color << 8) }); + mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, (mlx_color){ .rgba = 0x0000FFFF }); color += (color < 255); } for(int j = 0; j < 20; j++) { for(int k = 0; k < 20; k++) - mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, 0xFFFF0000); + mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, (mlx_color){ .rgba = 0xFF0000FF }); } - mlx_string_put(mlx->mlx, mlx->render_target_win, 20, 20, 0xFFAF2BFF, "yippeeee"); + mlx_string_put(mlx->mlx, mlx->render_target_window, 20, 20, (mlx_color){ .rgba = 0xAF2BFFFF }, "yippeeee"); for(int j = 0, color = 0; j < 200; j++) { - mlx_pixel_put(mlx->mlx, mlx->render_target_win, j, j, 0xFFFF0000 + color); - mlx_pixel_put(mlx->mlx, mlx->render_target_win, 199 - j, j, 0xFF0000FF); + mlx_pixel_put(mlx->mlx, mlx->render_target_window, j, j, (mlx_color){ .rgba = 0xFF0000FF + (color << 8) }); + mlx_pixel_put(mlx->mlx, mlx->render_target_window, 199 - j, j, (mlx_color){ .rgba = 0x0000FFFF }); color += (color < 255); } - mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 250, 0.5f, 33.0f); - - return 0; + static int i = 0; + i++; + mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 100, 1.0f, 1.0f, i); } -int key_hook(int key, void* param) +void key_hook(int key, void* param) { mlx_t* mlx = (mlx_t*)param; if(key == 41) mlx_loop_end(mlx->mlx); - return 0; } -int window_hook(int event, void* param) +void window_hook(int event, void* param) { if(event == 0) mlx_loop_end(((mlx_t*)param)->mlx); - return 0; } int main(void) @@ -63,24 +64,36 @@ int main(void) mlx_t mlx; mlx.mlx = mlx_init(); - mlx.win = mlx_new_resizable_window(mlx.mlx, 400, 400, "My window"); + + mlx_set_fps_goal(mlx.mlx, 60); + + mlx_window_create_info info = { 0 }; + info.title = "My window"; + info.width = 400; + info.height = 400; + info.is_resizable = true; + mlx.win = mlx_new_window(mlx.mlx, &info); mlx.render_target = mlx_new_image(mlx.mlx, 200, 200); - mlx.render_target_win = mlx_new_window(mlx.mlx, 200, 200, (char*)mlx.render_target); - mlx_clear_window(mlx.mlx, mlx.render_target_win, 0xFFC16868); + info.render_target = mlx.render_target; + info.title = NULL; + info.width = 200; + info.height = 200; + mlx.render_target_window = mlx_new_window(mlx.mlx, &info); + mlx_clear_window(mlx.mlx, mlx.render_target_window, (mlx_color){ .rgba = 0xC16868FF }); mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx); mlx_on_event(mlx.mlx, mlx.win, MLX_WINDOW_EVENT, window_hook, &mlx); - mlx_loop_hook(mlx.mlx, update, &mlx); + mlx_add_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx); mlx_destroy_window(mlx.mlx, mlx.win); - mlx_destroy_window(mlx.mlx, mlx.render_target_win); + mlx_destroy_window(mlx.mlx, mlx.render_target_window); mlx_destroy_image(mlx.mlx, mlx.render_target); - - mlx_destroy_display(mlx.mlx); + + mlx_destroy_context(mlx.mlx); return 0; } diff --git a/includes/mlx.h b/includes/mlx.h index 29babe4..0c4152a 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: 2024/12/17 02:58:07 by maldavid ### ########.fr */ +/* Updated: 2024/12/20 00:42:01 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,9 +97,18 @@ MLX_API void mlx_destroy_context(mlx_context mlx); /** * @brief Descriptor structure for window creation + * + * Note: if a valid mlx_image is passed as render_target, this window will not be a real system window + * and will rather act as a gate to use any draw function to draw directly on an image. + * + * Ex: you could use mlx_string_put or mlx_pixel_put to draw on a given image and then use this image + * with mlx_put_image_to_window to render it on a real window. + * + * See experimental/RenderToTexture/main.c for a concrete example. */ typedef struct mlx_window_create_info { + mlx_image render_target; const char* title; int width; int height; diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index cbdbbb2..8cc58c3 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -67,7 +67,8 @@ namespace mlx catch(...) { return nullptr; } m_graphics.emplace_back(std::make_unique(info, m_graphics.size())); - m_in.RegisterWindow(m_graphics.back()->GetWindow()); + if(m_graphics.back()->HasWindow()) + m_in.RegisterWindow(m_graphics.back()->GetWindow()); m_graphics.back()->GetScene().BindFont(p_last_font_bound); window->id = m_graphics.back()->GetID(); return window; diff --git a/runtime/Includes/Core/Handles.h b/runtime/Includes/Core/Handles.h index be97c39..a07381d 100644 --- a/runtime/Includes/Core/Handles.h +++ b/runtime/Includes/Core/Handles.h @@ -1,9 +1,6 @@ #ifndef __MLX_HANDLES__ #define __MLX_HANDLES__ -#include -#include - extern "C" { struct mlx_context_handler diff --git a/runtime/Sources/Core/Graphics.cpp b/runtime/Sources/Core/Graphics.cpp index 413d862..6560495 100644 --- a/runtime/Sources/Core/Graphics.cpp +++ b/runtime/Sources/Core/Graphics.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include namespace mlx { @@ -9,11 +11,24 @@ namespace mlx { MLX_PROFILE_FUNCTION(); - p_window = std::make_shared(info); - m_has_window = true; + if(info->render_target == nullptr) + { + p_window = std::make_shared(info); + m_has_window = true; + } + else + m_has_window = false; - m_renderer.Init(p_window.get()); - m_scene_renderer.Init(nullptr); + if(info->render_target != nullptr) + { + m_renderer.Init(info->render_target->texture); + m_scene_renderer.Init(info->render_target->texture); + } + else + { + m_renderer.Init(p_window.get()); + m_scene_renderer.Init(nullptr); + } p_scene = std::make_unique(); } @@ -25,7 +40,7 @@ namespace mlx m_scene_renderer.Render(*p_scene, m_renderer); m_renderer.EndFrame(); #ifdef GRAPHICS_MEMORY_DUMP - // dump memory to file every two seconds + // Dump memory usage to file every two seconds using namespace std::chrono_literals; static std::int64_t timer = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); if(std::chrono::duration{ static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer } >= 1s) diff --git a/runtime/Sources/Renderer/RenderPasses/Passes.cpp b/runtime/Sources/Renderer/RenderPasses/Passes.cpp index 41987d7..75c09a5 100644 --- a/runtime/Sources/Renderer/RenderPasses/Passes.cpp +++ b/runtime/Sources/Renderer/RenderPasses/Passes.cpp @@ -29,9 +29,9 @@ namespace mlx else extent = kvfGetSwapchainImagesSize(renderer.GetSwapchain().Get()); #ifdef DEBUG - m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_renderpasses_target"); + m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_UNORM, false, "mlx_renderpasses_target"); #else - m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, {}); + m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_UNORM, false, {}); #endif m_main_render_texture.TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); }