leonida-gpu: a list fills the same constant buffer bytes once (D3D12-CONSTANTS-ONCE) #2850

Closed
TheGeogeo wants to merge 1 commit from upstream-pr/2850 into main
TheGeogeo commented 2026-12-01 08:48:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-12-01T08:48:20Z.


Claimed in the coordination issue as: D3D12-CONSTANTS-ONCE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305437906)

What and why

A table that holds a constant buffer view is made afresh for every draw (draw.rs). Each such view got a 256-byte scratch piece and a fill step of its own, copied at submission on the submitting thread. A diagnostics count added here shows that most of those fills copy bytes the same list has already filled.

  • Measured on an RTX 3080, Series S settings, standing in the world: 11,578 fills a frame (5.8 MB).
  • Only 3,327 of them (28.8 %, 1.6 MB) were the first of their bytes in their list.

With LEONIDA_CONSTANTS_ONCE (on by default; 0 gives one fill each, as before; LEONIDA_AB can switch it), the same (source, bytes) asked again before the list next writes title memory gets the first fill's memory. Its slot joins that fill.

  • This is byte-exact. Fills run at submission in step order, and every step that writes title memory already clears the list's fill keys (Deferred::title_written, as for fill_scratch), so a second fill would have copied the same bytes.
  • HostList::constants replaces defer_constants at the one call site.
  • card_steps: FillTo::Slot becomes FillTo::Slots. With LEONIDA_CARD_STEPS=2 (on in the NVIDIA launcher), a fill the card takes re-points every slot that shows it, one op a slot at the same place, so the bytes are read once. Without this, the other slots would read scratch nothing filled.
  • Diagnostics: constants_line in the frame meter's lines prints fills a frame and how many were the first of their bytes in their list.
  • Docs: a findings.md section; switches.md regenerated.

Measured

RTX 3080 10 GB, driver 32.0.16.2140, Windows 11, Series S settings, standing. LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE in one 600 s run, read with tools/d3d12_ab_compare.py phases. Switch on minus off, 13 pairs of phases:

per frame
lists' steps on the submitting thread -0.99 ms (+-0.07)
its copies and fills -0.85 ms (+-0.02)
steps done on the card -0.16 ms
frame median -0.39 ms (+-0.14)

The tool's own verdict on the frame is "no clear difference". Per arm (14 phases each, after the first minutes):

  • off: 36.40 ms median frame, card busy 30.49 ms, card idle 7.21 ms, submitting thread 27.75 ms;
  • on: 36.05 ms, 30.48 ms, 6.44 ms, 26.55 ms.

So about 1.2 ms a frame comes off the submitting thread, the card waits about 0.8 ms less behind it, and its own work is unchanged.

Tested

