Skip to content

startup: write mesh pipeline requests on a background thread - #3664

Merged
R0Y00 merged 8 commits into
R0Y00:mainfrom
Jess-Doit:startup/mesh86-async-publish
Dec 2, 2026
Merged

R0Y00 merged 8 commits into
R0Y00:mainfrom
Jess-Doit:startup/mesh86-async-publish

Conversation

@Jess-Doit

@Jess-Doit Jess-Doit commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Refs #870, #3541.

Problem

Slot 86. Before it answers, try_cached_mesh86 publishes the request for the key:

  • four parts: .ps86, .root, .stream86 and .m86;
  • each part is a probe, a temporary file and a rename;
  • all of it happens under one function-wide lock (mesh86_cache.inl:228).

A title that creates many mesh pipelines through slot 86 waits on that file work, one creation at a time.

Measured on a package run with X2W_REFUSE_FIXED_MESH=1, where the title builds its mesh pipelines through slot 86 instead of slot 57:

  • The loading screen never ended. The run published 7,056 slot 86 mesh requests in 1,200 s, covering 3,875 distinct MS/PS/root combinations. That is about 7 a second.
  • Thread dumps (X2W_DUMP_AT) at 450, 700 and 950 s each showed 3 or 4 title worker threads queued on that lock, with one thread inside publish_shader_request.
  • The cache directory held about 100,000 files by then.

Details are in #870.

Slot 57. request_slot57_mesh (d3d12_draw.inl) does the same for each new key under its request_lock: .ps57, .root and .m57, plus .s57 with X2W_MESH57_CAPTURE_STATE=1. This is the default path. A run with the shared presets (run_preset.py --console x --gpu nvidia) stopped presenting for about 45 s during the prologue load. Stall dumps at 15 s intervals showed 4 title worker threads queued on that lock, with one thread inside publish_shader_request.

Change

  • New publisher. src/startup_stubs/request_publisher.h copies a request's parts into a queue and returns. One background thread writes them with the existing publish_shader_request.
  • Order. The parts of a request are written in the order given, and requests in the order queued.
  • Repeats. A key is queued once per process. A repeated enqueue copies nothing.
  • Bound. At most 64 MiB of parts wait at a time. A request past that is dropped and counted, and the title asks for it again in a later run.
  • Unload. A request stays at the front of the queue while it is written. At module unload, when the thread is gone, whatever is still queued is written on the unloading thread for at most 2 s. So a short run, such as the mesh cache fixtures, still leaves its request on disk.
  • No thread. If the thread cannot be created, the queuing threads write as before.
  • Both paths share one publisher. It lives next to publish_shader_request in d3d12_draw.inl. publish_mesh_request queues a request, or writes it on the calling thread under the negative control.
  • Slot 57. request_slot57_mesh still checks and updates its written set and the pipeline's stages under its lock, but it only copies the parts into the queue there. A dropped request keeps its stages and is queued again on a later call.
  • try_cached_mesh86 queues the request and then takes its lock only for the marker check and the pipeline build. Until the translator answers, the creation reports the request pending, the same answer it gave before.
  • Reporting. A periodic x2w-render mesh request publisher: line counts queued, repeated, written, failed and dropped requests, and the bytes pending. It is printed only once something has been queued.
  • Negative control. X2W_SYNC_MESH_REQUESTS=1 restores the inline writes under the locks of both paths.
  • Docs. docs/startup-runtime.md has a switch row for it.

Tests

startup_request_publisher checks the header without a device:

Case Expected
Three requests queued while the writer is held on an event all return at once (0 ms here) and nothing is written until the writer is released
Write order the parts and requests come out in queue order
A repeated key accepted and written once
Byte bound a request that would pass it is dropped and counted; the key is accepted once there is room
A failed part stops its request, which counts as failed
drain() writes the queue on the calling thread

startup_request_publisher_inline_control (--mutate inline) waits for each write, as creation did before. It must fail: it takes 1,531 ms to queue the three requests. Both are added to the pr preset.

Verification

Release build of 3f6f4ef9. Host: GTX 1060 6 GB, driver 32.0.15.6094, Windows 10 19045, debug layer off.

Command Result
ctest -R '^startup_request_publisher' 2 passed (the control fails as required)
ctest -R 'request_publisher|mesh86|mesh57|indirect_mesh|shader_cache' 97 passed, 0 failed; the mesh fixtures are capability skips
ctest with the pr preset's selection 92 passed, 0 failed; the skips are capability skips
tests/docs_links_check.py, tests/startup_run_preset_check.py, tests/bidi_controls_check.py, git diff --check pass

