From 5a04ad778136060de4c97a565f038644fc3c3c64 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 4 Dec 2024 18:34:53 +0100 Subject: [PATCH] moving experimental features to a new folder --- example/build.sh | 1 - example/main.c | 22 ----- experimental/RenderToTexture/build.sh | 11 +++ experimental/RenderToTexture/main.c | 86 +++++++++++++++++++ experimental/RenderToTexture/run.sh | 4 + includes/mlx.h | 2 +- runtime/Includes/Core/Application.h | 1 + runtime/Includes/Core/Application.inl | 3 + .../Sources/Renderer/Pipelines/Graphics.cpp | 28 ++++-- 9 files changed, 126 insertions(+), 32 deletions(-) create mode 100755 experimental/RenderToTexture/build.sh create mode 100644 experimental/RenderToTexture/main.c create mode 100755 experimental/RenderToTexture/run.sh diff --git a/example/build.sh b/example/build.sh index b332c18..ab5882b 100755 --- a/example/build.sh +++ b/example/build.sh @@ -9,4 +9,3 @@ if [ $(uname -s) = 'Darwin' ]; then else clang main.c ../libmlx.so -lSDL2 -g -Wall -Wextra -Werror; fi - diff --git a/example/main.c b/example/main.c index 84a524d..d32e563 100644 --- a/example/main.c +++ b/example/main.c @@ -9,8 +9,6 @@ typedef struct void* logo_jpg; void* logo_bmp; void* img; - void* render_target; - void* render_target_win; } mlx_t; int update(void* param) @@ -52,19 +50,6 @@ int update(void* param) mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, 0xFFFF0000); } - mlx_string_put(mlx->mlx, mlx->render_target_win, 20, 20, 0xFFAF2BFF, "cacaboudin"); - mlx_transform_put_image_to_window(mlx->mlx, mlx->render_target_win, mlx->logo_bmp, 100, 40, 0.5f, 75.0f); - mlx_put_image_to_window(mlx->mlx, mlx->render_target_win, mlx->img, 40, 60); - - 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); - color += (color < 255); - } - - mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 250, 0.5f, 33.0f); - i++; return 0; } @@ -151,10 +136,6 @@ int main(void) mlx_get_screens_size(mlx.mlx, mlx.win, &w, &h); printf("screen size : %dx%d\n", w, h); - 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); - mlx_set_fps_goal(mlx.mlx, 60); mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx); @@ -181,9 +162,6 @@ int main(void) mlx_destroy_image(mlx.mlx, mlx.img); mlx_destroy_window(mlx.mlx, mlx.win); - mlx_destroy_window(mlx.mlx, mlx.render_target_win); - mlx_destroy_image(mlx.mlx, mlx.render_target); - mlx_destroy_display(mlx.mlx); return 0; diff --git a/experimental/RenderToTexture/build.sh b/experimental/RenderToTexture/build.sh new file mode 100755 index 0000000..3ae5775 --- /dev/null +++ b/experimental/RenderToTexture/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ -e a.out ]; then + rm a.out +fi + +if [ $(uname -s) = 'Darwin' ]; then + clang main.c ../../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -g; +else + clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror; +fi diff --git a/experimental/RenderToTexture/main.c b/experimental/RenderToTexture/main.c new file mode 100644 index 0000000..fb91e81 --- /dev/null +++ b/experimental/RenderToTexture/main.c @@ -0,0 +1,86 @@ +#include "../../includes/mlx.h" + +typedef struct +{ + void* mlx; + void* win; + void* render_target; + void* render_target_win; +} mlx_t; + +int update(void* param) +{ + mlx_t* mlx = (mlx_t*)param; + + mlx_clear_window(mlx->mlx, mlx->win, 0xFF334D4D); + + 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"); + + 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); + 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_string_put(mlx->mlx, mlx->render_target_win, 20, 20, 0xFFAF2BFF, "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); + color += (color < 255); + } + + mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 250, 0.5f, 33.0f); + + return 0; +} + +int 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) +{ + if(event == 0) + mlx_loop_end(((mlx_t*)param)->mlx); + return 0; +} + +int main(void) +{ + mlx_t mlx; + + mlx.mlx = mlx_init(); + mlx.win = mlx_new_resizable_window(mlx.mlx, 400, 400, "My window"); + + 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); + + 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_loop(mlx.mlx); + + mlx_destroy_window(mlx.mlx, mlx.win); + + mlx_destroy_window(mlx.mlx, mlx.render_target_win); + mlx_destroy_image(mlx.mlx, mlx.render_target); + + mlx_destroy_display(mlx.mlx); + + return 0; +} diff --git a/experimental/RenderToTexture/run.sh b/experimental/RenderToTexture/run.sh new file mode 100755 index 0000000..993cb20 --- /dev/null +++ b/experimental/RenderToTexture/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +bash ./build.sh +./a.out diff --git a/includes/mlx.h b/includes/mlx.h index 9e3f6df..3c03052 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/11/05 18:18:22 by maldavid ### ########.fr */ +/* Updated: 2024/12/04 17:52:23 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h index e2abe45..840e6bf 100644 --- a/runtime/Includes/Core/Application.h +++ b/runtime/Includes/Core/Application.h @@ -58,6 +58,7 @@ namespace mlx FontRegistry m_font_registry; ImageRegistry m_image_registry; std::vector> m_graphics; + std::shared_ptr p_last_font_bound; std::function f_loop_hook; std::unique_ptr p_render_core; #ifdef PROFILER diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index bf2c9be..6b1be72 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -98,6 +98,7 @@ namespace mlx m_in.RegisterWindow(m_graphics.back()->GetWindow()); } } + m_graphics.back()->GetScene().BindFont(p_last_font_bound); return static_cast(&m_graphics.back()->GetID()); } @@ -162,6 +163,8 @@ namespace mlx m_font_registry.RegisterFont(font); } + p_last_font_bound = font; + for(auto& gs : m_graphics) { if(gs) diff --git a/runtime/Sources/Renderer/Pipelines/Graphics.cpp b/runtime/Sources/Renderer/Pipelines/Graphics.cpp index d7fba28..eee0446 100644 --- a/runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -180,14 +180,26 @@ namespace mlx attachment_views.push_back(image->GetImageView()); } - VkSubpassDependency& dependency = dependencies.emplace_back(); - dependency.srcSubpass = VK_SUBPASS_EXTERNAL; - dependency.dstSubpass = 0; - dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependency.srcAccessMask = 0; - dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependency.dependencyFlags = 0; + if(!render_targets.empty()) + { + VkSubpassDependency& first_depedency = dependencies.emplace_back(); + first_depedency.srcSubpass = VK_SUBPASS_EXTERNAL; + first_depedency.dstSubpass = 0; + first_depedency.srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + first_depedency.srcAccessMask = VK_ACCESS_SHADER_READ_BIT; + first_depedency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + first_depedency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + first_depedency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + VkSubpassDependency& second_depedency = dependencies.emplace_back(); + second_depedency.srcSubpass = 0; + second_depedency.dstSubpass = VK_SUBPASS_EXTERNAL; + second_depedency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + second_depedency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + second_depedency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + second_depedency.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + second_depedency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + } m_renderpass = kvfCreateRenderPassWithSubpassDependencies(RenderCore::Get().GetDevice(), attachments.data(), attachments.size(), GetPipelineBindPoint(), dependencies.data(), dependencies.size()); m_clears.clear();