gpu(native): make list upload memory title memory the GPU reaches #100

Closed
Olima34 wants to merge 4 commits from upstream-pr/100 into main
Olima34 commented 2026-11-26 14:42:34 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T14:42:34Z.


What this changes

Refs #15 (the Box32Rehydration dispatch whose t0 was the malloc address 0x60000022eb80).

On macOS a command list's upload block (list method 0x4a0, allocate_upload, where the BVH builder writes its instance descriptions) was vec![0; len]: heap memory above 2^40 that the native host's pool table did not know. Neither NativeHost::resolve (root views: "title address … is not in a mapped pool") nor the shaders' page table, built from the same mappings, could reach it, so the BVH builder's shaders read zeros there. On Windows the same block is VirtualAlloc2'd in [4 GiB, 2^40) and registered with shadow.

  • Allocation (macOS): each block is reserved and committed in [4 GiB, 2^40) with victory_platform::mem::reserve_in, in whole 16 KiB pages (what a Metal buffer over it needs, as the title's pools have), so it comes zeroed.
  • Visibility: each block is a pool of its own in glue::pools() (pool_created, then mapped at its own address), so pool_buffer wraps it like any title pool and the page table shows it. Its pool id has bit 63 set; xmem_physical numbers the title's pools from 1 up, so the two never meet.
  • Release: when a list is reset, or released for the last time, its blocks are retired: kept until the GPU is past everything the queues were given before (Title::retire: one fence of ours per queue, signalled behind held work), then unmapped, pool_released and unmapped from the address space, as the title's pools are. If a fence cannot be queued, the memory is kept for good rather than freed under the GPU. refresh drops what is retired and done.
  • The Windows path is unchanged; other non-Windows targets keep the plain block.

This addresses the t0 half of what the issue describes. Whether the [49, 1, 1] overrun goes away once the shader reads real instance data still needs a game run.

How it was tested

  • victory-gpu on arm64 with the Metal Shader Converter (164 tests): upload_blocks_are_pools_of_their_own (addresses in [4 GiB, 2^40), 16 KiB aligned, zeroed, resolved by the pool table as the same bytes, ids apart from the title's, gone from the table and in take_released after drop), retired_memory_outlives_the_work_queued_before_it (on Metal: what is retired is dropped only once the GPU is past the work queued before it), and the existing allocate_upload test now also checks that the block resolves as the same bytes and is gone once retired. The new checks fail on main.
  • arm64 cargo test --workspace, cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings, also with --all-features (the Windows path compiles unchanged)
  • cargo xtask policy
  • Ran the game: not done, no copy of it here

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • No new guesses about console behaviour (the block's semantics are the ones the Windows path already has)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5512060026 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/100). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T14:42:34Z. --- ## What this changes Refs #15 (the `Box32Rehydration` dispatch whose `t0` was the malloc address `0x60000022eb80`). On macOS a command list's upload block (list method 0x4a0, `allocate_upload`, where the BVH builder writes its instance descriptions) was `vec![0; len]`: heap memory above 2^40 that the native host's pool table did not know. Neither `NativeHost::resolve` (root views: "title address … is not in a mapped pool") nor the shaders' page table, built from the same mappings, could reach it, so the BVH builder's shaders read zeros there. On Windows the same block is `VirtualAlloc2`'d in [4 GiB, 2^40) and registered with `shadow`. - **Allocation (macOS):** each block is reserved and committed in [4 GiB, 2^40) with `victory_platform::mem::reserve_in`, in whole 16 KiB pages (what a Metal buffer over it needs, as the title's pools have), so it comes zeroed. - **Visibility:** each block is a pool of its own in `glue::pools()` (`pool_created`, then `mapped` at its own address), so `pool_buffer` wraps it like any title pool and the page table shows it. Its pool id has bit 63 set; `xmem_physical` numbers the title's pools from 1 up, so the two never meet. - **Release:** when a list is reset, or released for the last time, its blocks are retired: kept until the GPU is past everything the queues were given before (`Title::retire`: one fence of ours per queue, signalled behind held work), then `unmapped`, `pool_released` and unmapped from the address space, as the title's pools are. If a fence cannot be queued, the memory is kept for good rather than freed under the GPU. `refresh` drops what is retired and done. - The Windows path is unchanged; other non-Windows targets keep the plain block. This addresses the `t0` half of what the issue describes. Whether the `[49, 1, 1]` overrun goes away once the shader reads real instance data still needs a game run. ## How it was tested - [x] `victory-gpu` on arm64 with the Metal Shader Converter (164 tests): `upload_blocks_are_pools_of_their_own` (addresses in [4 GiB, 2^40), 16 KiB aligned, zeroed, resolved by the pool table as the same bytes, ids apart from the title's, gone from the table and in `take_released` after drop), `retired_memory_outlives_the_work_queued_before_it` (on Metal: what is retired is dropped only once the GPU is past the work queued before it), and the existing `allocate_upload` test now also checks that the block resolves as the same bytes and is gone once retired. The new checks fail on `main`. - [x] arm64 `cargo test --workspace`, `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`, also with `--all-features` (the Windows path compiles unchanged) - [x] `cargo xtask policy` - [ ] Ran the game: not done, no copy of it here ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] No new guesses about console behaviour (the block's semantics are the ones the Windows path already has) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
LakshanSivakumar commented 2026-11-26 15:04:39 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T15:04:39Z.


Thanks for this, the t0 side of #100 is right and a real step. We ran it, and on its own it doesn't yet get the story load past the prologue. Details below.

Setup: Apple M5 Pro, macOS 27.2.1, --console series-s, story-mode load, main at af380ca + #100.

What we saw

  1. #100 alone: the earlier crash (title reading float-pattern pointers such as 0x7f8000007f800000) no longer appeared at ~80 s. Instead the title hung there, and at ~112 s its own 30 s hang watchdog crashed it (first-chance write at image+0x23cf683, then unhandled at image+0x55e56f8). That run still logged 229 "title address ... is not in a mapped pool" warnings.
  2. The hang is a host problem that the extra pools exposed. NativeHost::memory_table calls PoolTable::mappings() on every compute dispatch that uses the page table (thousands per frame). That clones the whole mapping map under the device lock before checking whether the cached table is current. A sample showed 52 of 54 pump-thread samples in memory_args -> PoolTable::mappings, with title threads parked on that lock (one in glue::mirror via create_placed_uav). We have a local fix (PoolTable::mappings_if_changed(generation), with a test) and will send it as its own PR. Worth checking whether per-upload pools make other per-dispatch paths slower too.
  3. With #100 + that fix: no hang, but the title crashes at ~96-103 s (frames ~1910-2047) with access violations reading float-pattern data as pointers. The crash site moves between runs (image+0x8f92a, +0x55e56f8, +0x54877e0, +0x4da8c10), which points to memory corruption rather than one bad path.
  4. Same burst: compute dispatches of [38,1,1] and [49,1,1] groups (32 or 64 threads per group, memory=true). We checked dispatch decoding and it is correct: DispatchX/Dispatch store group counts unscaled, and the host passes them to dispatchThreadgroups with numthreads from the DXIL. So the over-wide writes (previously measured at 49x8192 bytes past the end of the BVH destination buffer) most likely come from the title's own inputs, for example a count in Constants[0] (0x4a80 in an earlier trace), or data that a refused shader or a GPU-written buffer never copied back to title memory (see #46).

Questions for the author

  • Is the instance-description data the BVH shaders read through t0 now non-zero and correct?
  • How do Constants[0] and the destination buffer size relate?
  • retire() covers only queues that exist at call time. Could it free an upload block that a later-created queue still reads?
  • A reviewer also asked to double-check the refs()==1 test in list_release against a concurrent AddRef.

Thanks again, this is useful groundwork, and we'll link the memory_table PR here once it's up.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7195747139 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/100#issuecomment-7195747139). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T15:04:39Z. --- Thanks for this, the t0 side of #100 is right and a real step. We ran it, and on its own it doesn't yet get the story load past the prologue. Details below. **Setup:** Apple M5 Pro, macOS 27.2.1, `--console series-s`, story-mode load, `main` at af380ca + #100. ## What we saw 1. **#100 alone:** the earlier crash (title reading float-pattern pointers such as `0x7f8000007f800000`) no longer appeared at ~80 s. Instead the title **hung** there, and at ~112 s its own 30 s hang watchdog crashed it (first-chance write at image+0x23cf683, then unhandled at image+0x55e56f8). That run still logged 229 "title address ... is not in a mapped pool" warnings. 2. **The hang is a host problem that the extra pools exposed.** `NativeHost::memory_table` calls `PoolTable::mappings()` on every compute dispatch that uses the page table (thousands per frame). That clones the whole mapping map under the device lock before checking whether the cached table is current. A `sample` showed 52 of 54 pump-thread samples in `memory_args -> PoolTable::mappings`, with title threads parked on that lock (one in `glue::mirror` via `create_placed_uav`). We have a local fix (`PoolTable::mappings_if_changed(generation)`, with a test) and will send it as its own PR. Worth checking whether per-upload pools make other per-dispatch paths slower too. 3. **With #100 + that fix:** no hang, but the title crashes at ~96-103 s (frames ~1910-2047) with access violations reading float-pattern data as pointers. The crash site moves between runs (image+0x8f92a, +0x55e56f8, +0x54877e0, +0x4da8c10), which points to memory corruption rather than one bad path. 4. **Same burst:** compute dispatches of [38,1,1] and [49,1,1] groups (32 or 64 threads per group, memory=true). We checked dispatch decoding and it is correct: `DispatchX`/`Dispatch` store group counts unscaled, and the host passes them to `dispatchThreadgroups` with numthreads from the DXIL. So the over-wide writes (previously measured at 49x8192 bytes past the end of the BVH destination buffer) most likely come from the title's own inputs, for example a count in Constants[0] (0x4a80 in an earlier trace), or data that a refused shader or a GPU-written buffer never copied back to title memory (see #46). ## Questions for the author - Is the instance-description data the BVH shaders read through t0 now non-zero and correct? - How do Constants[0] and the destination buffer size relate? - `retire()` covers only queues that exist at call time. Could it free an upload block that a later-created queue still reads? - A reviewer also asked to double-check the `refs()==1` test in `list_release` against a concurrent AddRef. Thanks again, this is useful groundwork, and we'll link the `memory_table` PR here once it's up.
Olima34 commented 2026-11-26 15:28:42 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T15:28:42Z.


Thanks for running it. I've pushed three commits for your points; the branch is rebased on main.

Q4, refs()==1 in list_release. You were right that it was unsafe, through two concurrent Releases rather than an AddRef. A concurrent AddRef on the last reference is already the caller's bug under COM rules. But two releases of a list holding two references could both read 2, so neither retired the blocks, and the last release_boxed freed them at once, under queued work. The retire now happens inside the release whose atomic decrement reaches 0: object::release_boxed_before, a fetch_update that runs the callback only on 1 → 0. glue::locked hands that callback the already locked host, so nothing locks twice. The new test a_list_released_from_two_threads_retires_its_upload_memory releases from two threads through a barrier while a queue is held behind a fence. It failed 27 times out of 30 with the old check, and 0 out of 30 now.

Q3, retire() and queues made later. A queue created after retire cannot read the block: the block is only read by work recorded before the reset or release that retired it, that work only reaches the GPU through ExecuteCommandLists on a queue that already exists (or sits in held, which holds_work covers), the native host has no bundles, and ExecuteIndirect isn't executed. A test now pins that rule. Checking it turned up a real gap, though: a queue the title releases. forget_queue dropped it from the table while command buffers it had already sent could still read the block. It now leaves a mark on that queue's fence, and retire waits for any marks not yet passed.

Per-dispatch cost. As you suspected, one pool per block changed the pool table at every allocate_upload and every free, so even with mappings_if_changed the page table would have been rebuilt every frame. Blocks are now carved from an arena (native/arena.rs):

  • pools of 64 MiB (or the block's size if larger), below 2^40, each told to the table once;
  • first fit with merging of neighbouring free ranges, 16-byte alignment, each block zeroed;
  • a range goes back only after retire.

A test runs 100 allocate/free cycles after warm-up and checks that the pool count and the table generation stay the same.

Q1, the instance data behind t0. I can't run the game, so I can't say it is correct. What the change does guarantee, and what a test checks, is that the GPU now reads the very bytes the title wrote through the pointer allocate_upload returned: the pool table resolves that address to the same host address. Before, the GPU could not read it at all. Whether those bytes are non-zero depends on the title having written them before the dispatch runs. A dump of [t0, t0 + 64) at the Box32Rehydration dispatch on your side would settle it.

Q2, Constants[0] and the destination. From the numbers in your trace:

  • u1 + Constants[0] = 0x831be0100 + 0x4a80 = 0x831be4b80, exactly where you measured the writes start. So Constants[0] looks like a byte offset into the destination.
  • u1 sits 0x100 past a 128 KiB boundary (0x831be0000), and the buffer ends at 0x831c00000, 128 KiB later. The destination looks like a 128 KiB block with a 256-byte header.
  • 49 groups × 8 KiB = 392 KiB, about three times that block. From the start offset, the room left is 0x1b480 bytes, which is 13.6 groups' worth.

So the group count, or the 8 KiB per group, doesn't come from the size of this block. That fits your point that the over-wide writes come from the title's own inputs. If the dispatch's group count is computed from data the GPU produced (a count written by an earlier pass), #46 (GPU writes not copied back to title memory) would be my first suspect.

Related: I sent #102 for the 229 warnings. Each comes from a whole ExecuteCommandLists piece being refused for one command at unmapped memory, which also drops the piece's valid dispatches, copies and value writes. It drops that command alone, the way #57 does.

Tests: victory-gpu on arm64 with Metal, 4 passes, 174/174 each; arm64 cargo test --workspace (692); the x86_64 suites; both Windows clippy runs. None of it has been checked in the game.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7196321938 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/100#issuecomment-7196321938). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T15:28:42Z. --- Thanks for running it. I've pushed three commits for your points; the branch is rebased on `main`. **Q4, `refs()==1` in `list_release`.** You were right that it was unsafe, through two concurrent `Release`s rather than an `AddRef`. A concurrent `AddRef` on the last reference is already the caller's bug under COM rules. But two releases of a list holding two references could both read 2, so neither retired the blocks, and the last `release_boxed` freed them at once, under queued work. The retire now happens inside the release whose atomic decrement reaches 0: `object::release_boxed_before`, a `fetch_update` that runs the callback only on 1 → 0. `glue::locked` hands that callback the already locked host, so nothing locks twice. The new test `a_list_released_from_two_threads_retires_its_upload_memory` releases from two threads through a barrier while a queue is held behind a fence. It failed 27 times out of 30 with the old check, and 0 out of 30 now. **Q3, `retire()` and queues made later.** A queue created after `retire` cannot read the block: the block is only read by work recorded before the reset or release that retired it, that work only reaches the GPU through `ExecuteCommandLists` on a queue that already exists (or sits in `held`, which `holds_work` covers), the native host has no bundles, and `ExecuteIndirect` isn't executed. A test now pins that rule. Checking it turned up a real gap, though: a queue the title *releases*. `forget_queue` dropped it from the table while command buffers it had already sent could still read the block. It now leaves a mark on that queue's fence, and `retire` waits for any marks not yet passed. **Per-dispatch cost.** As you suspected, one pool per block changed the pool table at every `allocate_upload` and every free, so even with `mappings_if_changed` the page table would have been rebuilt every frame. Blocks are now carved from an arena (`native/arena.rs`): - pools of 64 MiB (or the block's size if larger), below 2^40, each told to the table once; - first fit with merging of neighbouring free ranges, 16-byte alignment, each block zeroed; - a range goes back only after `retire`. A test runs 100 allocate/free cycles after warm-up and checks that the pool count and the table generation stay the same. **Q1, the instance data behind `t0`.** I can't run the game, so I can't say it is correct. What the change does guarantee, and what a test checks, is that the GPU now reads the very bytes the title wrote through the pointer `allocate_upload` returned: the pool table resolves that address to the same host address. Before, the GPU could not read it at all. Whether those bytes are non-zero depends on the title having written them before the dispatch runs. A dump of `[t0, t0 + 64)` at the `Box32Rehydration` dispatch on your side would settle it. **Q2, `Constants[0]` and the destination.** From the numbers in your trace: - `u1 + Constants[0]` = `0x831be0100 + 0x4a80` = `0x831be4b80`, exactly where you measured the writes start. So `Constants[0]` looks like a byte offset into the destination. - `u1` sits `0x100` past a 128 KiB boundary (`0x831be0000`), and the buffer ends at `0x831c00000`, 128 KiB later. The destination looks like a 128 KiB block with a 256-byte header. - 49 groups × 8 KiB = 392 KiB, about three times that block. From the start offset, the room left is `0x1b480` bytes, which is 13.6 groups' worth. So the group count, or the 8 KiB per group, doesn't come from the size of this block. That fits your point that the over-wide writes come from the title's own inputs. If the dispatch's group count is computed from data the GPU produced (a count written by an earlier pass), #46 (GPU writes not copied back to title memory) would be my first suspect. Related: I sent #102 for the 229 warnings. Each comes from a whole `ExecuteCommandLists` piece being refused for one command at unmapped memory, which also drops the piece's valid dispatches, copies and value writes. It drops that command alone, the way #57 does. Tests: `victory-gpu` on arm64 with Metal, 4 passes, 174/174 each; arm64 `cargo test --workspace` (692); the x86_64 suites; both Windows clippy runs. None of it has been checked in the game.
LakshanSivakumar commented 2026-11-26 15:55:44 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T15:55:44Z.

Review: COMMENTED


Holding this one. The t0 side is right, but together with #102 and #105 it still does not get the story load past the prologue (crash ~100 s in, preceded by a hang on a job semaphore). GPU compute writes are ruled out; the current lead is physical pages owned twice in xmem_posix. Changing which memory the GPU reaches before that is understood risks hiding or moving the fault, so it will not be merged until then. No further work needed on this PR for now.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6077114886 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/100#pullrequestreview-6077114886). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T15:55:44Z. **Review:** COMMENTED --- Holding this one. The t0 side is right, but together with #102 and #105 it still does not get the story load past the prologue (crash ~100 s in, preceded by a hang on a job semaphore). GPU compute writes are ruled out; the current lead is physical pages owned twice in xmem_posix. Changing which memory the GPU reaches before that is understood risks hiding or moving the fault, so it will not be merged until then. No further work needed on this PR for now.
Olima34 commented 2026-11-30 09:17:24 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-30T09:17:24Z.


Closing: the same change landed in BareMetalVI (e96b28ac "a command list's upload memory is graphics memory the GPU reaches", then 4a7532f7 and 68afe2a1). The project lives at BareMetalVI/BareMetalVI now.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7273997978 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/100#issuecomment-7273997978). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-30T09:17:24Z. --- Closing: the same change landed in BareMetalVI (e96b28ac "a command list's upload memory is graphics memory the GPU reaches", then 4a7532f7 and 68afe2a1). The project lives at BareMetalVI/BareMetalVI now.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!100
No description provided.