Skip to content

GPU and shaders: stores through title memory by address mark their copy, and only marked copies are taken for written (#284) - #346

Closed
ghost wants to merge 2 commits into
mainfrom
unknown repository
Closed

ghost wants to merge 2 commits into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Nov 24, 2026

Copy link
Copy Markdown

Closes #284.

What

A list whose shaders could store through title memory by address declared every numbered copy written. Declaring a copy written costs a snapshot of its bytes before the submission and a compare after it (shadow.rs). In the prologue with the structure builder on, 1,023 copies are numbered (the limit), 4.3 GB, and three to four lists a frame declared all of them: a frame took 3.2-3.4 s, of which about 2.8 s were those snapshots and compares. The module's comment already named the remedy ("a dirty-page mask written by the shader would bound both").

This PR does the copy-granular form of it:

  • Shader (Lower::memory): every store and atomic through a copy also sets that copy's mark, dword MEMORY_FAULTS + n of the buffer the fault notes already live in (MEMORY_MARKS = 1024 dwords after them). A plain store of 1, so no atomic; lanes that do not run write the sink. Loads do not mark.

  • Host (memory.rs): the marks are read and cleared once a frame, with the faults. A list that can store declares written only

    • the copies marked in one of the last 8 frames,
    • the copies its dispatches name (root views and root constants),
    • the copies numbered while it is recorded.

    Each copy at most once per list. map and name return the copy's index for this.

No new root parameter, no change to draw.rs, the pipeline code or the shader interface beyond the longer buffer. The translation changes, so cached translations of these programs are redone (the build fingerprint does that).

The shortcut it takes

The first store to a copy that none of the three rules holds is not copied back to title memory: its mark brings the copy in from the next frame the marks are read in. That is the loss a page without a copy already has, for a frame or two. The host logs how many copies that happened to (shaders stored to copies no list had declared written), 50 lines at most.

Measured

RTX 5080 16 GB (32-lane waves), Windows 11, diag build, Series X profile, VICTORY_SHADER_LOOP_LIMIT=256, warm caches, scripted Enter, 300 s runs. The builder numbers are with #156 merged locally, because on a 32-lane card the builder does not start without it; this branch itself is on main (2119b0e) and does not need #156.

Builder on (VICTORY_BVH_BUILDER=1 VICTORY_BVH_QUARANTINE=1), per frame, from each run's last reports:

#156 #156 + this
frame time 3,180-3,440 ms 529-577 ms
on the GPU 274-345 ms 220-229 ms
preparing 1,410-1,560 ms 212-239 ms
copying back 1,250-1,435 ms 78-90 ms
ranges compared after submissions 1,453 597

The marks named 38 of the 1,023 copies in the first frame they were read, then 1 to 5 new ones a frame.

Builder off (defaults), this branch on main against main: 375-418 ms a frame against 416-422 ms, no device loss and no title crash in 300 s either way. So nothing moves in a normal run.

Tested

  • cargo test --release -p victory-shader -p victory-gpu: 110 + 210 passed, 0 failed, on this branch and with Ray tracing: shadow-ray traces and the full structure builder translate on 32-lane GPUs #156 merged.
  • cargo clippy --release -p victory-shader -p victory-gpu: no warnings. The files this touches are rustfmt-clean where this touches them (the tree as a whole is not, so I did not run it over the crates).
  • In the game: the three runs above, plus one with the builder on and no quarantine (see below).

What I could not test

  • Write-back correctness with the builder's stores going to title memory. With VICTORY_BVH_BUILDER=1 and no quarantine the title crashes about 5-7 s after the builder starts, at the same addresses with and without this change (+0x27c195d, +0x2bd385b, +0x4f38ba5, +0x4e335b4, +0x8f92a): the crash of Ray-tracing builder runs on an RTX 5070 (32-lane) - then the title crashes on f16 box bounds #16. Until that is fixed, a store this PR's shortcut drops for a frame cannot be told from the builder's own trouble.
  • No test runs a store through title memory by address on a GPU, here or before this PR, so the suites do not exercise the mark itself. The evidence for it is the run above (38 copies marked, the frame time). A test would be a good follow-up; I have not written one.
  • Under quarantine the builder's own dispatches declare no writes at all, so the log line about undeclared stores fires on most frames there. That is expected in that mode and says nothing about a normal run.
  • One card, one scene.
  • With VICTORY_RELAY / VICTORY_LATE_WAIT the marks are read while earlier submissions may still run. A mark set after the read is found the next frame, and the 8-frame window is there for that; I did not run those modes.

Relation to other PRs

Not in this PR: the limit of 1,023 copies (the log says no number left for another copy; its pages read zeros in the prologue), the washed-out picture with the builder on, and #16.

AI-assisted (Claude Code). No game data: counts, timings and log lines only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TydV9AGmPCiUTkG8rHubBQ

…py, and only marked copies are taken for written

Each list that could store through title memory by address declared every
numbered copy written: a snapshot before the submission and a compare after
it, of all 1,023 copies (4.3 GB) in the prologue with the structure builder
on. A frame took 3.3 s on an RTX 5080, 2.8 s of it there.

Stores and atomics now set a mark per copy after the fault notes. The host
reads the marks once a frame, and a list declares written the copies marked
in the last 8 frames, the copies its dispatches name, and the copies
numbered while it is recorded. Same scene: 0.55 s a frame.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TydV9AGmPCiUTkG8rHubBQ
@ghost

ghost commented Nov 25, 2026

Copy link
Copy Markdown
Author

AI-assisted (Claude Code).

A clarification of one line in the description, from a measurement made after opening this: with the builder off (defaults) the memory-by-address path numbers no copies at all on this card (0 numbered, 0 loads, 0 stores over a 300 s run). So "nothing moves in a normal run" holds because nothing uses the path then, which is a simpler reason than the frame times I gave. The change only matters once something numbers copies, today the structure builder.

The counts behind this, and what they say about the 1,023-copy limit, are in #284.

A hand-assembled compute program stores one dword per thread at an address
it builds in a register pair, with a third of its threads masked out. On
the hardware adapter and on WARP: each running thread's dword lands in the
copy the directory names for the page, that copy's mark is set and no
other, and the masked threads store and mark nothing. With no copy for the
page, the stores are dropped at the base slot, the page is noted among the
faults and no copy is marked.

The test host gets a root UAV over one of the heap's writable buffers
(`Root::Written`), for the fault notes and marks.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TydV9AGmPCiUTkG8rHubBQ
@ghost

ghost commented Nov 25, 2026

Copy link
Copy Markdown
Author

AI-assisted (Claude Code).

Pushed ac36155: the test the description listed as missing. emit/memory_tests.rs runs a small compute program on the card (hardware adapter and WARP) in which 24 of 32 threads store through an address held in a register pair. It checks that each value lands in the copy the directory names for the page, that this copy's mark is set and no other, that the masked threads store and mark nothing, and that with no copy for the page the stores are dropped, the page is noted as a fault and no copy is marked.

Mutation check: with the mark removed from Lower::memory the test fails on "copy 1 is marked".

The test host gains Root::Written(n), a root UAV over the n-th writable heap buffer, so the fault notes and marks can be bound at their root slot and read back.

cargo test --release -p victory-shader -p victory-gpu: 110 + 211 passed, 0 failed. Clippy clean with --tests.

@Xeltax

Xeltax commented Nov 25, 2026

Copy link
Copy Markdown

AI-assisted (Claude Code). Numbers and log lines only; no game data.

Tested on an RTX 4080: with the builder on, 4.5 s a frame becomes 0.85–0.9 s (1.1–1.2 fps).

Setup

per frame, builder on before #346 with #346
frame 4,523 ms 849–916 ms
on the GPU 286 ms 264–301 ms
preparing 1,678 ms 237–256 ms
copying back 2,518 ms 327–354 ms
windows snapshotted and compared 16,225 MB (4,058 ranges) 515 MB (130 ranges)
page faults 4.17 million 102,000

What the marks do here. A per-frame tally added locally in ready gives the copies declared written in a frame:

  • 116–121 because they were marked lately;
  • 39–43 because a dispatch named them; 11–14 of those were never marked;
  • about 930 MB declared in all.

All 1,023 copies (21.5 GB) are still refreshed, by 5 lists a frame.

What remains.

  • Putting back what shaders wrote costs 248 ms a frame for 341 pieces, 0.73 ms a piece, against 0.23 ms with the builder off.
  • That is the per-run kernel writes in copy_back_with (measured below).
  • The 174 MB heap is still snapshotted and compared whole twice a frame (365 MB), with 2 MB written back.

Pitfall for anyone testing this PR. If you copy a target folder from another worktree to save build time, victory-shader's build script does not rerun and the shader cache key does not change. The run then uses the old translations without marks. Two tells:

  • an offline warm-up (translate --warm) right after the merge says "0 translated now";
  • no copy is ever marked. In that invalid run, 230 MB was compared and the builder's stores were simply not copied back, which also looked fast.

A cargo clean -p victory-shader fixes it. It cost me one invalid run.

Follow-ups on top of this PR. All are opt-in, on VIcky's rt branch (https://github.com/Xeltax/VIcky/tree/rt, VICKY-RT.md). Same card, single runs, same shot:

per frame this PR + the switches below
frame 849–916 ms 502–583 ms (1.7–2.0 fps)
copying back 327–354 ms 58–65 ms
preparing 237–256 ms 149–193 ms
on the GPU 264–301 ms 282–306 ms
  • VICTORY_BACK_JOIN=256: the biggest part.
    • copy_back_with writes each run of GPU bytes with its own NtWriteVirtualMemory. With the builder on, a frame put back about 350 pieces holding 780,000–910,000 runs, about 2,400 a piece.
    • Writing runs no more than 256 bytes apart as one call (gaps included, title bytes read just before) makes that 1,300 calls and 1.8 ms of writing.
    • The shortcut: a title write landing in a gap between the read and the write is lost. That is narrower than #2009's case, and the relay's merge already writes whole stretches.
  • Touch marks and VICTORY_MEMORY_REFRESH=touched.
    • The fault note's store of an access through a copy goes to that copy's own dword instead of the sink. That costs no extra store, and it tells which copies shaders read.
    • A list's first dispatch then refreshes only the copies touched lately (plus the named and new ones, those about to be declared written, and every copy once in 8 frames): 6.4–7.8 GB refreshed a frame instead of 21.5 GB.
  • VICTORY_MEMORY_NAMED_WRITES=0: a copy only named by a constant and never marked stops being snapshotted after 3 frames.

If any of these would be useful here or in a separate PR, say so and I'll open it against main.

triippywhale pushed a commit to triippywhale/victory-fork that referenced this pull request Nov 25, 2026
…marked copies are taken for written; 8,191 copies

Port of fxceless PR fxceless#346 (c3db707, Gargalindis) onto the fork's directory:
stores and atomics through title memory by address set a per-copy mark
after the fault notes (MEMORY_MARKS); the host reads the marks once a
frame, and a list that can store declares written only the copies
marked in the last 8 frames, the copies its dispatches name and the
copies numbered while it is recorded. Before, every list declared every
numbered copy written: with the structure builder on, 3.9 GB of
snapshots and compares a frame (run 3 at 266cd04, 584 ms/frame).
fxceless measured 3.3 s -> 0.55 s a frame. VICTORY_MEMORY_MARKS=0 takes
every copy for written, as before.

The copy table grows from 4,095 to 8,191 (as fxceless fxceless#980): run 3
filled 4,095 a minute in, after which pages without a number read
zeros (missing walls). The marks are clamped to the table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017V7WBCi8h4eymnRhdth77t
Repository owner closed this by deleting the head repository Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Builder on: the 3 s frames are CPU work in memory::ready (1,023 copies, 4.3 GB refreshed and declared written per list); taking the dirty-page mask

2 participants