acdream/tests/AcDream.Core.Tests/Lighting/LightManagerTests.cs
Erik 4d25e04d83 fix #176 #177: the camera-capped light snapshot evicted visible cells' torches — per-cell lighting popped at seams
The probe launch discriminated it: the user reproduced the purple floor
flash while [light] (ambient branch) and [pv-input] (portal flood) read
provably healthy — eliminating the last CPU-side theories and exposing
the one channel the probes could not see: per-cell 8-light set
composition.

BuildPointLightSnapshot kept the MaxGlobalLights=128 point lights
nearest THE CAMERA; the Facility Hub registers 366 fixtures, so 238
were evicted per frame by camera distance. SelectForObject (faithfully
camera-independent, and unit-pinned as such) could only choose from the
surviving 128 — an in-range torch of a visible cell that ranked past
the cap dropped out of that cell's 8-set, so per-cell Gouraud lighting
flipped as the chase boom swung the camera:

- #176: the flipping unit is a CELL -> discontinuity lines at exactly
  cell-seam granularity; a torch-losing floor drops to dim blue-grey
  stone at 0.2 ambient (the perceived purple), camera-angle dependent.
- #177: a stair room whose torches all ranked past the cap rendered at
  bare 0.2 ambient (near-black = 'not visible'); approach re-admitted
  them ('pops into existence'); the sweeping boundary dropped the
  ramp's lights mid-descent ('disappears on the last step'). The
  geometry never vanished - its lights did.

Retail's minimize_object_lighting (0x0054d480) has NO global
camera-nearest pool cap (lights register per cell, insert_light
0x0054d1b0). Fix: MaxGlobalLights 128 -> 1024, a non-biting safety
valve (GlobalLightPacker grows to fit; 64 B/light). Register row AP-85.

TDD pin: PointSnapshot_HubScaleLightCount_ObjectSelectionIsCameraInvariant
(RED at 128 with a Hub-scale 401-light layout, GREEN at 1024). The
pre-existing camera-independence pin covered the SELECTOR but not the
SNAPSHOT it selects from - the pop re-entered one stage upstream.

Suites: Core 2588 / App 719 / UI 425 / Net 385 green. Pending user gate.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-05 21:55:43 +02:00

315 lines
12 KiB
C#

using System.Numerics;
using AcDream.Core.Lighting;
using Xunit;
namespace AcDream.Core.Tests.Lighting;
public sealed class LightManagerTests
{
private static LightSource MakePoint(Vector3 pos, float range, uint ownerId = 0, bool lit = true)
=> new LightSource
{
Kind = LightKind.Point,
WorldPosition = pos,
Range = range,
IsLit = lit,
OwnerId = ownerId,
};
[Fact]
public void Register_Unregister_TracksList()
{
var mgr = new LightManager();
var a = MakePoint(Vector3.Zero, 5f);
var b = MakePoint(new Vector3(10, 0, 0), 5f);
mgr.Register(a);
mgr.Register(b);
Assert.Equal(2, mgr.RegisteredCount);
mgr.Unregister(a);
Assert.Equal(1, mgr.RegisteredCount);
}
[Fact]
public void Register_DuplicateInstance_Idempotent()
{
var mgr = new LightManager();
var light = MakePoint(Vector3.Zero, 5f);
mgr.Register(light);
mgr.Register(light);
Assert.Equal(1, mgr.RegisteredCount);
}
[Fact]
public void Tick_SelectsByDistance_Top8()
{
var mgr = new LightManager();
// 12 lights at varying distances, all with range 100 so none filter out.
for (int i = 0; i < 12; i++)
mgr.Register(MakePoint(new Vector3(i, 0, 0), 100f));
mgr.Tick(viewerWorldPos: Vector3.Zero);
Assert.Equal(8, mgr.ActiveCount);
// Top 8 should be the closest (i=0..7).
foreach (var l in mgr.Active)
{
Assert.NotNull(l);
Assert.True(l!.WorldPosition.X <= 7f);
}
}
[Fact]
public void Tick_SelectsByDistance_RegardlessOfViewerRange()
{
// Retail D3D-style: candidacy is distance-only (the nearest 8). A torch
// lights its OWN surfaces — the shader applies the hard `d < range` cutoff
// PER FRAGMENT (mesh_modern.frag) — so a torch the VIEWER is standing
// outside the range of is still selected; it lights the wall it sits on.
// Replaces the old viewer-range candidacy filter that suppressed it, which
// left dungeon rooms (2227 registered torches) at activeLights≈1 / flat 0.2
// ambient — the "dungeon lighting off" report (#133 A7).
var mgr = new LightManager();
mgr.Register(MakePoint(new Vector3(20, 0, 0), range: 5f)); // viewer outside the torch's range
mgr.Tick(viewerWorldPos: Vector3.Zero);
Assert.Equal(1, mgr.ActiveCount); // selected by distance; the shader culls per-surface
}
[Fact]
public void Tick_IncludesNearbyLight()
{
var mgr = new LightManager();
// A nearby point light is selected (distance-only candidacy; the shader
// applies the per-fragment range cutoff).
mgr.Register(MakePoint(new Vector3(5, 0, 0), range: 5f));
mgr.Tick(viewerWorldPos: Vector3.Zero);
Assert.Equal(1, mgr.ActiveCount);
}
[Fact]
public void Tick_SunSlot0_PreservedAcrossTicks()
{
var mgr = new LightManager();
var sun = new LightSource { Kind = LightKind.Directional, WorldForward = -Vector3.UnitZ };
mgr.Sun = sun;
mgr.Register(MakePoint(Vector3.Zero, 100f));
mgr.Tick(Vector3.Zero);
Assert.Equal(2, mgr.ActiveCount);
Assert.Same(sun, mgr.Active[0]);
}
[Fact]
public void Tick_UnlitLight_Excluded()
{
var mgr = new LightManager();
var light = MakePoint(Vector3.Zero, 100f, lit: false);
mgr.Register(light);
mgr.Tick(Vector3.Zero);
Assert.Equal(0, mgr.ActiveCount);
// Toggle lit: should now appear.
light.IsLit = true;
mgr.Tick(Vector3.Zero);
Assert.Equal(1, mgr.ActiveCount);
}
[Fact]
public void UnregisterByOwner_RemovesAttachedLights()
{
var mgr = new LightManager();
mgr.Register(MakePoint(Vector3.Zero, 5f, ownerId: 42));
mgr.Register(MakePoint(new Vector3(1, 0, 0), 5f, ownerId: 42));
mgr.Register(MakePoint(new Vector3(2, 0, 0), 5f, ownerId: 99));
mgr.UnregisterByOwner(42);
Assert.Equal(1, mgr.RegisteredCount);
}
[Fact]
public void DistSq_UpdatedEachTick()
{
var mgr = new LightManager();
var light = MakePoint(new Vector3(3, 0, 4), 10f); // dist 5
mgr.Register(light);
mgr.Tick(Vector3.Zero);
Assert.Equal(25f, light.DistSq, 2);
mgr.Tick(new Vector3(3, 0, 0)); // same x, same y, z diff 4
Assert.Equal(16f, light.DistSq, 2);
}
// ── Fix B: per-object selection (minimize_object_lighting) ────────────────
[Fact]
public void BuildPointLightSnapshot_ExcludesDirectionalAndUnlit()
{
var mgr = new LightManager();
mgr.Register(MakePoint(new Vector3(1, 0, 0), 5f)); // in
mgr.Register(MakePoint(new Vector3(2, 0, 0), 5f, lit: false)); // unlit → out
mgr.Register(new LightSource { Kind = LightKind.Directional }); // sun → out
mgr.BuildPointLightSnapshot(Vector3.Zero);
Assert.Single(mgr.PointSnapshot);
Assert.Equal(1f, mgr.PointSnapshot[0].WorldPosition.X, 3);
}
[Fact]
public void BuildPointLightSnapshot_IndexStable_InBudget()
{
var mgr = new LightManager();
// Registration order preserved when under MaxGlobalLights (no sort).
mgr.Register(MakePoint(new Vector3(100, 0, 0), 5f)); // far
mgr.Register(MakePoint(new Vector3(1, 0, 0), 5f)); // near
mgr.BuildPointLightSnapshot(Vector3.Zero);
Assert.Equal(2, mgr.PointSnapshot.Count);
Assert.Equal(100f, mgr.PointSnapshot[0].WorldPosition.X, 3); // index 0 = first registered
Assert.Equal(1f, mgr.PointSnapshot[1].WorldPosition.X, 3);
}
[Fact]
public void SelectForObject_EmptySnapshot_ReturnsZero()
{
Span<int> idx = stackalloc int[8];
int n = LightManager.SelectForObject(System.Array.Empty<LightSource>(), Vector3.Zero, 1f, idx);
Assert.Equal(0, n);
}
[Fact]
public void SelectForObject_InRange_Selected()
{
var snapshot = new[] { MakePoint(new Vector3(3, 0, 0), range: 5f) }; // dist 3 < range 5
Span<int> idx = stackalloc int[8];
int n = LightManager.SelectForObject(snapshot, Vector3.Zero, radius: 0f, idx);
Assert.Equal(1, n);
Assert.Equal(0, idx[0]);
}
[Fact]
public void SelectForObject_OutOfRange_Excluded()
{
// dist 10, range 5, radius 0 → 10 >= 5 → excluded.
var snapshot = new[] { MakePoint(new Vector3(10, 0, 0), range: 5f) };
Span<int> idx = stackalloc int[8];
int n = LightManager.SelectForObject(snapshot, Vector3.Zero, radius: 0f, idx);
Assert.Equal(0, n);
}
[Fact]
public void SelectForObject_ObjectRadiusExtendsReach()
{
// dist 7, range 5: out of reach at radius 0, but a radius-3 object sphere
// overlaps (7 < 5+3). The whole object catches the light — retail uses the
// object's bounding sphere, not its centre point.
var snapshot = new[] { MakePoint(new Vector3(7, 0, 0), range: 5f) };
Span<int> idx = stackalloc int[8];
Assert.Equal(0, LightManager.SelectForObject(snapshot, Vector3.Zero, radius: 0f, idx));
Assert.Equal(1, LightManager.SelectForObject(snapshot, Vector3.Zero, radius: 3f, idx));
}
[Fact]
public void SelectForObject_MoreThan8_KeepsNearest8()
{
// 10 candidate lights all in range; expect the 8 nearest the object centre,
// ascending by distance, with the two farthest dropped.
var snapshot = new LightSource[10];
for (int i = 0; i < 10; i++)
snapshot[i] = MakePoint(new Vector3(i + 1, 0, 0), range: 100f); // dist i+1, all in range
Span<int> idx = stackalloc int[8];
int n = LightManager.SelectForObject(snapshot, Vector3.Zero, radius: 0f, idx);
Assert.Equal(8, n);
// Nearest-first: index 0 (dist 1) … index 7 (dist 8). The two farthest
// (indices 8,9 / dist 9,10) are evicted.
for (int k = 0; k < 8; k++)
Assert.Equal(k, idx[k]);
}
[Fact]
public void SelectForObject_CameraIndependent_DependsOnlyOnObjectCentre()
{
// Same snapshot, same object centre → identical selection regardless of
// where any "camera" is (the method takes no camera). This is the property
// that kills the "lights up as I approach" popping.
var snapshot = new[]
{
MakePoint(new Vector3(2, 0, 0), range: 10f),
MakePoint(new Vector3(20, 0, 0), range: 10f), // out of reach of centre 0
};
Span<int> a = stackalloc int[8];
Span<int> b = stackalloc int[8];
int na = LightManager.SelectForObject(snapshot, Vector3.Zero, 1f, a);
int nb = LightManager.SelectForObject(snapshot, Vector3.Zero, 1f, b);
Assert.Equal(1, na);
Assert.Equal(na, nb);
Assert.Equal(a[0], b[0]);
}
/// <summary>
/// #176/#177 (2026-07-06): SelectForObject is camera-independent (the test
/// above), but the SNAPSHOT it selects from was not — BuildPointLightSnapshot
/// kept only the MaxGlobalLights nearest THE CAMERA. In the Facility Hub
/// (366 registered fixtures vs the old cap of 128), an in-range torch of a
/// VISIBLE cell could rank past the cap and be evicted, so the cell's 8-set
/// (and its Gouraud vertex lighting) flipped as the camera moved — per-cell
/// lighting pops at seam granularity (#176's flash), and a whole room's
/// torches vanishing until approach (#177's pop-in). Retail's
/// minimize_object_lighting (0x0054d480) has NO global camera-nearest cap —
/// every registered light reaching the object is a candidate. This pins the
/// end-to-end property: a light in range of an object stays selected no
/// matter where the camera is, at Facility-Hub-scale light counts.
/// </summary>
[Fact]
public void PointSnapshot_HubScaleLightCount_ObjectSelectionIsCameraInvariant()
{
var mgr = new LightManager();
// 400 fixtures clustered near the origin (the "camera side" of the
// dungeon) — these fill every low camera-distance rank.
for (int i = 0; i < 400; i++)
mgr.Register(MakePoint(new Vector3(i * 0.05f, 0, 0), range: 5f, ownerId: (uint)(i + 1)));
// The target torch: far from the origin-side camera (rank ~401), but
// squarely in range of the target cell around (200, 0, 0).
var torch = MakePoint(new Vector3(198f, 0, 0), range: 15f, ownerId: 0xF00DF00Du);
mgr.Register(torch);
Span<int> sel = stackalloc int[LightManager.MaxLightsPerObject];
// Camera parked at the origin end — the torch must still light the cell.
mgr.BuildPointLightSnapshot(cameraWorldPos: Vector3.Zero);
int n1 = LightManager.SelectForObject(mgr.PointSnapshot, new Vector3(200f, 0, 0), 6f, sel);
bool torchSelectedFar = SelectedContains(mgr.PointSnapshot, sel, n1, torch);
// Camera next to the cell — the reference behavior.
mgr.BuildPointLightSnapshot(cameraWorldPos: new Vector3(200f, 0, 0));
int n2 = LightManager.SelectForObject(mgr.PointSnapshot, new Vector3(200f, 0, 0), 6f, sel);
bool torchSelectedNear = SelectedContains(mgr.PointSnapshot, sel, n2, torch);
Assert.True(torchSelectedNear, "sanity: the torch reaches the cell when the camera is beside it");
Assert.True(torchSelectedFar,
"an in-range light of a visible cell was evicted by the camera-nearest snapshot cap — " +
"per-cell lighting would pop with camera movement (the #176/#177 mechanism)");
static bool SelectedContains(
System.Collections.Generic.IReadOnlyList<LightSource> snapshot,
Span<int> indices, int count, LightSource target)
{
for (int i = 0; i < count; i++)
if (ReferenceEquals(snapshot[indices[i]], target)) return true;
return false;
}
}
}