Skip to content

runtime: unlock resource_mutex while compiling unbuilt pipelines - #1742

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
IlyassRS3:fix/pipeline-compile-unlock-resources
Nov 25, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
IlyassRS3:fix/pipeline-compile-unlock-resources

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Item ID: RT-PIPELINE-COMPILE-UNLOCK-RESOURCES
Claim: #1608 (comment)
Addresses finding: #1608 (comment) (render thread stalled on VulkanContext::remove_resource -> resource_mutex_ behind 2–5 s compute pipeline compiles at world start, tripping the title's hang watchdog)

Root cause

VulkanContext previously guarded both resources (resources_, buffer_vas_, query_pools_, add_resource() / remove_resource()) and registered pipelines (pipelines_, graphics_pipelines_, pipeline_workers_, device_fault_->pipeline_names) under one std::shared_mutex resource_mutex_.

VulkanQueue::execute_now() (runtime/src/vulkan_backend.h) held std::shared_lock(resource_mutex_) across prepare() and build_pipelines() to keep PipelineState objects alive during compilation. Whenever a queue compiled unbuilt world shaders (2–5 s each in vkCreateComputePipelines), VulkanContext::remove_resource() / add_resource() on the title's render thread blocked on std::unique_lock(resource_mutex_), creating a lock convoy that also stalled other queues and VulkanPresenter until the hang watchdog fired.

Changes

  • runtime/src/vulkan_backend.h:
    • Added VulkanContext::pipeline_mutex_ (std::shared_mutex) to guard pipelines_, graphics_pipelines_, pipeline_workers_, and device_fault_->pipeline_names independently of resource_mutex_, keeping a strict lock hierarchy: state_mutex_ -> pipeline_mutex_ -> resource_mutex_ -> tracker_mutex_ -> submit_mutex_.
    • Updated VulkanQueue::execute_now() to hold pipeline_lock across prepare() / compilation / emit(), always collect unbuilt pipelines via PipelineCompiles (both with pipeline_workers_ and in serial mode), and on pass 0 when !compiles.empty() retire uncommitted pass-0 helper state and release tracker_lock and resource_lock before running compiles.build(...) under pipeline_lock alone. Pass 1 then re-acquires resource_lock and re-prepares against current resources so no stale VkBuffer/VkImage handle can survive a concurrent resource update.
    • Zero extra passes on the warm path (compiles.empty()).
  • runtime/src/vulkan_replay.h:
    • Added PipelineCompiles::lookup() / contains_ticket(), optional prior parameter to PipelineCompiles::build() and build_pipelines(), and shared check_compiled_pipelines() so pass-1 re-preparation reuses prior outcomes (preventing double build() calls or duplicate unresolved_at logs on non-strict failed compute dispatches).
  • runtime/test/replay_pipeline_compiles_test.cpp, runtime/test/replay_pipeline_compile_unlock_test.cpp, runtime/CMakeLists.txt:
    • Extended replay_pipeline_compiles_test with prior_compiles_reuse covering pass-1 reuse of built and non-strict failed compute dispatches.
    • Added CPU mock concurrency test replay_pipeline_compile_unlock_test verifying (both with PipelineWorkers(2) and serial pipeline_workers_ == nullptr) that add_resource(), remove_resource(), and a second VulkanQueue::execute() complete while a slow compile is blocked in build(), that remove_pipeline() safely waits on pipeline_mutex_ until compilation ends, that concurrent resource replacement is picked up on pass 1 (vkCmdCopyBuffer binds the new VkBuffer), and that a non-strict failed compute dispatch invokes build() only once.

Verification

  • AI-assisted: yes
  • GPUs tested: none (developed on macOS without Vulkan::Headers installed)
  • Tested locally:
    • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_replay.h runtime/src/vulkan_backend.h runtime/test/replay_pipeline_compiles_test.cpp runtime/test/replay_pipeline_compile_unlock_test.cpp runtime/CMakeLists.txt: PASS.
    • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
  • Not compiled/executed locally: runtime/src/vulkan_backend.h, runtime/src/vulkan_replay.h, runtime/test/replay_pipeline_compiles_test.cpp, and runtime/test/replay_pipeline_compile_unlock_test.cpp require Vulkan::Headers (not present on this macOS host) and rely on CI (replay_pipeline_compiles, replay_pipeline_compiles_strict, replay_pipeline_compile_unlock, and existing vk_queue_* / vk_presenter_sync tests).

Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@abnalstlll-byte abnalstlll-byte 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.

