From 54fedf1c93ed024f2d4eabcd9290a975d62ae1b4 Mon Sep 17 00:00:00 2001 From: 519q Date: Sat, 12 Apr 2025 17:42:58 +0300 Subject: [PATCH 1/4] Check for ARC, fallback for no ARC. slight fix in impl_osx --- backends/imgui_impl_metal.mm | 56 ++++++++++++++++++++++++++++++++++-- backends/imgui_impl_osx.mm | 4 +-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index 1cd2308d144d..281d730c2d61 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -162,6 +162,14 @@ void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) #ifdef IMGUI_IMPL_METAL_CPP bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; #else + // if no objc + if (!__has_feature(objc_arc)) { + // check for existing framebufferDescriptor, destroy if there is one + if (bd->SharedMetalContext.framebufferDescriptor) { + CFRelease( + (__bridge CFTypeRef)bd->SharedMetalContext.framebufferDescriptor); + } + } bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; #endif if (bd->SharedMetalContext.depthStencilState == nil) @@ -238,8 +246,52 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c size_t vertexBufferLength = (size_t)drawData->TotalVtxCount * sizeof(ImDrawVert); size_t indexBufferLength = (size_t)drawData->TotalIdxCount * sizeof(ImDrawIdx); - MetalBuffer* vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device]; - MetalBuffer* indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device]; + MetalBuffer *vertexBuffer = nil; + MetalBuffer *indexBuffer = nil; + // if objc do what did before + if (__has_feature(objc_arc)) { + vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength + device:commandBuffer.device]; + indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength + device:commandBuffer.device]; + } else { + // else caching + @synchronized(ctx.bufferCache) { + // Find suitable buffers in the cache + + for (NSInteger i = ctx.bufferCache.count - 1; i >= 0; i--) { + MetalBuffer *buffer = ctx.bufferCache[i]; + + // If buffer is large enough for vertex data and not yet assigned + if (!vertexBuffer && buffer.buffer.length >= vertexBufferLength) { + vertexBuffer = buffer; + [ctx.bufferCache removeObjectAtIndex:i]; + continue; + } + + // If buffer is large enough for index data and not yet assigned + if (!indexBuffer && buffer.buffer.length >= indexBufferLength) { + indexBuffer = buffer; + [ctx.bufferCache removeObjectAtIndex:i]; + } + + // If we found both buffers, break + if (vertexBuffer && indexBuffer) + break; + } + } + // Create buffers only in case none found before + if (!vertexBuffer) { + vertexBuffer = + [ctx dequeueReusableBufferOfLength:vertexBufferLength + device:commandBuffer.device]; + } + if (!indexBuffer) { + indexBuffer = + [ctx dequeueReusableBufferOfLength:indexBufferLength + device:commandBuffer.device]; + } + } ImGui_ImplMetal_SetupRenderState(drawData, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0); diff --git a/backends/imgui_impl_osx.mm b/backends/imgui_impl_osx.mm index e681d176b347..1579ca8dce78 100644 --- a/backends/imgui_impl_osx.mm +++ b/backends/imgui_impl_osx.mm @@ -788,8 +788,8 @@ static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view) default: return io.WantCaptureKeyboard; } - - NSEventModifierFlags modifier_flags = [event modifierFlags]; + // Declaration shadows a local variable + // NSEventModifierFlags modifier_flags = [event modifierFlags]; io.AddKeyEvent(key, (modifier_flags & mask) != 0); io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code) } From 567b1b362578b81f6d7c48448038e5410a07e661 Mon Sep 17 00:00:00 2001 From: 519q Date: Sat, 12 Apr 2025 17:54:10 +0300 Subject: [PATCH 2/4] mod flags back --- backends/imgui_impl_osx.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/imgui_impl_osx.mm b/backends/imgui_impl_osx.mm index 1579ca8dce78..dcc871622f25 100644 --- a/backends/imgui_impl_osx.mm +++ b/backends/imgui_impl_osx.mm @@ -789,7 +789,7 @@ static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view) return io.WantCaptureKeyboard; } // Declaration shadows a local variable - // NSEventModifierFlags modifier_flags = [event modifierFlags]; + modifier_flags = [event modifierFlags]; io.AddKeyEvent(key, (modifier_flags & mask) != 0); io.SetKeyEventNativeData(key, key_code, -1); // To support legacy indexing (<1.87 user code) } From cfe2b1181e2d995cb1daf20d4d1b8f2a9d8a41d3 Mon Sep 17 00:00:00 2001 From: 519q Date: Sat, 12 Apr 2025 17:59:02 +0300 Subject: [PATCH 3/4] fixed comments --- backends/imgui_impl_metal.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index 281d730c2d61..e0d7c2b17df7 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -162,7 +162,7 @@ void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) #ifdef IMGUI_IMPL_METAL_CPP bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; #else - // if no objc + // if no ARC if (!__has_feature(objc_arc)) { // check for existing framebufferDescriptor, destroy if there is one if (bd->SharedMetalContext.framebufferDescriptor) { @@ -248,7 +248,7 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c size_t indexBufferLength = (size_t)drawData->TotalIdxCount * sizeof(ImDrawIdx); MetalBuffer *vertexBuffer = nil; MetalBuffer *indexBuffer = nil; - // if objc do what did before + // if ARC do what did before if (__has_feature(objc_arc)) { vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device]; From 9ff64e1d205b75d7a1d4f99a5b8b59109e7356af Mon Sep 17 00:00:00 2001 From: 519q Date: Tue, 15 Apr 2025 01:59:38 +0300 Subject: [PATCH 4/4] fixed formatting --- backends/imgui_impl_metal.mm | 90 ++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/backends/imgui_impl_metal.mm b/backends/imgui_impl_metal.mm index e0d7c2b17df7..6dd06c081c24 100644 --- a/backends/imgui_impl_metal.mm +++ b/backends/imgui_impl_metal.mm @@ -162,14 +162,15 @@ void ImGui_ImplMetal_NewFrame(MTLRenderPassDescriptor* renderPassDescriptor) #ifdef IMGUI_IMPL_METAL_CPP bd->SharedMetalContext.framebufferDescriptor = [[[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]autorelease]; #else - // if no ARC - if (!__has_feature(objc_arc)) { - // check for existing framebufferDescriptor, destroy if there is one - if (bd->SharedMetalContext.framebufferDescriptor) { - CFRelease( - (__bridge CFTypeRef)bd->SharedMetalContext.framebufferDescriptor); + // if no ARC + if (!__has_feature(objc_arc)) + { + // check for existing framebufferDescriptor, destroy if there is one + if (bd->SharedMetalContext.framebufferDescriptor) + { + CFRelease((__bridge CFTypeRef)bd->SharedMetalContext.framebufferDescriptor); + } } - } bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor]; #endif if (bd->SharedMetalContext.depthStencilState == nil) @@ -249,48 +250,55 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id c MetalBuffer *vertexBuffer = nil; MetalBuffer *indexBuffer = nil; // if ARC do what did before - if (__has_feature(objc_arc)) { + if (__has_feature(objc_arc)) + { vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device]; indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device]; - } else { + } + else + { // else caching - @synchronized(ctx.bufferCache) { - // Find suitable buffers in the cache - - for (NSInteger i = ctx.bufferCache.count - 1; i >= 0; i--) { - MetalBuffer *buffer = ctx.bufferCache[i]; - - // If buffer is large enough for vertex data and not yet assigned - if (!vertexBuffer && buffer.buffer.length >= vertexBufferLength) { - vertexBuffer = buffer; - [ctx.bufferCache removeObjectAtIndex:i]; - continue; - } - - // If buffer is large enough for index data and not yet assigned - if (!indexBuffer && buffer.buffer.length >= indexBufferLength) { - indexBuffer = buffer; - [ctx.bufferCache removeObjectAtIndex:i]; - } - - // If we found both buffers, break - if (vertexBuffer && indexBuffer) - break; + @synchronized(ctx.bufferCache) + { + // Find suitable buffers in the cache + + for (NSInteger i = ctx.bufferCache.count - 1; i >= 0; i--) + { + MetalBuffer *buffer = ctx.bufferCache[i]; + + // If buffer is large enough for vertex data and not yet assigned + if (!vertexBuffer && buffer.buffer.length >= vertexBufferLength) + { + vertexBuffer = buffer; + [ctx.bufferCache removeObjectAtIndex:i]; + continue; + } + + // If buffer is large enough for index data and not yet assigned + if (!indexBuffer && buffer.buffer.length >= indexBufferLength) + { + indexBuffer = buffer; + [ctx.bufferCache removeObjectAtIndex:i]; + } + + // If we found both buffers, break + if (vertexBuffer && indexBuffer) + break; + } } - } - // Create buffers only in case none found before - if (!vertexBuffer) { - vertexBuffer = - [ctx dequeueReusableBufferOfLength:vertexBufferLength + // Create buffers only in case none found before + if (!vertexBuffer) + { + vertexBuffer = [ctx dequeueReusableBufferOfLength:vertexBufferLength device:commandBuffer.device]; - } - if (!indexBuffer) { - indexBuffer = - [ctx dequeueReusableBufferOfLength:indexBufferLength + } + if (!indexBuffer) + { + indexBuffer = [ctx dequeueReusableBufferOfLength:indexBufferLength device:commandBuffer.device]; - } + } } ImGui_ImplMetal_SetupRenderState(drawData, commandBuffer, commandEncoder, renderPipelineState, vertexBuffer, 0);