From 91c87e2b7a9782ea7e396131c950d77784b04fb7 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 12 Mar 2025 22:57:29 +0100 Subject: [PATCH] adding secret bindings --- in_depth_features/{ => GetProcAddr}/build.sh | 0 in_depth_features/GetProcAddr/main.c | 28 +++++++++ in_depth_features/{ => GetProcAddr}/run.sh | 0 in_depth_features/RTT/build.sh | 11 ++++ in_depth_features/{ => RTT}/main.c | 0 in_depth_features/RTT/run.sh | 4 ++ includes/mlx.h | 10 ++-- includes/mlx_extended.h | 11 +++- includes/mlx_profile.h | 5 +- runtime/Includes/Core/SDLManager.h | 2 + runtime/Includes/Platform/Window.h | 2 + runtime/Sources/Core/Bridge.cpp | 61 +++++++++++++++++++- runtime/Sources/Core/Logs.cpp | 2 + runtime/Sources/Core/SDLManager.cpp | 6 ++ runtime/Sources/Renderer/Descriptor.cpp | 6 +- 15 files changed, 137 insertions(+), 11 deletions(-) rename in_depth_features/{ => GetProcAddr}/build.sh (100%) mode change 100755 => 100644 create mode 100644 in_depth_features/GetProcAddr/main.c rename in_depth_features/{ => GetProcAddr}/run.sh (100%) create mode 100755 in_depth_features/RTT/build.sh rename in_depth_features/{ => RTT}/main.c (100%) create mode 100755 in_depth_features/RTT/run.sh diff --git a/in_depth_features/build.sh b/in_depth_features/GetProcAddr/build.sh old mode 100755 new mode 100644 similarity index 100% rename from in_depth_features/build.sh rename to in_depth_features/GetProcAddr/build.sh diff --git a/in_depth_features/GetProcAddr/main.c b/in_depth_features/GetProcAddr/main.c new file mode 100644 index 0000000..ce3c109 --- /dev/null +++ b/in_depth_features/GetProcAddr/main.c @@ -0,0 +1,28 @@ +#include +#include +#include "../../includes/mlx.h" +#include "../../includes/mlx_extended.h" + +#include + +typedef VkInstance (*PFN_mlx_get_vk_instance)(mlx_context mlx); + +int main(void) +{ + mlx_context mlx = mlx_init(); + + mlx_window_create_info info = { 0 }; + info.title = "My window"; + info.width = 400; + info.height = 400; + info.is_resizable = true; + mlx_window win = mlx_new_window(mlx, &info); + + PFN_mlx_get_vk_instance mlx_get_vk_instance = (PFN_mlx_get_vk_instance)mlx_get_proc_addr(mlx, "mlx_get_vk_instance"); + printf("%p\n", mlx_get_vk_instance(mlx)); + + mlx_destroy_window(mlx, win); + mlx_destroy_context(mlx); + + return 0; +} diff --git a/in_depth_features/run.sh b/in_depth_features/GetProcAddr/run.sh similarity index 100% rename from in_depth_features/run.sh rename to in_depth_features/GetProcAddr/run.sh diff --git a/in_depth_features/RTT/build.sh b/in_depth_features/RTT/build.sh new file mode 100755 index 0000000..5488aa8 --- /dev/null +++ b/in_depth_features/RTT/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 -lm -g; +else + clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror -lm; +fi diff --git a/in_depth_features/main.c b/in_depth_features/RTT/main.c similarity index 100% rename from in_depth_features/main.c rename to in_depth_features/RTT/main.c diff --git a/in_depth_features/RTT/run.sh b/in_depth_features/RTT/run.sh new file mode 100755 index 0000000..993cb20 --- /dev/null +++ b/in_depth_features/RTT/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +bash ./build.sh +./a.out diff --git a/includes/mlx.h b/includes/mlx.h index 4b373de..abc9adb 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: 2025/01/05 22:44:22 by maldavid ### ########.fr */ +/* Updated: 2025/03/12 22:26:32 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,7 +71,7 @@ typedef union mlx_color /** * @brief Initializes the MLX internal application * - * @return (mlx_context) An opaque handler to the internal MLX application or NULL (0x0) in case of error + * @return (mlx_context) An opaque handler to the internal MLX application or MLX_NULL_HANDLE (0x0) in case of error */ MLX_API mlx_context mlx_init(); @@ -122,7 +122,7 @@ typedef struct mlx_window_create_info * @param mlx Internal MLX application * @param info Pointer to a descriptor structure * - * @return (mlx_widnow) An opaque handler to the internal MLX window or NULL (0x0) in case of error + * @return (mlx_window) An opaque handler to the internal MLX window or MLX_NULL_HANDLE (0x0) in case of error */ MLX_API mlx_window mlx_new_window(mlx_context mlx, const mlx_window_create_info* info); @@ -328,7 +328,7 @@ MLX_API void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, mlx_co * @param width Width of the image * @param height Height of the image * - * @return (mlx_image) An opaque handler to the internal image or NULL (0x0) in case of error + * @return (mlx_image) An opaque handler to the internal image or MLX_NULL_HANDLE (0x0) in case of error */ MLX_API mlx_image mlx_new_image(mlx_context mlx, int width, int height); @@ -340,7 +340,7 @@ MLX_API mlx_image mlx_new_image(mlx_context mlx, int width, int height); * @param width Get the width of the image * @param heigth Get the height of the image * - * @return (mlx_image) An opaque handler to the internal image or NULL (0x0) in case of error + * @return (mlx_image) An opaque handler to the internal image or MLX_NULL_HANDLE (0x0) in case of error */ MLX_API mlx_image mlx_new_image_from_file(mlx_context mlx, char* filename, int* width, int* height); diff --git a/includes/mlx_extended.h b/includes/mlx_extended.h index 9489989..5041d29 100644 --- a/includes/mlx_extended.h +++ b/includes/mlx_extended.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/14 16:17:10 by maldavid #+# #+# */ -/* Updated: 2025/01/08 12:37:15 by maldavid ### ########.fr */ +/* Updated: 2025/03/12 22:03:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -171,6 +171,15 @@ MLX_API void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y */ MLX_API void mlx_put_transformed_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y, float scale_x, float scale_y, float angle); +/** + * @brief Get direct pointers to hidden functions + * + * @param mlx Internal MLX application + * + * @return (mlx_function) A function pointer or NULL (0x0) in case of error + */ +MLX_API mlx_function mlx_get_proc_addr(mlx_context mlx, const char* name); + #ifdef __cplusplus } #endif diff --git a/includes/mlx_profile.h b/includes/mlx_profile.h index 9c7e167..8424158 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: 2025/01/07 00:17:45 by maldavid ### ########.fr */ +/* Updated: 2025/03/12 22:01:07 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -202,6 +202,9 @@ #define MLX_VERSION_PATCH(version) ((uint32_t)(version) & 0xFFFU) #define MLX_DEFINE_HANDLE(object) typedef struct object##_handler* object +#define MLX_NULL_HANDLE NULL + +typedef void (*mlx_function)(void); #define MLX_VERSION MLX_MAKE_VERSION(2, 0, 0) #define MLX_TARGET_VULKAN_API_VERSION MLX_MAKE_VERSION(1, 0, 0) diff --git a/runtime/Includes/Core/SDLManager.h b/runtime/Includes/Core/SDLManager.h index 5db1a4f..12d9477 100644 --- a/runtime/Includes/Core/SDLManager.h +++ b/runtime/Includes/Core/SDLManager.h @@ -14,6 +14,8 @@ namespace mlx Handle CreateWindow(const mlx_window_create_info* info, std::int32_t& id, bool hidden); void DestroyWindow(Handle window) noexcept; + SDL_Window* GetRawWindow(Handle window) noexcept; + void InputsFetcher(std::function functor); VkSurfaceKHR CreateVulkanSurface(Handle window, VkInstance instance) const noexcept; diff --git a/runtime/Includes/Platform/Window.h b/runtime/Includes/Platform/Window.h index a3a28d5..e0076b2 100644 --- a/runtime/Includes/Platform/Window.h +++ b/runtime/Includes/Platform/Window.h @@ -38,6 +38,8 @@ namespace mlx MLX_FORCEINLINE std::vector GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); } MLX_FORCEINLINE Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); } + [[nodiscard]] inline Handle GetRawHandle() const noexcept { return p_window; } + void Destroy() noexcept; ~Window() { Destroy(); } diff --git a/runtime/Sources/Core/Bridge.cpp b/runtime/Sources/Core/Bridge.cpp index 21618f8..3a2cd92 100644 --- a/runtime/Sources/Core/Bridge.cpp +++ b/runtime/Sources/Core/Bridge.cpp @@ -2,11 +2,11 @@ #include #include -#include #include #include #include #include +#include static mlx::Application* __internal_application_ptr = nullptr; @@ -427,4 +427,63 @@ extern "C" return; gs->TexturePut(texture, x, y, scale_x, scale_y, angle); } + + // Hidden bindings + + VkInstance mlx_get_vk_instance(mlx_context mlx) + { + MLX_CHECK_APPLICATION_POINTER(mlx); + return mlx::RenderCore::Get().GetInstance(); + } + + VkPhysicalDevice mlx_get_vk_physical_device(mlx_context mlx) + { + MLX_CHECK_APPLICATION_POINTER(mlx); + return mlx::RenderCore::Get().GetPhysicalDevice(); + } + + VkDevice mlx_get_vk_device(mlx_context mlx) + { + MLX_CHECK_APPLICATION_POINTER(mlx); + return mlx::RenderCore::Get().GetDevice(); + } + + mlx_function mlx_get_vk_fn(const char* name) + { + #define MLX_VULKAN_GLOBAL_FUNCTION(fn) if(std::strcmp(name, #fn) == 0) return reinterpret_cast(mlx::RenderCore::Get().fn); + #define MLX_VULKAN_INSTANCE_FUNCTION(fn) if(std::strcmp(name, #fn) == 0) return reinterpret_cast(mlx::RenderCore::Get().fn); + #define MLX_VULKAN_DEVICE_FUNCTION(fn) if(std::strcmp(name, #fn) == 0) return reinterpret_cast(mlx::RenderCore::Get().fn); + #include + #undef MLX_VULKAN_GLOBAL_FUNCTION + #undef MLX_VULKAN_INSTANCE_FUNCTION + #undef MLX_VULKAN_DEVICE_FUNCTION + + return nullptr; + } + + void* mlx_get_window_handle(mlx_context mlx, mlx_window win) + { + MLX_CHECK_APPLICATION_POINTER(mlx); + mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); + if(!gs || !gs->HasWindow()) + return nullptr; + return mlx::SDLManager::Get().GetRawWindow(gs->GetWindow()->GetRawHandle()); + } + + mlx_function mlx_get_proc_addr(mlx_context mlx, const char* name) + { + MLX_CHECK_APPLICATION_POINTER(mlx); + #define MLX_MAKE_ENTRY(fn) { #fn, reinterpret_cast(fn) } + std::unordered_map entries = { + MLX_MAKE_ENTRY(mlx_get_vk_instance), + MLX_MAKE_ENTRY(mlx_get_vk_physical_device), + MLX_MAKE_ENTRY(mlx_get_vk_device), + MLX_MAKE_ENTRY(mlx_get_vk_fn), + MLX_MAKE_ENTRY(mlx_get_window_handle), + }; + auto it = entries.find(std::string{ name }); + if(it != entries.end()) + return it->second; + return nullptr; + } } diff --git a/runtime/Sources/Core/Logs.cpp b/runtime/Sources/Core/Logs.cpp index ae8721f..5d88d1e 100644 --- a/runtime/Sources/Core/Logs.cpp +++ b/runtime/Sources/Core/Logs.cpp @@ -21,8 +21,10 @@ namespace mlx { using namespace std::literals; + #ifndef DEBUG if(type == LogType::Debug && std::getenv("MLX_DEBUG_LOGS") == nullptr) return; + #endif std::string code_infos; if((type == LogType::Error || type == LogType::FatalError) && !file.empty() && !function.empty()) diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 3736dbc..bc783bd 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -80,6 +80,12 @@ namespace mlx delete infos; } + SDL_Window* SDLManager::GetRawWindow(Handle window) noexcept + { + Internal::WindowInfos* infos = static_cast(window); + return infos->window; + } + VkSurfaceKHR SDLManager::CreateVulkanSurface(Handle window, VkInstance instance) const noexcept { VkSurfaceKHR surface; diff --git a/runtime/Sources/Renderer/Descriptor.cpp b/runtime/Sources/Renderer/Descriptor.cpp index aea8595..b0ebd30 100644 --- a/runtime/Sources/Renderer/Descriptor.cpp +++ b/runtime/Sources/Renderer/Descriptor.cpp @@ -114,11 +114,11 @@ namespace mlx void DescriptorPool::ReturnDescriptorSet(std::shared_ptr set) { - std::size_t i = 0; + //std::size_t i = 0; auto it = std::find_if(m_used_sets.begin(), m_used_sets.end(), [&](const std::shared_ptr& rhs_set) { - i++; - std::cout << m_used_sets.size() << " " << i << std::endl; + //i++; + //std::cout << m_used_sets.size() << " " << i << std::endl; return set == rhs_set; }); if(it == m_used_sets.end())