|
14 | 14 | // CHANGELOG
|
15 | 15 | // (minor and older changes stripped away, please see git history for details)
|
16 | 16 | // 2022-XX-XX: Metal: Added support for multiple windows via the ImGuiPlatformIO interface.
|
| 17 | +// 2022-07-05: Metal: Add dispatch synchronization. |
| 18 | +// 2022-06-30: Metal: Use __bridge for ARC based systems. |
17 | 19 | // 2022-06-01: Metal: Fixed null dereference on exit inside command buffer completion handler.
|
18 | 20 | // 2022-04-27: Misc: Store backend data in a per-context struct, allowing to use this backend with multiple contexts.
|
19 | 21 | // 2022-01-03: Metal: Ignore ImDrawCmd where ElemCount == 0 (very rare but can technically be manufactured by user code).
|
@@ -309,8 +311,11 @@ void ImGui_ImplMetal_RenderDrawData(ImDrawData* drawData, id<MTLCommandBuffer> c
|
309 | 311 | ImGui_ImplMetal_Data* bd = ImGui_ImplMetal_GetBackendData();
|
310 | 312 | if (bd != NULL)
|
311 | 313 | {
|
312 |
| - [bd->SharedMetalContext.bufferCache addObject:vertexBuffer]; |
313 |
| - [bd->SharedMetalContext.bufferCache addObject:indexBuffer]; |
| 314 | + @synchronized(bd->SharedMetalContext.bufferCache) |
| 315 | + { |
| 316 | + [bd->SharedMetalContext.bufferCache addObject:vertexBuffer]; |
| 317 | + [bd->SharedMetalContext.bufferCache addObject:indexBuffer]; |
| 318 | + } |
314 | 319 | }
|
315 | 320 | });
|
316 | 321 | }];
|
@@ -600,28 +605,31 @@ - (MetalBuffer*)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLD
|
600 | 605 | {
|
601 | 606 | uint64_t now = GetMachAbsoluteTimeInSeconds();
|
602 | 607 |
|
603 |
| - // Purge old buffers that haven't been useful for a while |
604 |
| - if (now - self.lastBufferCachePurge > 1.0) |
| 608 | + @synchronized(self.bufferCache) |
605 | 609 | {
|
606 |
| - NSMutableArray* survivors = [NSMutableArray array]; |
607 |
| - for (MetalBuffer* candidate in self.bufferCache) |
608 |
| - if (candidate.lastReuseTime > self.lastBufferCachePurge) |
609 |
| - [survivors addObject:candidate]; |
610 |
| - self.bufferCache = [survivors mutableCopy]; |
611 |
| - self.lastBufferCachePurge = now; |
612 |
| - } |
| 610 | + // Purge old buffers that haven't been useful for a while |
| 611 | + if (now - self.lastBufferCachePurge > 1.0) |
| 612 | + { |
| 613 | + NSMutableArray* survivors = [NSMutableArray array]; |
| 614 | + for (MetalBuffer* candidate in self.bufferCache) |
| 615 | + if (candidate.lastReuseTime > self.lastBufferCachePurge) |
| 616 | + [survivors addObject:candidate]; |
| 617 | + self.bufferCache = [survivors mutableCopy]; |
| 618 | + self.lastBufferCachePurge = now; |
| 619 | + } |
613 | 620 |
|
614 |
| - // See if we have a buffer we can reuse |
615 |
| - MetalBuffer* bestCandidate = nil; |
616 |
| - for (MetalBuffer* candidate in self.bufferCache) |
617 |
| - if (candidate.buffer.length >= length && (bestCandidate == nil || bestCandidate.lastReuseTime > candidate.lastReuseTime)) |
618 |
| - bestCandidate = candidate; |
| 621 | + // See if we have a buffer we can reuse |
| 622 | + MetalBuffer* bestCandidate = nil; |
| 623 | + for (MetalBuffer* candidate in self.bufferCache) |
| 624 | + if (candidate.buffer.length >= length && (bestCandidate == nil || bestCandidate.lastReuseTime > candidate.lastReuseTime)) |
| 625 | + bestCandidate = candidate; |
619 | 626 |
|
620 |
| - if (bestCandidate != nil) |
621 |
| - { |
622 |
| - [self.bufferCache removeObject:bestCandidate]; |
623 |
| - bestCandidate.lastReuseTime = now; |
624 |
| - return bestCandidate; |
| 627 | + if (bestCandidate != nil) |
| 628 | + { |
| 629 | + [self.bufferCache removeObject:bestCandidate]; |
| 630 | + bestCandidate.lastReuseTime = now; |
| 631 | + return bestCandidate; |
| 632 | + } |
625 | 633 | }
|
626 | 634 |
|
627 | 635 | // No luck; make a new buffer
|
|
0 commit comments