feat(render): Phase A8 RR4 — wire BuildingRegistry into landblock load

LoadedCell.BuildingId (init + internal setter) — set exactly once at
    landblock load time by BuildingLoader; null when the cell isn't
    part of any building (outdoor surface cells; dungeon cells not
    enumerated in LandBlockInfo.Buildings).

  GameWindow landblock-load path: builds BuildingRegistry from
    LandBlockInfo.Buildings; stamps each cell's BuildingId; stores the
    registry on _buildingRegistries[landblockId] (GameWindow-level dict)
    for render-frame lookups. Note: LoadedLandblock is AcDream.Core.World
    (a sealed record) — adding an App-type field there would violate
    Code Structure Rule #2, so the registry is stored in a new
    GameWindow-level dictionary instead. Cleanup wired in both
    removeTerrain lambdas (OnLoad + OnResize paths).

  drainedCells dict: the existing _pendingCells drain loop is extended
    to also build a local CellId→LoadedCell dict; BuildingLoader.Build
    uses this dict for the stamping pass so no second iteration is needed.

  New BuildingLoaderTest verifies the stamping path. 5 BuildingLoader
  tests total (4 from RR3 + 1 new).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-05-27 11:13:48 +02:00
parent f125fdb220
commit f8d0499d8b
4 changed files with 93 additions and 4 deletions

View file

@ -70,6 +70,19 @@ public sealed class LoadedCell
/// </para> /// </para>
/// </summary> /// </summary>
public List<Vector3[]> PortalPolygons = new(); public List<Vector3[]> PortalPolygons = new();
/// <summary>
/// Phase A8 (2026-05-26): the building this cell belongs to, if any.
/// Set exactly once by <see cref="Wb.BuildingLoader"/> immediately after
/// LandblockLoader produces the cells. Null when the cell isn't part of
/// any building (outdoor surface cells; dungeon cells not enumerated in
/// LandBlockInfo.Buildings).
///
/// <para>Used by the render frame to derive the camera-buildings set
/// via <see cref="Wb.BuildingRegistry.GetBuildingsContainingCell"/>
/// and route IndoorPass cell scoping.</para>
/// </summary>
public uint? BuildingId { get; internal set; }
} }
/// <summary> /// <summary>

View file

@ -150,6 +150,14 @@ public sealed class GameWindow : IDisposable
// to _cellVisibility on the render thread in ApplyLoadedTerrain. // to _cellVisibility on the render thread in ApplyLoadedTerrain.
private readonly System.Collections.Concurrent.ConcurrentBag<LoadedCell> _pendingCells = new(); private readonly System.Collections.Concurrent.ConcurrentBag<LoadedCell> _pendingCells = new();
// Phase A8 (2026-05-26): per-landblock BuildingRegistry keyed by full landblock
// id (e.g. 0xA9B40000). Built from LandBlockInfo.Buildings at ApplyLoadedTerrain
// time; each entry's BuildingRegistry.GetBuildingsContainingCell drives render-frame
// indoor-cell scoping. Entries are removed in the removeTerrain callbacks.
// Only touched on the render thread — no lock required.
private readonly System.Collections.Generic.Dictionary<uint, AcDream.App.Rendering.Wb.BuildingRegistry>
_buildingRegistries = new();
/// <summary> /// <summary>
/// Phase 6.4: per-entity animation playback state for entities whose /// Phase 6.4: per-entity animation playback state for entities whose
/// MotionTable resolved to a real cycle. The render loop ticks each /// MotionTable resolved to a real cycle. The render loop ticks each
@ -1833,6 +1841,7 @@ public sealed class GameWindow : IDisposable
_terrain?.RemoveLandblock(id); _terrain?.RemoveLandblock(id);
_physicsEngine.RemoveLandblock(id); _physicsEngine.RemoveLandblock(id);
_cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu); _cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu);
_buildingRegistries.Remove(id); // Phase A8
}); });
// A.5 T22.5: apply max-completions from resolved quality. // A.5 T22.5: apply max-completions from resolved quality.
_streamingController.MaxCompletionsPerFrame = _resolvedQuality.MaxCompletionsPerFrame; _streamingController.MaxCompletionsPerFrame = _resolvedQuality.MaxCompletionsPerFrame;
@ -5665,8 +5674,13 @@ public sealed class GameWindow : IDisposable
_terrain.AddLandblockWithMesh(lb.LandblockId, meshData, origin); _terrain.AddLandblockWithMesh(lb.LandblockId, meshData, origin);
// Step 4: drain pending LoadedCells from the worker thread. // Step 4: drain pending LoadedCells from the worker thread.
// Also collect into a local dict for the BuildingLoader stamping pass below.
var drainedCells = new System.Collections.Generic.Dictionary<uint, LoadedCell>();
while (_pendingCells.TryTake(out var cell)) while (_pendingCells.TryTake(out var cell))
{
_cellVisibility.AddCell(cell); _cellVisibility.AddCell(cell);
drainedCells[cell.CellId] = cell;
}
// Compute the per-landblock AABB for frustum culling. XY from the // Compute the per-landblock AABB for frustum culling. XY from the
// landblock's world origin + 192 footprint. Z from the terrain vertex // landblock's world origin + 192 footprint. Z from the terrain vertex
@ -5826,6 +5840,20 @@ public sealed class GameWindow : IDisposable
_physicsEngine.AddLandblock(lb.LandblockId, terrainSurface, cellSurfaces, _physicsEngine.AddLandblock(lb.LandblockId, terrainSurface, cellSurfaces,
portalPlanes, origin.X, origin.Y); portalPlanes, origin.X, origin.Y);
// Phase A8 (2026-05-26): build per-landblock BuildingRegistry from
// LandBlockInfo.Buildings, stamping LoadedCell.BuildingId for each cell
// in a building's cell set. Uses the already-drained drainedCells dict
// (LoadedCells registered this frame) so stamping and registry build
// happen in the same render-thread pass — no extra dat reads required.
// Cells without a building stay at BuildingId == null (outdoor surface
// cells; dungeon cells not enumerated in LandBlockInfo.Buildings).
if (lbInfo is not null)
{
_buildingRegistries[lb.LandblockId] =
AcDream.App.Rendering.Wb.BuildingLoader.Build(
lbInfo, lb.LandblockId, drainedCells);
}
} }
// N.5: WbMeshAdapter.Tick() handles GPU upload for all GfxObj meshes via // N.5: WbMeshAdapter.Tick() handles GPU upload for all GfxObj meshes via
@ -8894,6 +8922,7 @@ public sealed class GameWindow : IDisposable
_terrain?.RemoveLandblock(id); _terrain?.RemoveLandblock(id);
_physicsEngine.RemoveLandblock(id); _physicsEngine.RemoveLandblock(id);
_cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu); _cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu);
_buildingRegistries.Remove(id); // Phase A8
}); });
_streamingController.MaxCompletionsPerFrame = newResolved.MaxCompletionsPerFrame; _streamingController.MaxCompletionsPerFrame = newResolved.MaxCompletionsPerFrame;

View file

@ -26,7 +26,7 @@ namespace AcDream.App.Rendering.Wb;
/// unloaded cell). In production, streaming loads all cells for a landblock /// unloaded cell). In production, streaming loads all cells for a landblock
/// before <see cref="Build"/> runs, so the dict is always complete.</para> /// before <see cref="Build"/> runs, so the dict is always complete.</para>
/// ///
/// <para><c>LoadedCell.BuildingId</c> stamping is wired in RR4, not here.</para> /// <para><c>LoadedCell.BuildingId</c> is stamped here in RR4 after <c>reg.Add(building)</c>.</para>
/// ///
/// <para>Retail references: /// <para>Retail references:
/// <c>docs/research/named-retail/acclient.h:32035</c> (<c>BuildInfo</c>) and /// <c>docs/research/named-retail/acclient.h:32035</c> (<c>BuildInfo</c>) and
@ -128,9 +128,14 @@ public static class BuildingLoader
}; };
reg.Add(building); reg.Add(building);
// NOTE: LoadedCell.BuildingId stamping is wired in RR4 (requires // Step 4: stamp BuildingId on each cell (Option C — both directions
// an internal setter on LoadedCell that doesn't exist yet). This // O(1)). The internal setter on LoadedCell.BuildingId is accessible
// comment is the placeholder called out in the plan's RR3-S11. // because this class lives in the same assembly (AcDream.App).
foreach (var cellId in envCellIds)
{
if (cellsByCellId.TryGetValue(cellId, out var cell))
cell.BuildingId = building.BuildingId;
}
} }
return reg; return reg;

View file

@ -85,4 +85,46 @@ public class BuildingLoaderTests
foreach (var b in reg.All()) ids.Add(b.BuildingId); foreach (var b in reg.All()) ids.Add(b.BuildingId);
Assert.Equal(new SortedSet<uint> { 1, 2 }, ids); // sequential 1, 2 Assert.Equal(new SortedSet<uint> { 1, 2 }, ids); // sequential 1, 2
} }
[Fact]
public void Build_StampsLoadedCellBuildingId()
{
// Fixture: minimal LoadedCell instances representing 2 cottage cells.
var cell150 = new AcDream.App.Rendering.LoadedCell
{
CellId = 0xA9B40150u,
Portals = new List<AcDream.App.Rendering.CellPortalInfo>(),
PortalPolygons = new List<Vector3[]>(),
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
LocalBoundsMin = new Vector3(-5, -5, -5),
LocalBoundsMax = new Vector3(5, 5, 5),
ClipPlanes = new List<AcDream.App.Rendering.PortalClipPlane>(),
};
var cell151 = new AcDream.App.Rendering.LoadedCell
{
CellId = 0xA9B40151u,
Portals = new List<AcDream.App.Rendering.CellPortalInfo>(),
PortalPolygons = new List<Vector3[]>(),
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
LocalBoundsMin = new Vector3(-5, -5, -5),
LocalBoundsMax = new Vector3(5, 5, 5),
ClipPlanes = new List<AcDream.App.Rendering.PortalClipPlane>(),
};
var cells = new Dictionary<uint, AcDream.App.Rendering.LoadedCell>
{
{ 0xA9B40150u, cell150 },
{ 0xA9B40151u, cell151 },
};
var info = MakeInfo((0x02000123u, new[] { 0x0150u, 0x0151u }));
var reg = BuildingLoader.Build(info, 0xA9B40000u, cells);
Assert.Equal(1, reg.Count);
var b = System.Linq.Enumerable.First(reg.All());
// Both cells stamped with the building id:
Assert.Equal(b.BuildingId, cell150.BuildingId);
Assert.Equal(b.BuildingId, cell151.BuildingId);
}
} }