Skip to content

Fallback for no ARC, fix for #8166 #8565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions backends/imgui_impl_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +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);
}
}
bd->SharedMetalContext.framebufferDescriptor = [[FramebufferDescriptor alloc] initWithRenderPassDescriptor:renderPassDescriptor];
#endif
if (bd->SharedMetalContext.depthStencilState == nil)
Expand Down Expand Up @@ -238,8 +247,59 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id<MTLCommandBuffer> 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 ARC 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);

Expand Down
4 changes: 2 additions & 2 deletions backends/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
}
Expand Down