Clean-Room & Technical Verification

  • Mergeability: 3-way merge against upstream main (bcaa13dad) is 100% clean with zero merge conflicts.
  • Clean-Room Compliance: 100% clean-room. Vulkan runtime concurrency architecture and synthetic mock tests only; zero retail code, game data, or keys (check_no_game_data.py clean).
  • Technical Rigor & Concurrency Design:
    • Introduces independent pipeline_mutex_ (std::shared_mutex) to protect pipeline registry and compilation tasks, decoupling them from resource_mutex_.
    • In VulkanQueue::execute_now(), releases resource_lock and tracker_lock before executing long-running compiles.build(...) passes, completely unblocking render-thread resource registration (add_resource / remove_resource) and avoiding lock convoys that previously triggered the title's hang watchdog during world start shader compilation.
    • On pass 1, safely re-acquires resource_lock and re-prepares submissions against up-to-date resources while leveraging prior compilation outcomes in PipelineCompiles, preventing duplicate compilations or spurious failure logs.
    • Zero overhead on warm paths (compiles.empty()).
    • Tested via synthetic mock suite runtime/test/replay_pipeline_compile_unlock_test.cpp and replay_pipeline_compiles_test.cpp.

LGTM! Approved.

@IlyassRS3
IlyassRS3 force-pushed the fix/pipeline-compile-unlock-resources branch from 5238021 to 92a2a93 Compare November 25, 2026 11:45
@adrianmjim

Copy link
Copy Markdown

Built 5238021a2 applied onto its base bcaa13dad (the current head 92a2a93c5 is a rebase of it, and the two test compile errors below are still in it) on Windows 11 build 26300 with VS 2022 (MSVC 19.44) RelWithDebInfo, and ran it on an AMD Radeon RX 7900 XT (driver 32.0.32029.2010). No game files.

1. The two tests don't compile. The runtime itself builds, but the tests stop with:

replay_pipeline_compile_unlock_test.cpp(162,57): error C2653: 'QueueKind': is not a class or namespace name
replay_pipeline_compile_unlock_test.cpp(277,5): error C2065: 'unresolved': undeclared identifier
replay_pipeline_compiles_test.cpp(570,35): error C2065: 'unresolved': undeclared identifier

QueueKind is nested in VulkanQueue (vulkan_backend.h, class VulkanQueue { enum class QueueKind ... }), and ReplayResult has unresolved_resource, not unresolved. With those two renames (VulkanQueue::QueueKind::compute, ReplayResult::unresolved_resource), everything builds. All the results below are with those renames applied locally.

2. The new tests pass. replay_pipeline_compile_unlock passes 5 of 5, and replay_pipeline_compiles and replay_pipeline_compiles_strict pass too. So do vk_queue, vk_queue_query_sync, vk_presenter_sync, vk_queue_constructor_oom, d3d12_pipeline_workers_eager, d3d12_stub_pipelines, vulkan_device_fault and replay_cpu_bench_selftest.

3. A regression: d3d12_pipeline_workers fails, 3 of 3. On the base it passes 3 of 3, built from the same tree with the PR taken out.

FAIL runtime/test/d3d12_pipeline_workers_test.cpp:688: hooks.creates == (workers ? int(kPipelines) : 5)

That is refused_graphics, serial case (threads "0"). When the driver refuses the 5th graphics compile, the base stops there: five creates, then unsupported. With this PR, execute_now() always collects the unbuilt pipelines through PipelineCompiles, also without workers, so a compile of every one of them is attempted before the refusal is acted on.

So in serial mode a refused compile no longer cuts the pass short. The rest of the submission's pipelines are compiled first, as the workers path already did, so a failing submission takes longer before it is reported unsupported; compute pipelines can take 2-5 s each, per the description. The pipelines that did build are kept, so this moves the work earlier more than it adds work, but it is still a change for the serial path.

The description says collecting through PipelineCompiles without workers is intended. If the resulting refusal behaviour is intended too, the serial expectations in refused_graphics need updating: line 688 (5 creates), and probably line 692 (kPipelines - 4 on the retry), which the failed check at 688 kept from running. If not, the serial path should stop at the first refused compile as before.

