From 520a29dd518be4f3bdfc4a4740660b491ecfd3bb Mon Sep 17 00:00:00 2001 From: Kian <92978255+MERL10N@users.noreply.github.com> Date: Fri, 3 Jul 2026 15:34:32 -0700 Subject: [PATCH] Backends: Metal4: added metal-cpp support. (#9461) with amends. --- backends/imgui_impl_metal.mm | 1 - backends/imgui_impl_metal4.h | 33 +++++++++++++++++++++++++++++++- backends/imgui_impl_metal4.mm | 36 ++++++++++++++++++++++++++++++++++- docs/CHANGELOG.txt | 2 +- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index 652668c78..445c4b36c 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -123,7 +123,6 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* draw_data, ImGui_ImplMetal_RenderDrawData(draw_data, (__bridge id)(commandBuffer), (__bridge id)(commandEncoder)); - } bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device) diff --git a/backends/imgui_impl_metal4.h b/backends/imgui_impl_metal4.h index b5ec4ca56..b5b54cad0 100644 --- a/backends/imgui_impl_metal4.h +++ b/backends/imgui_impl_metal4.h @@ -7,7 +7,6 @@ // [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). // [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). // Missing features or Issues: -// [ ] Metal-cpp support. // [ ] Texture view pool support? Reevaluate which type to use for ImtextureID. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. @@ -53,6 +52,38 @@ IMGUI_IMPL_API void ImGui_ImplMetal4_UpdateTexture(ImTextureData* tex); #endif +//----------------------------------------------------------------------------- +// C++ API +//----------------------------------------------------------------------------- + +// Enable Metal C++ binding support with '#define IMGUI_IMPL_METAL_CPP' in your imconfig.h file +// More info about using Metal from C++: https://developer.apple.com/metal/cpp/ + +#ifdef IMGUI_IMPL_METAL_CPP +#ifndef __OBJC__ + +// Forward declare relevant classes from the MTL and MTL4 namespace +namespace MTL { class Device; } +namespace MTL4 { class CommandQueue; class CommandBuffer; class RenderPassDescriptor; class RenderCommandEncoder; } + +// Follow "Getting Started" link and check examples/ folder to learn about using backends! +IMGUI_IMPL_API bool ImGui_ImplMetal4_Init(MTL::Device* device, MTL4::CommandQueue* commandQueue, int framesInFlight); +IMGUI_IMPL_API void ImGui_ImplMetal4_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplMetal4_NewFrame(MTL4::RenderPassDescriptor* renderPassDescriptor, int frameInFlightIndex); +IMGUI_IMPL_API void ImGui_ImplMetal4_RenderDrawData(ImDrawData* draw_data, + MTL4::CommandBuffer* commandBuffer, + MTL4::RenderCommandEncoder* commandEncoder); + +// Called by Init/NewFrame/Shutdown +IMGUI_IMPL_API bool ImGui_ImplMetal4_CreateDeviceObjects(MTL::Device* device); +IMGUI_IMPL_API void ImGui_ImplMetal4_DestroyDeviceObjects(); + +// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = nullptr to handle this manually. +IMGUI_IMPL_API void ImGui_ImplMetal4_UpdateTexture(ImTextureData* tex); + +#endif +#endif + //----------------------------------------------------------------------------- #endif // #ifndef IMGUI_DISABLE diff --git a/backends/imgui_impl_metal4.mm b/backends/imgui_impl_metal4.mm index f88d34b4e..d2d172a57 100644 --- a/backends/imgui_impl_metal4.mm +++ b/backends/imgui_impl_metal4.mm @@ -7,7 +7,6 @@ // [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). // [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). // Missing features or Issues: -// [ ] Metal-cpp support. // [ ] Texture view pool support? Reevaluate which type to use for ImtextureID. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. @@ -20,6 +19,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2026-07-07: Metal 4: Added metal-cpp support. (#9461) // 2026-07-02: Metal 4: Added new Metal 4 backend implementation. (#9458) #include "imgui.h" @@ -28,6 +28,10 @@ #import #import +#ifdef IMGUI_IMPL_METAL_CPP +#include +#endif + #pragma mark - Support classes and structs struct ImGui_Metal4_ConstantData @@ -95,6 +99,36 @@ static void ImGui_ImplMetal4_DestroyBackendData(){ IM_DELET static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return (CFTimeInterval)(double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1e9); } +#ifdef IMGUI_IMPL_METAL_CPP + +#pragma mark - Dear ImGui Metal C++ Backend API + +bool ImGui_ImplMetal4_Init(MTL::Device* device, MTL4::CommandQueue* commandQueue, int framesInFlight) +{ + return ImGui_ImplMetal4_Init((__bridge id)(device),(__bridge id)(commandQueue), framesInFlight); +} + +void ImGui_ImplMetal4_NewFrame(MTL4::RenderPassDescriptor* renderPassDescriptor, int frameInFlightIndex) +{ + ImGui_ImplMetal4_NewFrame((__bridge MTL4RenderPassDescriptor*)(renderPassDescriptor), frameInFlightIndex); +} + +void ImGui_ImplMetal4_RenderDrawData(ImDrawData* draw_data, + MTL4::CommandBuffer* commandBuffer, + MTL4::RenderCommandEncoder* commandEncoder) +{ + ImGui_ImplMetal4_RenderDrawData(draw_data, + (__bridge id)(commandBuffer), + (__bridge id)(commandEncoder)); +} + +bool ImGui_ImplMetal_CreateDeviceObjects(MTL::Device* device) +{ + return ImGui_ImplMetal4_CreateDeviceObjects((__bridge id)(device)); +} + +#endif // #ifdef IMGUI_IMPL_METAL_CPP + #pragma mark - Dear ImGui Metal Backend API void ImGui_ImplMetal4_NewFrame(MTL4RenderPassDescriptor* renderPassDescriptor, int frameInFlightIndex) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 445432bb1..b0afa23f8 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -148,7 +148,7 @@ Other Changes: - Backends: - Metal4: - Added new Metal 4 backend (forked from Metal 3 backend). (#9458, #9451) [@AmelieHeinrich] - Note that Metal-cpp is not yet supported. + - Added Metal-cpp support enabled with `IMGUI_IMPL_METAL_CPP` define. (#9461) [@MERL10N] - OpenGL3: - GLSL version detection assume GLSL 410 when GL context is 4.1. Fixes an issue running on macOS with Wine. [#9427, #6577) [@perminovVS]