native: a byte budget on host texture copies (copies nothing uses go first) #301
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/301"
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 m0rff on 2026-11-27T09:39:18Z.
Refs #294 (a piece of #8; proposed in #110)
What this changes
Host texture copies had no bound: a copy is made when a view first names a texture (
Title::realize, called fromglue::mirrorfor every view the device writes, staging heaps included) and kept until the title releases the texture. On electrifie's 16 GB M4 run in #8 (Series S), the host made about 13,000 copies during the first load while the footprint grew about 7.6 GB, thenkIOGPUCommandBufferCallbackErrorOutOfMemory.Budget (off by default, changed after review). Nothing is given up unless
VICTORY_METAL_TEXTURE_BUDGET_MB=<n>sets a budget ofnMiB (0or anything that is not a number: off). It is logged at startup astexture_budget_mibon thenative host: Metalline, next to the two numbers from #149 andsuggested_texture_budget_mib, the figure to try on that Mac:recommendedMaxWorkingSetSizeless the title's memory, at least 1536 MiB (4348 MiB on that M4; Series X on 16 GB gets the floor).What goes. When a submission finds the copies over budget (
Title::keep_to_budget, before its textures are synced), the least recently used copies that may go are given up until the total is back under budget. A copy may go only if all of these hold:Rcof it.Shaders index heaps themselves (#79), so a copy that an entry shows counts as used, even if no bound table has named it in a long time. D3D12 forbids rewriting an entry that submitted work still reads, so a copy that no entry shows is not reachable by work in flight.
Coming back. A given-up copy is released through
release_resource, like a released texture, and its description goes back toTitle::pending. A device-written view makes it again, and it is uploaded at the next submission.Tables a submission binds (changed after review). A record the title stores into a heap with plain stores is mirrored only when a list that binds its table is encoded, which happens after the sync. An entry that names a copy not made yet is cleared, and a shader samples a cleared entry as transparent black (
(0, 0, 0, 0)on an M2 Pro, macOS 26.6;victory-metalsampling_a_cleared_entry_reads_transparent_black). So before each submission's sync, when over budget or with any copy given up, the host reads the records of every table that the submission's draws and dispatches bind, plus those of work held behind fences (Title::keep_named,NativeHost::resources_named, the same entriesremirror_tableschecks). A given-up copy those records name is made again and uploaded at that sync, and no copy they name is given up at it. The names read stay out of the budget's reach until the submission is with its queue (Title::let_named_goaftersubmit_synced, counted per submission), so a sync that comes in between, whileglue::submithas the title lock let go, cannot give them up (changed after review). Under budget with nothing given up, nothing is read, except at the submission the trace logs at (once a second). Left (marked shortcut): a record the title changes after that read but before held work is encoded, and an entry mirrored outside a submission. Those stay cleared until the next submission.Trace. With
VICTORY_TRACE_TEXTURE_SYNC=1, once a second: live copy MiB, budget, MiB shown by heap entries or named by the submission's tables, MiB that could be given up, MiB given up, how many were made again, MiB of distinct title memory under the copies, and MiB of copies outside the title's pools. These are the per-second numbers electrifie asked for. The figures are the same with the budget off: at the submission it logs at, the trace reads that submission's tables itself.What this may not do. Olima34's 48 GB table in #79 puts 9.2 GB of textures in the heap sets. If most copies are shown by heap entries, this budget frees little, and the levers are the ones named in #79 (one copy instead of two, fewer top mips). The trace line answers that from a 16 GB run.
How it relates to other open work
VICTORY_METAL_RESIDENCY_SETS=heap. No second release path: an evicted copy leaves the heap residency set the same way a released texture does (its last reference goes,0133b82e). A copy that an entry shows, which is exactly what the heap set holds, is never evicted. So the two cannot fight.victory-metal(gfx.rs,metal.rs): how a submission is split into command buffers, and the command-buffer timeout. This one changes which host textures exist, invictory-gpu, before encoding.VICTORY_TRACE_HEAP_SET) and #267 (Olima34, wrapped heap buffer views), both merged. Both behaviours are kept. #292 is a diagnostic of what the heap sets hold, and this PR doesn't touch the sets. #267'smirrorrewrite only handles buffer views. This PR's evicted-texture branch is in the texture arm. The two traces complement each other: #292's covers bytes held by heap entries, this one's covers all host copies and what can be given up.60baf130(main: R9G9B9E5 placed views share an R32_UINT object's texture; GPU-written targets are not refilled). Rebased onto it. A placed view that shares an object's texture holds a thirdRcof it in the host's resources, so the object is never given up while the view is there (and an R32_UINT object compute writes is viewed for unordered access, which keeps it anyway).texture_released, which giving up calls, also releases that shared view, as on main. Textures the GPU drew into, cleared or wrote aregpu_writtenthere, and the budget already never gives those up.2d4cd0a1(sync off the lock). Neither bounds how many copies exist. They change probing and sync cost.How it was tested
New tests in
native::texture_tests, on Metal, arm64:the_least_recently_used_textures_go_first_until_the_excess_is_freedthe_texture_budget_is_off_unless_seta_texture_named_by_a_submission_in_flight_is_not_given_up_by_another: submission A's tables name a copy through a record that was never mirrored. Submission B'skeep_namedand sync run before A is queued, over budget. The copy stays until A lets its names go, then it is given up. Without the fix (B's read replaced A's names), it fails at "the first submission still names it".over_budget_only_textures_nothing_uses_are_given_up_and_come_back: a copy a heap entry shows, a copy a recorded list holds (a copy command), a render target, and a copy once viewed for unordered access all stay. The held copy goes once the list is dropped. The idle copy comes back with its texels.an_entry_copied_with_plain_stores_gets_an_evicted_texture_back(now the fallback outside a submission's scan)a_texture_given_up_is_back_for_the_draw_whose_table_names_itanda_texture_the_submission_names_is_not_given_up_under_its_draw: a real draw (tri_vs/tri_ps) samples, over budget, through a record stored with plain stores, and the test reads the pixel back. Without the fix the pixel was[0, 0, 0, 0]. Now it is the texel. Removing the remake fails the first test, and removing the protection fails both.victory-metalsampling_a_cleared_entry_reads_transparent_black: what a cleared entry samples.I checked that the guards are what make the tests pass. With the
Rccheck removed, the held test fails. With the heap-entry check removed, both tests fail.On main
60baf130:cargo test -p victory-hle -p victory -p victory-gpu -p xtask(victory-gpu native arm64: 360 passed, 3 ignored);cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warningsclean;cargo xtask policy384 files ok. Native clippy shows no warning in the lines this adds (twouseless vec!lints in its tests fixed).Ran the game: not yet. I need 16 GB and 24 GB testers on #8.
Checklist
VICTORY_*variable, off by default, documented in docs/native-host.mdImported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T10:32:35Z.
This is a draft, so only a first look. The guards (render targets, depth, GPU-written, shown by a heap entry, held by a list) are the right ones, and it is good that each has a test that fails without the check.
Two questions. You say yourself the budget may free little if most copies are shown by heap entries. Please post the trace of one run on a 16 or 24 GB Mac before you mark it ready, so we know if it matters. And "until then, that one entry shows nothing for that one submission": what happens to a draw that samples that entry in that submission? A black draw is worse than a late one, so please say. I have not run it.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T11:10:16Z.
Thanks. Rebased onto main
ce2b7860(#267 and #292 merged in with no conflict, both behaviours kept). There is one new commit,846ec4d1, and the PR stays a draft.(1) Trace from a 16 or 24 GB Mac. I can't post one: the machine I work on has no copy of the game, so I have no run of my own and no numbers. The request to testers is on #8. What I need from such a run:
Then, from
runs/<date>/victory.log:native host: Metal, withconsole,title_memory_mib,recommended_working_set_mibandtexture_budget_mib;host texture copies: <MiB>, budget <MiB>; <MiB> shown by heap entries, named by the submission's tables or viewed for unordered access, <MiB> could be given up; given up in the last second <n> (<MiB>), made again <n>, from the world load until the run ends or hitskIOGPUCommandBufferCallbackErrorOutOfMemory.If "shown by heap entries" makes up most of the copies, the budget frees little and #79's levers are the ones that matter. I won't mark this ready until such a run is posted.
(2) What does a draw read through that entry in that submission? Transparent black, so you were right to ask. I measured it on Metal:
victory-metalsampling_a_cleared_entry_reads_transparent_blacksamples an entry afterclear_entryand gets(0, 0, 0, 0)on an M2 Pro, macOS 26.6. That is a measurement, not a documented Metal guarantee. The path was:remirror_tables), after the submission's sync;There were two ways in:
A new test draws through such a record over budget (
tri_vs/tri_ps) and reads the pixel back:[0, 0, 0, 0]for both cases before the fix.Fixed the way you preferred, so the draw is late (one upload) rather than black. Before each submission's sync, when over budget or with any copy given up, the host now reads the records of every table that the submission's draws and dispatches bind, plus those of work held behind fences. It checks the same entries
remirror_tablesdoes (Title::keep_named,NativeHost::resources_named; the span is shared asNativeHost::table_span). A given-up copy those records name is made again and uploaded at that sync, and no copy they name is given up at it. Under budget with nothing given up, nothing is read.The tests are
a_texture_given_up_is_back_for_the_draw_whose_table_names_itanda_texture_the_submission_names_is_not_given_up_under_its_draw. Both now read the texel. Removing the remake fails the first, and removing the protection fails both.What is left, marked as a shortcut in the code and in docs/native-host.md:
Those still get the cleared entry until the next submission. Closing them would mean making textures during encoding, which needs the title's texture state inside
NativeHost::execute. That is a larger change, and I'd rather wait for a trace that shows it matters. The cost of the scan is one read of each distinct bound table range per submission, and only while over budget or after an eviction.Checks:
cargo test -p victory-hle -p victory -p victory-gpu -p xtaskpasses (victory-gpu on arm64, 325 passed), Windows clippy with-D warningsis clean, andcargo xtask policyis ok. I've updated the PR description and docs/native-host.md.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T11:39:59Z.
A trace from a 24 GB Mac, as asked: M4 Pro, 24 GB, macOS 15.8.2,
--console series-s, windowed,VICTORY_TRACE_TEXTURE_SYNC=1.The build was main
ce2b786with this PR's846ec4d1applied on top. #263, #253, #264, #278 and #304 were also applied (#296 too, at its new default of 0), plus a local title-watchdog patch. This run had no texture-level experiment and noVICTORY_METAL_RESIDENCY_SETS.On 24 GB the budget never comes into play.
Startup line:
title_memory_mib=7776 recommended_working_set_mib=16384 texture_budget_mib=8608. Metal recommends 16 GB here, so the budget is 8.4 GB.Host texture copies, from the once-a-second line:
Nothing was given up in the whole run. The copies never reached the budget.
Meanwhile the macOS memory level (
kern.memorystatus_level) held at 35 % until +90 s, then fell to 14 % by +108 s. I stop runs at 15 % so that the Mac does not freeze. The game was still alive at render frame 1536, at the world load, with no GPU errors.The last sync line before the stop covered 2.7 s: 2232 textures (2152 of them for the first time, 824 partly unreadable), 1932 MiB.
On this machine, then, the limit is system memory, not Metal's working set:
recommendedMaxWorkingSetSizelands above what a 24 GB Mac can hold next to the title's 7.6 GB.I have not tried a lower
VICTORY_METAL_TEXTURE_BUDGET_MB, because there was almost nothing for it to give up.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by daidalos4 on 2026-11-27T11:40:55Z.
Tested on a 16 GB Mac, as a follow-up to the residency-set run on #8.
Setup: MacBook Pro M1 (MacBookPro17,1), 16 GB, macOS 27.0.1,
--console series-s,VICTORY_METAL_RESIDENCY_SETS=heap, this branch (846ec4d1) merged onto main plus the read cache.Result: startup logs
texture_budget_mib=4348(recommended 12124, title 7776). The story loading screens ran for 22 minutes with nokIOGPUCommandBufferCallbackErrorOutOfMemory. Without the budget, the same load hit it atheap_set_mb=10651.heap_set_mbclimbed from 1849 to 10379 and stayed there.The run still ends at frame ~17000 on the same access violation as the two earlier runs:
0xc0000005 at image+0x55e56f8, info=[0, 0] (a read of address 0), r8=r10=0. On my runs this crash turned out to be independent of the GPU OOM. It hits at the same address with or without the OOM, so it looks like a separate bug further into the load.Edit:
image+0x55e56f8is not the first fault. In all three runs, a first-chance write to address 0 comes 1 to 5 s earlier, withinfo=[1, 0]andrax=0. It hitimage+0x23cf683in runs 1-2 andimage+0x2215c35in this one. The title's crash reporter (Breakpad/WER lines) then runs and faults at0x55e56f8. So the real failure looks like a null pointer being written through, possibly an allocation that came back empty, rather than anything in the crash path.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by ItsMariusBC on 2026-11-27T11:42:41Z.
A second 24 GB Mac, and it agrees with @alicans: the budget has almost nothing it may give up. Two things this adds: the run goes on into the world for the full 480 s, and I tried the lower budget.
Caveat first: this is the head before the rebase,
e656ddd, merged onto mainc1b9587, so withoutkeep_named. Apple M5 Pro, 24 GB, macOS 27.2.1, Series S, windowed, warm shader cache,VICTORY_HOLD_BYTE=1471b6330:1. The build also carried a local change that writes each shader container to disk when it is first translated (file writes in the first run's prewarm, none in the second). The log saystitle_memory_mib=7776 recommended_working_set_mib=18186 texture_budget_mib=10410.Run with
VICTORY_METAL_TEXTURE_BUDGET_MB=4096 VICTORY_TRACE_TEXTURE_SYNC=1, from the once-a-second line:Free memory (
memory_pressure, every 15 s) goes from 67 % at the start to 4 % at +76 s, where the copies jump from 2.5 to 7.9 GB, and to 0 % in the world. A run with the default budget also went down to 0 %.The freeze before the world is the same with and without the budget: 64 frames take 152 s with 4096, 143 s with the default, 185 s on plain main this morning. Each run then loses command buffers in the world (
run-summary.shcounts one out-of-memory failure in each, at +311 s and +439 s).One thing I cannot explain from the log: the copies add up to 10.1 GB counted by their size in title memory, while the title has 7.8 GB in all.
I can run the rebased head, or other switches, if that helps.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T11:44:14Z.
@daidalos4 on the first fault:
image+0x23cf683is the title's own watchdog, not an empty allocation. It is a deliberate null write; docs/ROADMAP.md lists it ("the game's watchdog fires 30 s into the frame: a deliberate null write atimage+0x23cf683, then the fault atimage+0x55e56f8").image+0x2215c35looks like the same watchdog's other path. It lies a few bytes after the call into that handler atimage+0x2215c27, and a local patch that skips that call (with the code right after it) and the write at0x23cf683stops both on my runs.When I've seen the write at
0x23cf683, it came about 30 s after the last frame. So on your runs it most likely means that something stalled for 30 s at frame ~17000, not that an allocation came back empty.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T11:54:33Z.
Thank you for the new commit, and the draw that is late instead of black is what I preferred.
The traces are in now. They say two things. On 16 GB (daidalos4) the budget kept the run free of
OutOfMemoryfor 22 minutes. On 24 GB (alicans, ItsMariusBC) there is almost nothing to give up: 310 MiB over a whole run with a 4 GB budget, because the copies are mostly named by heap entries. So this helps on 16 GB and not on 24 GB.ItsMariusBC also saw 10.1 GB of copies against 7.8 GB of title memory. Please look at what is counted twice (for example a copy made for each view of the same memory). Keep it as a draft until you have an answer for that.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T12:22:20Z.
Rebased onto main
465db23b(27 new commits, #263 included); the two commits applied cleanly. New commitf6265aa2. Still a draft.Short answer: nothing is counted twice, but some memory is held twice. The counter matches the host textures that exist. The 10.1 GB against 7.8 GB means the host keeps more than one copy of some title memory. Code alone can't say how much of the 2.3 GB this explains, so I've added a trace figure that will.
What the counter counts. One entry per title texture, at its layout's size in title memory: all mips, slices and planes (
Title::realizeadds it,texture_releasedtakes it off). A title texture is one of two things:placed_key).So:
create_texture_view), with no storage of its own. The same goes for several placed views of one description: the view's own description is not part of the key.views_of_one_texture_share_one_copy_counted_once,placed_views_of_one_description_share_one_key.a_textures_counted_size_is_never_more_than_its_footprint. So the count has no overcount of its own to fix.textures_over_one_memory_each_hold_a_copy_of_their_ownmakes three over 16 KiB: three Metal textures, the count is the sum, and the memory under them is 16 KiB. This is real memory, not a bookkeeping error.texture_placed,release_placed). If the title streams something else into that memory at other start addresses, the old copy stays. If a heap entry still holds the old record, that copy also counts as "shown".Committed resources come from the title's allocator (
XMemAlloc), so they should sit inside the pools; the trace checks that too.The trace that settles it. The once-a-second
VICTORY_TRACE_TEXTURE_SYNC=1line now ends:How to read it:
Copies in a pool are placed by their pool offset, so pages the title maps at two addresses (
use_page_mapper) count once (textures_over_one_memory_each_hold_a_copy_of_their_ownmaps one pool at a second address;a_copy_outside_the_titles_pools_is_logged_so).The texture sync line's "replaced at their address" count shows how often placed descriptions take one address from each other. @ItsMariusBC, @alicans: one world-load run of this head with the trace on would answer this. The quickest way to compare is the copies, distinct and outside figures at the peak.
What I'd build, if the trace shows a large overlap. Not started; your call. Two textures over one range would share one Metal texture where the formats allow a view of each other, as
alias_source/TEXTURES_ATalready does on Windows. A placed copy would also go once something else is placed over its memory, not only at its exact address. The first changes which texture a draw samples, and the second needs care with entries that still name the old copy. So I'd rather see the numbers first.Checks:
cargo test -p victory-hle -p victory -p victory-gpu -p xtaskpasses (victory-gpu on arm64: 333 passed). Windows clippy with-D warningsis clean, andcargo xtask policyis ok.a_texture_uploaded_blank_is_probed_at_each_submissionanda_texture_rewritten_whole_is_read_whole_only_now_and_thenflake about 2 runs in 5, and do the same on the head before this commit. The cause is other tests' textures sharing the probe's time budget, not this change. docs/native-host.md has a new paragraph under the budget section ("What the count counts, and why it can pass the title's memory").Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by daidalos4 on 2026-11-27T12:44:54Z.
@alicans thanks, that was it. With
VICTORY_HOLD_BYTE=1471b6330:1on the same 16 GB M1 (game partly on a network share), three more runs on this branch:OutOfMemory. Frames stopped after about 30 min atheap_set_mb=11924, with a fence wait held for over 130 s ("signal of 16138 queued 134.9s ago, GPU at 16136").VICTORY_METAL_TEXTURE_BUDGET_MB=2048, heap residency sets:OutOfMemoryafter 9 min atheap_set_mbof about 10.3 GB. On 16 GB the heap set alone takes most of Metal's 12124 MiB, so lowering the texture budget does not help there.The top frame above
_sigtrampis outside any image, so the fault may have hit while a signal was being handled in that path. This happened only once so far.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T12:54:45Z.
@m0rff two world-load runs of
f6265aa2with the trace on. Same Mac as before: M4 Pro, 24 GB, macOS 15.8.2, Series S. The build was mainc5d7f4d0with this PR and #296 on top. Neither run reached the first world frame, so these are the figures at the end of the load.VICTORY_METAL_TEXTURE_BUDGET_MB=0), no local texture change, +90 sSo most of it is overlap: 3 times the distinct memory in A, 5 times in B. On the story loading screens (+40 to +80 s) it is already 2447 MiB of copies over 1087 MiB in both runs.
In run B the budget gave up copies for the first time on this Mac: 6678 copies (140 MiB) at +89 s, then 211 more, then 91, with 37, 8, 5, 2 and 33 made again in the following seconds. At +103 s the process aborted:
mallocfound its free list corrupted (_tiny_check_and_zero_inline_meta_from_freelist, thenabort);-[AGXTexture newTextureViewWithPixelFormat:textureType:levels:slices:];ResourceBackend::create_texture_view,NativeHost::mirror,glue::heap_of,glue::set_heaps.It was one run, and run A (budget off) ended differently, so I can't say it is the budget. But it is the only abort of this kind I've had, and it came 14 s after the first copies were given up. A view made from a copy that was given up seems worth a look.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T12:56:04Z.
@daidalos4's SIGSEGV and my abort come through the same host path. Both are under
glue::mirror:create_placed_srv, thenglue::mirror,realizeandregister_texture;set_heaps, thenheap_of,NativeHost::mirrorandcreate_texture_view.Both ran with a budget on: theirs at the 16 GB default of 4348 MiB, mine 14 s after the first copies were given up. That's two single runs, so not proof. Still, it could be worth checking whether a mirror can reach a copy the budget has let go. @daidalos4, did your trace show copies given up before the crash?
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T13:03:18Z.
Review note, not merging yet (it is still a draft, and my 10.1 GB question is open).
glue::submitthe title lock is released betweenkeep_named/sync_jobsandsubmit_synced. Another queue's submission can runkeep_to_budgetin that gap and give up a texture that a table of this submission names through a record the title stored without a device call. The entry is then cleared and the draw samples transparent black.Holding the names of the submission in flight out of the budget's reach until
submit_syncedreturns would close it.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T13:06:19Z.
@m0rff a third run, with the same settings as run A above: budget off, all texture levels,
VICTORY_HOLD_BYTE. This time it got past the crash and further into the load (render frame 2240, +116 s). Then the copies took the Mac's memory:In those 12 s the copies grew by 5.4 GB over 0.48 GB of new distinct memory, about 11 copies per byte. The process RSS stayed at 6 GB, so the copies themselves are what used the memory. No GPU errors and no fault; I stopped it at 9 %.
So on 24 GB it is the overlap, not the budget, that stops the world load. If one Metal texture per range is what you'd build next, this Mac would be a good test for it. Run B fits the same picture: with the two top levels dropped it got to 10 GB of copies over 2 GB.
Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T13:14:20Z.
A correction to my two comments above about the
mallocabort: it is not the budget. A run with the budget off (VICTORY_METAL_TEXTURE_BUDGET_MB=0) on maind35ba927with this PR aborted the same way, 30 s in. It was still on the loading screens, before any copy could be given up, this time in a shader prewarm thread (_tiny_check_and_zero_inline_meta_from_freelistunder ahashbrownreserve invictory_shader::structure). So the free list is corrupted earlier, and the stack only shows wheremallocnoticed it.Both aborts had my local change that drops the two most detailed levels of sampled textures. The runs without it had none (two, budget off), so the cause is more likely on my side than in this PR. Please disregard the link I drew between the abort and the budget. @daidalos4's
register_texturecrash may be a separate thing. The race you found inglue::submitstands on its own.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T13:38:15Z.
Rebased onto main
60baf130and pushed. Still a draft. In order:1. Names of a submission in flight (
glue::submit). Done, with one correction to how the gap was described. Today another submission cannot get into that gap:glue::submittakesORDER(glue.rs:179) beforekeep_namedand holds it untilsubmit_syncedreturns.keep_to_budgetis reached only throughsync_jobs, whose only caller outside tests isglue::submit, and nothing else gives a copy up. But the title shouldn't depend on that lock to get this right, so it now holds the names itself:keep_namedadds the names it reads to a per-key count and returns them.keep_to_budgetskips any key with a count. It no longer takes the set, so a second submission's read cannot replace the first one's.Title::let_named_golowers the counts aftersubmit_synced. It is also called on theensure_queueerror path, so those copies can be given up again later.Test:
a_texture_named_by_a_submission_in_flight_is_not_given_up_by_another.keep_namedand sync run before A is queued. The copy stays.With the old code, where B's read replaced A's names, the test fails at "the first submission still names it". Left as before: under budget with nothing given up,
keep_namedreads nothing. Because ofORDER, nothing can go over budget in between.2. Off by default.
VICTORY_METAL_TEXTURE_BUDGET_MB=<n>turns it on atnMiB. Unset,0, or anything that is not a number means off. The startup line still showstexture_budget_mib(0 when off). Next to it,suggested_texture_budget_mibgives the figure to try on that Mac: the working set less the title's memory, at least 1536 MiB, so testers on other machines know what to set. The trace works the same with the budget off. At the submission the trace logs at (once a second),keep_namedalso reads that submission's tables, so the "named by the submission's tables" and "could be given up" figures match a run with the budget on. docs/native-host.md, the ROADMAP line and the PR description are updated.3. @daidalos4's SIGSEGV in
register_textureunderglue::mirror. I can't show from the code that a mirror reaches a released copy. What I checked:Rcs. Giving one up (evict_texture) removes the host's entry for it (release_resource) and the title'sTracked. The Metal texture is freed only when the lastRcgoes, and any list or entry still using it holds one. Nothing in this PR keeps a raw pointer to a copy. Its one raw read issafe_readof descriptor records inresources_named, which is not on that stack.realize. The key is inpending, so a new texture is made and registered.register_textureitself only touchesRefCellmaps (theevictedset, resources, the heaps'take_gone). That is safe code, with no path to address 0 apart from an allocator fault.60baf130, an R32_UINT object that a placed R9G9B9E5 view shares has a thirdRcheld by the host under the placed key. Soevictable(strong_count <= 2) is false and it is never given up while the view is there.texture_released, which giving up calls, releases that shared view too, as on main._sigtrampis at PC 0, outside any image. So a signal reached the thread while it was inregister_texture, and the fault happened on the delivery side: the stack shows where the thread was, not thatregister_texturefaulted.register_texturedoesn't call any Metal object. It clones anRcand edits maps, and on that stack the texture it registers was made a moment earlier byhost.textureinrealize. A use of a freed Metal texture would crash inside Metal/AGX orobjc_msgSend, not at PC 0 under a signal trampoline.@alicans has since seen
mallocfree-list corruption with the budget off, in a thread that has nothing to do with textures. I don't know what corrupted it.@daidalos4: did the trace show any copies given up (
given up in the last second <n>above 0) before frame ~7300? And could you repeat that configuration with the budget off, which is now the default? If it crashes the same way with nothing given up, it is not the budget.4. The 10.1 GB question. @alicans' third run (budget off) points to copies holding the same title memory several times over, not to a counting error. Between +115 s and +127 s of the world load, copies grew from 3147 to 8544 MiB (+5.4 GB) while the distinct title memory under them grew from 1190 to 1670 MiB (+0.48 GB): about 11 bytes of copies per new byte, and 5 times the distinct memory overall at the end. The process RSS stayed at 6 GB, and the free-memory level macOS reports fell to 9 % before they stopped it. Run A (4.0 GB over 1.3 GB) and run B (10.0 GB over 2.0 GB) fit the same pattern, as does the 10.1 GB against 7.8 GB on the M5 Pro. Caveats:
c5d7f4d0with this PR and #296 on top, run withVICTORY_HOLD_BYTE.A possible next step, which I haven't built: one Metal texture per range of title memory.
60baf130already shares one texture in one case: a placed R9G9B9E5 view laid out exactly like a live R32_UINT object.create_texture_view, no storage), as R9G9B9E5 does now.pending,take_wantedandkeep_namedpath.@LakshanSivakumar: is this wanted, and if so, as a separate PR after this one? I'd keep it out of #301. And is "the newest description owns the range" acceptable, given the same-frame case above, or should a range keep one copy per layout and share only between identical layouts? That would be safer, but it saves only the identical-layout part of the overlap.
Checks:
cargo test -p victory-hle -p victory -p victory-gpu -p xtask, with victory-gpu run natively on arm64: all passed. victory-gpu: 360 passed, 3 ignored.cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings: clean.cargo xtask policy: 384 files ok.useless vec!lints in this PR's own tests. Both are fixed.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T14:15:11Z.
The one-copy-per-range change is up as a separate draft, #359 (Refs #356). It is stacked on this PR, so #301 stays as it is.
#359 does two things:
@alicans, if the 24 GB Mac is free: a world load of #359's head with the budget off and
VICTORY_TRACE_TEXTURE_SYNC=1would show whether it helps. Compare the copies, the distinct memory and "made again" with your third run. The same line now also has "shared", "covered, given up" and "covered still in use".Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T14:25:30Z.
A second finding, and this one crashes. I merged this PR (budget off) into a local measuring branch and it died in the menus twice, at 43 s (SIGABRT from malloc, no panic text) and at 54 s (SIGSEGV).
Cause: the PR moves the snapshot of the submitted lists in
glue::submit(list.segments(), which runsNativeList::snapshot()) out from under the title lock. The snapshot copies thousands ofRcreferences to heaps, pipelines and textures while the recording thread copies and drops the same references under the lock.Rccounts are not atomic, so they get corrupted: a double free or a use after free. It happens on every submission, with the budget on or off.The tests cannot see it, because under
cfg(test)submit keeps the lock throughout.Fix I used locally: take the snapshots under the title lock again, once for
keep_named(dropped inside the lock) and once for the submit, as before this PR. Please fold that in, together with the lock gap from my earlier note.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T14:42:05Z.
Thanks, that is the bug. Folded in and pushed (
a0fbd51a, rebased onto main728845d1). Still a draft.The fix.
glue::submittakes the lists' snapshots under the title lock again, as you did it locally:with_title, forkeep_named; it is dropped there, beforesync_jobs;with_title, for the submission, where main had it.Nothing holding an
Rccrosses the unlocked reads now. On theensure_queueerror path the snapshot is not made at all.The compiler now rejects the old shape.
TitleList::segmentstakes a&Titleargument, and only code insidewith_titlehas one. I put the old loop (snapshot before the lock) back as a check, and it no longer compiles.Audit of the rest of this PR for
Rcs created, cloned or dropped outside the lock:keep_namedclones the held lists'Rcs, andkeep_to_budget,evict_textureandtexture_releaseddrop textureRcs. All of them take&mut Title, so they run under the lock.let_named_goand the names-in-flight counts areHashSet<usize>andHashMap<usize, _>: noRc.JobandPrepared(the unlockedprepare) hold noRc.preparehands&Jobto scoped threads, so the compiler already requires them to beSync.cfg(test)path (Title::submit) is fully under the lock.What stays untested, plainly:
cfg(test), submit keeps the lock throughout.The lock gap from your earlier note is covered by the names-in-flight counts from the last commit, which stay as they were:
keep_namedcounts each name per key.keep_to_budgetskips any key with a count.let_named_golowers the counts aftersubmit_synced, or on theensure_queueerror path.Test:
a_texture_named_by_a_submission_in_flight_is_not_given_up_by_another.Crashes reported here. @daidalos4's SIGSEGV in
register_textureand @alicans'mallocaborts (one of them with the budget off, in a shader prewarm thread) may have this cause:Rcfreed twice shows up later wherever the allocator next notices, which fits a fault in an unrelated thread.That is not shown. @alicans traced their aborts to a local change, and only a run of this head would tell.
Stacked drafts. #355 and #359 are rebased onto this head and pushed. I checked their own commits for the same mistake: everything they add runs through
&mut Titleor insidewith_title, and they add no snapshot orRcwork outside the lock. Both stay drafts.Checks on all three heads:
cargo test -p victory-hle -p victory -p victory-gpu -p xtask, with victory-gpu run natively on arm64: all passed (victory-gpu: 373 passed on this PR, 376 on #355 and #359).cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings: clean.cargo xtask policy: ok.The rebase onto
728845d1also neededused: 0in two test initialisers that main added forTracked. That is folded into the first commit.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:23:35Z.
This is on main through #359 (merged in
582670f1, which contains these commits; your rebase only changed docs context). Same checks as noted on #359, budget off only. Closing since it is fully on main. Thanks.Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by alicans on 2026-11-27T16:30:17Z.
Thanks, that race fits both
mallocaborts I reported here: both runs had this PR, the second with the budget off. So my guess that my local level-dropping change caused them was probably wrong. A later world load with the fix (main4cf84c83with #359) had no abort in 81 s, until my memory limit stopped it; that run did not have my local change.Pull request closed