This reverts ceec3bc4. Two independent reasons, either sufficient.
The rendering regression. The slice deleted TextRenderGlStateScope, which
saved GL_MULTISAMPLE and GL_SAMPLE_ALPHA_TO_COVERAGE on entry, disabled them
for the text pass, and restored them on exit (TextRenderGlStateScope.cs:111-112
and 153-154 at the parent commit). Its replacement bakes that state into the
text pipeline but nothing restores it, and GlGpuPassEncoder.Dispose does not
either. Every world renderer is still raw GL at this point in the campaign, so
from the first UI frame onward the world drew with multisampling disabled.
The offline pixel gate caught it: 1,791 of 563,200 compared pixels differed,
0.318% against a 0.001 threshold. The commit message attributed this to
wall-clock-driven ambient animation shifting phase, and committed through the
failure. That explanation does not survive its own control: capturing twice at
the reverted-to commit differs by 19 pixels and twice at the slice's own commit
by 8, while base-versus-head differs by 1,791 - a 224x gap that no shared-noise
source explains. An amplified difference image settles it visually: the changed
pixels are the silhouette edges of every tree, building and rock, with terrain
interiors, water and the entire UI untouched. That is the signature of losing
edge antialiasing, not of animated sprites.
This is the exact failure mode two existing memory notes already warn about -
a mid-frame renderer must set every GL state it uses rather than inherit it,
and issue #52's lesson that a rendering migration must audit per-pass GL state
before declaring itself done.
The scope. The brief was three small leaf renderers plus additive frame-
lifecycle wiring, roughly ten files. The commit changed 334 files with 3,665
insertions and 3,845 deletions, including 323 public-to-internal visibility
conversions across the App assembly, 55 test files, two retired conformance
tests, and a self-described temporary escape hatch for bridging raw-GL viewport
textures. Even without the regression, that is not separable into the part
worth keeping and the part worth dropping.
Reverting rather than patching because the good work here - the RHI frame
lifecycle wiring and a genuine render-state-cache staleness fix - is small
enough to redo cleanly against a tightened spec, while untangling it from 300+
files of unrelated churn is not.
Post-revert: Release build clean, App suite back to 3,843 passed / 3 skipped,
offline pixel gate passing at 19 differing pixels.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
204 lines
7.3 KiB
C#
204 lines
7.3 KiB
C#
using AcDream.App.World;
|
|
using AcDream.Core.Lighting;
|
|
using AcDream.Core.Physics;
|
|
using AcDream.Runtime.Entities;
|
|
using DatReaderWriter.DBObjs;
|
|
|
|
namespace AcDream.App.Rendering.Vfx;
|
|
|
|
/// <summary>
|
|
/// Projects Setup lights for live top-level and attached entities, while the
|
|
/// shared pose registry supplies their current root on each update frame.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Retail <c>CPartArray::SetFrame</c> (<c>0x00519310</c>) calls
|
|
/// <c>LIGHTLIST::set_frame</c> (<c>0x00517C60</c>) whenever the owning physics
|
|
/// object's frame changes. Logical ownership remains in
|
|
/// <see cref="LiveEntityRuntime"/>; leaving the world removes only this
|
|
/// cell-scoped presentation and re-entry registers it again.
|
|
/// </remarks>
|
|
public sealed class LiveEntityLightController : IDisposable
|
|
{
|
|
private readonly LiveEntityRuntime _liveEntities;
|
|
private readonly EntityEffectPoseRegistry _poses;
|
|
private readonly LightingHookSink _lighting;
|
|
private readonly Func<uint, Setup?> _loadSetup;
|
|
private readonly HashSet<RuntimeEntityKey> _trackedOwners = [];
|
|
private readonly HashSet<RuntimeEntityKey> _presentOwners = [];
|
|
|
|
public LiveEntityLightController(
|
|
LiveEntityRuntime liveEntities,
|
|
EntityEffectPoseRegistry poses,
|
|
LightingHookSink lighting,
|
|
Func<uint, Setup?> loadSetup)
|
|
{
|
|
_liveEntities = liveEntities ?? throw new ArgumentNullException(nameof(liveEntities));
|
|
_poses = poses ?? throw new ArgumentNullException(nameof(poses));
|
|
_lighting = lighting ?? throw new ArgumentNullException(nameof(lighting));
|
|
_loadSetup = loadSetup ?? throw new ArgumentNullException(nameof(loadSetup));
|
|
_lighting.OwnerLightingChanged += OnOwnerLightingChanged;
|
|
_liveEntities.ProjectionVisibilityChanged += OnProjectionVisibilityChanged;
|
|
}
|
|
|
|
public bool Register(uint serverGuid)
|
|
{
|
|
if (!_liveEntities.TryGetRecord(serverGuid, out LiveEntityRecord record)
|
|
|| record.WorldEntity is not { } entity
|
|
|| !record.IsSpatiallyProjected
|
|
|| !record.IsSpatiallyVisible)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
RuntimeEntityKey key = RequireProjectionKey(record);
|
|
_trackedOwners.Add(key);
|
|
_presentOwners.Add(key);
|
|
_lighting.UnregisterOwner(entity.Id, forgetState: false);
|
|
_lighting.InitializeOwnerLighting(
|
|
entity.Id,
|
|
(record.FinalPhysicsState & PhysicsStateFlags.Lighting) != 0);
|
|
if ((entity.SourceGfxObjOrSetupId & 0xFF000000u) != 0x02000000u)
|
|
return false;
|
|
|
|
if (record.ProjectionKind is LiveEntityProjectionKind.World)
|
|
{
|
|
if (!_poses.UpdateRoot(entity))
|
|
_poses.PublishMeshRefs(entity);
|
|
}
|
|
|
|
Setup? setup = _loadSetup(entity.SourceGfxObjOrSetupId);
|
|
if (setup is null
|
|
|| setup.Lights.Count == 0
|
|
|| !_lighting.IsOwnerLightingEnabled(entity.Id))
|
|
return false;
|
|
|
|
bool isDynamic = (record.FinalPhysicsState & PhysicsStateFlags.Static) == 0;
|
|
IReadOnlyList<LightSource> loaded = LightInfoLoader.Load(
|
|
setup,
|
|
entity.Id,
|
|
entity.Position,
|
|
entity.Rotation,
|
|
isDynamic,
|
|
entity.ParentCellId ?? 0u,
|
|
tracksOwnerPose: true);
|
|
for (int i = 0; i < loaded.Count; i++)
|
|
_lighting.RegisterOwnedLight(loaded[i]);
|
|
return loaded.Count > 0;
|
|
}
|
|
|
|
/// <summary>Withdraw cell-scoped presentation but retain logical light state.</summary>
|
|
public void Unregister(uint ownerLocalId)
|
|
{
|
|
if (_liveEntities.TryGetRecordByLocalEntityId(
|
|
ownerLocalId,
|
|
out LiveEntityRecord record)
|
|
&& record.ProjectionKey is { } key)
|
|
{
|
|
_presentOwners.Remove(key);
|
|
}
|
|
_lighting.UnregisterOwner(ownerLocalId, forgetState: false);
|
|
}
|
|
|
|
/// <summary>Apply a fresh authoritative PhysicsState Lighting transition.</summary>
|
|
public void OnStateChanged(uint serverGuid)
|
|
{
|
|
if (!_liveEntities.TryGetRecord(serverGuid, out LiveEntityRecord record)
|
|
|| record.WorldEntity is not { } entity)
|
|
{
|
|
return;
|
|
}
|
|
_lighting.SetOwnerLighting(
|
|
entity.Id,
|
|
(record.FinalPhysicsState & PhysicsStateFlags.Lighting) != 0);
|
|
}
|
|
|
|
/// <summary>End logical ownership after leave-world presentation teardown.</summary>
|
|
public void Forget(LiveEntityRecord record)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(record);
|
|
if (record.ProjectionKey is not { } key)
|
|
return;
|
|
|
|
_presentOwners.Remove(key);
|
|
_trackedOwners.Remove(key);
|
|
_lighting.UnregisterOwner(key.LocalEntityId);
|
|
}
|
|
|
|
public void Refresh() => _lighting.RefreshAttachedLights();
|
|
|
|
internal int TrackedOwnerCount => _trackedOwners.Count;
|
|
internal int PresentedOwnerCount => _presentOwners.Count;
|
|
|
|
/// <summary>
|
|
/// Attached projection visibility can become true before its holding-part
|
|
/// composition is published. The attachment owner calls this barrier only
|
|
/// after the composed child root is current.
|
|
/// </summary>
|
|
public void OnAttachedPoseReady(uint serverGuid)
|
|
{
|
|
if (!_liveEntities.TryGetRecord(serverGuid, out LiveEntityRecord record)
|
|
|| record.ProjectionKind is not LiveEntityProjectionKind.Attached
|
|
|| record.WorldEntity is not { } entity
|
|
|| record.ProjectionKey is not { } key
|
|
|| _presentOwners.Contains(key))
|
|
{
|
|
return;
|
|
}
|
|
Register(serverGuid);
|
|
}
|
|
|
|
private void OnOwnerLightingChanged(uint ownerLocalId, bool enabled)
|
|
{
|
|
if (!_liveEntities.TryGetRecordByLocalEntityId(
|
|
ownerLocalId,
|
|
out LiveEntityRecord record)
|
|
|| record.ProjectionKey is not { } key
|
|
|| !_presentOwners.Contains(key))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!enabled)
|
|
{
|
|
_lighting.UnregisterOwner(ownerLocalId, forgetState: false);
|
|
return;
|
|
}
|
|
|
|
Register(record.ServerGuid);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_lighting.OwnerLightingChanged -= OnOwnerLightingChanged;
|
|
_liveEntities.ProjectionVisibilityChanged -= OnProjectionVisibilityChanged;
|
|
foreach (RuntimeEntityKey key in _trackedOwners)
|
|
_lighting.UnregisterOwner(key.LocalEntityId);
|
|
_trackedOwners.Clear();
|
|
_presentOwners.Clear();
|
|
}
|
|
|
|
private void OnProjectionVisibilityChanged(LiveEntityRecord record, bool visible)
|
|
{
|
|
if (record.WorldEntity is not { } entity)
|
|
return;
|
|
if (record.ProjectionKind is LiveEntityProjectionKind.Attached)
|
|
{
|
|
// The true edge precedes attachment pose publication. False is
|
|
// safe and must immediately withdraw the cell-scoped light.
|
|
if (!visible)
|
|
Unregister(entity.Id);
|
|
return;
|
|
}
|
|
if (visible)
|
|
Register(record.ServerGuid);
|
|
else
|
|
Unregister(entity.Id);
|
|
}
|
|
|
|
private static RuntimeEntityKey RequireProjectionKey(
|
|
LiveEntityRecord record) =>
|
|
record.ProjectionKey
|
|
?? throw new InvalidOperationException(
|
|
$"Live entity 0x{record.ServerGuid:X8}/{record.Generation} " +
|
|
"has no exact projection key.");
|
|
}
|