Skip to content

d3d12: keep title writeback fenced past 64 pending batches (RT-TITLE-WRITEBACK-FENCE-OVERFLOW) - #2461

Open
73N1X wants to merge 2 commits into
ProjectLeonida:mainfrom
73N1X:fix/title-writeback-fence-overflow
Open

73N1X wants to merge 2 commits into
ProjectLeonida:mainfrom
73N1X:fix/title-writeback-fence-overflow

Conversation

@73N1X

@73N1X 73N1X commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-WRITEBACK-FENCE-OVERFLOW

What and why

DeviceState::write_back_titles() copied the oldest pending ranges back to title memory without their completion ticket once more than 64 batches accumulated. A held queue can stall the global retirement prefix while another queue continues submitting and completing work, so backlog size does not prove completion.

Remove that bypass and retain each batch until its ticket retires. No GPU wait is added under shadow_mutex, which another queue may need to submit the work that releases the dependency. Existing per-submission range/byte limits remain. Pending range metadata can grow during a stall; this change favors completion ordering over prematurely consuming it.

Add d3d12_writeback_retirement: hold one native Vulkan queue with an unsignaled timeline semaphore, complete 130 submissions on a second queue, and verify that no title-memory result appears early. Each submission writes a different value to a separate page, so the final check also catches lost batches. After release, a refresh without a new reached range must recover all 130 values. Uses transfers and synthetic buffers only, no shader tools or game files. Skips when two graphics-family queues are unavailable; failure cleanup releases the gate.

Related work: #2352 changes adjacent-range coalescing; #2032 is an alternative writeback implementation. This PR builds on neither and leaves those topics alone.

Tested

Native Windows 11 build 26200, MSVC 2022, AMD Radeon RX 5700 XT 8 GB, Windows driver 32.0.21045.5002:

  • Before the fix, the initial synthetic reproducer failed at refresh 65 in two separate runs on main 63185f4a3, with the first queue still held. The test was then strengthened to use 130 distinct pages and verify every retained result. That final page-isolated test passed three consecutive runs; the committed version, with an added result-count diagnostic, passed again. The final strengthened test was not rebuilt against unmodified main.
  • cmake --build runtime/build --config RelWithDebInfo --target d3d12_writeback_retirement_test -j 2: passed.
  • ctest --test-dir runtime/build -C RelWithDebInfo -R '^d3d12_writeback_retirement$' --output-on-failure: passed.
  • Full translator suite: 500 files, 6,374 tests, zero failed files (-j 4).
  • python -m compileall -q make_game.py tools tests, python -I make_game.py --help, CI-registration tests (18), text-line-ending tests (3), explicit-path no-game-data scan and diff checks passed.
  • Full runtime build encountered the existing vk_null_descriptor_test.cpp:97 C2065 (LEONIDA_TEST_NULL_DESCRIPTORS_SPV) with no glslangValidator installed; runtime tests: vk_null_descriptor_test builds without glslangValidator (TEST-NULL-DESCRIPTOR-NO-GLSLANG) #2055 addresses it. Full runtime CTest was not run. Khronos validation was requested but unavailable because VK_LAYER_KHRONOS_validation is not installed; no validation-layer success is claimed.

GPUs tested: AMD Radeon RX 5700 XT, 8 GB desktop, driver 32.0.21045.5002, Windows 11 build 26200 (profile M). No gameplay, performance improvement or retail-crash remediation claimed.

AI-assisted: yes, OpenAI Codex; a second agent reviewed the patch and independently checked the semantic-suite counts.

Checklist

  • Based on current main (63185f4a3).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in quoted logs or reports.
  • A new translator rule is in docs/design.md (not applicable: no translator change).

@timbot103

Copy link
Copy Markdown
Contributor

Independently checked head 78158bab9 on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.0, no game files.

Local build & dual-GPU verification:

  • Clean MSVC build of d3d12_writeback_retirement_test (zero warnings in PR files).
  • d3d12_writeback_retirement_test with LEONIDA_VK_VALIDATION=1:
    • NVIDIA GeForce RTX 5080 (616.64): PASS (retired writeback results: 130/130 matched; 130 pending writeback batches stayed fenced and drained after release), 0 validation errors.
    • AMD Radeon(TM) Graphics (25.10.30.02): PASS (retired writeback results: 130/130 matched; 130 pending writeback batches stayed fenced and drained after release), 0 validation errors.

Code review notes:

  • The 64-batch unfenced-drain removal is clean, adds no GPU wait under shadow_mutex, and d3d12_writeback_retirement_test genuinely exercises the production writeback queue with a held native queue and a second-queue fence across 130 pages.
  • Small documentation precision suggestion (docs/d3d12_vulkan.md:1729-1739): Because a batch still records a predicted submitted + 1 ticket before its writer submits (d3d12_vulkan_device.cpp:3327-3349) and writeback reads the live shadow buffer (which a later overlapping writer may touch), narrowing the doc wording to say batches are retained until their recorded retirement prefix completes (rather than implying full per-writer isolation across concurrent queues / overlapping later writes) keeps the documented guarantee exact.

