startup: write mesh pipeline requests on a background thread #3664
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3664"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-30T22:18:52Z.
Refs #870, #3541.
Problem
Slot 86. Before it answers,
try_cached_mesh86publishes the request for the key:.ps86,.root,.stream86and.m86;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:X2W_DUMP_AT) at 450, 700 and 950 s each showed 3 or 4 title worker threads queued on that lock, with one thread insidepublish_shader_request.Details are in #870.
Slot 57.
request_slot57_mesh(d3d12_draw.inl) does the same for each new key under itsrequest_lock:.ps57,.rootand.m57, plus.s57withX2W_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 insidepublish_shader_request.Change
src/startup_stubs/request_publisher.hcopies a request's parts into a queue and returns. One background thread writes them with the existingpublish_shader_request.publish_shader_requestind3d12_draw.inl.publish_mesh_requestqueues a request, or writes it on the calling thread under the negative control.request_slot57_meshstill checks and updates itswrittenset 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_mesh86queues 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.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.X2W_SYNC_MESH_REQUESTS=1restores the inline writes under the locks of both paths.docs/startup-runtime.mdhas a switch row for it.Tests
startup_request_publisherchecks the header without a device:drain()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 theprpreset.Verification
Release build of
3f6f4ef9. Host: GTX 1060 6 GB, driver 32.0.15.6094, Windows 10 19045, debug layer off.ctest -R '^startup_request_publisher'ctest -R 'request_publisher|mesh86|mesh57|indirect_mesh|shader_cache'ctestwith theprpreset's selectiontests/docs_links_check.py,tests/startup_run_preset_check.py,tests/bidi_controls_check.py,git diff --checkThe 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 runsstartup_indirect_mesh_cache_heap(_wide)?_warpin theprpreset. Those fixtures readreq_*.m86after 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=1and this change. A package run on this host will follow.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-30T22:35:31Z.
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)X2W_DUMP_AT=700)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,
mainon the same host reached the prologue and ran to the 1,200 s limit.AI-assisted.
Ready for more review
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-30T23:52:41Z.
Updated to
3f6f4ef9. The slot 57 path has the same problem, so this now covers both paths:request_slot57_meshwrote.ps57,.rootand.m57for each new key under itsrequest_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 insidepublish_shader_request.publish_shader_requestand serves both paths throughpublish_mesh_request. A dropped slot 57 request keeps its stages and is queued again on a later call.X2W_SYNC_MESH_REQUESTS=1and covers both paths. The description is updated to match.request_publisher|mesh86|mesh57|indirect_mesh|shader_cachegave 97 passed and 0 failed. Theprselection 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.ps1line, nottools/startup/run_preset.py. That line left out-SuspendOthers 0,X2W_PROBE_PRECHECK=0,X2W_D3D12_RELEASE_RETIRED=1and the other preset settings. With the presets on this branch, same host: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 (
mainbehaviour), the title reached the prologue and the device was then removed at 517 s. Details are on #870.AI-assisted.
Ready for more review
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-01T00:04:10Z.
Review: COMMENTED
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 atDLL_PROCESS_DETACH, afterExitProcesshas terminated every other thread.drain()takes the publisher lock withTryAcquireSRWLockExclusive(src/startup_stubs/request_publisher.h:117), but each part then goes throughpublish_shader_request. That function callscache_absentandnote_cache_write(d3d12_draw.inl:570,:580), which acquireCacheDirectoryIndex::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:CreateFileWandCloseHandleleavesreq_<key><ext>.<pid>.tmpopen 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()andfailed_part()(tests/startup_request_publisher_check.cpp:73,:116) return while their writer thread is still inrun(), which has no exit (request_publisher.h:171-183). That thread keeps waiting onwork_andlock_in a dead frame, withcontext_pointing to a destroyedWriter. 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 linksd3d12_xs_device.cppdirectly (offscreen_check,CMakeLists.txt:2247): there, static destructors run fromexit()while the writer may be insidewrite_frontwith 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 keywrittenas 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
mainonCMakePresets.json, so no CI has run at this head.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T00:14:11Z.
Review: COMMENTED
The current head conflicts with
maininCMakePresets.json. Please merge or rebase onto currentmainand 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.Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T00:59:48Z.
Thanks, both. Rebased onto
maina3bfd019and squashed into one commit (6752ce8d). Theprpreset keeps main's cases and adds the publisher target and its controls. On top of that,d6988e4baddresses the review:publish_shader_request. It writes with its own unload writer: a.<pid>.exit.tmpname of its own, then a rename. There is no cache index, so noCacheDirectoryIndex::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.stop()ends the thread after its current request, the destructor calls it, and the thread is created suspended so its handle is always known.AI-assisted.
Ready for more review
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T01:12:32Z.
Review: CHANGES_REQUESTED
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 namedconst Partand a count of 1 fixes the build in an isolated probe. The normal test passed, and--mutate inlinefailed its held-writer assertion as expected.Please also keep
seen_andqueue_consistent on enqueue failure. Ifqueue_.push_backthrows afterseen_.insertsucceeds, the catch reports a drop but leaves the key inseen_. A retry then reports success without publishing the request. Roll back the key on this failure and add a retry control.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T01:12:32Z.
Code review location:
src/startup_stubs/request_publisher.h; line 82An allocation failure here leaves the key in
seen_. The next enqueue of the same key returns success before it reaches the queue. Roll the insert back if this push fails.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T03:10:02Z.
Review: COMMENTED
Retested
9efad1d2with 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_allocafter three successful allocations during the firstenqueue. The call reported a drop, then a retry of the same key returned success withqueued=0. Withseen_.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 currentmainmerge still conflicts inCMakePresets.json.Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T03:49:07Z.
Thanks for the probe. Pushed
9ceb1f44:enqueuenow 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.cppaddsallocation_failure_retry. A per-threadoperator newhook 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.seen_.erase(key)line removed, that case fails withFAIL 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.main(1c634037). TheCMakePresets.jsonconflict kept main's list and appendedstartup_request_publisher(_inline_control)?.Release build of
d3d12_xs_shimandstartup_request_publisher_checkon Windows 10 19045, MSVC 2022 Build Tools:Ready for more review
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T08:43:43Z.
Review: APPROVED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T08:44:39Z.
Thanks for the retest and the independent fault-injection probe.
Ready to merge from our side
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T08:48:03Z.
Review: CHANGES_REQUESTED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T09:04:50Z.
Thanks, reproduced. Pushed
b64d3b06(rebased ona061032f).tests/startup_indirect_draw_indexed.cpp: after the dispatches, the exchange fixture now waits, for at most 10 s, until both.m57ready markers exist and no part is still under its.tmpname, 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 printsFAIL: 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_DIRandOFFSCREEN_WARP_DIR; the in-box WARP here has no mesh shaders, sostartup_mesh57_runtime_warpskips with 77 there), async default:slot 57 MS+PS and MS-only requests preserved stages and the matching rootin 10 of 10 runs;On this host the test then fails later, at
published pair without opt-in: runtime fixture exited 3with a D3D12 debug-layer warning or error. That happens withX2W_SYNC_MESH_REQUESTS=1as 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
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T11:21:44Z.
Review: APPROVED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T11:24:30Z.
Thanks for the retest on both adapters.
Ready to merge from our side
Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-12-01T21:47:20Z.
The Windows job was timing out at 30 minutes, which was a hang in the new
startup_request_publishertest under the Debug (dev) build. The out-of-memory injection made the nextoperator newthrow, and in Debug that call can be the iterator proxy that MSVC containers allocate insidenoexceptconstructors. The throw then reachedstd::terminate, and the CRT's modal report dialog waited until the job limit. Release builds never allocate the proxy, so they passed locally.1319a11f: the test'soperator newneither fails nor counts allocations ofsizeof(std::_Container_proxy)when_ITERATOR_DEBUG_LEVEL != 0. The publisher code is unchanged.Checks:
cl /Od /MDd): all cases pass, and--mutate inlinestill fails as expected.startup_request_publisherand_inline_controlpass.1319a11f: Windows build and test, gfx1013 fixtures, and Linux Python checks all pass.Ready to merge from our side
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T02:54:46Z.
Pushed merge resolution
e6e92bd0to 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 HEADsucceeds against main2b870868. Compiledstartup_request_publisher_check.cppwith MSVC/O2and ran the fixture: pass, with 0 ms to queue three requests while the writer was blocked. The--mutate inlinecontrol returned 1 as expected. The integrated Windows run is pending on the new head.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T03:13:12Z.
After #3770 landed,
b9288115resolves 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.