Frame Programs
A (frame …) fixes the order of work. Frame programs add controlled variation
without turning the payload into an imperative CPU application: controls choose
among predeclared steps, and every resource and arm is validated before execution.
The rule to hold is simple: no value the GPU produces decides what a frame does. Time, the frame counter, and host-set controls may steer the schedule; computed or rendered results may not. GPU-driven counts still belong in indirect draw/dispatch buffers, whose commands are already part of the static schedule.
A control in a fullscreen pass
Section titled “A control in a fullscreen pass”Declare one (control …), then read it as a member of controls in a pass. The
pass prelude generates the buffer and binding when it sees that reference:
(control :name warp :default 0.5)
(pass-graph (pass :name main :code """ @fragment fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f { let uv = pos.xy / vec2f(pngine.width, pngine.height); let p = uv - vec2f(0.4, 0.5); let r = length(p); let rings = 0.5 + 0.5 * sin(r * (20.0 + controls.warp * 60.0) - pngine.time * 2.0); let tint = mix(vec3f(0.9, 0.35, 0.1), vec3f(0.95, 0.85, 0.35), rings); return vec4f(tint * (0.35 + 0.65 * rings), 1.0); } """))That is a complete document. Set the control from the host with the same API used for reflected uniform fields:
setUniform(p, "warp", 1.0);setUniform(p, "warp", 0.75, { redraw: false }); // ride the next drawOr set it while rendering/inspecting from the CLI:
pngine render frame_pass_control.sjon --frame --control warp=1.0 -o warped.pngpngine inspect frame_pass_control.sjon --verbose --control warp=1.0Controls are persistent f32 values. A document can declare at most 64, in
declaration order; time is reserved. getUniforms(p) marks them with
control: true, and generated .d.ts sidecars include them as numbers.
Guard a reusable group
Section titled “Guard a reusable group”A (step-group …) names a sequence. With :when it runs only when every
comparison holds; :else supplies the alternative:
(step-group :name glow :when bloom :above 0.001 :perform [bloomPass] :else [plainPass])Comparisons are :is, :is-not, :below, :above, :at-least, and :at-most.
Two comparisons form a window. Bare :when debug means nonzero. Comparison values
are compile-time numbers, constants, or bounded expressions, not arbitrary runtime
expressions.
Use nested groups for AND across different controls. Richer Boolean logic, transitions, and latches belong in the host, which computes a control before the draw. The executor deliberately does not grow a general expression language.
Select a scene
Section titled “Select a scene”(select …) tries cases in source order and picks the first match. This complete
program selects three passes from scene:
(control :name scene)
(shader-module :name m :code """@vertex fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f { var p = array<vec2f, 3>(vec2f(-1.0, -1.0), vec2f(3.0, -1.0), vec2f(-1.0, 3.0)); return vec4f(p[i], 0.0, 1.0);}@fragment fn fs() -> @location(0) vec4f { return vec4f(1.0, 0.25, 0.1, 1.0); }""")
(render-pipeline :name p :layout auto (vertex :module m :entry vs) (fragment :module m :entry fs (color-target :format preferred-canvas-format)))
(render-pass :name linePass :pipeline p (color-attachment :view context-current-texture :clear-value [1 0 0 1] :load-op clear :store-op store) (draw :vertex-count 3))(render-pass :name gatesPass :pipeline p (color-attachment :view context-current-texture :clear-value [0 1 0 1] :load-op clear :store-op store) (draw :vertex-count 3))(render-pass :name ringPass :pipeline p (color-attachment :view context-current-texture :clear-value [0 0.3 1 1] :load-op clear :store-op store) (draw :vertex-count 3))
(select :name backdrop :on scene (case :is 0 :perform [linePass]) (case :is 1 :perform [gatesPass]) :else [ringPass])
(frame :name main :perform [backdrop])select is compile-time sugar. It lowers to a chain of guarded step groups, so no
second scheduler or runtime scene graph appears in the payload.
For several independent choices, use independent controls and groups rather than
writing the Cartesian product of whole frames. Use several (frame …) definitions
and setFrame when the choices really are mutually exclusive complete scenes.
A timeline inside the PNG
Section titled “A timeline inside the PNG”Use time as the control to make a timeline that needs no host JavaScript:
(select :name show :on time (case :below 12 :perform [introPass]) (case :below 40 :perform [tunnelPass flare]) :else [creditsPass])
(step-group :name flare :when time :at-least 12 :below 13 :perform [flarePass])
(frame :name main :perform [show])The stock player can wrap the whole clock:
play(p, { loop: 60 });At the seam, visual time and audio restart together. The frame counter, controls,
ping-pong history, and one-shot init work do not reset. seek uses the wrapped
clock; an explicit draw(p, { time }) uses exactly the time passed to it.
The full worked timeline is
examples/frame_timeline.sjon.
History, recovery, and skipped work
Section titled “History, recovery, and skipped work”A frame whose guards skip everything is valid. It submits no pass work and advances the frame counter once. Pool selection uses that counter whether a group ran or not, so a simulation that skips frames does not automatically resume the same history slot. Keep simulation state advancing and guard only presentation when that is the intended behavior, or author the reset/copy work explicitly.
Controls hold their values until the host changes them. Reload and device recovery restore authored defaults; the host must reapply application-owned values on its next draw. Seeking backward is likewise a host responsibility: supply the control value that belongs at that point instead of expecting the engine to reconstruct a host-owned latch or transition.
Export and compatibility boundaries
Section titled “Export and compatibility boundaries”The full viewer, dev runtime, native renderer, and inspector execute frame programs.
Direct --html and flat/mini are single-recording formats and refuse guards rather
than shipping only the arm that happened to execute during export.
An embedded-executor guarded PNG stays PNGB v0. An executorless guarded payload is PNGB v1, so an older shared runtime refuses it instead of decoding guard operands as ordinary instructions. Use the embedded executor or a current shared one.
Related
Section titled “Related”(frame …)- Frame keys, init, before, and perform- JavaScript API -
setUniform,setFrame,play, andseek - CLI reference -
--control, render, and inspect (pass-graph …)- The pass prelude and named graphs