feat: throttle Godot VM to a per-frame op budget on FrameClock (fixes opening speed-through)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -13,12 +13,15 @@ public sealed class GodotAdvHost : IHost
|
|||||||
// the single-scene harness skips; seed it so the first CG's anchor math stays correct (not 0x0).
|
// the single-scene harness skips; seed it so the first CG's anchor math stays correct (not 0x0).
|
||||||
private readonly Dictionary<int, (int W, int H)> _slotDims = new() { { 0, (800, 600) } };
|
private readonly Dictionary<int, (int W, int H)> _slotDims = new() { { 0, (800, 600) } };
|
||||||
private readonly SemaphoreSlim _gate = new(0, 1);
|
private readonly SemaphoreSlim _gate = new(0, 1);
|
||||||
|
private readonly Age.Engine.Hosting.FrameClock _clock;
|
||||||
|
private readonly System.Threading.AutoResetEvent _frameSignal = new(false);
|
||||||
|
private int _opsSinceYield;
|
||||||
public volatile bool IsWaiting;
|
public volatile bool IsWaiting;
|
||||||
public readonly List<(int Offset, string Text)> Captured = new();
|
public readonly List<(int Offset, string Text)> Captured = new();
|
||||||
|
|
||||||
public GodotAdvHost(Main main, ResourceMap res, string scene)
|
public GodotAdvHost(Main main, ResourceMap res, string scene, Age.Engine.Hosting.FrameClock clock)
|
||||||
{
|
{
|
||||||
_main = main; _res = res; _scene = scene;
|
_main = main; _res = res; _scene = scene; _clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ShowText(int offset, string text)
|
public void ShowText(int offset, string text)
|
||||||
@@ -42,16 +45,35 @@ public sealed class GodotAdvHost : IHost
|
|||||||
// called from the main thread (click) or the selftest auto-clicker
|
// called from the main thread (click) or the selftest auto-clicker
|
||||||
public void SignalInput() { if (_gate.CurrentCount == 0) _gate.Release(); }
|
public void SignalInput() { if (_gate.CurrentCount == 0) _gate.Release(); }
|
||||||
|
|
||||||
|
// Main thread, once per rendered frame: releases a VM thread parked in FrameYield/Sleep.
|
||||||
|
public void PulseFrame() => _frameSignal.Set();
|
||||||
|
|
||||||
|
// Called once per executed opcode (IHost.FrameYield). After a frame's worth of ops (the clock's
|
||||||
|
// budget), block the VM background thread until Main._Process advances the clock — throttling the
|
||||||
|
// interpreter to ~budget ops per rendered frame (the native engine's rate-limited cadence).
|
||||||
|
public void FrameYield()
|
||||||
|
{
|
||||||
|
if (++_opsSinceYield < _clock.EffectiveBudget) return;
|
||||||
|
_opsSinceYield = 0;
|
||||||
|
long start = _clock.NowMs;
|
||||||
|
while (_clock.NowMs == start) // wait until a real _Process advanced the clock
|
||||||
|
if (!_frameSignal.WaitOne(50)) break; // 50ms safety cap: never hang if _Process stalls
|
||||||
|
}
|
||||||
|
|
||||||
// op 0xc8: block the VM background thread so the main-thread compositor (Main.Recomposite in _Process)
|
// op 0xc8: block the VM background thread so the main-thread compositor (Main.Recomposite in _Process)
|
||||||
// presents the current retained GfxState — this is what makes the sleep-paced opening burst animate.
|
// presents the current retained GfxState — this is what makes the sleep-paced opening burst animate.
|
||||||
// Time-based sibling of WaitForInput's suspend. The native op arms a non-blocking main-loop-polled timer;
|
// Time-based sibling of WaitForInput's suspend. The native op arms a non-blocking main-loop-polled timer;
|
||||||
// blocking this throwaway task thread is behaviorally equivalent given our threading model. Operand is
|
// blocking this throwaway task thread is behaviorally equivalent given our threading model. Operand is
|
||||||
// MILLISECONDS (docs/engine-re.md sleep section + opcodes.toml 0xc8). Headless CLI hosts no-op it (parity).
|
// MILLISECONDS (docs/engine-re.md sleep section + opcodes.toml 0xc8). Headless CLI hosts no-op it (parity).
|
||||||
public double SleepScale = 1.0; // --sleep-scale <f>: debug multiplier to slow/speed the paced opening for inspection
|
public double SleepScale = 1.0; // --sleep-scale <f>: debug multiplier to slow/speed the paced opening for inspection
|
||||||
|
// Wait on the unified FrameClock timebase (not Thread.Sleep) so a future Speed multiplier scales
|
||||||
|
// sleeps together with the throttle and the tween. Main._Process advances the clock + pulses each frame.
|
||||||
public void Sleep(long duration)
|
public void Sleep(long duration)
|
||||||
{
|
{
|
||||||
int ms = (int)System.Math.Clamp(duration * SleepScale, 0, 60_000); // cap so a pathological script can't hang the window
|
long ms = (long)System.Math.Clamp(duration * SleepScale, 0, 60_000); // cap so a pathological script can't hang the window
|
||||||
if (ms > 0) Thread.Sleep(ms);
|
long deadline = _clock.NowMs + ms;
|
||||||
|
while (_clock.NowMs < deadline)
|
||||||
|
if (!_frameSignal.WaitOne(2000)) break; // safety cap
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- texture ops (run on the VM thread; marshal Godot node work to the main thread) ----
|
// ---- texture ops (run on the VM thread; marshal Godot node work to the main thread) ----
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public partial class Main : Godot.Control
|
|||||||
private AudioStreamPlayer _voice = null!; // interrupt-on-new voice
|
private AudioStreamPlayer _voice = null!; // interrupt-on-new voice
|
||||||
private VirtualMachine _vm = null!;
|
private VirtualMachine _vm = null!;
|
||||||
private GodotAdvHost _host = null!;
|
private GodotAdvHost _host = null!;
|
||||||
|
private readonly Age.Engine.Hosting.FrameClock _clock = new();
|
||||||
private GodotTraceSink _trace = null!;
|
private GodotTraceSink _trace = null!;
|
||||||
private Age.Engine.Diagnostics.HistogramTraceSink? _hist; // --trace-histogram: profile the real run
|
private Age.Engine.Diagnostics.HistogramTraceSink? _hist; // --trace-histogram: profile the real run
|
||||||
private string? _histFile;
|
private string? _histFile;
|
||||||
@@ -119,7 +120,7 @@ public partial class Main : Godot.Control
|
|||||||
IScriptProvider provider;
|
IScriptProvider provider;
|
||||||
if (_selftest) (script, provider) = BuildSelfTestScene(table);
|
if (_selftest) (script, provider) = BuildSelfTestScene(table);
|
||||||
else { script = Sys4Loader.Load(Paths.Scripts()[scene.ToUpperInvariant() + ".BIN"], table); provider = Sys4ScriptProvider.Load(table); }
|
else { script = Sys4Loader.Load(Paths.Scripts()[scene.ToUpperInvariant() + ".BIN"], table); provider = Sys4ScriptProvider.Load(table); }
|
||||||
_host = new GodotAdvHost(this, ResourceMap.Load(), scene) { SleepScale = sleepScale };
|
_host = new GodotAdvHost(this, ResourceMap.Load(), scene, _clock) { SleepScale = sleepScale };
|
||||||
_trace = new GodotTraceSink();
|
_trace = new GodotTraceSink();
|
||||||
// --trace-histogram: aggregate op/call-site execution counts of the REAL Godot run (headless flow
|
// --trace-histogram: aggregate op/call-site execution counts of the REAL Godot run (headless flow
|
||||||
// diverges — wait-for-input is a no-op there — so this is the only way to profile the live path).
|
// diverges — wait-for-input is a no-op there — so this is the only way to profile the live path).
|
||||||
@@ -156,6 +157,8 @@ public partial class Main : Godot.Control
|
|||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
_lastDelta = delta;
|
_lastDelta = delta;
|
||||||
|
_clock.Advance(delta);
|
||||||
|
_host?.PulseFrame();
|
||||||
if (!_selftest && _vm != null) Recomposite(); // retained per-frame compositor (surface+object model)
|
if (!_selftest && _vm != null) Recomposite(); // retained per-frame compositor (surface+object model)
|
||||||
// --shot-sequence: dump one PNG per frame across the opening so a time-based (paced) effect can be
|
// --shot-sequence: dump one PNG per frame across the opening so a time-based (paced) effect can be
|
||||||
// verified as distinct frames, not just the final state. Captures after Recomposite; quits when full.
|
// verified as distinct frames, not just the final state. Captures after Recomposite; quits when full.
|
||||||
@@ -278,7 +281,7 @@ public partial class Main : Godot.Control
|
|||||||
tw.TargetA = targetA;
|
tw.TargetA = targetA;
|
||||||
tw.Initialized = true;
|
tw.Initialized = true;
|
||||||
}
|
}
|
||||||
tw.Elapsed += _lastDelta;
|
tw.Elapsed += _lastDelta * _clock.Speed; // Speed==1 now => identical; future Ctrl scales the tween
|
||||||
double p = tw.Duration > 0 ? System.Math.Clamp(tw.Elapsed / tw.Duration, 0, 1) : 1;
|
double p = tw.Duration > 0 ? System.Math.Clamp(tw.Elapsed / tw.Duration, 0, 1) : 1;
|
||||||
tw.CurrentA = tw.StartA + (tw.TargetA - tw.StartA) * p;
|
tw.CurrentA = tw.StartA + (tw.TargetA - tw.StartA) * p;
|
||||||
return (float)tw.CurrentA;
|
return (float)tw.CurrentA;
|
||||||
|
|||||||
Reference in New Issue
Block a user