Metal's command buffer retains what its encoders were given until it completes, so a program may
release a buffer or texture once it committed. Two paths did not hold theirs:
- A blit between a buffer and a multisampled texture, emulated per sample, retained only its
copy's plan (the raw VkBuffer and its own images), not the program's Buffer or the texture.
- An indirect sparse mapping update's host step replaced the update with the updates its arguments
state; where they state none, nothing held the texture and buffer, and the replaced update was
dropped under the command buffer's state lock, so a no-copy buffer's last release could run its
deallocator under that lock.
What changes:
- EncoderCore::blit_at, the one way a blit command takes a buffer's VkBuffer, retains the buffer
until completion, so every blit path holds it; the per-sample copy holds the texture it copies
into or out of.
- read_mappings moves the replaced sparse update into the command buffer's retained list,
released at completion outside the lock.
- A buffer or texture these commands used is now released at completion rather than at the
program's release.
Why: the consumer's resource synchronization on an RTX 5080 copies a multisampled Depth32Float
texture into a no-copy buffer over guest pages, commits, waits until scheduled and drops the
buffer. The import was freed 3 ms after the commit, and the copy engine's write at its first page
faulted 285 ms later and lost the device. METAL2VULKAN_LOG_MEMORY placed the fault at offset 0 of
the freed import and showed the next import mapped at the same address: with that mapping first,
the copy would have written into other guest memory silently.
Verified:
- tests::blit_holds (device test): each leg puts the blit behind 1 GiB of fills, commits, waits
until scheduled, releases the no-copy buffer and asserts its deallocator has not run while
pending, then checks the bytes in the host pages (buffer to and from no-copy, fill, multisampled
R32Uint both ways, multisampled Depth32Float set Volatile after release).
- tests::encoder_holds (device tests), one leg per way a command reaches memory: draws reading
vertex, fragment, index and indirect buffers, a draw into a texture over a no-copy buffer,
dispatches, an indirect dispatch, use_resource and use_heap reaches, an indirect command
buffer's dispatch, and sparse indirect updates that map a level and that map nothing. While
pending, the deallocator has not run and every watched object lives.
- Before the fix, on lavapipe under the validation layer 17 passed and 2 failed (the sparse
update's lost texture, and a heap's placed buffer, which Metal's contract does not hold either
and the test no longer watches); on the Iris Xe 18 passed, 1 failed (the placed buffer).
- The fixed runtime built under m2v_gpu and m2v_cpu_icd with clippy -D warnings clean; the fixed
legs ran in the following device batches.