d3d12: one guarded copy per run of adjacent written geometry tiles (RT-GEOMETRY-READBACK-RUN-MERGE) #2085

Merged
BubsMcgee merged 2 commits from upstream-pr/2085 into main 2026-11-30 16:25:16 +00:00
BubsMcgee commented 2026-11-25 19:50:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by BubsMcgee on 2026-11-25T19:50:40Z.


A GeometryViews refresh copies only the 64 KiB tiles the title wrote (the page diffing already there), but it reads each written tile with its own readability probe and guarded copy. When the title writes a run of tiles across tile boundaries, that is one probe and one copy per 64 KiB for data that is contiguous in title memory.

This groups the written tiles (already in address order) into maximal runs whose addresses differ by one tile, and reads a run a ready view needs as one guarded copy. The per-tile pass then reuses those bytes through read_tile(). A run that cannot be read whole (an uncommitted page inside it) falls back to the per-tile reads, so the zeroing behaviour is unchanged. Whole-view refreshes (LEONIDA_GEOMETRY_WHOLE_VIEWS=1) read nothing here, as before.

Same behaviour as DexNom/VIctory's copy_fetch_runs (crates/victory-gpu/src/host.rs, GPL-3.0-or-later, commit 570a941), re-implemented for this header; no identifiers or code copied. docs/related_projects.md already credits the VIctory readback work.

