metal, gpu: scope per-encoder heap residency and batch useResources (#382) #405
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/405"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T16:58:23Z.
Summary
When residency sets are disabled (
VICTORY_METAL_RESIDENCY_SETS=0, and by default on Macs with less than 32 GiB RAM), every render and compute pass named all ~37,000 resource-heap allocations one-by-one withMTLResourceUsage::Read | WriteviauseResource, costing ~155 ms per render pass in the kernel even when a pass's shaders never touchedResourceDescriptorHeapor only indexed bounded descriptor-table entries (docs/metal-backend.md).This PR scopes per-encoder heap residency to the pass's proven heap accesses and batches
useResources:Binding::of,analyze_dxil):victory-shader's emitted DXIL bitcode alongsideHostShader::user_data(without modifyingvictory-shaderor invalidating testers' warm shader disk caches) to classify every resourcedx.op.createHandleFromHeap/dx.op.annotateHandlecall per stage (VS/MS,PS,CS):Table { slot, offset, writes },Pairs { writes }(memory_handle/ROOT_SRV_VIEW/ROOT_UAV_VIEW), andFixed { index, writes }(includingSelectand cycle-checkedPhis).unbounded/unbounded_writesfallback whenever any resource heap index is dynamic or unrecognized, tracking UAV write flags (ResourcePropertiesbit0x1000).ps_heap_accesson the pipeline's graphicsBindingeven whenps_bindingis filtered toNonebecause the pixel stage has no user-data/views of its own.NativeHost::execute,HeapAccess):StageHeapAccessagainst boundRoot::Tableslots and[bh.count .. bh.count + 2 * pairs)into a sharedVec<HeapRange>buffer perexecutecall, attachingHeapAccess<'a>slices toDispatchandDraw.victory-metal::gfx):can_write(truefor buffers andShaderWritetextures,falsefor read-only SRV textures) and caches lazy contiguous(read_only, writable)resource pointer slices onHeap.use_heap_encoderloops with encoder-leveluse_heaps_encoderusing Metal's batchuseResources:count:usage:/useResources:count:usage:stages::heaps.resource;ReadandRead | Writebatches over only the named entries for bounded passes (preserving Metal's automatic UAV Write -> SRV Read hazard ordering when!set_on_queue);Read+Read | Writebatch slices for unbounded bindless passes, keeping non-ShaderWritetexturesReadinstead ofRead | Write.MTLResidencySetmode (residency_sets == true) is unchanged.Measurements & Verification
encoder_residency_scopes_bounded_passes_and_batches_bindless_passescovering:set_heapsbound whose VS/PS do not read the heap (0heap resources named vs259);3 + 2 = 5heap resources named vs518, verifying UAV Write -> SRV Read hazard ordering without residency sets);Lower::memory+view_addressescompute pass (3heap resources named,0of256unrelated textures);unbounded && !unbounded_writes, batcheduseResourcesover260resources).per_encoder_residency_10k_textures_benchmark:with_residency_sets(false)): 148.38 ms total (0.742 ms/pass), naming 100,320 heap resources vs 2,000,400 unscoped (20x reduction; 0–3 resources per bounded pass).cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtaskcargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warningscargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warningscargo xtask policycc @Olima34
Closes #382
Refs #8, #79, #258, #392
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:37:09Z.
Review: CHANGES_REQUESTED
Thanks for this PR! We're helping the maintainer clear the queue tonight, and main has moved a lot (the preview branch landed as
86cefd22, then a batch of PRs; main is nowa6a58361). This branch now conflicts with main incrates/victory-gpu/src/native/heap_tests.rs.Could you merge current
maininto the branch (a merge commit is fine, no need to rebase) and resolve the conflict? It looks like tests appended at the same spot as tests that landed on main; keep both sides. Once GitHub shows it as mergeable and CI is green on the new head, we'll read it through and merge it if it holds up.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:40:37Z.
Please disregard my change request above: it crossed with the maintainer merging this PR (land/prs-2), so the conflict is resolved on main. Thanks!
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by ItsMariusBC on 2026-11-28T00:45:46Z.
A game data point for this from a 24 GB Mac, where the default is
useResource per encoder. It was taken before the merge, on the PR head as it was then.Apple M5 Pro, 24 GB, macOS 27.2.1, main
b998bb65+ this PR at9706344f, Series S, windowed, warm shader cache (no translation in the runs),VICTORY_HOLD_BYTE=1471b6330:1 VICTORY_TEXTURE_DROP_TOP_MIP=1,--seconds 480. The build also carried a local change that is off unless a switch is set.Per command buffer, in the world (
VICTORY_TRACE_COMMITS=1 VICTORY_PASSES_PER_BUFFER=1, the 370 s from the first indirect rectangle-list draw to the end):gpu_msSo nine buffers in ten are now near free, and what is left of the cost is in 891 of them at 100 to 140 ms each. I take those to be the unbounded passes, but the trace does not say which pass a buffer holds, so that is a guess.
The frame rate did not follow. World frames in 480 s runs without the trace: 32 and 47 with this PR, 11 on main
b998bb65earlier the same evening, about 30 on main in the morning. That spread is too wide to show a gain.What the trace says about why: the GPU is busy 118 s of the 370, and a buffer takes 263 ms from commit to done at the median (p90 1,018 ms) while its
gpu_msis near zero. During the run the free memory level (kern.memorystatus_level) was 2 to 3 % and 6.4 GB of swap was in use. So on 24 GB the time is no longer in the passes. I did not measure where it goes instead.Before the world: 19,498 buffers, 2.4 s of
gpu_msin all, the longest 30 ms. No GPU error in any of these runs, and the load reaches frame 1984 at about +83 s against +112 s on main.I can attach the logs.