The 64-batch fix itself looks solid. AI-assisted.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 78158bab9 locally merged onto main a3bafd05d02b on Windows 11 with MSVC 19.44 (Release), NVIDIA GeForce RTX 5070 (610.88); Vulkan validation was unavailable locally, so the GPU checks ran without it.

  • Red/green control: running d3d12_writeback_retirement against main a3bafd05d02b failed on refresh 65 (premature title writeback on refresh 65 (first queue still held) after logging the 64-batch bypass). With this PR merged, d3d12_writeback_retirement (130/130 matched), d3d12_title_va, shadow_keep, and title_va_alias all passed (4/4).
  • Additional synthetic check (scratch): had the held queue itself write page 0 of title and call note_reached while the second queue ran the remaining 129 batches on distinct pages. On main, the held batch was dropped early and page 0 remained lost after releasing the gate (129/130 matched); with this PR, no premature writeback occurred and all 130/130 pages matched after release.

No findings from these checks.

AI-assisted.

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Retail check for regressions on profile M (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11). Same tree with and without this PR: current main (with #2418/#2412) + #2260 #2020 #2166 #1851 + DadaCap window-retire, async ≥ 1 MiB, classic gate. Window: world phase +60 to +240 s.

without with #2461
frames/s 0.100 0.091
shadows (write-back is in it) ms/submission 102 87
heaps ms/submission 104 78
longest submission stall 2.7 s 1.7 s
peak private bytes 34.0 GB 31.8 GB
freeze / device loss / exceptions none none
device-stall: queue retirement has not advanced for 15 s lines 1 2

No regression seen. The differences are within the run-to-run spread I see here (shadows varies 80-270 ms across same-build runs). Pending-batch growth didn't show up as memory. MEASURED (n=1 each), world phase reached, run cut after 2.5 min of it. AI-assisted (Claude Code, Claude Opus 5.5).

@djrolfes

Copy link
Copy Markdown
Contributor

Independent Linux/Wine + NVIDIA check at head 78158bab9 (merged cleanly onto origin/main ec91d695c in a local integration stack alongside #2467, #2463, #2470, #2469, #2126, #2091, and two local V# patches).

  • Environment: Linux 7.2.15, wine-11.22, clang-cl + xwin cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.
  • CTest: d3d12_writeback_retirement passed (6.06 s): retired writeback results: 130/130 matched — 130 pending writeback batches stayed fenced and drained after release (both graphics-family queues exercised on NVIDIA under Wine).
  • Code review: clean removal of the 64-batch unfenced bypass without introducing any wait under shadow_mutex.

AI-assisted.

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Merge test for the #2464 campaign on an 8 GB laptop (profile M). Main ec91d69 was merged into the PR head 78158ba with no conflict, giving local 25b331779. The numbers were recomputed from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).

RUN m2461m | 2026-11-27 | OreoFR | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        this PR at 78158bab9 + main ec91d695c merged | shaders: main translation + #2447 (one-lane) pack
MACHINE:     RTX 4070 Laptop 8 GB, laptop (profile M) | NVIDIA 621.40 | Windows 11 | 40 GB
RECIPE:      documented + DEVIATIONS: ASYNC_PIPELINES=1 ASYNC_MIN_BYTES=1048576 VALUE_WAIT_TIMEOUT_MS=30000 MAP_READBACK=1 DEVICE_TYPE=6 ARENA_MB=4095 DEVICE_FAULT=1
OS/DRIVER:   TdrDelay 60
TARGET:      regression check against regA (main ec91d695c, same recipe and caches) | PREDICTED: world reached, no loss
MEASURED:    world entry ~265 s (regA 223 s) | 0 device lost, 0 exceptions (regA 0 / 0) | gpu-stall lines 2 (regA 10)
END:         stopped by our script 40 s after world entry (t=305 s): the known display stop after world entry is the same on main
CACHES:      warm | RUN 1 of 1
INSTRUMENTS: device fault checkpoints, thread stacks
VERDICT:     no regression seen (n=1); the later world entry is within what we see from run to run on this machine (223-313 s today)
  • d3d12_writeback_retirement_test from the merged head, RelWithDebInfo: passed (130/130 results matched, and the 130 pending batches stayed fenced until they were released).
  • I did not rerun the test on main here. The PR description shows it failing on main 63185f4.
  • An earlier run of this PR on another tree today (13:50 UTC+1) also showed no regression.

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Addendum to the RUN above: I built this PR's d3d12_writeback_retirement_test (the test file only, with its CMake target) against main ec91d69 on the same machine. On main it fails: premature title writeback on refresh 65 (first queue still held), exit 1. With the PR it passes 130/130. That matches the PR description. AI-assisted (Claude Code, Claude Opus 5.5).

@73N1X

73N1X commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the documentation precision suggestion in 2f3adc2. The text now states that batches wait for their recorded predicted ticket in the completed retirement prefix, without claiming per-writer isolation across concurrent queues or snapshots of live shadow bytes. Documentation only; diff check and explicit-path no-game-data scan passed. AI-assisted by OpenAI Codex.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 2f3adc279: the updated wording in docs/d3d12_vulkan.md accurately states the recorded predicted-ticket retirement guarantee without overclaiming per-writer isolation or shadow snapshotting.

Approving 2f3adc279 (runtime code unchanged from the previously verified 78158bab9).

AI-assisted (Antigravity, Gemini).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants