Skip to content

Mac and Windows: shader clock re-bases out of sight, so scenes no longer jump every 13 h 20 min - #137

Open
narrowstacks wants to merge 1 commit into
mainfrom
fix-c1-shader-time
Open

narrowstacks wants to merge 1 commit into
mainfrom
fix-c1-shader-time

Conversation

@narrowstacks

@narrowstacks narrowstacks commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

The shaders' clock (FrameParams.time) wrapped at 48,000 s (elapsed % 48000 in frame_params) to keep float precision. Only the black hole disk's flow (48 s period) and the planetary nebula's 400 s cycle actually repeat at that wrap; every other use in every scene jumped in one frame every 13 h 20 min of uptime, on both apps. Black Hole Merger's own clock wrapped at 100,000 s of scene time, and Quasar's knots and lobes at their own periods (the review item didn't list Quasar).

Audit

Every use of the clock in shaders/metal/*.metal. "Periodic" means periodic in a way that divides the wrap.

Shader Uses of the clock Periodic?
sky.metal skyColor (every scene) twinkle slots and flicker, pulsar blink sin(T*5.71), comet ion-tail streaks no
lensed.metal shadeDisk fract(T / flowPeriod) two-phase flow yes (48 s)
lensed.metal TDE star granulation T*0.02, stream drift T*0.012, jet knots T*0.25, pulse sin(T*0.5) no
galaxy.metal flowingDisk (period G.motion.w), star-forming regions om*T, dust T*0.003, lumps T*0.006, bursts sin(T*0.15), cells om*T no
quasar.metal flowingDisk(..., 72), torus T*0.06, flicker T*0.35; CPU: knot travel % 1.4*4096, lobe drift % 500 no
gasGiant.metal storm drift st.y + ws*0.12*T, T/cycle (40 s) trace-back, lightning slots no
ringed.metal rgDrift band drift, churn T*0.02, ring T*2pi/period, ring sparkle T*0.012; CPU: storm start % 48000 no
protoDisk.metal clumps T/cyc, gap planets om0*pow(g,-1.5)*T, spokes fract(T/110) no
planetary.metal rim fbm T*0.004/0.006/0.003, knot flicker T*0.12; shells fract(T/400) mostly no (400 s yes)
aurora.metal auroraLUT kernel (curtain noise and folds), emission T*0.01, clouds T*0.0004, lightning no
liveEarth.metal aurora folds and rays (T*0.02 to T*0.7), city flicker T*0.6 no
eclipse.metal corona sway and breathe sines, plume, ray and flow noise, prominences, threads, jets, clouds no
stars.metal binary/trinary granulation and rays; Star Close-up granulation, network, active regions T/life, prominences, loops, jets no
dyson.metal panel wobble sin(T*(0.12+...)), granulation T*0.05, rays T*0.06, flicker T*0.6 no
roguePlanet.metal aurora oval, folds, rays, breathe sin(T*0.14), pulse sin(T*0.11) no
neutronMerger.metal field flow sin(c*7 - T*2.5) no
pulsar.metal nebula rotation T*0.04, lanes, clumps, wisps, lobes, churn, beam knots T*3 no
comet.metal kink and sag sines, streak fbm, striae, grain no
nebula.metal sin(T*2pi/S.a.x) breathing, shimmer sin(T*0.3) no
blackHoleMerger.metal (S.p.w, own clock) jets T*0.8, gas fract(T/per), cos(th - T*0.9*...), cluster cos(om*T) no

No common wrap period exists, since noise isn't periodic, and a per-use fix would have needed about a hundred slots. So the clock moves only where nobody can see it, with a cross-fade as the fallback.

Fix

One clock per desktop, restarted only unseen. All screens of a desktop count the shader clock from one base, so displays showing one scene stay in step. It lives in DesktopClock in core/src/scene_params.rs: CoreClock.desktop on the Mac, and one per wallpaper process on Windows. The Customize preview on the Mac keeps its own, and the Windows Customize window renders from 0. The core moves the base to the last multiple of 19,200 s (400 × 48, so the disk flow and planetary shells keep their phase) only:

  • As the desktop shows again, if every screen was out of sight for at least 60 s (CLOCK_REVEAL_AFTER), or the displays slept, the session was locked or another user had it in between. What shows first on a reveal is the last frame drawn before hiding, so a short look away (swiping back from a full-screen app) doesn't restart the clock: that would re-roll every drifting feature in one frame. After a minute away the scene has moved on anyway.
    • Mac: each view reports isOnScreen and displayHidden (sleep, another session) to the shared clock.
    • Windows: the loop reports whether any screen is uncovered and visibility::desktop_hidden() (displays off, locked, other user).
    • A Mac screen lock counts through occlusion (after 60 s), since the app doesn't observe the lock itself.
  • As a new look comes in. A transition restarts the clock for the incoming look. The outgoing look keeps the old base in its Fade until it's gone. transition(id, start) returns the same pair for every screen. A cut to a new scene restarts it too.

Forced fallback. A look in view for 16 h (57,600 s = 3 × 19,200) without a restart wraps on screen. For 120 s after, the scene is drawn at both clocks and cross-faded with a smoothstep weight. That uses F.clock, the <name>Scene(FRAME_ARGS, ..., float T) bodies looped over clockPasses, Aurora's table blended the same way, and Black Hole Merger's and Quasar's pre-wrap values in F.clock.z, S.h and S.j.

  • Precision: the clock, including the old side of the fade (57,720 s), stays below 65,536 s, where a float's step is 3.9 ms. That is a quarter of a 60 fps frame and as fine as main's 48,000 s wrap gave. After an ordinary restart it's under 19,200 s (2 ms or less).
  • Black Hole Merger: its own clock is the shader clock times its Speed. The slider goes to 3×, and the setting is clamped at 4× (a saved config or the command line can reach it). At 1× it stays under 65,536 s (3.9 ms). At 4× it reaches 230,880 s at 16 h, a 15.6 ms step, but only on a screen in view for more than 4.5 h straight without a restart. Main's 100,000 s wrap gave 7.8 ms there, with a jump instead.
  • Quasar knots: each jet's knot travel is split on the CPU into whole knot cells (a multiple of 1,000) and the rest (under 1,400 jet lengths). The shader hashes knots by cell - cells, and the ripple's noise takes the cells as a whole-number lattice offset (qsrNoise). So the travel never wraps (no jump), and the part in float arithmetic stays under 1,400 (0.1 ms of travel at Knot speed 3), however long the clock runs. At normal times the split is 0, so the math is bit-for-bit the old one.
  • Why 16 h: a screen that sleeps, locks or gets covered for a minute once a day never sees it.
  • Why 120 s: the fallback is rare, so a minute more at double GPU time costs nothing noticeable, and a slower dissolve is harder to notice than the 60 s one.

Outside the fade there is one pass with weight exactly 1.0, so renders at normal times are byte-identical to main. Snapshots, deepfield render, captures and the lock-screen still count from 0.

API.

  • Core: frame() takes clock_base; scene_params::{DesktopClock, ShaderClock, rebase_clock, CLOCK_PERIOD, CLOCK_MAX_AGE, CLOCK_FADE, CLOCK_REVEAL_AFTER}.
  • C: df_frame(..., clock_base, ...) and df_clock_new/free/base/seen/cut/transition, all inside guard (every_export_is_guarded passes), plus the header.
  • wgpu: Renderer::encode_frame_clocked.
  • Swift: CoreClock, Core.frame(clockBase:), encodeFrame(clockBase:).

Verification

Core tests (cargo test --workspace --lib: 122 + 30 (ffi, including the guard test) + 17 pass):

  • frames_carry_on_across_a_forced_wrap: every scene, from base 0 and from a restarted base, at 1, 2, 3, 52 and 2000 forced wraps, across the wrap and the fade's end. The largest change in any float handed to the GPU over a 1/60 s frame is ≤ 2× the frame before (+1e-3, a whole turn counting as none). The dominant clock advances one frame, as does Black Hole Merger's own at 4× and Quasar's knots at Knot speed 3.
  • a_rebase_changes_only_the_clock: every scene, with all Show Me events active. A restart (unseen, so not required to be continuous) restarts the clock under 19,200 s with one pass, keeps the 48 s and 400 s phases, and changes no float but the clock slots. Events, orbits, colors and event placements are identical, and frames after it carry on.
  • a_transition_rebases_only_the_incoming_look: starting 1 s before the outgoing look's forced wrap. The outgoing look carries on through its wrap on its own base, and the incoming one carries on with one pass.
  • the_desktop_clock_restarts_only_unseen: a 5 s look away doesn't restart, 60 s does, sleep does however short, showing all along never does, and a transition gives every screen the same pair.
  • quasar_knots_keep_their_precision, frame_time_wraps, clock_fade_is_smooth.

Other checks:

  • Snapshots --time 300, every scene, against a main build: byte-identical, 22 of 22. Live Earth was compared with its time frozen; unfrozen it reads the wall clock and differs run to run on main too.
  • Pixels changed over one 0.2 s step (out of 309,600):
scene normal (t=300) main 47999.9→48000.1 PR forced wrap 57599.9→57600.1
gasGiant 1737 79578 1683
auroraOrbit 5514 108804 3897
protoplanetaryDisk 2034 55071 2241
nebula 2022 30102 1809
tidalDisruption 3546 29112 4113
comet 2007 28224 2277
trinary 801 26217 756

Quasar at Knot speed 3: 2052 across the forced wrap, against 1269 for a normal step at 40,000 s.

  • cargo run --features check -- --check (msl2wgsl): every module ok.
  • --check-core, --check-customization: ok.
  • scripts/parity/compare.py (default times): 88 of 88 ok (events included).
  • --benchmark --defaults --time 300: every scene under 2 ms, within noise of main. During the forced fade, about 2×, for 120 s at most once in 16 h of a screen in view.
  • The Windows code (windows/src/wallpaper.rs) can't build on a Mac (a cross cargo check stops in ring's C build); CI's Windows job builds it.

Screenshots

Before (main) at 47999.9 | 48000.1 s, and this PR's forced fallback at 57599.9 | 57600.1 s (base 0, no restart for 16 h):

(attached by the coordinator)

@narrowstacks narrowstacks changed the title Mac and Windows: shader clock wraps under a cross-fade, so scenes no longer jump every 13 h 20 min Mac and Windows: shader clock re-bases out of sight, so scenes no longer jump every 13 h 20 min Dec 1, 2026
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