d3d12: a page another tracker keeps read-only is copied once, not at every submission (RT-SHADOW-FOREIGN-RECOPY) #1897
Closed
leonida/mandy
3L1A5-1 wants to merge 0 commits from
fix/shadow-readonly-recopy into main
pull from: fix/shadow-readonly-recopy
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!1897
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/shadow-readonly-recopy"
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?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
Claimed in the coordination issue as: RT-SHADOW-FOREIGN-RECOPY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164455285)
Depends on: #1867 conflicts: it also edits
ShadowPages(guard_read_only, therefreshskip test, the class comment) inruntime/shims/d3d12_vulkan_device.cpp, which this pull request moves toruntime/src/shadow_pages.h. #1867 was claimed first: if it lands first, I rebase this one onto it (redoing the move from the new code), so #1867 needs no change for this; the two changes compose (an aliased page that is clean by #1867's cells and a page another tracker keeps read-only are separate cases of "clean").What and why
In the retail title a slot-133 buffer sits over the last 27 MiB of the memory the console descriptor heap is carved from. Each
ShadowPagestracker (runtime/src/shadow_pages.h) decides per page from the page's actual protection inShadowPages::refresh:guard_writable: the tracker makes it read-only, copies it and marks itkProtected("mine, unchanged since the copy"); later refreshes pass over it (unchanged);guard_read_only: only akProtectedpage is clean. Every other read-only page is copied, asCopy::read_only(the "read-only (not ours)" item of the "shadow copies by cause" log line), because the tracker cannot tell whether a page that another tracker made read-only has changed since.The console heap has its own tracker (
DeviceState::refresh_console_heap,make_shadow_pagesforheap->pages) that keeps the heap's pageskProtected. The buffer's tracker (DeviceState::add_shadow) finds the pages it shares with the heap read-only and not its own, so it copies them at every submission. A write faults inshadow_write_fault, which reopens the page in the tracker that protected it; the other tracker is never told.Measured in play logs of the retail title on the RTX 2060 SUPER (counts only): "read-only (not ours)" is 27,648 KiB of the roughly 28,000 KiB copied per submission (99%), and the shadows phase is about 10 ms of a 22 ms submission (about 2.7 GB/s of memcpy).
The fix, all in
runtime/src/shadow_pages.h:kForeign: "read-only, made so by another tracker, copied since; unchanged until told otherwise".guard_read_onlycopies a read-only page,note_foreignasks the other trackers (other_guards->ShadowPages::guards) which of the copied pages they keep askProtected. Those pages getkForeign; the next refreshes pass over them (guard_read_only,unchanged), without aVirtualQuery. Only pages that another tracker keeps read-only qualify: the title's own read-only pages (read-only data, copy-on-write, geometry-watch tiles) are copied every time, as before.shadow_write_fault: the tracker over the address reopens its 64 KiB block and clearskForeignthere; a tracker that only overlaps the block is told withforget_foreign); a kernel write through a file shim (reopen_range); the other tracker going away (ShadowPagesDeleter,remove_target, throughforget_foreign/forget_foreign_elsewhere); a change of the title's memory map (kRequery) and a full walk (alsoLEONIDA_SHADOW_FULL_WALK=1); a page that becomes writable, zero, copy-on-write, aliased or pinned loses the mark through the existing state resets.other_guardsnever waits for a lock: it usesTryAcquireSRWLockSharedong_shadow_pages_lockandTryAcquireSRWLockExclusiveon the other tracker's lock, because it runs under the asking tracker's lock while the fault handler holds the global lock shared and waits for tracker locks. A busy tracker means "not foreign": the page is copied again, as before this change.The first commit is a pure move, so that a test needs no GPU:
ShadowPages, the list of trackers, the write-fault handler andcopy_guarded/copy_streaming/copy_rungo fromruntime/shims/d3d12_vulkan_device.cpp(an anonymous namespace in a 12,000-line file that links the GPU layer) toruntime/src/shadow_pages.h, in namespaceleonida::shadow_pages, with using-declarations in the .cpp. The code is moved unchanged apart from these differences:inlineon the free functions, the include and namespace lines, and the VirtualProtect-refused message, which a small helper in the header logs once per error instead oflog_once(the same text). Reviewing the second commit alone shows the fix; the first one is best read withgit diff --color-moved.Tested
Unit test, no GPU and no game data:
runtime/test/shadow_pages_test.cpp(ctest nameshadow_pages). Two real trackers (make_shadow_pages, the real fault handler) over overlapping pages ofVirtualAllocmemory; the test thread writes (and faults), and callsopen_shadow_pagesfor a kernel write. It checks that the shadows equal the title's bytes and how many bytes each refresh copied: the first copy and the "read-only (not ours)" class; three refreshes with no write copy nothing; a write to a shared page copies only its 64 KiB block, then nothing; writes to a page only one tracker covers; the other refresh order; a kernel write; the guard destroyed, a write, the guard created again; a tracker that starts inside the faulting tracker's block.FAILED, 19 checks fail (for example, the buffer's tracker copies the 512 KiB it shares at every refresh with nothing written, and 512 KiB after a write that should copy 64 KiB). All "copies are current" checks pass: the old code was correct, only wasteful.ok, 37 PASS and 0 FAIL, 30 consecutive runs without a failure.reopen_rangenot clearing the mark;reopennot clearing the block; the fault handler not telling overlapping trackers.d3d12_console_descriptor_heap_test,d3d12_xmem_placed_test(paired views, pins, host and title writes),d3d12_title_va_test(also withLEONIDA_TEST_CONSOLE_SHADOW=1).main2e2c51c09(head8d2bf8d10). One upstream change was in the moved code:ShadowPages::write_back(#2129), carried intoshadow_pages.h; its page-state copy uses the widened 16-bit type.shadow_pages_testdoes not callwrite_back.shadow_pages30 of 30 runs; the GPU tests above plusxbox_linear_texture_map,xbox_linear_gpu_staging,d3d12_title_va_shadow,d3d12_geometry_viewspass.xbox_present(present failed (VkResult -11)) andd3d12_set_descriptor_heaps(CHECK at line 353). Both fail the same way on a clean build ofmain2e2c51c09on this PC.In-game (RTX 2060 SUPER)
Measured before the rebase. Two 600 s retail boots with the same build and settings apart from this change:
main98c2f7777plus #1756, #1742 and #1705, then this pull request's two commits. Native Windows 11, RTX 2060 SUPER,LEONIDA_ARENA_MB=2048. Counts from the runtime's own log lines only.submit phases: shadows ms per submission, t = 40-140 ssubmit phases: frames/s, t = 40-140 sPresentXbefore the story load starts (about 170 s)What remains of the shadows phase is the walk over the pages, not the copy. Both runs then stop at the same later point in the story load, which is unrelated to this change.
Known limits: a tracker that is permanently busy keeps its pages copied (shown as "read-only (not ours)" still large); a write to a page made read-only through a mapping or protection change that nothing journals would be missed, the same trust that skipping
kProtectedpages already places on the journaled paths (titleVirtualProtect, xmem) and on the fault and opener paths.GPUs tested: NVIDIA GeForce RTX 2060 SUPER 8 GB (Turing), driver 617.14, Windows 11 26300
AI-assisted: yes (Claude Code, Opus 5.5)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator rule)🤖 Generated with Claude Code
Independent verification of head
782096cbd9952e42c214d688892ff18f0247f26d.Windows 11 Pro, RTX 4070 SUPER (NVIDIA 621.01), MSVC 19.52.37029, RelWithDebInfo.
Blocker: it no longer merges with
main. Merging currentmain(a73a57e13) conflicts inruntime/shims/d3d12_vulkan_device.cpp:mainchanged theShadowPagescode this PR moves intosrc/shadow_pages.h, so the move needs redoing on top ofmain. I tested the head as it is instead.shadow_pages(new) passes, all its cases PASS; neighboursxmem_no_write_watch,xmem_write_watch,title_write_watch,title_write_watch_on,d3d12_existing_heap(+_no_write_watch,_write_watch),d3d12_xmem_placed,d3d12_title_va_shadowpass (10/10).xmemfailed once underctest -j 6and passed 3/3 run alone.main: the new test needs the new header.Diff read (compared the removed block with the new header, ignoring whitespace): apart from the move, the change is the new
kForeignstate. A page another tracker guards is copied once and then counted unchanged; it is cleared by the write-fault handler for every tracker over the block, by a pin (reopen_range), by a full walk, and when the guarding tracker drops the range (forget_foreign_elsewhere). I found no path that leaves a stalekForeignpage.No game files used. AI-assisted (Claude Code).
Run of this pull request on a second machine (head only: this branch does not merge cleanly into current main).
Heads: PR head 8d2bf8d10 (2 commits, built on main 2e2c51c09), current main 8fb974878.
Merge into main: conflict in runtime/shims/d3d12_vulkan_device.cpp (the PR moves ShadowPages out of that file, and main has changed that file since). I therefore built and tested the PR head itself (detached), not a merge.
Commands (RelWithDebInfo, Ninja, -DLEONIDA_REQUIRE_VULKAN=ON):
Green: 6/6 pass (shadow_pages is the new test; the others exercise the moved ShadowPages code through the D3D12 device).
Red (behavioural: the test compiles and runs): same head with runtime/src/shadow_pages.h restored to the first commit (the pure move), test and CMake entry kept. shadow_pages then fails: the buffer's tracker copies the 512 KiB it shares with the heap at every refresh (want 0 KiB: "refresh 1/2/3 with no write", and after each write "copies only the written block (512 KiB, at most 64 KiB)"), while the heap's tracker copies 0 KiB. That is the re-copy the PR removes.
Link: gta6rt.exe of the PR head vs gta6rt.exe of main 8fb974878 (dumpbin /imports, DLL!symbol, sorted): the only import present in the head and not in main is api-ms-win-crt-stdio-l1-1-0.dll!__stdio_common_vswscanf. It comes from the older base (main has since changed an swscanf call), not from this diff, which adds no scanf call. No other new imports.
Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44, Vulkan SDK 1.4.363.1.
The branch is behind main and conflicts in the file above; could you merge main into it? Thank you.
Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (
d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools,d3d12/, or general docs), please feel free to reopen it.