On Windows 11, from d3d12/ with the pinned toolchain (tested on main 1d73465e8; main has no change under d3d12/crates since):

  • cargo test -p leonida-gpu --locked: 601 passed, 0 failed, 55 ignored.

  • cargo test -p leonida-gpu --locked --features diagnostics: 681 passed, 0 failed, 55 ignored (+1 doc test).

  • New tests:

    • host::batch_scratch_tests::constant_buffers_with_the_same_bytes_share_one_fill_until_title_memory_is_written (a real host list): two asks for the same bytes share one fill and its slots; other bytes, and the same bytes after a title-memory write, get fills of their own.
    • host::card_steps::tests::a_fill_shown_by_several_slots_points_every_one_at_its_bytes.

    On main they do not build (HostList::constants and FillTo::Slots are new).

  • cargo clippy -p leonida-gpu --all-targets --locked --features diagnostics: no warning in the changed code.

  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md, python -I tests/test_docs_repo_paths.py and python -I scripts/check_no_game_data.py on the changed files: OK.

  • In game: the A/B run above passed the game gate (605 s in the world).

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11.

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md (no translator change here).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5582230753 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2850). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-12-01T08:48:20Z. --- Claimed in the coordination issue as: `D3D12-CONSTANTS-ONCE` (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7305437906) ### What and why A table that holds a constant buffer view is made afresh for every draw (`draw.rs`). Each such view got a 256-byte scratch piece and a fill step of its own, copied at submission on the submitting thread. A diagnostics count added here shows that most of those fills copy bytes the same list has already filled. - Measured on an RTX 3080, Series S settings, standing in the world: 11,578 fills a frame (5.8 MB). - Only 3,327 of them (28.8 %, 1.6 MB) were the first of their bytes in their list. With `LEONIDA_CONSTANTS_ONCE` (on by default; `0` gives one fill each, as before; `LEONIDA_AB` can switch it), the same (source, bytes) asked again before the list next writes title memory gets the first fill's memory. Its slot joins that fill. - This is byte-exact. Fills run at submission in step order, and every step that writes title memory already clears the list's fill keys (`Deferred::title_written`, as for `fill_scratch`), so a second fill would have copied the same bytes. - `HostList::constants` replaces `defer_constants` at the one call site. - `card_steps`: `FillTo::Slot` becomes `FillTo::Slots`. With `LEONIDA_CARD_STEPS=2` (on in the NVIDIA launcher), a fill the card takes re-points every slot that shows it, one op a slot at the same place, so the bytes are read once. Without this, the other slots would read scratch nothing filled. - Diagnostics: `constants_line` in the frame meter's lines prints fills a frame and how many were the first of their bytes in their list. - Docs: a `findings.md` section; `switches.md` regenerated. ### Measured RTX 3080 10 GB, driver 32.0.16.2140, Windows 11, Series S settings, standing. `LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE` in one 600 s run, read with `tools/d3d12_ab_compare.py phases`. Switch on minus off, 13 pairs of phases: | | per frame | | --- | --- | | lists' steps on the submitting thread | -0.99 ms (+-0.07) | | its copies and fills | -0.85 ms (+-0.02) | | steps done on the card | -0.16 ms | | frame median | -0.39 ms (+-0.14) | The tool's own verdict on the frame is "no clear difference". Per arm (14 phases each, after the first minutes): - off: 36.40 ms median frame, card busy 30.49 ms, card idle 7.21 ms, submitting thread 27.75 ms; - on: 36.05 ms, 30.48 ms, 6.44 ms, 26.55 ms. So about 1.2 ms a frame comes off the submitting thread, the card waits about 0.8 ms less behind it, and its own work is unchanged. ### Tested On Windows 11, from `d3d12/` with the pinned toolchain (tested on main 1d73465e8; main has no change under `d3d12/crates` since): - `cargo test -p leonida-gpu --locked`: 601 passed, 0 failed, 55 ignored. - `cargo test -p leonida-gpu --locked --features diagnostics`: 681 passed, 0 failed, 55 ignored (+1 doc test). - New tests: - `host::batch_scratch_tests::constant_buffers_with_the_same_bytes_share_one_fill_until_title_memory_is_written` (a real host list): two asks for the same bytes share one fill and its slots; other bytes, and the same bytes after a title-memory write, get fills of their own. - `host::card_steps::tests::a_fill_shown_by_several_slots_points_every_one_at_its_bytes`. On `main` they do not build (`HostList::constants` and `FillTo::Slots` are new). - `cargo clippy -p leonida-gpu --all-targets --locked --features diagnostics`: no warning in the changed code. - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md`, `python -I tests/test_docs_repo_paths.py` and `python -I scripts/check_no_game_data.py` on the changed files: OK. - In game: the A/B run above passed the game gate (605 s in the world). GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11. AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md` (no translator change here).
JavierBlanco23 commented 2026-12-01 13:00:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by JavierBlanco23 on 2026-12-01T13:00:54Z.


Independent source-only review (no build or GPU run yet).

Invariants verified:

  • Invalidation: Deferred::constants is keyed by (src_ptr, len) and cleared inside the same Deferred::title_written() path that clears filled/refreshed/gpu_written (host.rs:9338, :9891, :10062, place.rs:406), so list steps writing title memory invalidate cached entries. On a cache hit, if the referenced step was already consumed or replaced, it falls back safely to a fresh Step::Fill.
  • LEONIDA_CARD_STEPS 0/1: FillTo::Slots is only read by card stage 2 (card_steps.rs:597-605, :1064). In CPU fallback, host.rs:10000 ignores the to field and fills the shared scratch buffer that draw.rs:5911 already bound to each slot via host.constant_at, so stages 0 and 1 stay intact. Hazard checks (host.rs:4451) are likewise unaffected.
  • Scratch lifetime & switch-off path: list.constants() preserves the scratch_held None -> host.make_null fallback and list-scoped buffer lifetime. With LEONIDA_CONSTANTS_ONCE=0, it emits a fresh scratch slice and single-slot FillTo::Slots per call, matching the prior defer_constants behaviour.

