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>
295 lines
9.6 KiB
C#
295 lines
9.6 KiB
C#
using System.Numerics;
|
|
using AcDream.App.Rendering;
|
|
using AcDream.UI.Abstractions.Input;
|
|
using Silk.NET.Input;
|
|
|
|
namespace AcDream.App.Input;
|
|
|
|
internal interface IMouseEventSurface
|
|
{
|
|
void AddMouseDown(Action<MouseButton> callback);
|
|
void RemoveMouseDown(Action<MouseButton> callback);
|
|
void AddMouseUp(Action<MouseButton> callback);
|
|
void RemoveMouseUp(Action<MouseButton> callback);
|
|
void AddMouseMove(Action<Vector2> callback);
|
|
void RemoveMouseMove(Action<Vector2> callback);
|
|
void AddScroll(Action<float> callback);
|
|
void RemoveScroll(Action<float> callback);
|
|
bool IsButtonPressed(MouseButton button);
|
|
}
|
|
|
|
internal sealed class SilkMouseEventSurface : IMouseEventSurface
|
|
{
|
|
private readonly IMouse _mouse;
|
|
private Action<MouseButton>? _downCallback;
|
|
private Action<MouseButton>? _upCallback;
|
|
private Action<Vector2>? _moveCallback;
|
|
private Action<float>? _scrollCallback;
|
|
private readonly Action<IMouse, MouseButton> _down;
|
|
private readonly Action<IMouse, MouseButton> _up;
|
|
private readonly Action<IMouse, Vector2> _move;
|
|
private readonly Action<IMouse, ScrollWheel> _scroll;
|
|
|
|
public SilkMouseEventSurface(IMouse mouse)
|
|
{
|
|
_mouse = mouse ?? throw new ArgumentNullException(nameof(mouse));
|
|
_down = OnDown;
|
|
_up = OnUp;
|
|
_move = OnMove;
|
|
_scroll = OnScroll;
|
|
}
|
|
|
|
public void AddMouseDown(Action<MouseButton> callback)
|
|
{
|
|
_downCallback = callback ?? throw new ArgumentNullException(nameof(callback));
|
|
_mouse.MouseDown += _down;
|
|
}
|
|
|
|
public void RemoveMouseDown(Action<MouseButton> callback)
|
|
{
|
|
if (!ReferenceEquals(_downCallback, callback)) return;
|
|
_mouse.MouseDown -= _down;
|
|
_downCallback = null;
|
|
}
|
|
|
|
public void AddMouseUp(Action<MouseButton> callback)
|
|
{
|
|
_upCallback = callback ?? throw new ArgumentNullException(nameof(callback));
|
|
_mouse.MouseUp += _up;
|
|
}
|
|
|
|
public void RemoveMouseUp(Action<MouseButton> callback)
|
|
{
|
|
if (!ReferenceEquals(_upCallback, callback)) return;
|
|
_mouse.MouseUp -= _up;
|
|
_upCallback = null;
|
|
}
|
|
|
|
public void AddMouseMove(Action<Vector2> callback)
|
|
{
|
|
_moveCallback = callback ?? throw new ArgumentNullException(nameof(callback));
|
|
_mouse.MouseMove += _move;
|
|
}
|
|
|
|
public void RemoveMouseMove(Action<Vector2> callback)
|
|
{
|
|
if (!ReferenceEquals(_moveCallback, callback)) return;
|
|
_mouse.MouseMove -= _move;
|
|
_moveCallback = null;
|
|
}
|
|
|
|
public void AddScroll(Action<float> callback)
|
|
{
|
|
_scrollCallback = callback ?? throw new ArgumentNullException(nameof(callback));
|
|
_mouse.Scroll += _scroll;
|
|
}
|
|
|
|
public void RemoveScroll(Action<float> callback)
|
|
{
|
|
if (!ReferenceEquals(_scrollCallback, callback)) return;
|
|
_mouse.Scroll -= _scroll;
|
|
_scrollCallback = null;
|
|
}
|
|
|
|
public bool IsButtonPressed(MouseButton button) => _mouse.IsButtonPressed(button);
|
|
|
|
private void OnDown(IMouse _, MouseButton button) => _downCallback?.Invoke(button);
|
|
private void OnUp(IMouse _, MouseButton button) => _upCallback?.Invoke(button);
|
|
private void OnMove(IMouse _, Vector2 position) => _moveCallback?.Invoke(position);
|
|
private void OnScroll(IMouse _, ScrollWheel scroll) => _scrollCallback?.Invoke(scroll.Y);
|
|
}
|
|
|
|
/// <summary>Reversible Silk mouse bridge with immediate logical cutoff.</summary>
|
|
public sealed class SilkMouseSource : IMouseSource, IDisposable
|
|
{
|
|
private readonly IMouseEventSurface _surface;
|
|
private readonly IInputCaptureSource _capture;
|
|
private readonly HostQuiescenceGate _quiescence;
|
|
private readonly Action<MouseButton> _mouseDown;
|
|
private readonly Action<MouseButton> _mouseUp;
|
|
private readonly Action<Vector2> _mouseMove;
|
|
private readonly Action<float> _scroll;
|
|
private readonly bool[] _attached = new bool[4];
|
|
private ResourceShutdownTransaction? _detach;
|
|
private bool _attachStarted;
|
|
private int _disposeRequested;
|
|
private int _active;
|
|
private float _lastX;
|
|
private float _lastY;
|
|
private bool _haveLastPosition;
|
|
|
|
public event Action<MouseButton, ModifierMask>? MouseDown;
|
|
public event Action<MouseButton, ModifierMask>? MouseUp;
|
|
public event Action<float, float>? MouseMove;
|
|
public event Action<float>? Scroll;
|
|
|
|
public IKeyboardSource? ModifierSource { get; set; }
|
|
|
|
private SilkMouseSource(
|
|
IMouseEventSurface surface,
|
|
IInputCaptureSource capture,
|
|
IKeyboardSource? modifierSource,
|
|
HostQuiescenceGate quiescence)
|
|
{
|
|
_surface = surface ?? throw new ArgumentNullException(nameof(surface));
|
|
_capture = capture ?? throw new ArgumentNullException(nameof(capture));
|
|
ModifierSource = modifierSource;
|
|
_quiescence = quiescence ?? throw new ArgumentNullException(nameof(quiescence));
|
|
_mouseDown = OnMouseDown;
|
|
_mouseUp = OnMouseUp;
|
|
_mouseMove = OnMouseMove;
|
|
_scroll = OnScroll;
|
|
}
|
|
|
|
internal static SilkMouseSource CreateDetached(
|
|
IMouse mouse,
|
|
IInputCaptureSource capture,
|
|
IKeyboardSource? modifierSource,
|
|
HostQuiescenceGate quiescence) =>
|
|
new(
|
|
new SilkMouseEventSurface(mouse),
|
|
capture,
|
|
modifierSource,
|
|
quiescence);
|
|
|
|
internal static SilkMouseSource CreateDetached(
|
|
IMouseEventSurface surface,
|
|
IInputCaptureSource capture,
|
|
IKeyboardSource? modifierSource,
|
|
HostQuiescenceGate quiescence) =>
|
|
new(surface, capture, modifierSource, quiescence);
|
|
|
|
public bool IsDisposalComplete => _attached.All(static value => !value);
|
|
|
|
public void Attach()
|
|
{
|
|
ObjectDisposedException.ThrowIf(
|
|
Volatile.Read(ref _disposeRequested) != 0,
|
|
this);
|
|
if (_attachStarted)
|
|
throw new InvalidOperationException("Mouse source attachment has already started.");
|
|
_attachStarted = true;
|
|
try
|
|
{
|
|
_attached[0] = true;
|
|
_surface.AddMouseDown(_mouseDown);
|
|
_attached[1] = true;
|
|
_surface.AddMouseUp(_mouseUp);
|
|
_attached[2] = true;
|
|
_surface.AddMouseMove(_mouseMove);
|
|
_attached[3] = true;
|
|
_surface.AddScroll(_scroll);
|
|
Volatile.Write(ref _active, 1);
|
|
}
|
|
catch (Exception attachError)
|
|
{
|
|
Deactivate();
|
|
RollBackOrThrow(attachError);
|
|
}
|
|
}
|
|
|
|
public bool IsHeld(MouseButton button) => _surface.IsButtonPressed(button);
|
|
public bool WantCaptureMouse => _capture.WantCaptureMouse;
|
|
public bool WantCaptureKeyboard => _capture.WantCaptureKeyboard;
|
|
|
|
public void Deactivate() => Interlocked.Exchange(ref _active, 0);
|
|
|
|
public void Dispose()
|
|
{
|
|
Interlocked.Exchange(ref _disposeRequested, 1);
|
|
Deactivate();
|
|
EnsureDetachTransaction().CompleteOrThrow();
|
|
}
|
|
|
|
private ModifierMask ReadModifiers() =>
|
|
ModifierSource?.CurrentModifiers ?? ModifierMask.None;
|
|
|
|
private void OnMouseDown(MouseButton button) =>
|
|
_quiescence.Invoke(() =>
|
|
{
|
|
if (Volatile.Read(ref _active) != 0)
|
|
MouseDown?.Invoke(button, ReadModifiers());
|
|
});
|
|
|
|
private void OnMouseUp(MouseButton button) =>
|
|
_quiescence.Invoke(() =>
|
|
{
|
|
if (Volatile.Read(ref _active) != 0)
|
|
MouseUp?.Invoke(button, ReadModifiers());
|
|
});
|
|
|
|
private void OnMouseMove(Vector2 position) =>
|
|
_quiescence.Invoke(() =>
|
|
{
|
|
if (Volatile.Read(ref _active) == 0)
|
|
return;
|
|
|
|
float dx = 0f;
|
|
float dy = 0f;
|
|
if (_haveLastPosition)
|
|
{
|
|
dx = position.X - _lastX;
|
|
dy = position.Y - _lastY;
|
|
}
|
|
else
|
|
{
|
|
_haveLastPosition = true;
|
|
}
|
|
|
|
_lastX = position.X;
|
|
_lastY = position.Y;
|
|
MouseMove?.Invoke(dx, dy);
|
|
});
|
|
|
|
private void OnScroll(float delta) =>
|
|
_quiescence.Invoke(() =>
|
|
{
|
|
if (Volatile.Read(ref _active) != 0)
|
|
Scroll?.Invoke(delta);
|
|
});
|
|
|
|
private ResourceShutdownTransaction EnsureDetachTransaction() =>
|
|
_detach ??= new ResourceShutdownTransaction(
|
|
new ResourceShutdownStage("mouse source callbacks",
|
|
[
|
|
new("scroll", () => Remove(3)),
|
|
new("move", () => Remove(2)),
|
|
new("up", () => Remove(1)),
|
|
new("down", () => Remove(0)),
|
|
]));
|
|
|
|
private void Remove(int index)
|
|
{
|
|
if (!_attached[index])
|
|
return;
|
|
switch (index)
|
|
{
|
|
case 0: _surface.RemoveMouseDown(_mouseDown); break;
|
|
case 1: _surface.RemoveMouseUp(_mouseUp); break;
|
|
case 2: _surface.RemoveMouseMove(_mouseMove); break;
|
|
case 3: _surface.RemoveScroll(_scroll); break;
|
|
default: throw new ArgumentOutOfRangeException(nameof(index));
|
|
}
|
|
_attached[index] = false;
|
|
}
|
|
|
|
private void RollBackOrThrow(Exception attachError)
|
|
{
|
|
try
|
|
{
|
|
EnsureDetachTransaction().CompleteOrThrow();
|
|
}
|
|
catch (Exception rollbackError)
|
|
{
|
|
throw new AggregateException(
|
|
"Mouse source registration and rollback both failed.",
|
|
new InvalidOperationException(
|
|
"Mouse source registration failed.", attachError),
|
|
rollbackError);
|
|
}
|
|
|
|
throw new InvalidOperationException(
|
|
"Mouse source registration failed and was rolled back.",
|
|
attachError);
|
|
}
|
|
}
|