Skip to content

world clock C2: every libcvc demo on app.world_clock() (tick_scene binding, nav/lsystem/terrain demos) - #630

Merged
transfix merged 10 commits into
masterfrom
feat/world-clock-demos
Oct 15, 2026
Merged

transfix merged 10 commits into
masterfrom
feat/world-clock-demos

Conversation

@transfix

Copy link
Copy Markdown
Owner

Step C2 of docs/roadmap/WORLD-CLOCK-SYNC-ROADMAP.md. Every demo row of the migration checklist now takes its time from app.world_clock(), driven once per frame through a world_clock_binding, so the sim.* keys control them all. Stacked on #629, which adds the clock modes, clock_follower and the binding.

Library

  • cvc::world_clock_keys: one state path per binding key; an empty path turns that lane off (never seeded, read or written). under(prefix) gives the existing layout. There is a binding constructor that takes keys.
  • world_clock_binding::request_steps(n), for host verbs such as sim_transport's Step button.
  • tick_scene drives the clock through a binding keyed by scene_clock_keys(), so sim.mode, sim.lockstep_steps and sim.step now reach Ariadne scenes. It returns the frame's step_result.
  • sim_thread::wait_caught_up(timeout): a capture waits until the worker has run and published every due step, so threaded demos capture reproducibly.
  • pycvc wraps the keys struct and the new constructor.

Demos

  • nav_convoy: the scene declares clock: {}, and the convoy is drawn at clock.t().
  • nav_fog_ghost: follows the app clock; navdemo::SimPacer is deleted.
  • nav_city_swarm, nav_city_drive: sim_thread(world, clock) with the frame loop as the driver.
    • The initial speed is --hz × veh.dt (3.6×), printed at start-up.
    • The "world speed" slider writes sim.speed, and Step runs one navigator step.
    • The fog and belief-wall repaints run every 15 navigator steps, and the belief pulse follows world time.
  • nav_finale: the acts, the targets and the replans run in world time. Pinch and two-finger gestures now apply while the story runs.
  • lsystem_coast, lsystem_forest, terrain_lab: every animated layer is on world time.
  • ariadne_hello, ariadne_stream: a capture passes its fixed frame dt.

Verified on Windows: MSVC 2022, with VTK 9.5, ImGui and SDL3 from cvcpkg, so the ImGui branches compile.

  • app_test clock binding tests 9/9; world_clock_test 49/49; nav_test NavSimThread 5/5, including the new WaitCaughtUpCoversEveryDueStep.

  • cvcgl_ariadne_realize 100/100, including lockstep, stepping and sim.step through a scene's keys.

  • pycvc test_pycvc_world_clock.py 15/15.

  • Two offscreen captures of each demo are identical frame for frame:

    demo frames
    nav_convoy 24
    nav_fog_ghost 90
    nav_city_swarm (200 agents) 60
    nav_city_drive (120 agents) 60
    nav_finale 160
    lsystem_coast 30
    lsystem_forest 24
    terrain_lab 24

    In each, the scene moves between frames.

  • clang-format 18 is clean on every changed file.

Not verified here: the emscripten builds, the no-worker world.step branches and the browser build's #ifdef paths. They compile only on the wasm toolchain, so CI covers them.

…lock_binding; nav_convoy follows it

world_clock_binding:
- new cvc::world_clock_keys: one state path per key; an empty path turns that
  lane off (never seeded, read or written). world_clock_keys::under(prefix)
  is the existing layout, and the prefix constructor uses it.
- a second constructor takes explicit keys.
- request_steps(n), for host verbs (sim_transport's Step button is
  `on: sim.step`).
- pycvc wraps all of this.

tick_scene:
- builds one binding per realized scene on its first tick, from
  scene_clock_keys(): the scene's own speed/paused/time/tick keys, plus mode,
  lockstep_steps, step, fixed_dt, realized_speed and dropped beside them
  (clk.sim.speed -> clk.sim.mode, ...).
- sim.mode=lockstep, single-step requests and the telemetry now reach Ariadne
  scenes. Before, tick_scene forced live/paused every frame.
- returns the frame's step_result instead of dropping it.
- the declared initial scale and pause still seed unset keys.

Hosts:
- ariadne_hello and ariadne_stream pass their fixed frame dt to tick_scene
  when capturing.
- nav_convoy drops its private simT accumulator. Its scene declares
  `clock: {}`, the convoy is drawn at app.world_clock().t() and rebuilt only
  when the clock moved, Step queues one quantum, and Restart resets the
  clock.

Tests:
- app_test: explicit keys and empty lanes, under(), request_steps;
  9/9 clock binding tests pass.
- cvcgl_ariadne_realize: the derived keys; lockstep runs 3 quanta per frame
  regardless of wall time; stepping runs none; sim.step runs one per
  increment; tick_scene returns first_tick. 100/100 pass.
