From e29ae80a12069b1373335b735b315890138d99b1 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Thu, 26 Nov 2026 07:03:05 +0100 Subject: [PATCH 1/2] Texture registry: a registration finds its overlap run by binary search instead of sorting Register copied every registered texture into a list and sorted it again for each new texture. A loading screen registers thousands of textures over a registry of about 15,000: the CPU sampler (--cpu-sample) showed the game thread that creates views at addresses (CreateShaderResourceViewAtAddress) 100 % busy in that sort's comparer (GpuTexture.Address), the other game threads blocked behind it, frames of 18 to 37 s, and the game's 30 s hang watchdog ending the boot before the world. Registered ranges never overlap, so the textures a new one overlaps are one run of the sorted array: OverlapRun finds it from LastStartingBefore and walks back, the run is retired as before, and the new array is the prefix, the texture and the suffix (two block copies, no sort). Co-Authored-By: Claude Opus 5.5 --- docs/gpu-resources.md | 5 +- .../D3D12X/Resources/GpuTextureRegistry.cs | 46 +++++++++---- .../GpuTextureRegistryRunTests.cs | 65 +++++++++++++++++++ 3 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs diff --git a/docs/gpu-resources.md b/docs/gpu-resources.md index a40665ab..88d6c602 100644 --- a/docs/gpu-resources.md +++ b/docs/gpu-resources.md @@ -39,7 +39,10 @@ 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). 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..142f96f1 100644 --- a/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs +++ b/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs @@ -122,28 +122,48 @@ 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); } /// diff --git a/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs b/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs new file mode 100644 index 00000000..3f37839a --- /dev/null +++ b/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs @@ -0,0 +1,65 @@ +// 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 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]; + } +} From 32aec6eef54bb1d7e321f02fa9b6b4bb0cdce74a Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Thu, 26 Nov 2026 07:11:40 +0100 Subject: [PATCH 2/2] Texture registry: a page mapping marks only the textures of its run After the sort, the CPU sampler showed the same game thread 100 % busy in CopyPageMappings: each page the game maps or unmaps went through Mark, which scanned all 15,000 registered textures for the ones over the page. Mark now visits only the run OverlapRun finds; a test checks Unmapped and Mapped against a scan over 2,000 random ranges. Co-Authored-By: Claude Opus 5.5 --- docs/gpu-resources.md | 3 +- .../D3D12X/Resources/GpuTextureRegistry.cs | 14 ++++-- .../GpuTextureRegistryRunTests.cs | 45 +++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/docs/gpu-resources.md b/docs/gpu-resources.md index 88d6c602..7ffa0ba1 100644 --- a/docs/gpu-resources.md +++ b/docs/gpu-resources.md @@ -42,7 +42,8 @@ flowchart LR 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). Of those over the same range (other + 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 142f96f1..1c69bbca 100644 --- a/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs +++ b/src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs @@ -210,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 index 3f37839a..07e2a4fe 100644 --- a/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs +++ b/tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs @@ -40,6 +40,51 @@ public void The_overlap_run_is_every_texture_a_scan_finds_and_its_place_keeps_th } } + [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() {