Files

  • runtime/shims/d3d12_geometry_views.h: read_run() / run_needed() and the run grouping in refresh().
  • runtime/test/geometry_views_test.cpp: case 5, two address-adjacent written tiles of one view are one 128 KiB copy (case 4's non-adjacent tiles stay two 64 KiB copies).

Tests

  • geometry_views_test: 14/14 checks pass (synthetic, fake Vulkan device, no game data or GPU).
  • gta6rt_d3d12_com builds (MSVC 19.44, Windows 11 26300; RTX 3080 Ti on 616.64 present but not needed by the test).

Claimed as RT-GEOMETRY-READBACK-RUN-MERGE in the coordination issue. Independent of #1678, which touches only d3d12_title_buffer_windows.h. No game data. AI-assisted (opencode). Depends on: none.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5498094828 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2085). Originally posted by [BubsMcgee](https://github.com/BubsMcgee) on 2026-11-25T19:50:40Z. --- A `GeometryViews` refresh copies only the 64 KiB tiles the title wrote (the page diffing already there), but it reads each written tile with its own readability probe and guarded copy. When the title writes a run of tiles across tile boundaries, that is one probe and one copy per 64 KiB for data that is contiguous in title memory. This groups the written tiles (already in address order) into maximal runs whose addresses differ by one tile, and reads a run a ready view needs as **one guarded copy**. The per-tile pass then reuses those bytes through `read_tile()`. A run that cannot be read whole (an uncommitted page inside it) falls back to the per-tile reads, so the zeroing behaviour is unchanged. Whole-view refreshes (`LEONIDA_GEOMETRY_WHOLE_VIEWS=1`) read nothing here, as before. Same behaviour as DexNom/VIctory's `copy_fetch_runs` (`crates/victory-gpu/src/host.rs`, GPL-3.0-or-later, commit `570a941`), re-implemented for this header; no identifiers or code copied. `docs/related_projects.md` already credits the VIctory readback work. ## Files - `runtime/shims/d3d12_geometry_views.h`: `read_run()` / `run_needed()` and the run grouping in `refresh()`. - `runtime/test/geometry_views_test.cpp`: case 5, two address-adjacent written tiles of one view are one 128 KiB copy (case 4's non-adjacent tiles stay two 64 KiB copies). ## Tests - `geometry_views_test`: 14/14 checks pass (synthetic, fake Vulkan device, no game data or GPU). - `gta6rt_d3d12_com` builds (MSVC 19.44, Windows 11 26300; RTX 3080 Ti on 616.64 present but not needed by the test). Claimed as `RT-GEOMETRY-READBACK-RUN-MERGE` in the coordination issue. Independent of #1678, which touches only `d3d12_title_buffer_windows.h`. No game data. AI-assisted (opencode). Depends on: none.
Snooozs commented 2026-11-25 20:01:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Snooozs on 2026-11-25T20:01:44Z.


From the code at this head (runtime/shims/d3d12_geometry_views.h), not run:

  • written_vas_ is cleared at the end of refresh() (line 354), written_ at its start. If anything in between throws (tile_scratch_.resize in read_run or read_tile, or the copy functor), the next refresh starts with an empty written_ and a filled written_vas_, so the loop over written_vas_.size() indexes written_[w] past its end, and the stale addresses are read again. Clearing both at the start closes it.
  • The run has no upper bound: N adjacent written tiles are one scratch resize of N x 64 KiB. A large contiguous title write makes that a single big allocation, which is the one that can throw. A cap (about 1 MiB, the size the stream-pool extents use) would keep it like the per-tile path.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7173086564 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2085#issuecomment-7173086564). Originally posted by [Snooozs](https://github.com/Snooozs) on 2026-11-25T20:01:44Z. --- From the code at this head (`runtime/shims/d3d12_geometry_views.h`), not run: - `written_vas_` is cleared at the end of `refresh()` (line 354), `written_` at its start. If anything in between throws (`tile_scratch_.resize` in `read_run` or `read_tile`, or the copy functor), the next refresh starts with an empty `written_` and a filled `written_vas_`, so the loop over `written_vas_.size()` indexes `written_[w]` past its end, and the stale addresses are read again. Clearing both at the start closes it. - The run has no upper bound: N adjacent written tiles are one scratch resize of N x 64 KiB. A large contiguous title write makes that a single big allocation, which is the one that can throw. A cap (about 1 MiB, the size the stream-pool extents use) would keep it like the per-tile path. AI-assisted (Claude Code, Claude Opus 5.5).
qhukz13 commented 2026-11-25 22:23:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-25T22:23:49Z.


Code review at 82169e0e0, plus a heads-up on an overlap with #2094 (RT-GEOMETRY-VIEW-EVICTION, mine), which rewrites the same part of refresh().

  • Snooozs's two points look handled at this head: written_ and written_vas_ are both cleared at the start of refresh(), and a run is capped at 1 MiB (kMaxRunBytes).
  • run_needed() reads written_[i]->views through the raw const Tile* pointers. In #2094 those pointers are gone on purpose: refresh() lets mutex_ go every 64 tiles, and with eviction retire_range() can erase a tile from tiles_ in that window, so a Tile* taken before the unlock can dangle. #2094 keeps only written_vas_ and looks each tile up again (tiles_.find(va), skipping a missing one and any !alive view). Whichever of the two lands second has a textual conflict here, and resolving it by keeping written_ would bring the dangling pointer back. Doing the same lookup in run_needed() now makes this PR independent of the order:
    const auto it = tiles_.find(written_vas_[i]);
    if (it == tiles_.end()) continue;
    for (const auto id : it->second.views) { ... }
    
    On main today nothing erases a tile during the walk, so this is not a bug at this head, only at the merge.

If #2094 lands first I am happy to send the rebase of read_run/run_needed onto it, or to review yours; geometry_views_test can run both together.

Measured: geometry_views_test at 82169e0e0 passes 16/16 checks, 3/3 runs, on a GTX 1050 Ti (Windows 10 19045, NVIDIA 582.66, MSVC RelWithDebInfo). git merge-tree of this head with #2094 (6fd430ebc) conflicts in runtime/shims/d3d12_geometry_views.h and runtime/test/geometry_views_test.cpp.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7176309868 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2085#issuecomment-7176309868). Originally posted by [qhukz13](https://github.com/qhukz13) on 2026-11-25T22:23:49Z. --- Code review at `82169e0e0`, plus a heads-up on an overlap with #2094 (RT-GEOMETRY-VIEW-EVICTION, mine), which rewrites the same part of `refresh()`. - Snooozs's two points look handled at this head: `written_` and `written_vas_` are both cleared at the start of `refresh()`, and a run is capped at 1 MiB (`kMaxRunBytes`). - **`run_needed()` reads `written_[i]->views` through the raw `const Tile*` pointers.** In #2094 those pointers are gone on purpose: `refresh()` lets `mutex_` go every 64 tiles, and with eviction `retire_range()` can erase a tile from `tiles_` in that window, so a `Tile*` taken before the unlock can dangle. #2094 keeps only `written_vas_` and looks each tile up again (`tiles_.find(va)`, skipping a missing one and any `!alive` view). Whichever of the two lands second has a textual conflict here, and resolving it by keeping `written_` would bring the dangling pointer back. Doing the same lookup in `run_needed()` now makes this PR independent of the order: ```cpp const auto it = tiles_.find(written_vas_[i]); if (it == tiles_.end()) continue; for (const auto id : it->second.views) { ... } ``` On main today nothing erases a tile during the walk, so this is not a bug at this head, only at the merge. If #2094 lands first I am happy to send the rebase of `read_run`/`run_needed` onto it, or to review yours; `geometry_views_test` can run both together. Measured: `geometry_views_test` at `82169e0e0` passes 16/16 checks, 3/3 runs, on a GTX 1050 Ti (Windows 10 19045, NVIDIA 582.66, MSVC RelWithDebInfo). `git merge-tree` of this head with #2094 (`6fd430ebc`) conflicts in `runtime/shims/d3d12_geometry_views.h` and `runtime/test/geometry_views_test.cpp`. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 12:42:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T12:42:28Z.


@BubsMcgee this PR now conflicts with main (runtime/shims/d3d12_geometry_views.h, runtime/test/geometry_views_test.cpp). Could you merge main into your branch (no force-push needed)? It was verified before the conflict: https://github.com/ProjectLeonida/mandy/pull/2085#issuecomment-7176309868. If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7256005699 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2085#issuecomment-7256005699). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T12:42:28Z. --- @BubsMcgee this PR now conflicts with `main` (`runtime/shims/d3d12_geometry_views.h`, `runtime/test/geometry_views_test.cpp`). Could you merge `main` into your branch (no force-push needed)? It was verified before the conflict: https://github.com/ProjectLeonida/mandy/pull/2085#issuecomment-7176309868. If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)
OreoFR commented 2026-11-30 13:40:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T13:40:28Z.


Carried as #2733 because of the conflict with main after #2566; @BubsMcgee, if you come back, say so and the carry is dropped.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7280188814 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2085#issuecomment-7280188814). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T13:40:28Z. --- Carried as #2733 because of the conflict with main after #2566; @BubsMcgee, if you come back, say so and the carry is dropped.
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!2085
No description provided.