Skip to content

d3d12: blocks of textures and staging buffers are made with their pool's lock released (D3D12-POOL-GROW-UNLOCKED) - #2693

Merged
MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-pool-grow-unlocked
Nov 30, 2026
Merged

MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-pool-grow-unlocked

Conversation

@TheGeogeo

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-POOL-GROW-UNLOCKED (#2570 (comment))

What and why

On main, the texture pool and the staging pool make a new 64 MiB block while they hold the pool's mutex:

  • textures: lock(&TEXTURES).take_ripe(.., || CreateHeap + MakeResident + wait for the mapping);
  • staging: lock(&STAGING).take(.., || upload_buffer(BLOCK)).

With blocks made ahead (LEONIDA_RIPE_BLOCKS, 12), a block is made each time one is begun. While it is being made, everything else that takes the same mutex waits:

  • every pooled texture that is let go (release -> give);
  • texture_room(), which the residency policy (resource.rs) and the once-a-second sweep read;
  • every other texture or staging piece, even one that fits in an existing block.

This change:

  • Pool::take_or_need finds a piece, or says that a block has to be made first. It never makes anything.
  • take_unlocked makes the block with the lock released, then adds it.
  • One block at a time is made for want of room. A thread that needs a block while another is being made waits for that one on a condvar, instead of making a second block.
  • The thread that made the block takes the first piece of it before the others are woken.
  • Blocks being made count toward the blocks made ahead, so there is no overshoot.
  • After a failed block, none is asked for again for half a second, and a later failure never shortens that delay. A thread that arrives during that half second while a block is being made waits for that block.
  • texture_room() reads a block count kept next to the pool and takes no lock.
  • A block made this way is counted as made by the submissions begun when it is added, after its making. That number is the same as or later than before, so it is no less safe under the rule that new blocks are used only once the lists running at their making have finished.
  • LEONIDA_POOL_GROW_UNLOCKED=0 keeps the old path.
  • With diagnostics, the memory line now also reports the blocks made and their time, and the waits for the pools' locks and for blocks being made.

The callers of pool::texture/pool::staging are unchanged. The two call sites only change the function they call.

Tested

On Windows 11, from d3d12\:

  • New tests in pool::tests:
    • the_room_in_texture_blocks_is_read_without_their_lock: fails on main (texture_room() waits for the lock held by the test) and passes with the change;
    • a_piece_or_a_block_to_make_first, a_block_is_made_with_its_pools_lock_free, threads_wanting_room_wait_for_the_block_being_made (four threads that each need an eighth of a block make one block), a_block_that_failed_is_not_made_again_at_once, a_maker_that_panics_leaves_no_one_waiting.
  • cargo test -p leonida-gpu --locked: 558 passed, 49 ignored. The pool tests also pass with --features diagnostics.
  • cargo clippy -p leonida-gpu --all-targets --locked --features diagnostics: nothing in the changed lines. git diff --check and check_no_game_data.py are clean.
  • An independent review of the concurrency found no deadlock or lost wakeup and no new lock-order dependency. Making a block no longer runs under the pool's lock, so the driver calls, MakeResident and the mapping wait are no longer nested inside it. Its other findings are fixed in this change.
  • The diag build (build-nvidia.ps1, diagnostics,dlss,mimalloc) built.
  • It merges cleanly with d3d12-gpu: castable R9G9B9E5 views over R32_UINT/SINT textures without copying (D3D12-ALIAS-CAST-R9G9B9E5) #2660 (texture), runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 (staging's signature; that pull request's own conflicts with main are elsewhere) and d3d12: a landing pool asks its limit once a batch, and parks no thread without LEONIDA_AB (D3D12-LAND-POOL-LIMIT-ONCE) #2692 (git merge-tree).
  • In game, with the same executable, the switch was turned off, then on, then off again. Each run went from the menus to the world, then a scripted cutscene in the Series S preset. The numbers are the memory line's totals since the start:
run blocks made time making them longest waits for the pools time waiting longest wait cutscene p50 / p99 / max (ms)
=0 (old path) 129 819 ms 67.0 ms 654 220 ms 57.0 ms 45.6 / 122.9 / 797
on 93 817 ms 66.5 ms 651 34 ms 14.6 ms 44.9 / 122.9 / 322
=0 (old path, cutscene cut short at 69 s) 92 918 ms 71.7 ms 613 274 ms 65.3 ms —

How to read it:

  • With the old path, the longest wait for a pool's lock is about one block's making (57-65 ms against 67-72 ms), and the waits add up to 220-274 ms per run.
  • With the change, they add up to 34 ms, and the longest is 14.6 ms.
  • The frame times of the cutscene are the same within the noise. Most blocks are made while the world loads, so this removes stalls on the threads that let textures go or read the room, not a steady per-frame cost.

Limits of this measurement:

  • The "on" run used the build from before the review fixes. In that build, a wait for a block being made (on the condvar) was not yet counted in "waits for the pools".
  • The A/B with the final build was stopped part way, so only the third, old-path run used the final build.
  • I will add the final build's numbers for the switch on in a comment.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11 Pro 10.0.26300.

AI-assisted: yes

Checklist

…l's lock released (D3D12-POOL-GROW-UNLOCKED)

A new 64 MiB block of textures (CreateHeap, MakeResident, the wait for its
mapping) or of staging buffers was made inside the pool's mutex, and with
blocks made ahead (LEONIDA_RIPE_BLOCKS) one is made each time one is begun.
Meanwhile every pooled texture let go, every read of texture_room() (the
residency policy and the once-a-second sweep) and every other texture or
staging piece, even one with room in a block, waited for it.

Pool::take_or_need now only finds a piece, or says a block is to be made
first; take_unlocked makes it with the lock released and adds it. One block
at a time is made for want of room: a thread that needs one while another
is made waits for that one on a condvar, and the maker takes the first
piece of its block before the others are woken. Blocks being made count
toward the blocks made ahead. A block that failed is not asked for again
for half a second, and a later failure never shortens that.
texture_room() reads a block count kept beside the pool and takes no lock.
A block is counted as made by the submissions begun when it is added,
after its making. LEONIDA_POOL_GROW_UNLOCKED=0 keeps the old path, with the
submissions begun asked for under the lock as before.

With diagnostics, the memory line gives the blocks made and the time they
took, and the waits for the pools' locks and for blocks being made.
@lixonik

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Independent native Windows 10 check of c78140d110d141d874076af301b6bf9a29816113 merged locally into 1d22590b9fb5c0f51a6a7c0405f1f136e0696c0d. Windows 10 22H2 build 19045, Ryzen 9 5950X, Rust 1.98.1 MSVC.

cargo test --locked --offline --profile diag -p leonida-gpu --features diagnostics --lib pool::tests reported 14 passes. In a follow-up focused on CPU coverage, all 13 host-independent pool cases passed with four test threads. The four-request contention case separately passed in five fresh processes, each asserting exactly one block creation and no overlapping ranges. The lock-free room reader, growth-failure backoff and panic cleanup fixtures also passed.

This checks the pool's synthetic allocation and synchronization behavior. The GPU-only test can return early on setup, so I did not count it as verified driver-side growth. The no-game-data check passed on both changed files.

AI-assisted.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Independent check on Windows 11 (Rust toolchain from d3d12/rust-toolchain.toml, CPU only, no game run). Head c78140d11 merged (no commit) into main bbd45f5cc:

AI-assisted (Claude Code).

@TheGeogeo

Copy link
Copy Markdown
Contributor Author

The semantic-tests failure here is not from this change, which only touches Rust files under d3d12/. The same test, test_interp_computed_ij_fallback (ComputedIJDerivativeFallbackTests), also fails in CI on main (run 46365003804). The other checks pass.

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent Wine A/B for c78140d110d141d874076af301b6bf9a29816113 locally merged into main dcba958e52efe681343fc25bbb9706b40420e222. CachyOS, Wine 11.22, VKD3D-Proton 3.1.0, Rust 1.98.1 MSVC (diag, diagnostics, pool::, one test thread). Tested on an RTX 4070 SUPER (NVIDIA 620.73.04) and Radeon 610M (RADV 26.3.1), selecting each Vulkan ICD separately.

  • On each GPU, main passed 11/11 pool tests. The PR passed 17/17 with the variable unset and 17/17 with LEONIDA_POOL_GROW_UNLOCKED=0.

  • A separate synthetic D3D12 probe called take_staging with real mapped upload buffers. It left exactly 64 KiB free in a 64 MiB block and held an 8 MiB request's growth closure at a channel gate. A second thread's 64 KiB request completed before release with the default setting on both GPUs. With LEONIDA_POOL_GROW_UNLOCKED=0, it completed after release on both. In every run the 64 KiB allocation used the tail of block 0, the 8 MiB allocation used offset 0 of block 1, and GetDeviceRemovedReason returned success. This checks the ordering under controlled contention. The runs used synthetic buffers and covered no in-game timing.

I also reproduced the reported F16 semantic failure on dcba958e5 (7/9 passed) and confirmed 9/9 with the existing test-only fix #2633 (71db4c405) applied to that same base.

AI-assisted: yes (Codex).

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

RUN - in-game gate (Verified by OreoFR), PROVISIONAL - RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11.

  • Base: main e1fe82f63; B = main + this PR at c78140d11 (git merge --no-ff), diag build of leonida.exe.
  • Recipe: play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, standing in the world, warm pipeline cache, 600 s per run. Runs interleaved with main.

Result: PASS - 0 device loss, 0 panics, every run alive at the end (653-668 s of world frames).

Arm runs (well warmed) fps median p5 preparing (ms/frame)
main 6 22.40 (21.9-23.0) 17.90 28.80
main + #2693 2 22.15 (21.8, 22.5) 17.80 29.10
  • No fps regression: -1.1 %, within the ~4 % run-to-run noise on this machine.
  • Warm-up bias: the first warm run after the cold one is slow on every build, main included. Those runs are excluded on both sides; with them this PR reads -4.0 %.
  • Pool lock waits over a whole run:
    • New path: 5 / 6 / 10 ms in total, longest single wait at most 3.4 ms, over 923-1023 waits.
    • Same build with LEONIDA_POOL_GROW_UNLOCKED=0 (one run, 21.2 fps): 124 ms in total, longest 13.7 ms, over 910 waits.
    • Blocks made are the same on both paths: 45-47 blocks in 267-288 ms. The direction matches the author's, at a smaller scale on this card.
  • The texture-block race risk: nothing showed in the runs (no device loss, no failure, no wrong picture seen in the screenshots).

This run was AI-assisted (Claude Code, oreofr-agent).

@MoonEyes

Copy link
Copy Markdown
Collaborator

@TheGeogeo the in-game gate on an RTX 4070 (OreoFR, above) passes on this head, and the only red check is semantic-tests, which comes from the older base (fixed on main by #2566). Could you merge current main into this branch (a merge, no rebase or force-push) so CI runs again? Merging needs a fully green CI.

AI-assisted (Claude Code).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Closing and reopening so that CI runs against current main: the only red check here (semantic-tests) comes from the older base. The head does not change, so the verdicts above still stand. @TheGeogeo no need to update the branch if this is enough.

AI-assisted (Claude Code).

@MoonEyes MoonEyes closed this Nov 30, 2026
@MoonEyes MoonEyes reopened this Nov 30, 2026
@MoonEyes

Copy link
Copy Markdown
Collaborator

Verified by MoonEyes on head 6619d9294 (a merge of main on top of c78140d11; the pull request's own change is the same), on main f9f3ec4c6. Windows 11, Rust toolchain of d3d12/rust-toolchain.toml.

AI-assisted (Claude Code).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Merging. Head 6619d9294 merges cleanly with main 3c07d1677 and does not force a rebase of #2566. Verdict and a 600 s in-game run on this head above; OreoFR's gate on an RTX 4070 (lock waits 5-10 ms a run against 124) and lixonik's, merzleckeier's checks were on the head before main was merged in. Its CI is green. main's Runtime workflow is red for a known reason (macos, ps_exp_f32_denorm_rt); merging green pull requests meanwhile is agreed with aslan2142. Merged by MoonEyes.

AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit 20fe0c7 into ProjectLeonida:main Nov 30, 2026
5 checks passed
djrolfes added a commit to djrolfes/leonida that referenced this pull request Nov 30, 2026
… 1D wave-base (ProjectLeonida#2685), NaN-exact, by_due texture queue, unlocked pool growth (ProjectLeonida#2693), pipeline wait queue (ProjectLeonida#2622)
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