Lands the working A8 indoor-rendering and streaming fixes accumulated this session. User has verified these visually to some degree (e.g. lifestone / translucent meshes confirmed fine under the FrontFace flip; bridge / wall / collision regressions confirmed fixed after travel); not every path has been exhaustively gated. The cellar-flap defect remains OPEN and will be solved the retail-faithful way via a dedicated brainstorm (see handoff docs). Rendering core (reviewed, high confidence): - EnvCellRenderer SSBO stride fix: upload packed Matrix4x4[] (64B) instead of the 80B CPU InstanceData struct the shader never expected — fixes the transform/texture "explosion" for any draw with >1 instance (cells that dedupe to a shared cellGeomId). Real root cause. - WB-style global FrontFace(CW) + per-batch CullMode carried through the MDI layout (GroupKey + BuildIndirectArrays + DrawIndirectRange split into same-cull runs with absolute uDrawIDOffset per run). - EntitySet partitioning (IndoorPass / OutdoorScenery / LiveDynamic) + WorldEntity.BuildingShellAnchorCellId so building shells scope to their dat-derived building cell instead of rendering everywhere. - RenderOutsideInAcdream (look into buildings from outside) + CollectVisiblePortalBuildings frustum cull of portal bounds. - Sky-when-inside-building + per-cell audit probe + GL-state probe. Streaming / perf (test-covered; not independently code-reviewed this session): - Near/far priority queues so near work wins over far; PromoteToNear carries full landblock + mesh data; LandblockEntriesWithoutAnimatedIndex avoids rebuilding the animated-lookup dict in the hot draw path. Fixes the bridge-not-appearing / missing-walls / broken-collision-after-travel regressions and improves post-transition FPS. Tooling + docs: - tools/A8CellAudit: offline dat cell/portal/building dumper (portals + buildings modes) — reproduces the cellar-flap investigation with no launch. - docs/research cellar-flap root-cause + option-2 handoff (the didInsideStencil double-duty finding + the WB-recursive design decision + brainstorm prompt), entity-taxonomy, replan, issue-78 visibility investigation. Diagnostics retained on purpose: ACDREAM_A8_DIAG_* gates, portal_stencil.vert provisional pos.w clamp, and the probe families are kept (env-var gated, zero cost when off) because the pending option-2 cellar-flap brainstorm needs them. Strip in the option-2 ship commit. Indoor branch stays behind ACDREAM_A8_INDOOR_BRANCH=1 (default off = pre-A8 visual). Build green; App tests + Core (streaming/dispatcher/loader) tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
160 lines
7.2 KiB
C#
160 lines
7.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using AcDream.App.Rendering;
|
|
using DatReaderWriter.DBObjs;
|
|
|
|
namespace AcDream.App.Rendering.Wb;
|
|
|
|
/// <summary>
|
|
/// Phase A8 (2026-05-26): static factory that builds a per-landblock
|
|
/// <see cref="BuildingRegistry"/> from a <see cref="LandBlockInfo"/>'s
|
|
/// <c>Buildings</c> array.
|
|
///
|
|
/// <para>Algorithm (mirrors WB's <c>PortalService.GetPortalsByBuilding</c> at
|
|
/// <c>WorldBuilder.Shared/Services/PortalService.cs:43-97</c>):</para>
|
|
/// <list type="bullet">
|
|
/// <item>Step A — seed the cell set from <c>BuildingInfo.Portals</c> entry portals.</item>
|
|
/// <item>Step B — BFS through <see cref="LoadedCell.Portals"/> to discover all
|
|
/// interior cells reachable from the entry portals (interior portals only;
|
|
/// exit portals — <c>OtherCellId == 0xFFFF</c> — terminate each BFS branch).</item>
|
|
/// <item>Step C — collect exit portal polygons in world space for the stencil
|
|
/// pipeline (Phase A8 Steps 1+2, RR7 scope).</item>
|
|
/// </list>
|
|
///
|
|
/// <para>Cells whose <c>LoadedCell</c> entries are missing from
|
|
/// <paramref name="cellsByCellId"/> are silently skipped (BFS bails at the
|
|
/// unloaded cell). In production, streaming loads all cells for a landblock
|
|
/// before <see cref="Build"/> runs, so the dict is always complete.</para>
|
|
///
|
|
/// <para><c>LoadedCell.BuildingId</c> is stamped here in RR4 after <c>reg.Add(building)</c>.</para>
|
|
///
|
|
/// <para>Retail references:
|
|
/// <c>docs/research/named-retail/acclient.h:32035</c> (<c>BuildInfo</c>) and
|
|
/// <c>:32094</c> (<c>CBldPortal</c>).</para>
|
|
/// </summary>
|
|
public static class BuildingLoader
|
|
{
|
|
/// <summary>
|
|
/// Builds a <see cref="BuildingRegistry"/> from the supplied landblock data.
|
|
/// Building IDs are allocated sequentially starting at 1 (0 is reserved for
|
|
/// "no building" semantics used by <c>LoadedCell.BuildingId</c> in RR4).
|
|
/// </summary>
|
|
/// <param name="info">The dat-loaded <see cref="LandBlockInfo"/> for this landblock.</param>
|
|
/// <param name="landblockId">The 32-bit landblock id (e.g. <c>0xA9B40000</c>).
|
|
/// The high 16 bits are ORed with each 16-bit <c>OtherCellId</c> to produce
|
|
/// the full cell id.</param>
|
|
/// <param name="cellsByCellId">Pre-loaded cells keyed by full 32-bit cell id.
|
|
/// Used for BFS extension (Step B) and exit-polygon collection (Step C).
|
|
/// An empty dict is valid for unit tests; Step B and C are skipped per cell.</param>
|
|
/// <returns>A fully populated <see cref="BuildingRegistry"/>; never null.</returns>
|
|
public static BuildingRegistry Build(
|
|
LandBlockInfo info,
|
|
uint landblockId,
|
|
IReadOnlyDictionary<uint, LoadedCell> cellsByCellId)
|
|
{
|
|
var reg = new BuildingRegistry();
|
|
if (info.Buildings is null || info.Buildings.Count == 0)
|
|
return reg;
|
|
|
|
uint lbMask = landblockId & 0xFFFF0000u;
|
|
uint nextId = 1;
|
|
|
|
foreach (var bInfo in info.Buildings)
|
|
{
|
|
var envCellIds = new HashSet<uint>();
|
|
var exitPortalPolys = new List<Vector3[]>();
|
|
|
|
// Step A: seed the cell set from BuildingInfo.Portals (entry portals).
|
|
// Each BuildingPortal.OtherCellId is a 16-bit cell-local id; OR with
|
|
// the landblock prefix for the full id.
|
|
// Defensive: skip OtherCellId == 0xFFFF (exit-portal sentinel in
|
|
// BuildingInfo — rare but WB guards against it too at PortalService.cs:58).
|
|
if (bInfo.Portals is not null)
|
|
{
|
|
foreach (var portal in bInfo.Portals)
|
|
{
|
|
if (portal.OtherCellId == 0xFFFF) continue;
|
|
envCellIds.Add(lbMask | portal.OtherCellId);
|
|
}
|
|
}
|
|
|
|
// Step B: BFS through interior CellPortals to find the full cell set.
|
|
// Uses pre-loaded LoadedCell.Portals (avoids a duplicate dat fetch per
|
|
// BFS step). Mirrors WB PortalService.cs:67-79.
|
|
// When cellsByCellId is empty (unit-test path), BFS immediately exits.
|
|
var queue = new Queue<uint>(envCellIds);
|
|
while (queue.Count > 0)
|
|
{
|
|
var current = queue.Dequeue();
|
|
if (!cellsByCellId.TryGetValue(current, out var cell)) continue;
|
|
foreach (var p in cell.Portals)
|
|
{
|
|
if (p.OtherCellId == 0xFFFF) continue; // exit portal — stop BFS here
|
|
uint neighbourId = lbMask | p.OtherCellId;
|
|
if (envCellIds.Add(neighbourId))
|
|
queue.Enqueue(neighbourId);
|
|
}
|
|
}
|
|
|
|
// Step C: collect exit portal polygons in world space.
|
|
// For each interior cell, iterate its portals; for each exit portal
|
|
// (OtherCellId == 0xFFFF), transform the portal polygon vertices from
|
|
// cell-local space to world space via WorldTransform.
|
|
// Mirrors WB PortalService.cs:81-86 (GetPortalsForCell return path).
|
|
foreach (var cellId in envCellIds)
|
|
{
|
|
if (!cellsByCellId.TryGetValue(cellId, out var cell)) continue;
|
|
for (int pi = 0; pi < cell.Portals.Count; pi++)
|
|
{
|
|
if (cell.Portals[pi].OtherCellId != 0xFFFF) continue;
|
|
if (pi >= cell.PortalPolygons.Count) continue;
|
|
var localPoly = cell.PortalPolygons[pi];
|
|
if (localPoly.Length < 3) continue;
|
|
var worldPoly = new Vector3[localPoly.Length];
|
|
for (int v = 0; v < localPoly.Length; v++)
|
|
worldPoly[v] = Vector3.Transform(localPoly[v], cell.WorldTransform);
|
|
exitPortalPolys.Add(worldPoly);
|
|
}
|
|
}
|
|
|
|
bool hasPortalBounds = false;
|
|
var portalMin = new Vector3(float.MaxValue);
|
|
var portalMax = new Vector3(float.MinValue);
|
|
foreach (var poly in exitPortalPolys)
|
|
{
|
|
foreach (var v in poly)
|
|
{
|
|
hasPortalBounds = true;
|
|
portalMin = Vector3.Min(portalMin, v);
|
|
portalMax = Vector3.Max(portalMax, v);
|
|
}
|
|
}
|
|
|
|
// WB PortalService.cs:89: skip buildings with no interior cells.
|
|
if (envCellIds.Count == 0) continue;
|
|
|
|
var building = new Building
|
|
{
|
|
BuildingId = nextId++,
|
|
EnvCellIds = envCellIds,
|
|
ExitPortalPolygons = exitPortalPolys,
|
|
HasPortalBounds = hasPortalBounds,
|
|
PortalBounds = hasPortalBounds
|
|
? new WbBoundingBox(portalMin, portalMax)
|
|
: default,
|
|
};
|
|
reg.Add(building);
|
|
|
|
// Step 4: stamp BuildingId on each cell (Option C — both directions
|
|
// O(1)). The internal setter on LoadedCell.BuildingId is accessible
|
|
// 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;
|
|
}
|
|
}
|