d3d12-gpu: mapped_bytes counts readable pages between unmapped ends #3046
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3046"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T05:04:55Z.
Claimed in the coordination issue as: D3D12-GPU-MAPPED-BYTES-INTERIOR-PAGES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7283293483)
What and why
mapped_bytes(at, len)ind3d12/crates/leonida-gpu/src/texture.rsreturned 0 as soon as neither the first nor the last page of the range was committed, without looking at the pages between. A streaming texture's first resident level can lie in the middle of its memory, so such a texture counted as not there and was not uploaded until one of its ends was mapped too.The change: the early return is gone; with the first page not committed the host's regions are walked (one
VirtualQuerya region, a few for a texture that is away). A page that is committed butPAGE_GUARDorPAGE_NOACCESSis not counted, since it can't be read.This ports the
texture.rspart of TaynanGT's fix from the sibling VIctory tree (TaynanGT/Vice-Tayaf7a9ed, GPL-3.0), credited as co-author.Tested
texture::title_memory_tests::mapped_bytes_counts_readable_pages_between_unmapped_ends: reserves 8 pages, commits pages 2-3 read/write and page 5 no-access, then asks for 7 pages from an unaligned start. Withmain's code (8422def75) it fails (left: 0, right: 8192); with the change it gets the two readable pages, and 0 for the unmapped head.cargo test -p leonida-gpu --lib: 669 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.GPUs tested: n/a (CPU unit tests)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T07:18:02Z.
Independent verification on Windows 10 Pro 22H2 (build 19045), NVIDIA GeForce RTX 5070 12 GB (driver
621.40), MSVC toolchain (1.98.1). Head0f19a0819merged cleanly ontomain(f37c4a87d).python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/texture.rs: PASS.cargo test -p leonida-gpu --locked --lib mapped_bytes_counts_readable_pages_between_unmapped_ends: 1 passed, 0 failed.cargo test -p leonida-gpu --locked --lib -- --skip depth_bounds_pipeline_loads_and_filters_draws: 668 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --locked --lib --tests: clean (0 new warnings).mapped_bytes(texture.rs:2876-2936) preserves the fast 64 KiB block walk whencommitted(at, 1)holds, falls through to theVirtualQueryregion walk when the first page is uncommitted so interior resident levels are counted, and excludesPAGE_GUARD | PAGE_NOACCESS.Verdict: PASS (unit tests & source review; title not run).
AI-assisted: yes (Antigravity).
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T07:56:30Z.
VERIFY-3046 — Linux, Fedora 45, Rust 1.98.1, cross-compiled for x86_64-pc-windows-gnu, Wine 11.21, no GPU device, no game files
Verdict: PASS. The change removes a shortcut that returned the wrong answer for a streaming texture whose first resident level lies in the middle of its memory, and it also stops counting committed-but-unreadable pages as resident. Both are fixed in
mapped_bytesitself, not worked around. The new test fails on the merge base with exactly that mismatch (0against8192) and passes with the pull request. No regression: 728 tests on each side, 0 regressions; the only failures are thewatchtests that fail on the base too (one of them flaked once in an earlier run — see section 4, where I give the per-side rates).head
0f19a08194f3322c10a2310ce2d3e04b5db1ac3d("d3d12-gpu: mapped_bytes counts readable pages between unmapped ends"), baseorigin/main=0ae5a2664b9cd2fd30abfb1f6096f1c07b030f39, merge base8422def759cda12c89f918438eeb8d53dd5972a6(4 commits behindmain). One file, +35/−12:d3d12/crates/leonida-gpu/src/texture.rs.1. What it changes
texture::mapped_bytes(at: *const u8, len: usize) -> u64answers "how many bytes of[at, at + len)are committed, readable memory". Its callers (texture.rs:1550,:1592,:2491) use it as "how much of this title texture has arrived" — the number is compared against what was read back to decide whether the texture is fully resident, and it is stored asstaging.expected.The old body had a shortcut and an over-count:
The new body:
Two defects, both fixed at the source:
VirtualQueryper region, a few for a texture that is away — which is what the new doc comment says.MEM_COMMITwithPAGE_NOACCESSorPAGE_GUARDis not readable memory; the walk counted it. The new condition excludes both. (ThePAGE_NOACCESShalf matters for the title's own memory: a region can be committed and made inaccessible on purpose.)2. The new test checks the property that matters
texture::title_memory_tests::mapped_bytes_counts_readable_pages_between_unmapped_endsbuilds the exact situation the fix is about, with plain Windows memory calls and no GPU device and no game files:VirtualAlloc(8 * PAGE, MEM_RESERVE, PAGE_NOACCESS)— the reservation of a texture's memory whose first and last pages are not committed.VirtualAlloc(base + 2 * PAGE, 2 * PAGE, MEM_COMMIT, PAGE_READWRITE)— two readable pages in the middle: the first resident level.VirtualAlloc(base + 5 * PAGE, PAGE, MEM_COMMIT, PAGE_NOACCESS)— one page committed but not readable.mapped_bytes(base + 19, 7 * PAGE)must be exactly2 * PAGE(8192): the two readable pages in the middle are counted, the reserved pages at the ends are not, and the committed-but-PAGE_NOACCESSpage is not counted as readable.mapped_bytes(base, 2 * PAGE)must be 0: a range that is only reserved reads as nothing.That is the property the callers depend on — "how much of this texture is really there and readable" — and not an implementation detail of the walk.
The test body really runs here; it is not a "no device ⇒ ok" no-op. I built the red side alone and ran the new test by name under Wine (
lane/red_3046.log): the binary reported1 failed; 727 filtered outwithi.e. the old shortcut answered 0 where 8192 is the truth, exactly as analysed. The second assertion (the fully reserved range reads as 0) is never reached on the red side — the first one fails first — and passes on the green side, so the test is discriminating the middle-pages property and not merely tripping over the fixture.
3. What the tests do here
I cross-compiled the crate for
x86_64-pc-windows-gnuand ran the test binaries under Wine withpython3 tools/wine_red_green.py 3046 --crate leonida-gpu(run from/home/shangli/oc-workspace/gta6); the red side is built the wayscripts/red_green.pybuilds it (merge base + only the PR's test hunks), the green side from the PR head, and every test binary of the crate is run under Wine on both sides.8422def7+ the PR's test hunks0f19a081(PR head)The tool's verdict: "red then green" — red→green 2, regressions 0, 3 failing on both sides, 665 passing on both sides, no test on one side only. The second red→green entry is
watch::tests::a_write_opens_the_view_it_goes_through, awatchtest that has nothing to do withtexture.rs; section 4 shows it fails on the base too.I also ran the crate's whole suite several times on each side to tell the
watchfailures from a real regression (numbers in section 4).4. Any failure I saw and why it is not this PR
The three
watchtests that fail on both sides —watch::many_askers_tests::holes_are_known_until_the_runtime_tells,watch::many_askers_tests::no_mapping_is_lost_between_askers,watch::tests::what_is_left_alone_is_no_known_hole. They are inwatch.rs, which this pull request does not touch, and they fail identically with the pull request reverted. Per-side rates I measured (full suite, six runs per side,lane/flake_3046_full.json):0f19a0818422def7watch::many_askers_tests::holes_are_known_until_the_runtime_tellswatch::many_askers_tests::no_mapping_is_lost_between_askerswatch::tests::what_is_left_alone_is_no_known_holewatch::tests::a_block_just_written_restswatch::tests::what_is_put_in_beside_the_guards_is_told_at_every_viewA flaky
watchtest that made an earlier run of the tool report a false regression. The first run oftools/wine_red_green.py 3046reported the verdict "NOT red-then-green" with one regression,watch::tests::kernel_reads_into_guarded_blocks_after_opening, and one flaky failure,watch::tests::a_range_that_begins_in_an_unmapped_block_is_guarded. The second run of the same command reported 0 regressions. The difference is the flakiness of thewatchguard-page tests under Wine, not the change:watch::tests::, eight runs per side: onlywhat_is_left_alone_is_no_known_holefails, 8/8 on both sides;a_write_opens_the_view_it_goes_throughandkernel_reads_into_guarded_blocks_after_openingpass 8/8 on both sides.watch(themany_askerstests included), six runs per side: the three always-failing tests fail 6/6 on both sides, and one different test failed once on each side (a_refresh_leaves_open_what_the_kernel_is_writingon green,other_memory_at_a_guarded_address_keeps_its_protectionon red).watch::tests::a_write_opens_the_view_it_goes_throughis the clearest case: it failed on the red (base) side in both runs of the tool for this pull request, and it failed on the green side in the tool's run for pull request #3005, whose red side is a different commit entirely. A test that fails on the base in one run and on the head in another, with the same code, is flaky, not a regression.The root of the flakiness is the
watchmodule's guard pages and the exceptions they raise, which behave differently under Wine without a real GPU; nothing in this pull request touches that module.One thing I noticed while reading, not a failure: the change makes the region walk run in the "the texture is away" case (the first page is not committed) where the old shortcut answered 0 after two cheap questions. The walk costs one
VirtualQueryper region and the range of an absent texture is usually one or two regions, so the cost is small — but it is a change in the hot path of a per-texture, per-frame query, and I could not measure it (no GPU, no title).5. What I did not check
mapped_bytesexists to say how much of a streaming title texture has arrived. I could not check that a real streamed texture with its first resident level in the middle is now measured correctly, nor that the upload path reacts to the new number; the test only exercises the function over host memory it allocates itself.LEONIDA_COMMITTED_QUERY=1path (by_query()): with that switch the code now always takes the region walk, where the old code still applied the shortcut. I did not run the suite with that switch set, so the behaviour under it is only checked by reading.LEONIDA_MAPPED_BY_PAGE=1path: likewise only read, not run.crate::inplace::shown_bytesfast path at the top ofmapped_bytes: it short-circuits before any of this and is unaffected by the change; I did not exercise it.cargo test --target x86_64-pc-windows-gnuunder Wine 11.21 on Fedora 45.VirtualQuery'sProtectvalue for aPAGE_GUARDregion and Wine's fidelity forK32QueryWorkingSetExare the parts most likely to differ from a real Windows run.leonida-gpuwas, because the change is confined to it.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:16:10Z.
RUN (in game, grouped): this head
0f19a0819was merged withgit merge --no-ff, together with nine other rendering pull requests, ontomainf6dd2242e(no conflicts), and the d3d12 layer was rebuilt from that tree (diag profile, with DLSS). RTX 4060 8 GB, driver 610.88, Windows 11;scripts/play-d3d12.ps1 -SeriesS -Resolution 1080 -FrameGen 2, low-memory profile, keyboard-driven entry into the world and a walking route.Heads in this run: #2854
81a6e94a9, #293668215ad0b, #2939561bbc19c, #2997ef429f923, #3043915d4d5aa, #3045cb30a595a, #30460f19a0819, #3047c33e5b48e, #30482ab0b4b9d, #3051856615ce7.AI-assisted (Claude Code, Claude Opus 5.5)
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:21:23Z.
Merging. Head
0f19a0819merges cleanly withmainbf7886746and does not force a rebase of #2566. Checked on head0f19a0819: all 6 CI checks green; third-party red/green by imShangLi on this head; an in-game run of at least 381 s with this head merged onto mainf6dd2242e(grouped with nine other rendering pull requests, comment above) showed no device loss, no unhandled exception and no hang; item claimed in the coordination issue; author has earlier merged pull requests; conflict-free merge against main. Merged by MoonEyes.AI-assisted (Claude Code).