No correctness issues found. One minor note: the ~28% miss/insertion path takes the per-list deferred() mutex twice (likely intentional to avoid holding it across scratch_held), which looks uncontended per list and is already reflected in your RTX 3080 submit-thread win (-0.99 ms).

If a second GPU data point would help before merge, I can follow up with a LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE run on an RTX 5080 + i9-13900KF where GPU headroom is wider.

AI-assisted: OpenCode (hy4-flash-free) with Gemini review. Independent review, not the author; no game data accessed. Disclosure per CONTRIBUTING.md:77-88.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311977470 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2850#issuecomment-7311977470). Originally posted by [JavierBlanco23](https://github.com/JavierBlanco23) on 2026-12-01T13:00:54Z. --- Independent source-only review (no build or GPU run yet). Invariants verified: - **Invalidation:** `Deferred::constants` is keyed by `(src_ptr, len)` and cleared inside the same `Deferred::title_written()` path that clears `filled`/`refreshed`/`gpu_written` (`host.rs:9338`, `:9891`, `:10062`, `place.rs:406`), so list steps writing title memory invalidate cached entries. On a cache hit, if the referenced step was already consumed or replaced, it falls back safely to a fresh `Step::Fill`. - **`LEONIDA_CARD_STEPS` 0/1:** `FillTo::Slots` is only read by card stage 2 (`card_steps.rs:597-605`, `:1064`). In CPU fallback, `host.rs:10000` ignores the `to` field and fills the shared scratch buffer that `draw.rs:5911` already bound to each slot via `host.constant_at`, so stages 0 and 1 stay intact. Hazard checks (`host.rs:4451`) are likewise unaffected. - **Scratch lifetime & switch-off path:** `list.constants()` preserves the `scratch_held` `None -> host.make_null` fallback and list-scoped buffer lifetime. With `LEONIDA_CONSTANTS_ONCE=0`, it emits a fresh scratch slice and single-slot `FillTo::Slots` per call, matching the prior `defer_constants` behaviour. No correctness issues found. One minor note: the ~28% miss/insertion path takes the per-list `deferred()` mutex twice (likely intentional to avoid holding it across `scratch_held`), which looks uncontended per list and is already reflected in your RTX 3080 submit-thread win (`-0.99 ms`). If a second GPU data point would help before merge, I can follow up with a `LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE` run on an RTX 5080 + i9-13900KF where GPU headroom is wider. AI-assisted: OpenCode (`hy4-flash-free`) with Gemini review. Independent review, not the author; no game data accessed. Disclosure per `CONTRIBUTING.md:77-88`.
merzleckeier commented 2026-12-01 13:07:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T13:07:29Z.


Checked constant-buffer sharing and its off switch on two GPUs with ecee350f7 + ed867d7b2. The only merge conflict was the count in generated d3d12/docs/switches.md. Regenerating the page added just the new switch row and updated the count. The PR test passed with LEONIDA_CONSTANTS_ONCE=1 on RADV and RTX 4070 SUPER. Forcing 0 made the same test fail at the expected second-ask buffer-sharing assertion on both GPUs. With sharing enabled, the fixture also verified distinct lengths and a new fill after a title-write marker. The card-step suite passed 11 tests per GPU with one ignored per run, including the offscreen copy and readback tests. This run covers synthetic buffers and descriptor slots.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7312139896 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2850#issuecomment-7312139896). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-01T13:07:29Z. --- Checked constant-buffer sharing and its off switch on two GPUs with `ecee350f7` + `ed867d7b2`. The only merge conflict was the count in generated `d3d12/docs/switches.md`. Regenerating the page added just the new switch row and updated the count. The PR test passed with `LEONIDA_CONSTANTS_ONCE=1` on RADV and RTX 4070 SUPER. Forcing `0` made the same test fail at the expected second-ask buffer-sharing assertion on both GPUs. With sharing enabled, the fixture also verified distinct lengths and a new fill after a title-write marker. The card-step suite passed 11 tests per GPU with one ignored per run, including the offscreen copy and readback tests. This run covers synthetic buffers and descriptor slots. AI-assisted with OpenAI Codex.
lixonik commented 2026-12-01 13:14:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-01T13:14:01Z.


RUN

Verdict: better on submission constant-buffer fill cost (copies_fills_ms -0.659 +- 0.053 ms, steps_ms -0.475 +- 0.159 ms, card_steps_ms -0.165 +- 0.012 ms) and same on whole-frame median (-0.175 +- 0.411 ms, no clear difference, 4 valid pairs / 5 dropped for draw jumps), with 0 errors and 0 device-lost events.

Setup:

  • PR #2850 head ecee350f7 (LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE, Series S, -NoFrameGen)
  • GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver 621.40, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T)

Unit test:

  • cargo test --locked -p leonida-gpu --lib constants: 2 passed, 0 failed.

In-run A/B (tools/d3d12_ab_compare.py phases):

  • Overall run: 9.1 s precompile, 76 frame lines (53 world samples >= 1000 draws, median 24.3 fps / 41.1 ms, gpu_wait 0.0 ms, 4,426 median / 4,825 max draws, 0 errors, 0 device lost).
  • Paired deltas (4 pairs):
    • median: -0.175 +- 0.411 ms (no clear difference)
    • copies_fills_ms: -0.659 +- 0.053 ms
    • steps_ms: -0.475 +- 0.159 ms
    • card_steps_ms: -0.165 +- 0.012 ms

Merge note: d3d12/docs/switches.md currently conflicts with origin/main.

AI-assisted: Antigravity

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7312303608 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2850#issuecomment-7312303608). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-01T13:14:01Z. --- RUN Verdict: `better` on submission constant-buffer fill cost (`copies_fills_ms -0.659 +- 0.053 ms`, `steps_ms -0.475 +- 0.159 ms`, `card_steps_ms -0.165 +- 0.012 ms`) and `same` on whole-frame median (`-0.175 +- 0.411 ms`, `no clear difference`, 4 valid pairs / 5 dropped for draw jumps), with 0 errors and 0 device-lost events. Setup: - PR `#2850` head `ecee350f7` (`LEONIDA_AB=20,LEONIDA_CONSTANTS_ONCE`, `Series S`, `-NoFrameGen`) - GPUs tested: NVIDIA GeForce RTX 5070 12 GB (driver `621.40`, Windows 10 Pro 22H2 build 19045, Ryzen 9 5950X 16C/32T) Unit test: - `cargo test --locked -p leonida-gpu --lib constants`: `2 passed, 0 failed`. In-run A/B (`tools/d3d12_ab_compare.py phases`): - Overall run: `9.1 s` precompile, `76` frame lines (`53` world samples `>= 1000` draws, median `24.3 fps` / `41.1 ms`, `gpu_wait 0.0 ms`, `4,426` median / `4,825` max draws, `0` errors, `0` device lost). - Paired deltas (`4` pairs): - `median`: `-0.175 +- 0.411 ms` (`no clear difference`) - `copies_fills_ms`: `-0.659 +- 0.053 ms` - `steps_ms`: `-0.475 +- 0.159 ms` - `card_steps_ms`: `-0.165 +- 0.012 ms` Merge note: `d3d12/docs/switches.md` currently conflicts with `origin/main`. AI-assisted: Antigravity
MoonEyes commented 2026-12-01 14:24:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T14:24:38Z.


The code of this pull request is in main since 357c71023 ("gpu: reuse constant-buffer fills, From TheGeogeo #2850", by ubiquitarianism, 14:11 today): draw.rs, host.rs and card_steps.rs, apart from the spelling of a few lines. So I am closing it. What main does not have: the 14 lines of d3d12/docs/findings.md with your measurements, and the LEONIDA_CONSTANTS_ONCE row of d3d12/docs/switches.md. A small docs pull request with those two would be welcome. Thank you @TheGeogeo, and @merzleckeier and @lixonik for the checks.

Commit: github.com/ProjectLeonida/mandy@357c71023

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7314204494 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2850#issuecomment-7314204494). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T14:24:38Z. --- The code of this pull request is in `main` since 357c71023 ("gpu: reuse constant-buffer fills, From TheGeogeo #2850", by ubiquitarianism, 14:11 today): `draw.rs`, `host.rs` and `card_steps.rs`, apart from the spelling of a few lines. So I am closing it. What `main` does not have: the 14 lines of `d3d12/docs/findings.md` with your measurements, and the `LEONIDA_CONSTANTS_ONCE` row of `d3d12/docs/switches.md`. A small docs pull request with those two would be welcome. Thank you @TheGeogeo, and @merzleckeier and @lixonik for the checks. Commit: https://github.com/ProjectLeonida/mandy/commit/357c71023 AI-assisted (Claude Code).

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
ProjectLeonida/mandy!2850
No description provided.