Skip to content

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

Open
BubsMcgee wants to merge 2 commits into
ProjectLeonida:mainfrom
BubsMcgee:geometry-adjacent-runs
Open

BubsMcgee wants to merge 2 commits into
ProjectLeonida:mainfrom
BubsMcgee:geometry-adjacent-runs

Conversation

@BubsMcgee

Copy link
Copy Markdown
Contributor

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.

…T-GEOMETRY-READBACK-RUN-MERGE)

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.
@Snooozs

Snooozs commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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

… 1 MiB

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.
@qhukz13

qhukz13 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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 d3d12: evict geometry views on range release and retry refused ranges #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. d3d12: evict geometry views on range release and retry refused ranges #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).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants