From 3c67d864326dc9a41b91387cf3a9f5267cce6add Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Thu, 26 Nov 2026 07:17:05 +0100 Subject: [PATCH] Texture refresh: a submission takes the queued textures on several threads Settle refreshed every texture still queued on the submitting game thread, one after another, before the submission could go on. Through a loading screen about 1,300 textures were queued between two summaries, mostly first fills, and the CPU sampler showed the submitting thread in ContentHash.Of (33 to 58 %), NtReadVirtualMemory (14 to 17 %) and the detiling: up to 16.8 s a frame in texture refresh, and the game's 30 s hang watchdog ended the boot before the world. The game thread waits for the whole queue there anyway, and reading, hashing and detiling run outside the uploader's lock (copy buffers are per thread, the counters atomic, each texture behind its own gate), so Settle now drains the queue with Parallel.ForEach on half the logical processors. The background worker keeps one thread. Co-Authored-By: Claude Opus 5.5 --- docs/gpu-resources.md | 4 ++- .../D3D12X/Resources/TextureRefreshWorker.cs | 36 +++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/docs/gpu-resources.md b/docs/gpu-resources.md index a40665ab..7ddc67ef 100644 --- a/docs/gpu-resources.md +++ b/docs/gpu-resources.md @@ -105,7 +105,9 @@ flowchart LR - **Written textures refresh incrementally** (`TextureRefreshWorker`): a write marks the 64 KiB pages it touched; a background thread reads those pages, compares each with its hash from the last upload, and uploads only the subresources (mips, slices) a changed page lies in (`XgSubresourceSpans`, counted as `UploadPartial`). A - submission takes what is left and waits for refreshes in progress, so a texture written before it is current. A + submission takes what is left on half the logical processors (`SettleThreads`; one thread took up to 16.8 s a frame + through a loading screen, past the game's hang watchdog) and waits for refreshes in progress, so a texture written + before it is current. A texture never filled is still filled whole. w109's whole-texture refresh on the submitting thread cost 19 ms a frame. - **A slot-70 view of a registered texture** (`GpuTextures.RefreshForView`) marks all its pages for that worker instead of reading and hashing the texture on the game's thread: the CPU may have written its memory, which nothing diff --git a/src/FiveStar.Loader/Shims/D3D12X/Resources/TextureRefreshWorker.cs b/src/FiveStar.Loader/Shims/D3D12X/Resources/TextureRefreshWorker.cs index 9c002e07..507dd181 100644 --- a/src/FiveStar.Loader/Shims/D3D12X/Resources/TextureRefreshWorker.cs +++ b/src/FiveStar.Loader/Shims/D3D12X/Resources/TextureRefreshWorker.cs @@ -25,6 +25,14 @@ internal sealed unsafe class TextureRefreshWorker : IDisposable private const int BitsPerWord = 64; + /// + /// Threads a submission refreshes the queued textures on: half the logical processors, at least one. The game's + /// thread waits for the whole queue there anyway, and reading, hashing and detiling a texture run outside the + /// uploader's lock; a loading screen queued about 1,300 textures between two summaries, and one thread took up to + /// 16.8 s a frame refreshing them, past the game's 30 s hang watchdog. + /// + internal static readonly int SettleThreads = Math.Max(1, Environment.ProcessorCount / 2); + private readonly GpuTextures _textures; private readonly GameMemorySource _source; private readonly ConcurrentDictionary _marked = new(); @@ -62,7 +70,7 @@ public void Settle(nint device) if (!_marked.IsEmpty) { - Drain(device); + Parallel.ForEach(_marked.Keys, new ParallelOptions { MaxDegreeOfParallelism = SettleThreads }, texture => Take(device, texture)); } // The event can be set a moment before another refresh begins; the count decides. @@ -176,20 +184,26 @@ private void Drain(nint device) { foreach (var texture in _marked.Keys) { - Begin(); + Take(device, texture); + } + } - try - { - if (_marked.TryRemove(texture, out _)) - { - Refresh(device, texture); - } - } - finally + /// Refreshes if it is still queued; it counts as in progress before it leaves the queue. + private void Take(nint device, GpuTexture texture) + { + Begin(); + + try + { + if (_marked.TryRemove(texture, out _)) { - End(); + Refresh(device, texture); } } + finally + { + End(); + } } private void Begin()