The mesh fixtures that publish slot 57 and slot 86 requests (startup_indirect_mesh_*) report a Mesh capability skip on this host, on both WARP and the GTX 1060. They rely on CI's WARP, which runs startup_indirect_mesh_cache_heap(_wide)?_warp in the pr preset. Those fixtures read req_*.m86 after the process exits, so they also exercise the unload drain.

Not measured yet

Whether the title's loading screen completes with X2W_REFUSE_FIXED_MESH=1 and this change. A package run on this host will follow.

🤖 Generated with Claude Code

@Jess-Doit

Copy link
Copy Markdown
Contributor Author

A package run on this branch (1dc77e0e). Host: GTX 1060 6 GB, Windows 10 19045, 22 GiB cap, X2W_REFUSE_FIXED_MESH=1, warm shader cache. These are the same settings as the run in the description, which never left the loading screen.

main (run in the description) this branch
Slot 86 mesh requests asked for 7,056 in 1,200 s, still rising 24,905 by about 594 s, then none
Rate on the loading screen about 7 a second 70–90 a second
Title threads queued on the mesh86 lock (X2W_DUMP_AT=700) 3 or 4 in every dump none
Placed resources after the loading screen starts stop at the menu count past it at about 600 s, 4,056 at 817 s

So the creation no longer waits on the file writes, and the load gets past the point where it stopped. The publisher line at the end showed 19,730 queued, 574 repeated, 16,648 written, 0 failed, 5,175 dropped and 3,082 pending. The 64 MiB bound was full at its high-water mark. Once the burst was over, the writer fell behind, at about 1 to 40 requests a second, while it shared the disk with the title's own streaming. The dropped requests are asked for again in a later run.

At 858 s the title then stopped itself: a deliberate write to address 0 from its own fatal path, not a fault in the stand-in. That happened with every mesh pipeline a placeholder, local video memory at 9.5 GiB against a 5.26 GiB budget, 32 desktop graphics pipeline creations returning null and 4 upload-ring buffer copies failing. That stop is a separate question and needs its own investigation. It does not come from this change: with slot 57 accepted, main on the same host reached the prologue and ran to the 1,200 s limit.

AI-assisted.

Ready for more review

@Jess-Doit Jess-Doit changed the title startup: write slot 86 mesh requests on a background thread startup: write mesh pipeline requests on a background thread Nov 30, 2026
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Updated to 3f6f4ef9. The slot 57 path has the same problem, so this now covers both paths:

  • request_slot57_mesh wrote .ps57, .root and .m57 for each new key under its request_lock. A run with the shared presets (below) stopped presenting for about 45 s during the prologue load. Stall dumps showed 4 title worker threads queued on that lock, with one inside publish_shader_request.
  • The publisher moved next to publish_shader_request and serves both paths through publish_mesh_request. A dropped slot 57 request keeps its stages and is queued again on a later call.
  • The negative control is renamed X2W_SYNC_MESH_REQUESTS=1 and covers both paths. The description is updated to match.
  • Release build tests: request_publisher|mesh86|mesh57|indirect_mesh|shader_cache gave 97 passed and 0 failed. The pr selection gave 92 passed and 0 failed. The mesh fixtures are capability skips on this host.

A correction to my run numbers. The runs in the description and in my previous comment were started with an older hand-written run.ps1 line, not tools/startup/run_preset.py. That line left out -SuspendOthers 0, X2W_PROBE_PRECHECK=0, X2W_D3D12_RELEASE_RETIRED=1 and the other preset settings. With the presets on this branch, same host:

  • The intro video and menus are smooth, where the old line gave choppy video and audio.
  • The loading screen appeared at 92 s instead of about 200 s.

The slot 86 finding does not depend on those settings: it is the lock around the file writes. The rate of about 7 a second was measured with the old line, though.

The 858 s stop in that comment is not specific to this change. With the presets and slot 57 accepted (main behaviour), the title reached the prologue and the device was then removed at 517 s. Details are on #870.

AI-assisted.

