Backends: Metal: Add dispatch synchronization. (#5447)

This commit is contained in:
luigifcruz 2022-07-02 14:24:25 -07:00 committed by ocornut
parent 0b2da67912
commit 82e10f1b61
2 changed files with 30 additions and 21 deletions

View File

@ -12,6 +12,8 @@
// CHANGELOG // CHANGELOG
// (minor and older changes stripped away, please see git history for details) // (minor and older changes stripped away, please see git history for details)
// 2022-07-05: Metal: Add dispatch synchronization.
// 2022-06-30: Metal: Use __bridge for ARC based systems.
// 2022-06-01: Metal: Fixed null dereference on exit inside command buffer completion handler. // 2022-06-01: Metal: Fixed null dereference on exit inside command buffer completion handler.
// 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts. // 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
// 2022-01-03: Metal: Ignore ImDrawCmd where ElemCount == 0 (very rare but can technically be manufactured by user code). // 2022-01-03: Metal: Ignore ImDrawCmd where ElemCount == 0 (very rare but can technically be manufactured by user code).
@ -295,10 +297,13 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id<MTLCommandBuffer> c
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData(); ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
if (bd != NULL) if (bd != NULL)
{
@synchronized(bd->SharedMetalContext.bufferCache)
{ {
[bd->SharedMetalContext.bufferCache addObject:vertexBuffer]; [bd->SharedMetalContext.bufferCache addObject:vertexBuffer];
[bd->SharedMetalContext.bufferCache addObject:indexBuffer]; [bd->SharedMetalContext.bufferCache addObject:indexBuffer];
} }
}
}); });
}]; }];
} }
@ -440,6 +445,8 @@ void ImGui_ImplMetal_DestroyDeviceObjects()
{ {
uint64_t now = GetMachAbsoluteTimeInSeconds(); uint64_t now = GetMachAbsoluteTimeInSeconds();
@synchronized(self.bufferCache)
{
// Purge old buffers that haven't been useful for a while // Purge old buffers that haven't been useful for a while
if (now - self.lastBufferCachePurge > 1.0) if (now - self.lastBufferCachePurge > 1.0)
{ {
@ -463,6 +470,7 @@ void ImGui_ImplMetal_DestroyDeviceObjects()
bestCandidate.lastReuseTime = now; bestCandidate.lastReuseTime = now;
return bestCandidate; return bestCandidate;
} }
}
// No luck; make a new buffer // No luck; make a new buffer
id<MTLBuffer> backing = [device newBufferWithLength:length options:MTLResourceStorageModeShared]; id<MTLBuffer> backing = [device newBufferWithLength:length options:MTLResourceStorageModeShared];

View File

@ -43,6 +43,7 @@ Other Changes:
Enter keep the input active and select all text. Enter keep the input active and select all text.
- Misc: io.Framerate moving average now converge in 60 frames instead of 120. (#5236, #4138) - Misc: io.Framerate moving average now converge in 60 frames instead of 120. (#5236, #4138)
- Backends: Metal: Use __bridge for ARC based systems. (#5403) [@stack] - Backends: Metal: Use __bridge for ARC based systems. (#5403) [@stack]
- Backends: Metal: Add dispatch synchronization. (#5447) [@luigifcruz]
- Backends: OSX: Fixes to support full app creation in C++. (#5403) [@stack] - Backends: OSX: Fixes to support full app creation in C++. (#5403) [@stack]