From 1243d24bdfbcc9ac69e9d80b8b0fe225ffb2f60c Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:02:24 +0300 Subject: [PATCH 1/5] Update imgui_impl_metal.mm --- backends/imgui_impl_metal.mm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index edd7fa181e29..2ffbb178b18b 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -3,7 +3,7 @@ // Implemented features: // [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID! -// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). +// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. @@ -156,7 +156,7 @@ void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) { ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?"); - bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; + bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; if (bd->SharedMetalContext.depthStencilState == nil) ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device); @@ -306,14 +306,17 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c indexBufferOffset += (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx); } - __block MetalContext* sharedMetalContext = bd->SharedMetalContext; [commandBuffer addCompletedHandler:^(id) { dispatch_async(dispatch_get_main_queue(), ^{ - @synchronized(bd->SharedMetalContext.bufferCache) + ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); + if (bd != nullptr) { - [sharedMetalContext.bufferCache addObject:vertexBuffer]; - [sharedMetalContext.bufferCache addObject:indexBuffer]; + @synchronized(bd->SharedMetalContext.bufferCache) + { + [bd->SharedMetalContext.bufferCache addObject:vertexBuffer]; + [bd->SharedMetalContext.bufferCache addObject:indexBuffer]; + } } }); }]; @@ -360,7 +363,7 @@ void ImGui_ImplMetal_DestroyFontsTexture() bool ImGui_ImplMetal_CreateDeviceObjects(id device) { ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; + MTLDepthStencilDescriptor* depthStencilDescriptor = [[[MTLDepthStencilDescriptor alloc] init] autorelease]; depthStencilDescriptor.depthWriteEnabled = NO; depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; From f81d420c82dde29ac5e5f28a90b708795eaf5c57 Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:04:15 +0300 Subject: [PATCH 2/5] Update imgui_impl_metal.mm --- backends/imgui_impl_metal.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index 2ffbb178b18b..d1cbb042ab8c 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -3,7 +3,7 @@ // Implemented features: // [X] Renderer: User texture binding. Use 'MTLTexture' as ImTextureID. Read the FAQ about ImTextureID! -// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices. +// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. From 6e46086f2dbc43235afd07c765088bce3a7f0fdb Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:14:21 +0300 Subject: [PATCH 3/5] Update imgui_impl_metal.mm --- backends/imgui_impl_metal.mm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index d1cbb042ab8c..3c88d25459b7 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -363,12 +363,14 @@ void ImGui_ImplMetal_DestroyFontsTexture() bool ImGui_ImplMetal_CreateDeviceObjects(id device) { ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - MTLDepthStencilDescriptor* depthStencilDescriptor = [[[MTLDepthStencilDescriptor alloc] init] autorelease]; + MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; depthStencilDescriptor.depthWriteEnabled = NO; depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; + bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - ImGui_ImplMetal_CreateFontsTexture(device); + [depthStencilDescriptor release]; + ImGui_ImplMetal_CreateFontsTexture(device); return true; } @@ -376,6 +378,8 @@ void ImGui_ImplMetal_DestroyDeviceObjects() { ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); ImGui_ImplMetal_DestroyFontsTexture(); + [bd->SharedMetalContext.depthStencilState release]; + bd->SharedMetalContext.depthStencilState = nil; [bd->SharedMetalContext.renderPipelineStateCache removeAllObjects]; } From 4d6483df600f8a409407a7a84ac71c27c4d9c22d Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:43:29 +0300 Subject: [PATCH 4/5] Update imgui_impl_metal.mm --- backends/imgui_impl_metal.mm | 56 ++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index 3c88d25459b7..aaee5a8cd6f1 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -154,12 +154,17 @@ void ImGui_ImplMetal_Shutdown() void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) { - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?"); - bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; + ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); + IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?"); + +#ifdef IMGUI_IMPL_METAL_CPP + bd->SharedMetalContext.framebufferDescriptor = (__bridge FramebufferDescriptor*)CFBridgingRelease([[FramebufferDescriptor alloc] initWithRenderPassDescriptor:(__bridge MTLRenderPassDescriptor*)renderPassDescriptor]); +#else + bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; +#endif - if (bd->SharedMetalContext.depthStencilState == nil) - ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device); + if (bd->SharedMetalContext.depthStencilState == nil) + ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device); } static void ImGui_ImplMetal_SetupRenderState(ImDrawData* drawData, id commandBuffer, @@ -306,17 +311,14 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c indexBufferOffset += (size_t)draw_list->IdxBuffer.Size * sizeof(ImDrawIdx); } + __block MetalContext* sharedMetalContext = bd->SharedMetalContext; [commandBuffer addCompletedHandler:^(id) { dispatch_async(dispatch_get_main_queue(), ^{ - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - if (bd != nullptr) + @synchronized(bd->SharedMetalContext.bufferCache) { - @synchronized(bd->SharedMetalContext.bufferCache) - { - [bd->SharedMetalContext.bufferCache addObject:vertexBuffer]; - [bd->SharedMetalContext.bufferCache addObject:indexBuffer]; - } + [sharedMetalContext.bufferCache addObject:vertexBuffer]; + [sharedMetalContext.bufferCache addObject:indexBuffer]; } }); }]; @@ -362,24 +364,34 @@ void ImGui_ImplMetal_DestroyFontsTexture() bool ImGui_ImplMetal_CreateDeviceObjects(id device) { - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; - depthStencilDescriptor.depthWriteEnabled = NO; - depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; + ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - [depthStencilDescriptor release]; +#ifdef IMGUI_IMPL_METAL_CPP + @autoreleasepool { + MTLDepthStencilDescriptor* depthStencilDescriptor = [MTLDepthStencilDescriptor new]; + depthStencilDescriptor.depthWriteEnabled = NO; + depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; + depthStencilDescriptor.backFaceStencil = nil; + depthStencilDescriptor.frontFaceStencil = nil; + id state = [(__bridge id)device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; + bd->SharedMetalContext.depthStencilState = (__bridge id)state; + [depthStencilDescriptor release]; + } +#else + MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; + depthStencilDescriptor.depthWriteEnabled = NO; + depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; + bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; +#endif - ImGui_ImplMetal_CreateFontsTexture(device); - return true; + ImGui_ImplMetal_CreateFontsTexture(device); + return true; } void ImGui_ImplMetal_DestroyDeviceObjects() { ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); ImGui_ImplMetal_DestroyFontsTexture(); - [bd->SharedMetalContext.depthStencilState release]; - bd->SharedMetalContext.depthStencilState = nil; [bd->SharedMetalContext.renderPipelineStateCache removeAllObjects]; } From e4d9ab25f7df723c9a4b6b2e6099d92d6c583db0 Mon Sep 17 00:00:00 2001 From: Selim Sandal <49725809+selimsandal@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:51:32 +0300 Subject: [PATCH 5/5] Update imgui_impl_metal.mm --- backends/imgui_impl_metal.mm | 42 ++++++++++++------------------------ 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index aaee5a8cd6f1..6de0e064ab8e 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -154,17 +154,15 @@ void ImGui_ImplMetal_Shutdown() void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) { - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?"); - + ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); + IM_ASSERT(bd != nil && "Context or backend not initialized! Did you call ImGui_ImplMetal_Init()?"); #ifdef IMGUI_IMPL_METAL_CPP - bd->SharedMetalContext.framebufferDescriptor = (__bridge FramebufferDescriptor*)CFBridgingRelease([[FramebufferDescriptor alloc] initWithRenderPassDescriptor:(__bridge MTLRenderPassDescriptor*)renderPassDescriptor]); + bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; #else - bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; + bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; #endif - - if (bd->SharedMetalContext.depthStencilState == nil) - ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device); + if (bd->SharedMetalContext.depthStencilState == nil) + ImGui_ImplMetal_CreateDeviceObjects(bd->SharedMetalContext.device); } static void ImGui_ImplMetal_SetupRenderState(ImDrawData* drawData, id commandBuffer, @@ -364,28 +362,16 @@ void ImGui_ImplMetal_DestroyFontsTexture() bool ImGui_ImplMetal_CreateDeviceObjects(id device) { - ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); - + ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); + MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; + depthStencilDescriptor.depthWriteEnabled = NO; + depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; + bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; #ifdef IMGUI_IMPL_METAL_CPP - @autoreleasepool { - MTLDepthStencilDescriptor* depthStencilDescriptor = [MTLDepthStencilDescriptor new]; - depthStencilDescriptor.depthWriteEnabled = NO; - depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; - depthStencilDescriptor.backFaceStencil = nil; - depthStencilDescriptor.frontFaceStencil = nil; - id state = [(__bridge id)device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - bd->SharedMetalContext.depthStencilState = (__bridge id)state; - [depthStencilDescriptor release]; - } -#else - MTLDepthStencilDescriptor* depthStencilDescriptor = [[MTLDepthStencilDescriptor alloc] init]; - depthStencilDescriptor.depthWriteEnabled = NO; - depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; - bd->SharedMetalContext.depthStencilState = [device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; + [depthStencilDescriptor release]; #endif - - ImGui_ImplMetal_CreateFontsTexture(device); - return true; + ImGui_ImplMetal_CreateFontsTexture(device); + return true; } void ImGui_ImplMetal_DestroyDeviceObjects()