Ready for more review

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Read at 3f6f4ef9.

  • SHOULD The exit drain takes blocking locks that terminated threads may hold. In d3d12_xs.dll, ~MeshRequestDrain (src/startup_stubs/d3d12_draw.inl:620-625) runs at DLL_PROCESS_DETACH, after ExitProcess has terminated every other thread. drain() takes the publisher lock with TryAcquireSRWLockExclusive (src/startup_stubs/request_publisher.h:117), but each part then goes through publish_shader_request. That function calls cache_absent and note_cache_write (d3d12_draw.inl:570, :580), which acquire CacheDirectoryIndex::lock_ without a try (src/startup_stubs/cache_index.h:260, :281). The writer thread (add_hash, :281) and the change-feed thread (note_key_change, :268) hold that lock exclusively. If either was terminated while holding it, the acquire can never be granted, so the drain cannot complete and the process does not exit cleanly. Two smaller points on the same path:

    • A writer terminated between CreateFileW and CloseHandle leaves req_<key><ext>.<pid>.tmp open with share mode 0 (d3d12_draw.inl:572-574). The drain rewrites the same front request with the same name, gets a sharing violation, and counts it failed.
    • mesh_request_publisher() allocates the publisher during the drain when no mesh request was ever queued (:612-616, :623).

    An explicit flush before exit, for example flush() from a fixture export, would avoid file and lock work at detach. So would a drain that skips the cache index.

  • SHOULD The test publishers live on the stack and are never stopped. held_writer() and failed_part() (tests/startup_request_publisher_check.cpp:73, :116) return while their writer thread is still in run(), which has no exit (request_publisher.h:171-183). That thread keeps waiting on work_ and lock_ in a dead frame, with context_ pointing to a destroyed Writer. In the stand-in the publisher is leaked on purpose, so this affects only the test. A stop-and-join path, or leaked test instances, would remove the undefined behaviour.

  • NIT drain() assumes the writer thread is already gone. That holds in the DLL. It does not hold in an executable that links d3d12_xs_device.cpp directly (offscreen_check, CMakeLists.txt:2247): there, static destructors run from exit() while the writer may be inside write_front with the lock released. Two threads would then pop the same queue (request_publisher.h:150-162).

  • NIT A failed write is no longer retried in the same process. seen_ is set at enqueue (request_publisher.h:77), and slot 57 marks the key written as soon as it is queued (d3d12_draw.inl:1136-1137). Before this change, both slots retried a failed publication on the next creation.

The branch conflicts with main on CMakePresets.json, so no CI has run at this head.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The current head conflicts with main in CMakePresets.json. Please merge or rebase onto current main and keep both the existing PR preset cases and the request publisher target and controls. Once CI can run, the review can verify that publishing stays off the creation thread and that the inline control observes the same completed request.

@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks, both. Rebased onto main a3bfd019 and squashed into one commit (6752ce8d). The pr preset keeps main's cases and adds the publisher target and its controls. On top of that, d6988e4b addresses the review:

  • Exit drain: it no longer goes through publish_shader_request. It writes with its own unload writer: a .<pid>.exit.tmp name of its own, then a rename. There is no cache index, so no CacheDirectoryIndex::lock_, and it takes the cache directory captured when the publisher was made, so no environment read. It runs only once the writer thread has ended. While the thread still runs, as with an executable's static destructors, it waits for the thread instead, so the queue is never popped twice. Nothing is allocated at unload when no request was ever queued.
  • Test publishers: they now stop and join. stop() ends the thread after its current request, the destructor calls it, and the thread is created suspended so its handle is always known.
  • Retry: a request that could not be written forgets its key, so slot 86 queues it again on the next creation. Slot 57 releases a request's stages once it is queued, so a failed slot 57 write comes back only in a later run. That is stated in the code comment.
  • Tests: the check now also covers the retry, the unload writer, a drain while the thread runs, and a stopped publisher. Locally: passes 30 out of 30 runs with no hang, and the inline control still fails (1,547 ms to queue three requests).

AI-assisted.

Ready for more review

@Jess-Doit
Jess-Doit force-pushed the startup/mesh86-async-publish branch from 3f6f4ef to d6988e4 Compare December 1, 2026 00:59

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I reproduced Windows CI's C2664 on this head with MSVC 14.51. All five std::begin({RequestPublisher::Part{...}}) calls in the fixture fail to compile. Passing the address of a named const Part and a count of 1 fixes the build in an isolated probe. The normal test passed, and --mutate inline failed its held-writer assertion as expected.

Please also keep seen_ and queue_ consistent on enqueue failure. If queue_.push_back throws after seen_.insert succeeds, the catch reports a drop but leaves the key in seen_. A retry then reports success without publishing the request. Roll back the key on this failure and add a retry control.

