#2068·Cemu

Metal: staging ring allocator can overwrite pending upload data (stale lastSyncpointCommandBuffer pointer after command buffer address reuse)

Author: domitakesCreated Sep 20, 2026Updated Sep 20, 2026
Labelsbug

Current Behavior

On the Metal backend, texture and buffer-cache uploads can end up with the contents of other uploads. In The Legend of Zelda: Twilight Princess HD this shows up as:

  • sky / cloud / sun billboards on the title screen rendered with the pixel data of unrelated textures (font atlas, title logo) or with garbage stripes (screenshot attached)
  • huge exploding polygons covering the screen when Metal buffer cache mode is auto (device private). Setting it to host hides this part, because vertex data no longer goes through the staging allocator

Vulkan (MoltenVK) renders the same scenes correctly. This is very likely the same glitch reported in SamoZ256/cemu-metal-game-compatibility#48 ("occurs when entering/exiting locations", "sometimes restarting Cemu fixes it"): it triggers during upload bursts, and the corrupted textures then stay in the texture cache.

ImageImage

For a clear comparison using Metal vs Vulkan:

ImageImage

Root cause

MetalSynchronizedRingAllocator identifies command buffers by their MTL::CommandBuffer* address. addUploadBufferSyncPoint() skips adding a sync point when the current command buffer equals buffer.lastSyncpointCommandBuffer:

cpp
auto commandBuffer = m_mtlr->GetCurrentCommandBuffer();
if (commandBuffer == buffer.lastSyncpointCommandBuffer)
    return;

lastSyncpointCommandBuffer is never cleared. In ProcessFinishedCommandBuffers() a finished command buffer is passed to CleanupBuffers() and then released. The next command buffer obtained from the queue is frequently allocated at the same address. When that happens the pointer comparison above is a false positive: no sync point is recorded for the new command buffer, so its reservations are not protected. With an empty sync point queue the allocator considers the whole ring free, and once writeIndex wraps around (easy during loading, the staging ring is 32 MiB) it hands out memory that still backs blits which have been encoded but not yet executed by the GPU. Those blits then copy whatever was written later.

The Vulkan allocator this was ported from uses monotonically increasing command buffer IDs, so it is not affected.

Proposed fix

Clear the stale pointer when the command buffer it refers to has finished:

diff
--- a/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.cpp
+++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.cpp
@@ -119,6 +119,11 @@ void MetalSynchronizedRingAllocator::CleanupBuffer(MTL::CommandBuffer* latestFin
 		{
 			itr.queue_syncPoints.pop();
 		}
+		// the finished command buffer gets released after this and a new command buffer is likely to be allocated at the same address
+		// if we kept the stale pointer around, addUploadBufferSyncPoint() would skip adding a sync point for the new command buffer,
+		// leaving its allocations unprotected and allowing the ring buffer to overwrite data that hasn't been consumed by the GPU yet
+		if (itr.lastSyncpointCommandBuffer == latestFinishedCommandBuffer)
+			itr.lastSyncpointCommandBuffer = nullptr;
 		if (itr.queue_syncPoints.empty())
 			itr.cleanupCounter++;
 	}

A more robust long-term option would be to tag sync points with a monotonically increasing command buffer index instead of the object address, like the Vulkan backend does.

With this change applied to a local build, the title screen and gameplay render correctly, both with Metal buffer cache mode = host and with auto. No other settings were changed between runs.

Expected Behavior

Textures and vertex data uploaded through the Metal staging allocator should keep their own contents. The Twilight Princess HD title screen should look the same as on Vulkan (sun, clouds and sky rendered correctly, no oversized polygons), regardless of the Metal buffer cache mode setting.

Steps to Reproduce

  1. Build Cemu from main (tested at 0314ec9) on an Apple Silicon Mac and select Metal as the graphics API
  2. Launch The Legend of Zelda: Twilight Princess HD (US, v81)
  3. Wait for the title screen (Link riding Epona across Hyrule Field)
  4. With Metal buffer cache mode = auto: huge polygons cover the screen. With host: the polygons are gone but the sky / clouds / sun show the contents of other textures (font atlas, logo) or garbage

Reproducible on every launch on my machine. Since it depends on a released command buffer's address being reused by the next one, the exact frequency may vary between systems.

System Info (Optional)

OS: macOS 26.6.2 GPU: Apple M2 Max (64 GB unified memory) Cemu: built from main at 0314ec9, release build, native arm64

Emulation Settings (Optional)

Graphics API: Metal Async shader compile: on Shader fast math: true (default) Position invariance: auto (default) Metal buffer cache mode: auto and host (see above) Graphic packs: Resolution (3840x2160) CPU mode: Multi-core recompiler

Logs (Optional)

No response