- world_clock_test: 49/49.
- pycvc: an explicit-keys test.
- Verified on Windows (MSVC 2022, VTK 9.5 from cvcpkg): two 24-frame
  nav_convoy captures are byte-identical.
…SimPacer

nav_fog_ghost configures app.world_clock() at the suite quantum. Settings:
- fixed_dt 0.01, starting at --speed;
- a stall cap of 8 navigator steps, which is what SimPacer allowed.

It drives the clock once per frame through a world_clock_binding, so
sim.speed / sim.paused / sim.mode / sim.step steer it from outside the
window too. The navigator is a clock_follower, stepping every
veh.dt / fixed_dt = 6 quanta.

The ImGui speed slider writes sim.speed. Restart resets the clock along
with the world, so world time starts at 0 again.

SimPacer was a second accumulator that dropped time silently on a stall.
Nothing else used it.

Verified on Windows: two 90-frame offscreen captures (--capture orbit) are
byte-identical frame for frame, and the hero moves between the first and
last frames.
…k; sim_thread::wait_caught_up

Both city demos now run on app.world_clock(), configured as follows:
- the suite quantum, fixed_dt 0.01;
- the old pace as the initial speed: --hz x veh.dt, 3.6x at the defaults;
  the start-up line says so, and --hz's help now describes it;
- a stall cap of 8 navigator steps that scales with the rate.

The frame loop is the one driver, through a world_clock_binding (sim.*):
- the sim_thread follows the shared clock;
- the no-worker browser build steps the navigator with a clock_follower
  instead of once per render frame regardless of wall time.

The ImGui controls are now the clock's own:
- Paused writes sim.paused;
- Step requests exactly one navigator step (veh.dt / fixed_dt quanta)
  and stays paused. Before, "Step one frame" un-paused the worker for a
  render frame and ran however many steps fit;
- the "sim Hz" slider becomes "world speed", which writes sim.speed.

Restart resets the clock with the world.

sim_thread::wait_caught_up(timeout) blocks until the worker has run and
published every step due up to the clock's current tick. A capture calls it
after driving the clock, so each frame shows exactly the steps its quanta
hold.

Verified on Windows (MSVC 2022; ImGui and SDL3 from cvcpkg, so the UI
branches compile):
- nav_test NavSimThread 5/5, including the new
  WaitCaughtUpCoversEveryDueStep;
- nav_city_swarm (200 agents) and nav_city_drive (120): two 60-frame
  --capture orbit runs each are identical frame for frame, 119 navigator
  steps at 3.6x, and the agents move.
Before, nav_finale ran a fixed hz/fps navigator steps per render frame, so it
went faster on a faster display, and moved its targets on a separate wall
time. Now:
- app.world_clock() at the suite quantum is driven once per frame through a
  world_clock_binding, and the navigator is a clock_follower;
- the initial speed is --hz x veh.dt (3.6x), so a 30 fps capture still runs
  about hz/fps steps per frame;
- the acts are scripted in world time. Act 2 opens at the world time a
  capture reaches at 45% of its frames. The targets orbit on clock time, at
  the old 0.25 rad per wall second at the initial speed. The pursuers replan
  every 30 navigator steps instead of every 15 render frames;
- Pause is sim.paused, so vehicles, targets and the act clock stop together.
  The paused early-out is gone;
- Jump to Act 2 and Replay move the arc's origin in world time.

touch.update() now runs every interactive frame. Before, it ran only while
paused, so pinch and two-finger gestures did nothing while the story ran.

Verified on Windows: two 160-frame --capture fly runs are identical frame
for frame, and Act 2 (targets, pursuit lines, replanned spines) shows on
frame 150.
Before, the sea ran on a private world_clock, steered by coast.clock.scale and
coast.clock.paused, while the tree wind and the cloud drift, morph and ground
shadow animated on wall or capture time. Now app.world_clock() (default
1/120 s quantum, as for L-system scenes without a navigator) is driven once
per frame through a world_clock_binding. Its keys are the demo's own knobs:
- speed is coast.clock.scale;
- paused is coast.clock.paused;
- time is published to coast.clock.t;
- mode, step, tick and the rest sit beside them under coast.clock.

The FFT ocean, the tree re-pose and the sky field all read that one world
time, so a pause or a speed change applies to the whole scene.

