Skip to content

physics: terrain_lab's field step, cheaper and on worker threads, with the same values - #1322

Merged
transfix merged 9 commits into
masterfrom
feat/physics-step-cost
Nov 17, 2026
Merged

transfix merged 9 commits into
masterfrom
feat/physics-step-cost

Conversation

@transfix

Copy link
Copy Markdown
Owner

terrain_lab's physics step cost about 16 ms of CPU a frame in arigl: the largest cost #1321's profiling found. This PR profiles it and cuts it to about 6 ms without changing anything it computes. Every pinned hash is the same:

  • the ocean scene's 0xbe86208109133b1a and the swell scene's 0xf0085b4212e1a8c0 (CrossPlatformGoldenHash);
  • ocean_sea's IsTheSameOnEveryPlatform and the source lattice's digests;
  • ewave's digests;
  • terrain_lab's own state hash after 400 frames (2501c4f54a7f5cab), with and without worker threads.

Where the time went

A headless harness built terrain_lab's world with SceneWorld::from_scene and stepped it on the snapshot's fixed 1/30 s a frame. That is four physics steps of 1/120 s, with the three floating crates' 512 cells each.

per frame, master ms
Jolt's own step 0.04 (0.01 a step)
the sea's per-time fields: each step's modes (1.4) and 64² transforms (2.4) ~3.8
the cells' height reads (6144 a frame, each finding its source point) ~4.2
their velocity reads (3444 a frame) ~1.5
SceneWorld::sync 0.04

The water runtimes' own work and the wake's immersion are not in the step. The generated world's colliders cost nothing per step: their bodies are static and asleep. step_report now carries fields_ms and engine_ms, so the split can be read anywhere.

What changed (all bit-exact)

  • det math (det_math.cpp):
    • det::sin and det::cos take the quadrant from an integer remainder below 2⁵³ instead of fmod. That is the same value, and fmod is slow. Above 2⁵³, fmod as before.
    • New det::sincos shares one reduction. The sea's modes and the shore layer's trains use it.
    • The sea's Catmull-Rom stencil wraps its lattice index the same way.
  • fft_plan::run_2d transforms eight rows or columns abreast. The butterflies are run()'s, operation for operation, so the inner loop over the block vectorises. A block of +0 rows is left as it is: every butterfly on +0 gives +0, and the long cascades fill a few rows of 64.
  • world_config::field_threads (new; default 0, changeable by reconfigure) reads the cells on worker threads beside the stepping thread.
    • Each cell's reads land in their own slot, and the sums run over them in the cells' order.
    • The sea's per-time work (each cascade's modes, each of its transforms) runs as tasks on the same workers.
    • The reads go through the sea held at the step's time (sea_surface::at, fluid_surface::at), without the frame cache's lock and reference count on every read. The source lattice's corners are read under a shared lock.
    • SceneWorld uses up to three workers. A build without threads (single-threaded wasm) ignores it.
  • The water node (water_node.cpp) is untouched.

