Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/gpu-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions src/FiveStar.Loader/Shims/D3D12X/Resources/TextureRefreshWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ internal sealed unsafe class TextureRefreshWorker : IDisposable

private const int BitsPerWord = 64;

/// <summary>
/// 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.
/// </summary>
internal static readonly int SettleThreads = Math.Max(1, Environment.ProcessorCount / 2);

private readonly GpuTextures _textures;
private readonly GameMemorySource _source;
private readonly ConcurrentDictionary<GpuTexture, byte> _marked = new();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
/// <summary>Refreshes <paramref name="texture"/> if it is still queued; it counts as in progress before it leaves the queue.</summary>
private void Take(nint device, GpuTexture texture)
{
Begin();

try
{
if (_marked.TryRemove(texture, out _))
{
End();
Refresh(device, texture);
}
}
finally
{
End();
}
}

private void Begin()
Expand Down
Loading