diff --git a/docs/gpu-resources.md b/docs/gpu-resources.md index a40665ab..7ffa0ba1 100644 --- a/docs/gpu-resources.md +++ b/docs/gpu-resources.md @@ -39,7 +39,11 @@ flowchart LR ``` - **Registry** (`GpuTextureRegistry`): textures sorted by console address over the span of their XG layout; lookups - take no lock. A texture registered over an overlapping range retires the old one. Of those over the same range (other + take no lock. A texture registered over an overlapping range retires the old one; the overlapped textures are one run + of the sorted array (registered ranges never overlap), found by binary search, so a registration copies the array + around the run instead of sorting it (sorting 15,000 textures per registration kept a loading screen busy for seconds + a frame, and the game's hang watchdog ended the boot). A page the game maps or unmaps marks only the textures of + its run too, instead of scanning them all. Of those over the same range (other views of the same memory, or a streaming slot reused) the newest keeps its reference until shutdown, since descriptors written earlier may still name it (`--kept-aliases `, default 1; it was 2, a second texture of video memory per reused slot charged to commit, while a placed texture's heap range stays mapped after release anyway), and older ones are released 30 presents later; any other is diff --git a/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs b/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs index f195bcfa..1c69bbca 100644 --- a/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs +++ b/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs @@ -122,30 +122,50 @@ public string Summary } } + /// + /// Adds and retires the textures its range overlaps. Registered ranges never overlap, so + /// those form one run of the sorted array, found by binary search, and the new array is the run's prefix, the + /// texture and its suffix. It used to copy and sort the whole array on every registration: with 15,000 textures a + /// loading screen's registrations kept the thread that creates the views busy for seconds a frame, and the game's + /// 30 s hang watchdog ended the boot. + /// public void Register(GpuTexture texture) { lock (_gate) { - var kept = new List(_sorted.Length + 1); + var sorted = _sorted; + var (first, count) = OverlapRun(sorted, texture.Address, texture.Size); - foreach (var entry in _sorted) + for (int index = first; index < first + count; index++) { - if (entry.Address < texture.Address + texture.Size && texture.Address < entry.Address + entry.Size) - { - Retire(entry, texture); - } - else - { - kept.Add(entry); - } + Retire(sorted[index], texture); } - kept.Add(texture); - kept.Sort((left, right) => left.Address.CompareTo(right.Address)); - Volatile.Write(ref _sorted, kept.ToArray()); + var next = new GpuTexture[sorted.Length - count + 1]; + Array.Copy(sorted, 0, next, 0, first); + next[first] = texture; + Array.Copy(sorted, first + count, next, first + 1, sorted.Length - first - count); + Volatile.Write(ref _sorted, next); } } + /// + /// The run of overlapping [, + ): its first + /// index and length; with no overlap, the index the range belongs at and 0. + /// + internal static (int First, int Count) OverlapRun(GpuTexture[] sorted, ulong address, ulong bytes) + { + int last = LastStartingBefore(sorted, address + bytes); + int first = last + 1; + + while (first > 0 && address < sorted[first - 1].Address + sorted[first - 1].Size) + { + first--; + } + + return (first, last + 1 - first); + } + /// /// --retire-unmapped-textures: a texture whose memory stays unmapped for /// presents is retired (released after its grace). Off by default: the game maps pages again at the same address and @@ -190,14 +210,22 @@ public void Mapped(ulong address, ulong bytes) } } - /// Marks the textures over the range as over unmapped memory (from now) or not, under the lock; how many changed. + /// + /// Marks the textures over the range as over unmapped memory (from now) or not, under the lock; how many changed. + /// Only the run over the range is visited (): each page the game maps or unmaps used to scan + /// all 15,000 textures, which kept the page-mapping thread busy through a loading screen. + /// private int Mark(ulong address, ulong bytes, bool unmapped) { int changed = 0; + var sorted = _sorted; + var (first, count) = OverlapRun(sorted, address, bytes); - foreach (var entry in _sorted) + for (int index = first; index < first + count; index++) { - if (entry.Address < address + bytes && address < entry.Address + entry.Size && entry.OverUnmapped != unmapped) + var entry = sorted[index]; + + if (entry.OverUnmapped != unmapped) { Unmark(entry); diff --git a/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs b/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs new file mode 100644 index 00000000..07e2a4fe --- /dev/null +++ b/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs @@ -0,0 +1,110 @@ +// Copyright (c) 2026 Five Star Recomp contributors. +// SPDX-License-Identifier: GPL-3.0-or-later + +using FiveStar.Loader.Shims.D3D12X; +using FiveStar.Loader.Shims.D3D12X.Resources; +using FiveStar.Loader.Shims.Xg; +using Xunit; + +namespace FiveStar.Loader.Tests; + +/// +/// Registering a texture finds the textures its range overlaps by binary search, a run of the sorted array (registered +/// ranges never overlap), instead of copying and sorting the whole array: the run matches a scan of every texture. +/// +public sealed class GpuTextureRegistryRunTests +{ + private const int Textures = 200; + private const int Ranges = 2000; + private const ulong Page = 0x1000; + + [Fact] + public void The_overlap_run_is_every_texture_a_scan_finds_and_its_place_keeps_the_order() + { + var random = new Random(7); + var sorted = Layout(random); + + for (int i = 0; i < Ranges; i++) + { + ulong address = (ulong)random.Next(0, Textures * 8) * Page / 2; + ulong bytes = (ulong)random.Next(0, 6) * Page; + var (first, count) = GpuTextureRegistry.OverlapRun(sorted, address, bytes); + + var scanned = Enumerable.Range(0, sorted.Length) + .Where(index => sorted[index].Address < address + bytes && address < sorted[index].Address + sorted[index].Size) + .ToArray(); + + Assert.Equal(scanned, Enumerable.Range(first, count).ToArray()); + Assert.True(first == 0 || sorted[first - 1].Address <= address, $"range 0x{address:x} + 0x{bytes:x} placed after a later texture"); + Assert.True(first + count == sorted.Length || sorted[first + count].Address >= address + bytes || bytes == 0, $"range 0x{address:x} + 0x{bytes:x} placed before an earlier texture"); + } + } + + [Fact] + public void Unmapping_and_mapping_mark_exactly_the_textures_over_the_range() + { + var random = new Random(11); + var textures = Layout(random); + var registry = new GpuTextureRegistry(() => [], () => []); + + foreach (var texture in textures.Reverse()) + { + registry.Register(texture); + } + + var expected = new bool[textures.Length]; + + for (int i = 0; i < Ranges; i++) + { + ulong address = (ulong)random.Next(0, Textures * 8) * Page / 2; + ulong bytes = (ulong)random.Next(1, 6) * Page; + bool unmap = random.Next(2) == 0; + int changes = 0; + + for (int index = 0; index < textures.Length; index++) + { + var texture = textures[index]; + + if (texture.Address < address + bytes && address < texture.Address + texture.Size && expected[index] != unmap) + { + expected[index] = unmap; + changes++; + } + } + + if (unmap) + { + Assert.Equal(changes, registry.Unmapped(address, bytes)); + } + else + { + registry.Mapped(address, bytes); + } + + Assert.Equal(expected, textures.Select(texture => texture.OverUnmapped).ToArray()); + } + } + + [Fact] + public void An_empty_registry_places_a_texture_first() + { + Assert.Equal((0, 0), GpuTextureRegistry.OverlapRun([], 0x10000, Page)); + } + + /// Non-overlapping textures of one to three pages, with gaps, sorted by address. + private static GpuTexture[] Layout(Random random) + { + var textures = new List(); + ulong address = 0; + + for (int i = 0; i < Textures; i++) + { + address += (ulong)random.Next(0, 3) * Page; + ulong size = (ulong)random.Next(1, 4) * Page; + textures.Add(new GpuTexture(address, default(ResourceDescription), 0, 0, 0, false, new XgSurface { Size = size })); + address += size; + } + + return [.. textures]; + } +}