Comment thread src/startup_stubs/request_publisher.h Resolved

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Retested 9efad1d2 with MSVC 14.51. The named-part fix builds, the request publisher test passes, and the inline mutation fails the held-writer control as expected.

I reproduced the retry issue on this head by injecting std::bad_alloc after three successful allocations during the first enqueue. The call reported a drop, then a retry of the same key returned success with queued=0. With seen_.erase(key) in the enqueue catch under the existing lock, all 205 injected allocation-failure retries in my probe queued successfully. Please add that rollback and a retry control. The current main merge still conflicts in CMakePresets.json.

@Jess-Doit
Jess-Doit force-pushed the startup/mesh86-async-publish branch from 9efad1d to 9ceb1f4 Compare December 1, 2026 03:48
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks for the probe. Pushed 9ceb1f44:

  • enqueue now erases the key in its catch, under the same lock, so a copy that runs out of memory leaves the key unrecorded and a retry is queued.
  • tests/startup_request_publisher_check.cpp adds allocation_failure_retry. A per-thread operator new hook in the test fails each allocation of an enqueue in turn (the writer thread is unaffected), from the first allocation until the enqueue no longer fails. Each failure must count one drop, and the retry of the same key must be queued (not counted as repeated) and later written.
  • Control: with the seen_.erase(key) line removed, that case fails with FAIL a retry after an allocation failure queues the request; with it, all cases pass. The inline mutation still fails the held-writer case as before.
  • Rebased on current main (1c634037). The CMakePresets.json conflict kept main's list and appended startup_request_publisher(_inline_control)?.

Release build of d3d12_xs_shim and startup_request_publisher_check on Windows 10 19045, MSVC 2022 Build Tools:

ctest --preset release --no-tests=error -R '^startup_request_publisher'
1/2 startup_request_publisher ................ Passed
2/2 startup_request_publisher_inline_control . Passed (WILL_FAIL)

Ready for more review

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The current head erases the deduplication key under the enqueue lock and adds retry coverage. The MSVC 14.51 /O2 fixture passed, its inline mutant failed the held-writer assertion, and my independent fault-injection probe queued all 205 retries. This resolves my change request.

@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks for the retest and the independent fault-injection probe.

Ready to merge from our side

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The enqueue rollback passes, but the full integration exposed a Slot 57 regression. startup_mesh57_runtime_warp failed on the first run twice with the async default, and passed 3/3 with X2W_SYNC_MESH_REQUESTS=1 on the same binaries. In tests/startup_indirect_draw_indexed.cpp the exchange fixture enumerates the cache immediately after the dispatches and requires exactly two requests and six files. Please make the async fixture wait for both complete request markers with a bounded timeout before checking the existing exact bytes and file counts. Keep the async default in that coverage.

@Jess-Doit
Jess-Doit force-pushed the startup/mesh86-async-publish branch from 9ceb1f4 to b64d3b0 Compare December 1, 2026 09:04
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks, reproduced. Pushed b64d3b06 (rebased on a061032f).

tests/startup_indirect_draw_indexed.cpp: after the dispatches, the exchange fixture now waits, for at most 10 s, until both .m57 ready markers exist and no part is still under its .tmp name, then runs the unchanged exact byte, root and count checks (two requests, one with a PS, six files). The async default is kept; a timeout prints FAIL: the two slot 57 requests were not complete within 10 s.

Measured on Windows 10 19045, MSVC 2022 Build Tools, app-local WARP 1.0.22 with Agility SDK 1.620.2 (a separate build with OFFSCREEN_AGILITY_620_DIR and OFFSCREEN_WARP_DIR; the in-box WARP here has no mesh shaders, so startup_mesh57_runtime_warp skips with 77 there), async default:

  • with the wait: the request half printed slot 57 MS+PS and MS-only requests preserved stages and the matching root in 10 of 10 runs;
  • control, the same binary with the wait removed: 0 of 10.

On this host the test then fails later, at published pair without opt-in: runtime fixture exited 3 with a D3D12 debug-layer warning or error. That happens with X2W_SYNC_MESH_REQUESTS=1 as well, so it does not depend on the publisher, and I take it as specific to this WARP/Agility/Windows 10 combination. Your run on your host is the check for that half.