Verified on Windows: the OceanFFT self-test passes, and two 30-frame
--capture orbit runs are identical frame for frame.
lsystem_forest (tree wind, sea and cloud, plus the cloud's ground shadow) and
terrain_lab (sea and cloud) animated on wall or capture time. Both now drive
app.world_clock() once per frame through a default world_clock_binding
(sim.*) and animate every layer from clock.t(). That gives one speed and one
pause for the scene, steerable through the state tree, and reproducible
fixed-dt captures. Camera paths and the FPS readout stay on frame/wall time;
they are presentation.

Verified on Windows: two 24-frame --capture orbit runs of each are
identical frame for frame.
…se follow world time

nav_city_swarm and nav_city_drive counted render frames for work that tracks
the simulation:
- the fleet fog repaint and the belief-wall rebuild now run every 15
  navigator steps, from sim_thread::ticks(), or world.tick() without a
  worker. Before, they ran every 15 frames;
- the belief colour sweep's phase is a function of clock.t(): 5 s per sweep
  at the initial speed. Before, it added 1/300 per frame.

So a pause freezes them and a capture repaints at the same world time on
every run. Pure render throttles (recolour every 3 frames, goals every 5,
restyle every 2, the HUD every 15) stay per frame.

Verified on Windows: two 60-frame --capture orbit runs of each demo are
identical frame for frame.
Two pycvc functions:
- nav_sim_thread_create_on_clock(world, clock) makes the C++ sim_thread follow
  a pycvc.world_clock, usually app.world_clock(). It runs one navigator step
  every veh.dt / fixed_dt quanta as the Python driver advances the clock.
- nav_sim_thread_wait_caught_up(handle, timeout) blocks, with the GIL
  released, until the worker has run every step due up to the clock's current
  tick.

GRL-SNAM's NativeSimThread(world, clock=...) is built on them (clock step C5).
GRL-SNAM's tests/test_sim_thread.py drives it in lockstep against this build:
after each wait, ticks and the snapshot tick equal tick // k on every frame.
@transfix transfix reopened this Oct 15, 2026
@transfix
transfix changed the base branch from feat/world-clock-followers to master October 15, 2026 01:45
@transfix
transfix force-pushed the feat/world-clock-demos branch from d86708b to aa39d41 Compare October 15, 2026 01:45
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 15, 2026
…a WorldClock

The Python side of "every simulation on one world clock"
(libcvc docs/roadmap/WORLD-CLOCK-SYNC-ROADMAP.md, step C5). It builds on the
clock parity work in #116.

clock.WorldClock:
- a Condition notified on every tick change;
- wait_for_tick_change(tick, timeout), so a follower thread sleeps until the
  driver moves the clock instead of polling (as cvc::world_clock does).

sim_thread.SimThread:
- SimThread(world, clock=clock) FOLLOWS a shared clock: one step every
  world.dt / fixed_dt quanta, run as the driver advances it, as
  cvc::nav::sim_thread does. Pause skips this sim's due steps; the clock's
  own pause pauses everything.
- SimThread(world, hz=60) keeps its old behaviour on a private clock (quantum
  world.dt, speed hz * dt) that the thread drives. Pause, SetRate and Nudge
  act on that clock, so world time and the steps run always agree.
- wait_caught_up(timeout), so a capture shows exactly each frame's steps.

nav_native.NativeSimThread(world, clock=app.world_clock()) is the C++ worker
following a shared pycvc clock, plus wait_caught_up. It needs the pycvc
functions from transfix/libcvc#630; HAS_SIM_THREAD_CLOCK gates it.

demos._common.SimPacer keeps its API but now drives a WorldClock at the
suite quantum (0.01 s), with a ClockFollower for the navigator. It was a
private accumulator; its .clock is exposed for a host. A stall is capped at
8 navigator steps, and the clock drops and counts the rest.

Tests, run against a local libcvc build with #630:
- test_clock: the tick wait wakes on advance, times out without one, and
  counts a rewind as a change;
- test_swarm: SimThread on a shared lockstep clock runs tick // k steps on
  every frame, and none while paused;
- test_sim_thread: NativeSimThread on app.world_clock(), the same check on the
  C++ worker;
- test_sim_pacer: frame-rate independence, speed and the stall drop still
  hold.

All 19 tests in the clock, sim_thread and SimThread selection pass, and so do
test_sim_pacer and test_clock (17). ruff 0.16 and black 26.5.1 are clean.
…master's include spacing

Two fixes from #630's CI.

- NavSimThread.RunsLockFreeAndTakesCommands failed in package-linux /
  libcvc-debug-grpc: 4 steps after set_paused(true), where the test allows 2.
  At 2000 Hz the hz-form worker is always behind, so each pass holds up to a
  cap's worth (8) of due steps. The loop finished them all even after the
  pause landed, and slow Debug steps let several complete after the test
  sampled. The step loop now re-checks the pause before every step. Steps cut
  off are skipped, as due steps are while paused, so a pause takes effect
  after at most the one step in flight. Locally, NavSimThread passes 20/20
  repeats.
- clang-format (changed lines): a whole-file format had changed the
  untouched `#include <cvc/lod/select.h>  //` line in nav_city_drive.cpp and
  nav_city_swarm.cpp. It is back to master's single space, and
  git clang-format against master reports nothing.
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 15, 2026
…a WorldClock (#117)

The Python side of "every simulation on one world clock"
(libcvc docs/roadmap/WORLD-CLOCK-SYNC-ROADMAP.md, step C5). It builds on the
clock parity work in #116.

clock.WorldClock:
- a Condition notified on every tick change;
- wait_for_tick_change(tick, timeout), so a follower thread sleeps until the
  driver moves the clock instead of polling (as cvc::world_clock does).

sim_thread.SimThread:
- SimThread(world, clock=clock) FOLLOWS a shared clock: one step every
  world.dt / fixed_dt quanta, run as the driver advances it, as
  cvc::nav::sim_thread does. Pause skips this sim's due steps; the clock's
  own pause pauses everything.
- SimThread(world, hz=60) keeps its old behaviour on a private clock (quantum
  world.dt, speed hz * dt) that the thread drives. Pause, SetRate and Nudge
  act on that clock, so world time and the steps run always agree.
- wait_caught_up(timeout), so a capture shows exactly each frame's steps.

nav_native.NativeSimThread(world, clock=app.world_clock()) is the C++ worker
following a shared pycvc clock, plus wait_caught_up. It needs the pycvc
functions from transfix/libcvc#630; HAS_SIM_THREAD_CLOCK gates it.

demos._common.SimPacer keeps its API but now drives a WorldClock at the
suite quantum (0.01 s), with a ClockFollower for the navigator. It was a
private accumulator; its .clock is exposed for a host. A stall is capped at
8 navigator steps, and the clock drops and counts the rest.

Tests, run against a local libcvc build with #630:
- test_clock: the tick wait wakes on advance, times out without one, and
  counts a rewind as a change;
- test_swarm: SimThread on a shared lockstep clock runs tick // k steps on
  every frame, and none while paused;
- test_sim_thread: NativeSimThread on app.world_clock(), the same check on the
  C++ worker;
- test_sim_pacer: frame-rate independence, speed and the stall drop still
  hold.

All 19 tests in the clock, sim_thread and SimThread selection pass, and so do
test_sim_pacer and test_clock (17). ruff 0.16 and black 26.5.1 are clean.
@transfix
transfix merged commit 95ae894 into master Oct 15, 2026
15 checks passed
@transfix
transfix deleted the feat/world-clock-demos branch October 15, 2026 04:23
transfix added a commit that referenced this pull request Oct 15, 2026
…stic ordering (#631)

StateTestFixture.WaitForValueWithTimeout failed on a loaded windows-2022
runner (#630, recipe / windows-2022 / Release):

    cvc::timeout_error: Timeout waiting for state value to be initialized

after 345 ms. Its success half started a writer that sleeps 50 ms and allowed
the wait only 200 ms, and the runner took longer than that to schedule the
write. Several neighbouring tests had the same weakness or a worse one:

- WaitForValueWithTimeout, WaitForDataWithTimeout and ValueFutureGetFor
  (50 ms writer, 200 ms wait) and FutureProducerConsumerPattern (1 s per
  item): waits that are expected to SUCCEED now use kSucceedWait (5 s). They
  return as soon as the write lands, so passing runs take as long as before.
  The halves that are expected to time out keep their short 50 ms waits on
  keys nothing writes.
- ValueFutureGet: a state_future sees only writes made after it connects
  (it listens to valueChanged). The writer slept 100 ms and the future was
  created after the thread started, so on a slow runner the future could
  miss the write, and future.get() would then block forever. The future is
  now created first, and the writer waits on a go signal released after the
  not-ready check.
- MultipleFuturesOnSameState: the same miss through a fixed 50 ms "give
  threads time" sleep. A boost::barrier now holds the write until all five
  futures are connected.

ValueFutureWaitFor needed nothing: the setter emits valueChanged
synchronously.

Verified on Windows (MSVC 2022, local cvcpkg prefix):
- the nine wait/future tests pass 50/50 repeats;
- under load, 12 concurrent processes x 20 repeats: 240/240;
- the full state_test passes, 164 tests;
- ValueFutureGet and MultipleFuturesOnSameState now finish in about 1 ms
  instead of 100 and 50 ms;
- git clang-format against master reports nothing.
transfix added a commit that referenced this pull request Oct 17, 2026
Minor release. It carries:
- the world clock: lockstep and batch, followers, the sim.* binding, and nav
  on the clock (#629, #630, #631, #640);
- the units registry with SI <-> normalized-frame conversions (#632);
- cvc::physics with the Jolt backend (#641);
- the material trainer on the kinematic bicycle and its pycvc bindings
  (#642).
Bumps project(VERSION) 3.5.1 -> 3.6.0 and every cvcpkg recipe's
upstream_version to match. cvc_revision stays 1, per the recipe-version gate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant