D3D12.X: write a mapped texture's download where each page is kept #3759
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3759"
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 ivall/xcl. Originally posted by BBareth on 2026-12-03T03:08:52Z.
Stacked on #3549; only the top commit (
09de2dfd) belongs here.Merge after #3781: its fix to two
kernelxtests keeps this PR's new GPU test from crashing the test binary when they run side by side.A texture over pages the title mapped with
CopyPageMappingsBatchXhas a reserved GPU view as its own address, and each 64 KiB page is kept elsewhere. Uploads already read such a texture where its pages are kept, but a download of it was written at the view. Since #3549, the view's reserved pages are left out, so the fence becomes visible without the GPU's bytes reaching the pages the title reads. This PR writes each page where it is kept. Part of #505.This is the part taken out of #3549 after review. Reworked after the reviews of
0d9a09ddand6548204f:MapTitlePhysicalPagesof the same physical page) gets the bytes at its new place; before, a move was taken for a remap and the fence was published without them.mapping_changed. Its bytes are not written, the other pages are, and the download's writers are retired, so no later download writes the earlier mapping's bytes into the new page.6548204freview) A remap between the GPU write and the signal is caught too. A download records with each page the mapping call that last changed what it shows. A page changed by a call newer than the write (Entry::written_call) is left out at preparation, and at publication a page must still show the same physical page from the same call. So an unmap and remap of the same physical page counts as a remap. A move keeps the call, so a moved page is still written.0d31bf1d, @Gevroska's case) Two view pages that show different parts of one physical page, as when a texture starts inside a 64 KiB page, each write their part. Before, only the last view page showing a physical page wrote, so the first page's bytes were lost. Now later eligible pages take only the bytes they share with earlier ones.What is in it
crates/xcl/src/hle/kernelx.rs:mirrors::resolve_with_pages:resolve, also giving the physical page each view page shows and the call that last changed it (Shown::page,Shown::call).resolveis unchanged for its callers.settled_mappings(), a non-blocking hold on theAWAYlock, whichapply_batchesand the page moves take.settled_uncommittedfrom #3549 is now built on it.crates/xcl/src/hle/d3d12x/title_textures.rs:prepare_downloadsrecords whether the texture was paged and what each page showed (mapped_download_pages), leaving out pages remapped after the GPU write. A paged texture whose pages cannot be resolved is not prepared, instead of falling back to its reserved view.submit_prepared_downloadschecks only that the texture is still paged as prepared: a remap before submission is handled at publication like one after it.publish_downloadresolves the pages again and reads the current bytes from where each page is kept now.write_changed_mapped_pagesholds the mapping calls back (settled_mappings) and checks that nothing was mapped or moved since that resolve; if anything was, the publication retries with a fresh read. Then it writes each page that still shows its physical page from the same call, where that page is kept. Where eligible view pages show the same bytes of a home, the last of them writes those bytes, against what the home holds now; pages that show different parts of one home each write their part.a_download_over_mapped_pages_is_written_where_they_are_kept: the pool page and the CPU view of a never-named page take the bytes; an unmapped page takes none; nothing is written while a mapping call holds the lock; a move after the read makes the write retry.a_download_writes_a_home_shown_twice_from_its_last_pagea_download_writes_both_halves_of_a_home_shown_twice_at_different_offsets,uncovered_leaves_out_the_taken_rangesmapped_download_publishes_moved_pages_and_skips_remapped_pages(GPU): a real download throughprepare_downloads,submit_prepared_downloadsandpublish_download. It covers a retry under a mapping call (the writer is kept), a page moved withMapTitlePhysicalPages(the bytes land at the new place), and a remap (the new page keeps its bytes, the unchanged page is written, the writer is retired). Once the page table is gone, nothing more is prepared. It fails on0d9a09ddat the moved-page assertion.mapped_download_skips_a_page_remapped_before_preparation,mapped_download_skips_a_page_unmapped_and_restored_before_preparation,mapped_download_skips_a_page_unmapped_and_restored_after_preparation(GPU): the same download with the remap made between the write and preparation, or an unmap and remap of the same physical page before or after preparation. The remapped page keeps its bytes, the others are written, the writer is retired.Tested
cargo build --workspace, thencargo test --workspace --no-fail-fast -- --test-threads=4on09de2dfd(on #3549fc7abe51, on main078358a0): 3018 passed, 0 failed, 52 ignored.SKIPPEDon this adapter, 28 in all:no wave64no heap windowsno d3d12SDKLayers.dllno D3D12 debug layercargo test -p xcl --lib -- a_download mapped_download uncovered_leaveson09de2dfd: 8 passed. The new shared-home test fails on0d31bf1d(the test added to the old code) at its first-half assertion.cargo test -p xcl --lib mappedon0d31bf1d: 31 passed. With the preparation and publication checks reverted and the tests kept, the three new tests failed at their byte assertions (run by Codex on this machine).6548204f,cargo test -p xcl --lib mapped, 20 runs each:6548204f: 18 passed, 1 failed (a_page_whose_move_fails_stays_mapped_where_it_was), 1 crashed with 0xc0000005;fc7abe51: 20 passed.--skip a_page_whose_move_fails): 20 of 20.kernelxtests on main, not in this change. They reserve an address and release it to get one with nothing reserved. A test beside them, here the new GPU test, can be given that address in between, and the page move then writes into its memory. The fix is a separate PR: #3781.16c2fd08.Regression run
Each run follows one warm-up run of the same build; the A button is pressed once at 70 s.
Not tested
mirrors::released(a view torn down) does not takeAWAY. Such a teardown is caught by the comparison under the hold, not excluded by the lock.AI use
AWAYbefore the views'STATE, as inapply_batches; the publisher onlytry_locksAWAY), found the test race behind the crash, wrote the fix and test for the shared-home case, ran the suite and the regression runs, and wrote this description.🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T03:14:40Z.
Review of
0d9a09dd(code reading only, not a merge-check; draft) · 1 high, 2 mediumIn short: Stacked on #3549 (as the description says); only the top commit
0d9a09dd(266+/15-) belongs here, which is under the size target. For a paged texture it writes the download to the place where each page is kept, under a non-blocking hold onAWAY, clears the writers on success, and keeps them when the pages changed. That closes the gap #3549 leaves on its own (gpu_writtencleared without the bytes). But any change in where a page is kept between prepare and publish drops the download and still makes the fence visible without the GPU's bytes, including a plain move of the same physical page.High
A page move counts as a remap, and the fence is published without the bytes.
download_pages_current(title_textures.rs~3914) andmapped_home(~4208) are where a page's bytes are kept, not which physical page the view shows.MapTitlePhysicalPagescopies the page there and callspage_placed→mirrors::page_at(kernelx.rs~2633, ~694). That changes where the page is kept, but the view still shows the same page.gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changesdoes exactly this (~4957).prepare_downloadsand the fence completing,publish_downloadreturnsOk(true)(~4053, orMovedat ~4244 / ~4059).complete_fence_downloads(~4320) then drops the download and exposes the fence value, with the GPU's bytes at neither place.Fix: record the physical page each view page showed at preparation (
Shown::page,kernelx.rs~365), compare that, and write where that page is kept now while the lock is held. Add a test that moves the page withMapTitlePhysicalPagesbetweenmapped_nowand the write.Medium
A page remapped after preparation still loses the GPU's bytes (still open from #3549).
CopyPageMappingsBatchXpoints a view page at another page after preparation,Moved/ not current returnsOk(true)(~4053, ~4059). The page the GPU wrote through never gets the bytes, and the fence is published.mapping_changed(~866-876), a GPU write older than a mapping call gives way.Movedand say in the description that the fence is published without these bytes.The publish path is not tested. The four new tests call
write_changed_mapped_pagesanddownload_pages_currentdirectly. Nothing checks:publish_downloadreturns forDone,AgainandMoved;gpu_writtenare cleared onDoneand kept otherwise;?skip inprepare_downloads(~3937).gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes(~4808) already sets up a paged texture on a real device and queue and publishes a GPU write to it, so a download through its fence can be built on it.Checked and fine
fc7abe51unchanged. This PR needs #3549'ssettled_uncommittedandnote_download_page_left_out, so it does not build onmainalone.gpu_writers/gpu_written, and one that is not current leaves them set.publish_downloadholdsTEXTURESand onlytry_locksAWAY(settled_mappings).shows/resolvetakeSTATEand the pages lock afterAWAY, in the same order asapply_batches.lazy::ensuredoes not takeAWAY.mapped_homeencodes pages the same way asresolve.Againholds the fence back, and pages written before the retry are not written twice.submit_prepared_downloadschecksdownload_pages_currentagain at insertion. A paged texture with no page table is not prepared, instead of falling back to its reserved view.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T09:29:41Z.
On the review of
0d9a09dd, pushed6548204f(the top commit is reworked; #3549's commitfc7abe51is unchanged):A page move counts as a remap: confirmed. A download now records which physical page each GPU-view page shows (
kernelx::mirrors::resolve_with_pages, fromShown::page), not where it is kept. At publication the pages are resolved again, and each page is written where it is kept now. Under thesettled_mappingshold, a page that was mapped or moved since that resolve makes the publication retry with a fresh read.A remap keeps the writers: a view page that shows another physical page than at preparation gives way to the newer mapping, as in
mapping_changed. Its bytes are not written, the other pages are, and the download's writers are retired. A remap before submission is handled the same way: insertion no longer compares the pages, so the unaffected pages still get their bytes. The commit message says this now.The publish path is not tested:
mapped_download_publishes_moved_pages_and_skips_remapped_pagesruns a real download on a device and queue throughprepare_downloads→submit_prepared_downloads→publish_download. It covers:MapTitlePhysicalPages(the bytes land at the new place);It fails on
0d9a09ddat the moved-page assertion.Suite on
6548204f: 3013 passed, 0 failed, 52 ignored, 28 SKIPPED. Smoke: 3051 frames, peak commit 32313 MB, no device removal, no crash. The blocks are in the description.The
mappedlib-test filter crashed with 0xc0000005 in 3 of 61 runs on this head. The new GPU test alone passed 30 of 30. The cause is two kernelx tests on main that reserve an address and release it to get one with nothing reserved: a test beside them can be given that address in between, and the page move then writes into its memory. With that fixed (#3781), the filter passed 40 of 40.AI use: Codex (GPT-6.2-Sol) wrote the rework and the test from a brief; Claude Code (Claude Opus 5.5) checked the review points, reviewed the diff, ran the suite and the smoke runs, and wrote this comment.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T09:34:34Z.
Follow-up review of
6548204f, since0d9a09dd(code reading only, not a merge-check; draft) · 2 mediumIn short: Since
0d9a09dd, a download records which physical page each GPU-view page shows (Shown::page), not where the page is kept. At publication it writes each page where that page is kept now, skips pages that show another page, and retires the writers. A new GPU test drives the download from prepare through publish. The three earlier points are fixed. Two remain: a remap between the GPU write and the signal is not detected, and the new GPU test can crash the test binary until #3781 is in.Medium
A remap between the GPU write and the signal still writes the earlier mapping's bytes into the new page.
CopyPageMappingsBatchXtakes effect when the title calls it (q_copy_page_mappings,real.rs~11852), and the writer andwritten_callare stamped at the write (title_textures.rs~951-954).gshowingP, then a remap ofgtoQ, thenSignalX,prepare_downloads(~3927) recordsQas whatgshowed.write_changed_mapped_pagesthen sees no change (~4215, ~4223), and writes the PC texture's bytes, the GPU's output forP, intoQ's page.mapping_changed(~872-886) treats that write as older than the mapping. It clearsgpu_writtenbut notgpu_writers, so the download still goes ahead. The commit message says a newer mapping wins "as inmapping_changed", which only holds for a remap after preparation.Fix: return
Shown::callwith the page fromresolve_with_pages. Treat a page whosecallis newer than the entry'swritten_callas remapped at preparation, and compare (page, call) at publication; a move keepscall. Add a test that remaps aftercapture_gpu_writeand beforeprepare_downloads.The new GPU test needs #3781 first. The description reports 1 failure and 1 access violation (
0xc0000005) in 20 runs ofcargo test -p xcl --lib mappedat this head, against 20/20 onfc7abe51. The cause is twokernelxtests that release a reservation and then map into it, whilemapped_download_publishes_moved_pages_and_skips_remapped_pagescan be handed that address in between. #3781 fixes them. Say "Merge after #3781" in the description.Checked and fine
resolve_with_pages,kernelx.rs~616) and writes where that page is kept at publication. The GPU test covers a move made withMapTitlePhysicalPages.publish_download~4048-4063). The description and commit message now say those bytes are not written.?skip inprepare_downloadsonce the page table is gone.publish_downloadholdsTEXTURESand onlytry_locksAWAY. The resolve under the hold takesSTATE, then the pages lock, asapply_batchesdoes.fc7abe51.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T11:01:18Z.
Both points are addressed in
0d31bf1d.resolve_with_pagesnow gives each page's(Shown::page, Shown::call). At preparation, a page whosecallis newer thanEntry::written_callis left out: its bytes are not written, the other pages are, and the writers are retired as before. At publication, a page must still show the same physical page from the same call, so an unmap and remap of the same physical page counts as a remap. A move keepscall(placeonly sets it when the page appears or goes), so a moved page is still written where it is kept now. New GPU tests: a remap between the write and preparation, and an unmap + remap of the same physical page before and after preparation. With the checks reverted and the tests kept, all three fail at their byte assertions.Suite on
0d31bf1d: 3016 passed, 0 failed, 52 ignored (28 SKIPPED, the same as before). The regression run in the description is of0d31bf1d.Claude Code (Claude Opus 5.5); the fix was written by Codex (GPT-6.2-Sol) from a brief and reviewed by Claude.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T11:03:15Z.
Follow-up review of
0d31bf1d, since6548204f(code reading only, not a merge-check; draft) · no new findingsIn short: This push fixes both points from my last review. A download now records, for each view page, the physical page it shows and the mapping call that last changed it. A page whose call is newer than the GPU write is left out at preparation, so a remap between the write and
SignalXgives way to the new mapping as inmapping_changed. At publication, both the page and the call must still match, which also catches an unmap and remap of the same physical page; a move keeps the call and still gets the bytes. Four GPU cases cover this, and the description says to merge after #3781. Nothing blocks it.Checked and fine
prepare_downloadskeeps(page, call)only wherecall <= entry.written_call, andNonefor a page remapped after the write.write_changed_mapped_pageswrites a page only when its snapshot isSomeand equals what the view shows under the hold.MapTitlePhysicalPagesmove keeps the call, so it is written at the new place. A page never named has call 0, and the test checks that.kernelxtest race the new GPU test sets off.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Gevroska on 2026-12-03T13:44:37Z.
@BBareth I found a case on
0d31bf1dwhere mapped-download publication returnstruebut drops a valid write. The view starts at offset0x8000and spans two adjacent 64 KiB view pages. Both map to the same physical page, but the first writes its upper 32 KiB and the second writes its lower 32 KiB. Their destination bytes don’t overlap.I used a fenced RGBA8 readback from my RTX 3080 as the payload and ran the PR’s publication function against a synthetic mapping. The first slice should have changed from
0x5ato0x2a. It stayed at0x5a, while the second slice correctly changed to0xc5. When I left the second slice unchanged, publication still returnedtrueand wrote zero bytes. The first 32 KiB were lost in both cases. The controls with distinct physical pages or a newer second mapping behaved as expected.The function keeps the last eligible view-page index for each physical page. A local change that suppresses only the bytes covered by a later eligible slice passed all four cases. An independent replay confirmed both the original failures and the corrected outputs.
AI disclosure: OpenAI Codex GPT-6.2 Sol built and ran the fixture and local correction. A separate Codex review checked the results. GPT-6.2 Astra drafted this comment.
I reviewed and approved this comment before posting.
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T14:15:27Z.
@Gevroska thanks, confirmed. The last eligible view page won the whole physical page, so a texture starting inside a 64 KiB page lost the first slice whenever the next view page showed the same physical page.
Fixed in
09de2dfd, squashed into the commit:write_changed_mapped_pagesnow walks the slices last first and records, per physical page, the byte ranges a later eligible slice wrote. An earlier slice writes only the bytes no later one covers. Same-byte overlaps keep the last-page rule; disjoint halves each get written.a_download_writes_both_halves_of_a_home_shown_twice_at_different_offsetscovers your layout, a texture at view offset0x8000spanning two view pages of one physical page, with the second slice changed and unchanged. On0d31bf1dit fails at the first-half assertion; on09de2dfdit passes.uncovered_leaves_out_the_taken_rangestests the range helper.09de2dfd: 3018 passed, 0 failed, 52 ignored, 28 SKIPPED. The smoke block in the description was rerun on this head.AI use: Claude Code (Claude Opus 5.5) wrote the fix, the tests and this reply, and ran the suite and smoke.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T14:17:55Z.
Follow-up review of
09de2dfd, since0d31bf1d(code reading only, not a merge-check) · no new findingsIn short: Since
0d31bf1d, only the top commit changed, inwrite_changed_mapped_pages. When several view pages of a texture show one physical page, the earlier code let the last eligible page write the whole home. Now each page writes only its own part of the home, and a later page wins only on the bytes they share. This fixes the case raised in the thread, a texture that starts inside a 64 KiB page, where the first page's bytes were lost. The earlier review had no open points, and this push adds no new problem.Checked and fine
0d31bf1dhad no findings.title_textures.rs~4215-4258). For each home, the in-page ranges that later eligible pages cover are recorded intaken, keyed onhome & !1. An earlier slice writes only whatuncoveredleaves. Pages that are not eligible, and pages whose home is 0, cover nothing, so an earlier eligible page still writes those bytes. This keeps the earlier "last eligible page" rule.to = (home & !1) + off;liveis read throughPageMap::new(a, vec![home])for the same bytes;changed_page_runsis moved into page offsets, clipped byuncovered, and moved back.live[..n]stays in bounds, becausen <= PAGE.min(new.len()).a_download_writes_a_home_shown_twice_from_its_last_pageasserts. A failed write still returnsfalse, so the caller retries with a fresh read.uncovered(~4261) handles taken ranges that are unsorted, overlapping, adjacent or partly outside the request.uncovered_leaves_out_the_taken_rangescovers those cases.a_download_writes_both_halves_of_a_home_shown_twice_at_different_offsetssets up the reported layout: a texture at view offsetPAGE / 2, with both view pages showing one pool page. It covers both the changed and the unchanged second half, and its first-half assertion fails with the old code.0d31bf1d: #3549'sfc7abe51is the same, and the rest of the diff istitle_textures.rsonly (78+/16-).Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Gevroska on 2026-12-03T15:51:54Z.
@BBareth Thanks, I reran both cases from my earlier report on
09de2dfd. The revised writer restored all 32,768 missing bytes in both cases, including the one where the later slice was already unchanged.I replayed the same saved 64 KiB RTX 3080 readback against controlled mappings on Windows. All backing bytes matched in all four cases, including the distinct-page and stale-mapping controls. A separate byte-by-byte check also passed for 223,608 interval combinations.
That resolves the data loss I reported on
0d31bf1d. This was a replay of the publication function with the original payload.AI disclosure: OpenAI Codex GPT-6.2 Sol ran the replay. An independent Codex reviewer checked the results. GPT-6.2 Astra drafted this comment.
I reviewed and approved this comment before posting.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.