Ready for more review

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Re-tested this exact head with X2W_SYNC_MESH_REQUESTS=0. Both startup_request_publisher tests passed. startup_mesh57_runtime_warp passed once in the integrated suite and three consecutive repeats, and startup_mesh57_runtime_hardware passed on RTX 4060 Laptop (616.56, Windows build 26300). The bounded wait covers both ready markers and preserves the existing count and byte checks.

@R0Y00
R0Y00 enabled auto-merge December 1, 2026 11:22
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

Thanks for the retest on both adapters.

Ready to merge from our side

Jess-Doit and others added 5 commits December 1, 2026 12:34
Slot 57 and slot 86 mesh pipeline creations wrote each new key's request
parts (a probe, a temporary file and a rename each) under one lock before
they answered. A title that creates thousands of mesh pipelines then
waits on that file work one creation at a time.

Creations now copy the parts into a bounded queue and return; one thread
writes them in order (request_publisher.h). A key is queued once per
process and at most 64 MiB wait. X2W_SYNC_MESH_REQUESTS=1 restores the
inline writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he writer

The unload drain writes with its own writer (a temporary name of its
own, a rename, no cache index) from a directory read when the publisher
is made, and only once the writer thread has ended; while it still runs
the drain waits for it. Nothing is made at unload when no request was
queued. stop() ends and joins the thread (the destructor calls it), and
a request that could not be written forgets its key so it is accepted
again. The check covers the retry, the unload writer, a drain while the
thread runs and stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… on a brace list

Another MSVC version resolves std::begin({...}) to the array overload and
rejects it (C2664), which failed the Windows build on CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…memory

enqueue recorded the key before pushing the request. If the push threw, the key
stayed recorded, so a retry of the same key was counted as repeated and never
written. The catch now erases the key under the same lock.

The publisher test fails each allocation of an enqueue in turn and checks that
the drop is counted and that a retry of the same key is queued and written.
Without the rollback that case fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The request fixture enumerated the shader cache right after its dispatches.
With the background publisher a request can still be in flight then, so the
exact count of two requests and six files failed intermittently
(startup_mesh57_runtime_warp). It now waits, for at most 10 s, until both
.m57 ready markers exist and no part is still under its temporary name, then
runs the same byte and count checks. The async default is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
auto-merge was automatically disabled December 1, 2026 20:42

Head branch was pushed to by a user without write access

@Jess-Doit
Jess-Doit force-pushed the startup/mesh86-async-publish branch from b64d3b0 to 534b3ee Compare December 1, 2026 20:42
…ailures

The Debug build (the dev preset) allocates an iterator proxy inside noexcept
container constructors. The first injected failure landed there, so the test
called std::terminate and the CRT's modal report waited until the CI job's
30-minute limit. Those allocations are now neither failed nor counted.

Debug build, cl /Od /MDd: 0 cases failed; --mutate inline still fails (1).
Release: startup_request_publisher and its inline control pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jess-Doit

Copy link
Copy Markdown
Contributor Author

The Windows job was timing out at 30 minutes, which was a hang in the new startup_request_publisher test under the Debug (dev) build. The out-of-memory injection made the next operator new throw, and in Debug that call can be the iterator proxy that MSVC containers allocate inside noexcept constructors. The throw then reached std::terminate, and the CRT's modal report dialog waited until the job limit. Release builds never allocate the proxy, so they passed locally.

  • 1319a11: the test's operator new neither fails nor counts allocations of sizeof(std::_Container_proxy) when _ITERATOR_DEBUG_LEVEL != 0. The publisher code is unchanged.
  • Also rebased onto current main (no conflicts).

Checks:

  • Debug (cl /Od /MDd): all cases pass, and --mutate inline still fails as expected.
  • Release: startup_request_publisher and _inline_control pass.
  • CI on 1319a11: Windows build and test, gfx1013 fixtures, and Linux Python checks all pass.

Ready to merge from our side

R0Y00 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Pushed merge resolution e6e92bd0 to this PR's maintainer-editable branch, preserving the original commits and both main's shared-heap and this PR's publisher targets and filters. git merge-tree --write-tree review-latest HEAD succeeds against main 2b870868. Compiled startup_request_publisher_check.cpp with MSVC /O2 and ran the fixture: pass, with 0 ms to queue three requests while the writer was blocked. The --mutate inline control returned 1 as expected. The integrated Windows run is pending on the new head.

R0Y00 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

After #3770 landed, b9288115 resolves the new preset overlap and keeps the shared-heap, typed-V# and request-publisher targets and filters. No publisher source changed in this update. The original PR head now needs its new integrated CI run.

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.

3 participants