Before / after

  • Headless (terrain_lab's physics only, medians of 400 frames after 60): 8.6–9.0 ms a frame on master against 3.3–4.0 ms on this branch, three runs each. On the stepping thread that is 26–27 million cycles against 8–10, with the workers beside it. Without workers (field_threads 0, the same-arithmetic changes alone) the frame is 7.2 ms.
  • In a process drawing terrain_lab as arigl does (1280 x 720 offscreen, the crate lake):
    • Before and after were alternated in blocks of 10 frames, through a scratch switch in the library (not in this PR). Both give the same physics, so the frames are the same.
    • "Before" was master's arithmetic (the fmod reductions, separate sin and cos, the row-by-row transform) with no workers. It still read through this PR's held sea, so master's own figure is a little higher. It matches the 16 ms first measured.
    • The physics steps went from 16.3 ms to 6.2 ms a frame, and tick_scene from 25.8 to 16.9 ms.
    • The frames are bound by the GPU, 74 → 63 ms: these runs were on the laptop's Intel Arc iGPU, arigl's default device here. The CPU numbers are unaffected by which GPU draws.

How it was checked

  • New tests:
    • OceanFft.TheBlockedTransformIsRunsBitForBit: run_2d against run() over the rows and then the columns. It compares bits for n = 1–128, both signs, with rows of +0 and -0 and lone zeros.
    • OceanSea.TheSeaAtATimeReadsTheSameValues: sea_surface::at reads the sea's height and velocity bit for bit over the beach, the swash and the open sea, with and without the source lattice.
    • AlgoDetMath.SinCosIsSinAndCos: sincos gives sin's and cos's bits over 20,000 points from 1e-3 to 1e9, at quadrant edges, at +-0, beyond 2⁵³ and at the non-finite.
    • PhysicsOceanSurface.TheFieldThreadsChangeNoValue: four bodies on the sea at 0, 1 and 3 workers, and switched mid-run by reconfigure. All give the same state_hash, with and without the lattice. field_threads outside 0–64 is refused.
  • The pinned hashes did not move:
    • PhysicsOceanSurface.CrossPlatformGoldenHash (be86208109133b1a) and PhysicsFluidSurface.CrossPlatformGoldenHash (f0085b4212e1a8c0);
    • OceanSea.IsTheSameOnEveryPlatform and the lattice's;
    • ewave's digests;
    • AlgoDetMath.Bits.
  • ctest over the Ocean, Ewave, AlgoDetMath, Physics, Ariadne scene-world, loader and components, Nav, world-water, and cvcgl ocean, ewave, wake, ariadne-realize and ariadne-physics suites: 791 tests, all passed (Windows Release, MSVC). The whole tree builds.
  • Linux, macOS and wasm: CI only. The single-threaded wasm build compiles the workers out.

Not done

  • Fewer cells, fewer reads, or a coarser sea read. Each would change what a body feels, and so the pinned hashes. None of the old values was a correctness bug.
  • Skipping cells provably above or below the sea. Tried: a bound over the whole tile is ±5 m on terrain_lab's lake, so it skips nothing. A local bound would need a bound on the source point, which the lattice and Broyden steps don't give.
  • Fewer physics steps a frame. The step is the clock's 1/120 s quantum. Fewer steps would change results.

…ith the same values

terrain_lab's physics step cost about 16 ms of CPU a frame in arigl, all of
it the water field reading the sea at every cell of every floating body
(Jolt's own step: 0.01 ms). Nothing it computes changes: every pinned
hash holds.

- det::sin / det::cos take the quadrant from an integer remainder below
  2^53 (fmod above); det::sincos shares one reduction. The sea's modes
  and the shore layer's trains use it. The sea's Catmull-Rom stencil
  wraps its index the same way.
- fft_plan::run_2d transforms eight rows or columns abreast, run()'s
  butterflies operation for operation, and leaves blocks of +0 rows as
  they are (a +0 row transforms to itself bit for bit).
- world_config::field_threads reads the cells on worker threads, each
  cell into its own slot, summed in the cells' order; the sea's per-time
  fields run as their tasks. The reads go through the sea held at the
  step's time (sea_surface::at, fluid_surface::at), without the cache's
  lock; the source lattice's corners are read under a shared lock.
  SceneWorld uses up to three workers.
- step_report carries fields_ms and engine_ms.
Where terrain_lab's 16 ms of physics went (the sea's per-time fields and
the cells' reads; Jolt's own step is 0.01 ms), what was cut without
changing a value, and the before / after numbers.
Conflicts in sea_surface.h / sea_surface.cpp, both sides kept:
- the frame stays sea_surface_frame (held by sea_surface::at_time) and gains O13's crossfade
  weight, gust frame and current frame;
- compute() keeps the per-cascade tasks (modes, then each transform) and det::sincos, with
  O13's h0 crossfade inside each cascade's modes task;
- at(), at_time's reads and O13's forcing_at / weight / taps all stay; height_from and
  velocity_from (the held reads) carry O13's fetch, gust and current terms.

Tests for O13's paths through the held reads and the tasks:
OceanSea.TheWindsSeaAtATimeReadsTheSameValues (a sea mid-crossfade with two fetch maps, gusts
and a current, its tasks run last first, bit-identical to the plain reads), and windy runs in
PhysicsOceanSurface.TheFieldThreadsChangeNoValue (0, 1, 3 workers and switching mid-run give
one state hash).
…sics-step-cost

# Conflicts:
#	src/cvc/physics/field_step.h
master fails to build arisrvx's python_engine.cpp: #1302 (bridged users)
added room(), bridge_presence(), bridge_absent(), bridge_post() and
listen_chat() calling server_link() with no argument, written against the
process-wide server link that the P0a cleanup replaced with the module's
own state, server_link(PyObject *self). Each of the five now names its
module argument self and passes it, as the other server ops (lent(),
publish(), ...) already do.

Found in #1324's pycvc-linux / cp312 job (run 43956686020),
which builds the PR merged with master.
The realize suite runs every node type's checks in one process. In a
Linux Debug job (Xvfb, llvmpipe) it took 250.6 s, 50 s under the 300 s a
test without a TIMEOUT gets; on a slower runner (#1309's Debug job, where
PhConvoy and Ewave also ran about 1.3x longer) it timed out on all three
attempts. As cvcgl_ewave, cvcgl_wake and cvcgl_volren_node have, it gets
a budget of its own with headroom, so only a real hang fails it.
@transfix
transfix merged commit 94c208c into master Nov 17, 2026
29 checks passed
@transfix
transfix deleted the feat/physics-step-cost branch November 17, 2026 10:43
transfix added a commit that referenced this pull request Nov 17, 2026
…ry change of step (#1324)

* cvcGL: the volume's draw queued without VTK's glFinish on still renders

vtkOpenGLGPUVolumeRayCastMapper::GPURender ends every draw with glFinish() so
that its CPU timer measures the GPU's work, which only an auto-adjusting
(interactive) draw uses. The CPU waited there for the whole frame queued so
far: in terrain_lab, 45-48 % of the main thread's samples, about 25 ms of a
45 ms offscreen frame on the Intel Arc iGPU.

OverlappingVolumeMapper is a vtkSmartVolumeMapper that swaps glad's glFinish
pointer for an empty function for the length of its own Render, unless the
draw is timed (the window's desired update rate at or above the interactive
rate), the loader has not loaded it, or an enclosing draw swapped it already.
VolumeNode and the scene views' twin volumes use it. WebAssembly keeps VTK's
behaviour.

cvcgl_volume_overlap: no glFinish on still renders and the pointer put back,
VTK's kept on interactive ones, one per still draw with a plain
vtkSmartVolumeMapper, and byte-identical pixels between the two mappers.

* EWave: the step tables kept per step, not remade at every change of step

The exact step's coefficients, the symmetric step's multipliers and the
real-space factors are n^2 modes of det:: math each. EWave kept one of each,
for the last step only. The world clock moves in whole 1/120 s quanta, so a
window stepped by the frame's clock step takes only a few steps, but under
arigl run's wall clock the step changed nearly every frame and the tables
were remade each time: about 10 ms of terrain_lab's 22 ms AriRuntime::frame
on the Intel Arc iGPU (12-13 ms now).

Each table is now kept for the last six steps it was made for, dropped as
before when the depth or the mask changes. Same functions, same inputs, so
the same values.

cvcgl_ewave 2b: twelve steps of 1/30 s and 1/40 s in turn work out four
tables and follow the CPU's exact step; a new depth works the coefficients
out again. Docs: the wake window in OCEAN-AND-WEATHER-ROADMAP.md, and the
volume's wait and what is left of terrain_lab's frame under
RENDER_PERF_ROADMAP.md's Phase 6.

* docs: terrain_lab's frame, measured interleaved on a quiet machine (with and without #1322)

* Revert "cvcGL: the volume's draw queued without VTK's glFinish on still renders"

This reverts commit 8d5858d11d5bf6b84b9c78de79cf7cd79193d17d.

The swap of glad's glFinish pointer is process-wide state. The wait is to be removed in VTK
itself instead: an opt-in vtkGPUVolumeRayCastMapper::FinishEveryRender (a cvcpkg patch, written
for upstream), which libcvc then turns off per mapper.

* docs: the volume's GPU wait is fixed in VTK (an opt-in FinishEveryRender), and the step tables' own measurement

* ariadne_python: the bridge ops pass self to server_link

master fails to build arisrvx's python_engine.cpp: #1302 (bridged users)
added room(), bridge_presence(), bridge_absent(), bridge_post() and
listen_chat() calling server_link() with no argument, written against the
process-wide server link that the P0a cleanup replaced with the module's
own state, server_link(PyObject *self). Each of the five now names its
module argument self and passes it, as the other server ops (lent(),
publish(), ...) already do.

Found in #1324's pycvc-linux / cp312 job (run 43956686020),
which builds the PR merged with master.

* cvcGL: cvcgl_ariadne_realize gets its own ctest timeout (1200 s)

The realize suite runs every node type's checks in one process. In a
Linux Debug job (Xvfb, llvmpipe) it took 250.6 s, 50 s under the 300 s a
test without a TIMEOUT gets; on a slower runner (#1309's Debug job, where
PhConvoy and Ewave also ran about 1.3x longer) it timed out on all three
attempts. As cvcgl_ewave, cvcgl_wake and cvcgl_volren_node have, it gets
a budget of its own with headroom, so only a real hang fails it.
transfix added a commit that referenced this pull request Nov 17, 2026
…feat/scene-wind

#1322 moved each cell's reads into read_cell() (and ahead of the loop, on the field workers, for a
free surface's cells). The scene wind's per-cell work moves with it: body_wind holds a body's wind
(its reference at the centre of mass and its height profile), made once per body and field as
before, and read_cell takes it to clip the cell at the wind's base and to read its velocity. A
wind is a flow's and a free surface a fluid's, so the workers' reads never need it. The arithmetic
is the branch's, in the same order. world.cpp keeps #1322's timing and the branch's field_time()
for the vehicles' pre_step; the tests are both sides'.
transfix added a commit that referenced this pull request Nov 17, 2026
… into feat/water-drift

#1322 moved each cell's reads into read_cell(); the inertial fluid's setup (its free surface, the
displaced mass and moment) now sits before that loop, and its displaced mass reads the cell's
value from the cell_read (cr.value) as the rest of the loop does.
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