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
6 changes: 5 additions & 1 deletion docs/gpu-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <n>`, 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
Expand Down
60 changes: 44 additions & 16 deletions src/FiveStar.Loader/Shims/D3D12X/Resources/GpuTextureRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,50 @@ public string Summary
}
}

/// <summary>
/// Adds <paramref name="texture"/> 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.
/// </summary>
public void Register(GpuTexture texture)
{
lock (_gate)
{
var kept = new List<GpuTexture>(_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);
}
}

/// <summary>
/// The run of <paramref name="sorted"/> overlapping [<paramref name="address"/>, + <paramref name="bytes"/>): its first
/// index and length; with no overlap, the index the range belongs at and 0.
/// </summary>
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);
}

/// <summary>
/// <c>--retire-unmapped-textures</c>: a texture whose memory stays unmapped for <see cref="UnmappedGraceFrames"/>
/// presents is retired (released after its grace). Off by default: the game maps pages again at the same address and
Expand Down Expand Up @@ -190,14 +210,22 @@ public void Mapped(ulong address, ulong bytes)
}
}

/// <summary>Marks the textures over the range as over unmapped memory (from now) or not, under the lock; how many changed.</summary>
/// <summary>
/// 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 (<see cref="OverlapRun"/>): 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.
/// </summary>
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);

Expand Down
110 changes: 110 additions & 0 deletions tests/FiveStar.Loader.Tests/GpuTextureRegistryRunTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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.
/// </summary>
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));
}

/// <summary>Non-overlapping textures of one to three pages, with gaps, sorted by address.</summary>
private static GpuTexture[] Layout(Random random)
{
var textures = new List<GpuTexture>();
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];
}
}
Loading