fix(gpu): serialize SetPipelineState against pipeline creation and library calls #3345

Open
rdj415 wants to merge 2 commits from upstream-pr/3345 into main
rdj415 commented 2026-12-03 14:58:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by rdj415 on 2026-12-03T14:58:45Z.


Claimed in the coordination issue as: D3D12-PIPELINE-STATE-LOCK (https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7382304892)

Fixes: #3327

What and why

Serializes ID3D12GraphicsCommandList::SetPipelineState (and external FrameGen passes: dlss::record, fsr::before_present, fsr::generate) against concurrent SetPipelineState calls and parallel Create*PipelineState / Load*Pipeline / StorePipeline / ffx.create work via a writer-preferring PipelineLock (LEONIDA_SERIAL_SET_PIPELINE, enabled unless 0). Prevents nvwgf2umx.dll concurrent pipeline-cache heap corruption (0xC0000374) without starving render threads behind background shader compiles, and regenerates d3d12/docs/switches.md.

Tested

  • python -I scripts/check_no_game_data.py (pass)
  • python -I tests/test_d3d12_switches.py (21 passed)
  • cargo clippy -p leonida-gpu -p leonida --all-targets --locked and --features diagnostics,mimalloc --locked
  • cargo test -p leonida-gpu --locked (720 passed, 59 ignored)
  • cargo test -p leonida-gpu --features diagnostics --locked --lib host::tests (73 passed, 1 ignored)
  • cargo test -p leonida-gpu --features diagnostics --test replay --locked (1 passed)

GPUs tested: NVIDIA GeForce RTX 5060 Ti 16 GB, driver 616.64, Windows 11 (runtime), plus WARP/DX12 test suite

AI-assisted: yes (implementation and independent read-only reviewer subagent audit)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5629002576 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3345). Originally posted by [rdj415](https://github.com/rdj415) on 2026-12-03T14:58:45Z. --- Claimed in the coordination issue as: D3D12-PIPELINE-STATE-LOCK (https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7382304892) Fixes: #3327 ### What and why Serializes `ID3D12GraphicsCommandList::SetPipelineState` (and external FrameGen passes: `dlss::record`, `fsr::before_present`, `fsr::generate`) against concurrent `SetPipelineState` calls and parallel `Create*PipelineState` / `Load*Pipeline` / `StorePipeline` / `ffx.create` work via a writer-preferring `PipelineLock` (`LEONIDA_SERIAL_SET_PIPELINE`, enabled unless `0`). Prevents `nvwgf2umx.dll` concurrent pipeline-cache heap corruption (`0xC0000374`) without starving render threads behind background shader compiles, and regenerates `d3d12/docs/switches.md`. ### Tested - `python -I scripts/check_no_game_data.py` (pass) - `python -I tests/test_d3d12_switches.py` (21 passed) - `cargo clippy -p leonida-gpu -p leonida --all-targets --locked` and `--features diagnostics,mimalloc --locked` - `cargo test -p leonida-gpu --locked` (720 passed, 59 ignored) - `cargo test -p leonida-gpu --features diagnostics --locked --lib host::tests` (73 passed, 1 ignored) - `cargo test -p leonida-gpu --features diagnostics --test replay --locked` (1 passed) GPUs tested: NVIDIA GeForce RTX 5060 Ti 16 GB, driver 616.64, Windows 11 (runtime), plus WARP/DX12 test suite AI-assisted: yes (implementation and independent read-only reviewer subagent audit) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
merzleckeier commented 2026-12-03 15:01:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T15:01:30Z.


I checked the lock tests at d4a8d3a7 with synthetic Rust threads. The current lock passes. Two test changes would make the regressions visible and keep CI from hanging.

  • In pipeline_lock_prefers_waiting_set_over_new_creates, spawning h_c2 does not prove that the second reader reached the lock before the first reader was released. I removed the pending-writer check in a local test fixture and confirmed that the test still passed when the second reader was scheduled after the writer. With a signal at the second reader’s first lock decision while the first reader is still held, the current lock passes and the no-priority variant fails.
  • In pipeline_lock_excludes_create_during_set_and_allows_parallel_creates, the barrier inside both readers hangs if a regression makes reads exclusive. A local exclusive-reader fixture reached the process timeout. A bounded channel wait inside the first reader would release the lock when the second reader fails to enter and turn this into an assertion failure.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7382432872 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3345#issuecomment-7382432872). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T15:01:30Z. --- I checked the lock tests at `d4a8d3a7` with synthetic Rust threads. The current lock passes. Two test changes would make the regressions visible and keep CI from hanging. - In `pipeline_lock_prefers_waiting_set_over_new_creates`, spawning `h_c2` does not prove that the second reader reached the lock before the first reader was released. I removed the pending-writer check in a local test fixture and confirmed that the test still passed when the second reader was scheduled after the writer. With a signal at the second reader’s first lock decision while the first reader is still held, the current lock passes and the no-priority variant fails. - In `pipeline_lock_excludes_create_during_set_and_allows_parallel_creates`, the barrier inside both readers hangs if a regression makes reads exclusive. A local exclusive-reader fixture reached the process timeout. A bounded channel wait inside the first reader would release the lock when the second reader fails to enter and turn this into an assertion failure. AI-assisted with OpenAI Codex.
rdj415 commented 2026-12-03 15:07:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by rdj415 on 2026-12-03T15:07:42Z.


Thanks for the sharp review! Addressed both test points in c5f911bb5: replaced Barrier::new(2) with a bounded mpsc::recv_timeout(2s) check so exclusive-read regressions fail instead of hanging, and gated h1 release on waiting_readers > 0 || create2_entered so create2 is verified to hit the lock decision while h1 is held.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7382609250 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3345#issuecomment-7382609250). Originally posted by [rdj415](https://github.com/rdj415) on 2026-12-03T15:07:42Z. --- Thanks for the sharp review! Addressed both test points in c5f911bb5: replaced Barrier::new(2) with a bounded mpsc::recv_timeout(2s) check so exclusive-read regressions fail instead of hanging, and gated h1 release on waiting_readers > 0 || create2_entered so create2 is verified to hit the lock decision while h1 is held.
merzleckeier commented 2026-12-03 15:12:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T15:12:08Z.


Re-tested c5f911bb with the exact lock and its three synthetic concurrency tests. All three passed. An exclusive-read variant now fails with “second read() blocked while first read() was held” in two seconds. The writer-priority test passed 100/100 on this head and failed 100/100 with the pending_writers check removed. Both of my test findings are addressed. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7382737166 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3345#issuecomment-7382737166). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T15:12:08Z. --- Re-tested `c5f911bb` with the exact lock and its three synthetic concurrency tests. All three passed. An exclusive-read variant now fails with “second read() blocked while first read() was held” in two seconds. The writer-priority test passed 100/100 on this head and failed 100/100 with the `pending_writers` check removed. Both of my test findings are addressed. AI-assisted with OpenAI Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3345:upstream-pr/3345'
git switch 'upstream-pr/3345'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3345'
git switch 'upstream-pr/3345'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3345'
git switch 'upstream-pr/3345'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3345'
git switch 'main'
git merge --squash 'upstream-pr/3345'
git switch 'main'
git merge --ff-only 'upstream-pr/3345'
git switch 'main'
git merge 'upstream-pr/3345'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!3345
No description provided.