Whole ctest -j 6 (with the two renames): 382 of 386 pass or skip. The failures are d3d12_pipeline_workers (above), plus d3d12_texture_tiles, d3d12_clear and d3d12_geometry_views, which fail on this card on main too.

Address Windows MSVC / RX 7900 XT review findings on ProjectLeonida#1742:
- Qualify VulkanQueue::QueueKind::compute and ReplayResult::unresolved_resource
  in replay_pipeline_compile_unlock_test.cpp and replay_pipeline_compiles_test.cpp.
- Add PipelineCompiles::build_serial() / build_pipelines_serial() so the
  no-workers serial path still compiles outside resource_mutex_ while stopping
  at the first fatal refusal (preserving d3d12_pipeline_workers::refused_graphics
  serial expectations and avoiding wasted compiles after a failed draw).
- Add serial_stops_at_refused_draw() unit coverage in replay_pipeline_compiles_test.cpp.
@IlyassRS3

Copy link
Copy Markdown
Contributor Author

Thank you @adrianmjim for the MSVC / RX 7900 XT build and ctest run — super helpful!

Both items are now fixed and pushed (rebased on main 0d80bc5df):

  1. Test compile errors: qualified VulkanQueue::QueueKind::compute in replay_pipeline_compile_unlock_test.cpp and renamed ReplayResult::unresolved -> ReplayResult::unresolved_resource in both replay_pipeline_compile_unlock_test.cpp and replay_pipeline_compiles_test.cpp.
  2. Serial d3d12_pipeline_workers::refused_graphics regression: added PipelineCompiles::build_serial() / build_pipelines_serial() and routed the !context_.pipeline_workers_ path through it. Serial mode still releases resource_mutex_ and tracker_mutex_ across compilation, but compiles in command order and stops immediately at the first fatal refusal (a failed draw, failed indirect dispatch, or failed dispatch under strict_replay()), preserving refused_graphics's 5-create / kPipelines - 4 serial expectations without spending extra compile time after a fatal refusal. Also added serial_stops_at_refused_draw() to replay_pipeline_compiles_test.cpp.

@aslan2142

Copy link
Copy Markdown
Collaborator

Thank you, @IlyassRS3: the render thread no longer waits behind shader compiles for resource_mutex_.

AI-assisted (Claude Code, Claude Opus 5.5).

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Intel integration verification of the merged fix: Windows 11, MSVC RelWithDebInfo, Intel UHD Graphics / i5-1334U, driver 32.0.101.6314. Integrated main 3657294 into an existing private runtime tree; no changes to this PR's logic were needed.

14/14 targeted tests passed with LEONIDA_VK_VALIDATION=1: replay_pipeline_compile_unlock, replay_pipeline_compiles and strict, d3d12_pipeline_workers and eager, vk_queue, vk_queue_query_sync, vk_presenter_sync, vk_queue_constructor_oom, vulkan_device_fault, xbox_bvh_builder, d3d12_unbound_tsharp_rules, d3d12_compute, and d3d12_m2_textured. This includes the serial-refusal behavior raised in the earlier review.

This is a targeted combined-tree result, not a full Windows-suite or playability claim. No game data included. AI-assisted with OpenAI Codex.

aslan2142 added a commit to TheGeogeo/leonida that referenced this pull request Nov 25, 2026
Brings ProjectLeonida#1889 up to date with main after ProjectLeonida#1742 (the pipeline compile unlock),
resolving the five conflicts as described in the guide on the PR:
ProjectLeonida#1889 (comment)

- vulkan_replay.h: PipelineCompiles::build takes both the large-compile
  budget and main's `prior` compiles; build_pipelines passes the budget,
  and main's check_compiled_pipelines / finish_compiled_pipelines stay.
- vulkan_backend.h: execute_now takes the large-compile demand and passes
  the budget to the pass-0 compile, and pass 1 passes the budget too.
  set_large_compiles locks pipeline_mutex_, since ProjectLeonida#1742 moved the
  pipeline state under that lock.
- replay_pipeline_compiles_test.cpp: both test blocks kept, and the
  prior_compiles_reuse call passes an empty budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

6 participants