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

Open
github-bot wants to merge 2 commits from github-bot/gh-2085 into main AGit
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 19:50 UTC · original
BubsMcgee:geometry-adjacent-runs → main · +68 −3 · 2 files · 2 commits · open

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.

**@⁠BubsMcgee** on GitHub · 2026-11-25 19:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2085) `BubsMcgee:geometry-adjacent-runs` → `main` · +68 −3 · 2 files · 2 commits · open 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.
A GeometryViews refresh reads each written 64 KiB tile with its own
readability probe and guarded copy, even when the title wrote a run of
adjacent tiles across tile boundaries. Group the written tiles (already in
address order) into maximal runs whose addresses differ by one tile and read
each 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 falls back to
the per-tile read, so an uncommitted page still zeroes only what it did 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.

Tested: geometry_views_test 14/14, including the new case (two adjacent written
tiles are one 128 KiB copy); gta6rt_d3d12_com builds. Synthetic only, no game
files or GPU. AI-assisted.
Each refresh now clears written_ and written_vas_ together at the start, so an exception from a read or a guarded copy cannot leave addresses the next refresh indexes past written_. Adjacent-tile runs are capped at 1 MiB (16 tiles), so a long contiguous write cannot force one large scratch resize.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:17:17 +00:00
Author
Owner

@⁠Snooozs on GitHub · 2026-11-25 20:01 UTC · original

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).

**@⁠Snooozs** on GitHub · 2026-11-25 20:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2085#issuecomment-7173086564) 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).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-25 22:23 UTC · original

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).

**@⁠qhukz13** on GitHub · 2026-11-25 22:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2085#issuecomment-7176309868) 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).
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_geometry_views.h
  • runtime/test/geometry_views_test.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2085/head:github-bot/gh-2085'
git switch 'github-bot/gh-2085'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2085'
git switch 'github-bot/gh-2085'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2085'
git switch 'github-bot/gh-2085'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2085'
git switch 'main'
git merge --squash 'github-bot/gh-2085'
git switch 'main'
git merge --ff-only 'github-bot/gh-2085'
git switch 'main'
git merge 'github-bot/gh-2085'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.