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()