The splanc effects engine

Effects are small GLSL-ish shader programs that run per-LED across a fixture’s real 3D geometry. This document is the reference for the whole effects stack: the language, the bytecode VM, the .fxb container, uniform/texture/MIDI plumbing, measured performance on the ESP32-C6, and how the built-in AI writes effects.

For narrative design rationale see docs/design/effects-runtime.md and docs/design/effects-compiler.md; for the perf model see docs/design/perf-monitoring.md.

Overview

Two Rust crates make up the engine:

  • fx_compiler/ (ledmapper_fx_compiler) — a single-pass, type-checking compiler from the source language to .fxb bytecode. Recursive-descent parse → precedence-climbing expression codegen, emitting bytecode directly with no AST.

  • firmware/fx_vm/ (ledmapper_fx_vm) — a no_std, no-alloc stack machine that executes .fxb. The same crate is what runs on the ESP32-C6 and what compiles to wasm for the browser preview, so the editor preview and the device render bit-for-bit identically.

Both compile to wasm (fx_compile(src) and the FxPreview class) and are loaded by the web app at runtime. The authoring/preview/upload flow:

source ──fx_compiler(wasm)──▶ .fxb ──┬─▶ FxPreview (fx_vm wasm)  → live preview in the editor
                                      └─▶ submit_effect over wss  → fx_vm on the ESP32-C6

The language

A program is plain text with two entry points, plus an optional one-time setup:

void init();              // optional: runs once, when the effect loads
void update();            // runs once per frame; may write `state`
vec3 shade(Led led);      // runs once per LED; returns linear RGB in 0..1

shade is required; update and init are optional. There is no recursion, and every for loop must have a compile-time-bounded trip count (the VM enforces a per-frame instruction budget — see Execution model).

Types

float, vec2, vec3, vec4, int, bool, plus fixed-point fixed (Q16.16), fixed16 (Q1.14) and fixed8 (Q1.6), and user structs and fixed-size arrays (Type name[N]). A fixed-point literal takes a suffix: 0.6fx is fixed(0.6), 0.6fx16 is fixed16(0.6) and 0.6fx8 is fixed8(0.6) (folded to the same constant; fx16/fx8 literals must be below 2). At runtime everything is an f32 slot; the compiler types every operation so the VM stays untyped (integers and fixed-point values ride in the f32 slot bit-for-bit).

Read-only contexts

Per-frame and per-LED inputs are exposed as globals (each compiles to a LoadCtx opcode):

Identifier

Type

Meaning

time, dt, frame

float

seconds, delta seconds, frame counter

led.pos

vec3

LED position (x, y, z)

led.idx, led.count

int/float

LED index, total live LED count

led.seg, led.s, led.branch

int/float

segment id, position along segment, branch id

led.dist

float

geodesic distance 0..1 from the topology root (or a flood_from source)

led.uv

vec2

per-LED texture coordinate (top-down projection to 0..1)

led.room

vec3

LED position in the shared room frame, metres (== led.pos while unposed)

room.min, room.max

vec3

room bounding box (the host’s, else this fixture’s own room-frame box)

imu.accel, imu.gyro

vec3

device IMU (when driven)

Declarations

  • Uniforms — live-tunable parameters with UI hints:

    uniform float speed : 0.0 .. 5.0 = 1.0;      // slider
    uniform vec3  tint  : color      = 0.2, 0.6, 1.0;   // color picker
    uniform int   mode  : {"fire","ice"} = 0;    // dropdown
    uniform bool  invert = false;                // toggle
    uniform float tilt  : -1.0 .. 1.0 = -0.2;    // signed range
    uniform vec4  plane : -5 .. 5 = 0, 1, 0, 1;  // ranged vector: numeric, not a colour
    uniform vec3  p : vec3(-1,-1,0) .. vec3(1,1,2) = vec3(0.0);  // per-component range
    uniform mat4  pose;                          // matrix, identity default (`: lo .. hi` optional)
    

    A vec3/vec4 without a range is a colour (ui.kind: color, 0..1); with a range it is a numeric slider vector (ui.kind: slider; the per-component form adds mins/maxs to the manifest, min/max being their envelope). A mat3/mat4 is ui.kind: matrix, width 9/16, column-major, range ±1000 unless declared.

  • Matrices — mat3/mat4 (column-major, GLSL): m[c] column, m[c][r] element, constructors (diagonal scalar, N columns, N×N scalars, mat3(m4), mat4(m3)), m*v v*m m*m m*s m/s m+m m-m -m, transpose(m), and transform(m, p) = (m * vec4(p, 1.0)).xyz. No VM opcode knows matrices: the compiler lowers every operation to the vecN ops (Load / Swizzle-broadcast / Mul / Add / Dot), parking operands in per-function scratch locals, so matrix programs are ordinary bytecode for every shipped VM, JIT and cost model. Per LED a mat4 * vec4 is 16 multiplies + 12 adds: pre-multiply in update() where you can.

  • State — state variables persist across frames; written by update() (and init()), read-only in shade(). Good for phases/integrators. A state starts at zero unless it has an initial value: state float level = 0.5; (scalars, vectors and matrices; a compile-time constant; set arrays and structs, and anything computed from uniforms or time, in init()).

  • void init() — optional; runs once when the effect loads. See State initialisation.

  • Buffers — buffer vec3 trail; is a hidden per-LED buffer (one element per LED) that persists across frames. Indexed trail[led.idx]; unlike state it may be written from shade() (each LED owns its slot). Ideal for trails/persistence-of-vision feedback.

  • Textures — texture vec3 img(64, 64); is a hidden W×H 2D texture. Sample with sample(img, uv) (bilinear, edge-clamped) and write with paint(img, uv, color) (nearest texel). This is the target for video-texture streaming.

A : fixed8 / : fixed16 annotation on a buffer/texture element compresses its storage (dequantized to float on access) to save arena bytes on-device.

Built-in functions

Math (sin cos abs floor ceil fract sqrt exp log sign tan, min max pow mod step atan2), vector ops (clamp mix smoothstep dot cross length normalize distance), color (hsv2rgb, palette0/1/2), noise (hash), texture (sample, paint), and topology graph queries (seg_count seg_len seg_node node_deg node_seg node_side term_count term, plus flood_from(node) to re-root led.dist).

On int and fixed-point (fixed/fixed16/fixed8) arguments, the arithmetic builtins run natively — min max abs clamp mod sign step floor ceil fract mix each have integer/fixed opcodes, and sin/cos/exp use a compile-time LUT (angle in turns; exp covers only -2..0.69) — instead of coercing to soft-float. sqrt tan log on a fixed argument, and atan2 pow / hsv2rgb when every argument is the same fixed type, take integer opcodes too (atan2 then returns turns). smoothstep hash dot length distance normalize cross are always float. Everything that stays float-valued converts its int/fixed args (never reinterpreting the bits; ~320 cycles each). This is the main lever for keeping hot paths off the FPU-less C6’s soft-float.

Example

A decaying comet chasing the geodesic distance field, with live controls:

uniform float decay : 0.5 .. 0.98 = 0.85;
uniform float speed : 0.0 .. 4.0  = 1.2;
uniform float width : 0.02 .. 0.3 = 0.08;
uniform vec3  tint  : color       = 0.2, 0.8, 1.0;
uniform float rainbow : 0.0 .. 1.0 = 0.6;
buffer float trail;
state  float head;

void update() { head = fract(head + dt * speed * 0.2); }

vec3 shade(Led led) {
  float spark = smoothstep(width, 0.0, abs(led.dist - head));
  float v = max(trail[led.idx] * decay, spark);
  trail[led.idx] = v;
  vec3 hue = hsv2rgb(led.dist, 0.85, 1.0);
  vec3 col = tint * (1.0 - rainbow) + hue * rainbow;
  return col * v;
}

Rate controls: integrate, never time * speed

The example advances head by dt * speed each frame instead of computing fract(time * speed * 0.2). That is deliberate: time * speed rescales the whole elapsed time, so moving the speed slider jumps the phase by Δspeed × time (whole cycles after a few minutes) and the effect stutters while the knob moves. Keep one state phase per rate uniform, advance it in update() with ph = fract(ph + dt * speed); (the fract bounds float precision; integrate non-periodic offsets such as a noise scroll without it), and use ph in shade() — fract(led.s - ph), sin(ph * 6.2832). In fixed point: ph = fract(ph + fixed(dt) * fixed(speed)); and sin(fixed16(ph)) (turns). time alone, or times a literal, is fine. The compiler warns when a value it can trace to time × a uniform reaches a phase builtin (fract, sin/cos, mod/%, floor/ceil, hash, hsv2rgb, palette*): Compiled::warnings (fx_compiler/src/lint.rs), surfaced by the editor, its AI and the agent’s compile_effect / deploy_effect.

The compiler entry point is compile(src) -> Result<Compiled, Vec<Diagnostic>> where Compiled { fxb: Vec<u8>, uniforms: Vec<UniformInfo>, warnings, .. }; disassemble(fxb) produces a readable op listing (exposed to the editor as fx_disassemble).

State initialisation

state float x = expr; and void init() { ... } both run once, when the effect loads, before its first update():

  1. every state initial value, in declaration order;

  2. then init()’s body (loops, arrays, structs, helper calls, return;);

  3. then, each frame, update() and shade() as usual.

An initial value is a compile-time constant (D-506): literals and constant maths over them, including vector/matrix constructors, casts and the pure maths built-ins (state vec3 c = vec3(0.1, 0.2, 0.3) * 2.0;, state float a = sin(0.5);). Anything that reads the effect’s inputs (a uniform, time/dt/frame, led.*, another state, a helper function, textures, topology, I2C) is a compile error that points at that name and says to move it into init():

uniform float start : 0.0 .. 1.0 = 0.25;
state float ph;                    // not `= start` (an error)
void init() { ph = start; }        // init() reads uniforms, time/dt/frame and state

init() reads what update() reads (D-507): uniforms, time/dt/frame and state. On the device it runs at load, so it sees the load moment’s time and frame and dt = 0; a host that has no load hook (see below) runs it at the start of the first update() and it sees that frame’s values.

“Loads” is every load of the program: a deploy or push, a boot (the resumed effect), loading it onto the other deck for a crossfade, and in the editor every recompile of the preview. Uniform changes, keypoint recalls and morphs do not re-run it, and a deck that is parked (off screen, not rendering) keeps its state and does not re-initialise when it comes back. A state without an initial value is zero, exactly as before.

Ordering with a restore. Init is tied to the load, not to the first frame: anything that writes the state after the load (a mesh catch-up restoring shared state from the gateway) wins over it, and the first update() does not re-run init on top of the restored values.

The compiler lowers this to plain bytecode, with no new opcode: a hidden state flag guards an init block in front of update() (or a driver’s poll()), and the .fxb sets flags bit FLAG_INIT (0x80, no extra bytes). The firmware runs the init code when it loads the effect (Vm::run_init). A host that doesn’t (the browser preview, a driver, firmware that predates the flag) gets it from the guard on its first update() instead, so it runs the same on the VM, the JIT and the preview. After that it costs three ops per frame (none per LED). The flag is set before the init runs, so an init() that exceeds the instruction budget is cut off once (like a long update()) rather than retried every frame. Programs that use neither feature compile to exactly the bytecode they did before.

Execution model

The VM (firmware/fx_vm/src/lib.rs) is a stack machine over f32 slots — a vecN is N contiguous slots. Everything is fixed-size and allocation-free: MAX_STACK = MAX_STATE = MAX_LOCALS = MAX_UNIFORM_SLOTS = 128, call stack depth 16.

  • A compiled Program carries an update_entry and a shade_entry (0xFFFF = absent). run_update executes update() once per frame, evolving state; run_shade executes shade(led) per LED and returns Rgb. shade runs against a copy of state, so it can’t mutate frame state.

  • Per-LED/per-frame inputs come from Frame (time, dt, frame, led_count, imu) and Led (pos, idx, seg, s, branch, dist, uv) structs, read via LoadCtx.

  • No libm. Transcendentals use small polynomial approximations (deterministic across host/wasm/device); the narrow-fixed SinFix/CosFix/ExpFix ops use 256-entry LUTs with linear interpolation.

  • Bounded execution. Every invocation runs under a Budget: a hard instruction count (DEFAULT_BUDGET = 100_000) plus an optional wall-time deadline flag a hardware timer raises (polled every 1024 ops). Outcomes are Ok/Budget/Timeout; a timed-out shade returns black. Counters (instrs retired, stack high-water) feed the perf stream.

  • Topology tables (MAX_SEG = 64, MAX_NODE = 96, MAX_NODE_DEG = 6) are populated by set_graph and read by GraphQuery; FloodFrom runs a single-source geodesic (Bellman-Ford) sweep whose result persists across frames.

Opcode reference

The authoritative enum is Op (#[repr(u8)], contiguous 0..=79) in firmware/fx_vm/src/lib.rs; the compiler mirrors and asserts these discriminants. Operands follow inline in the code stream, little-endian.

#

Opcode

Operands

Semantics

0

PushConst

u16 idx

push consts[idx]

1

LoadUniform

u8 slot, u8 n

push n uniform slots

2

LoadState

u8 slot, u8 n

push n state slots

3

StoreState

u8 slot, u8 n

pop n into state

4

LoadLocal

u8 slot, u8 n

push n locals

5

StoreLocal

u8 slot, u8 n

pop n into locals

6

LoadCtx

u8 id

push a context value (see context ids)

7–10

Add Sub Mul Div

u8 n

element-wise arithmetic over n (÷0 → 0)

11

Neg

u8 n

negate n components

12

Scale

u8 n

n-vec × scalar

13

UnMath

u8 fn, u8 n

unary math per component

14

BinMath

u8 fn, u8 n

binary math per component

15

Clamp

u8 n

clamp(x, lo, hi)

16

Mix

u8 n

a + (b − a)·t

17

Smoothstep

u8 n

smoothstep(e0, e1, x)

18

Dot

u8 n

→ scalar

19

Cross

u8 (=3)

vec3 × vec3

20

Length

u8 n

→ scalar

21

Normalize

u8 n

normalize (no-op if len < 1e-9)

22

Distance

u8 n

→ scalar

23

Swizzle

u8 srcN, u8 dstN, idx…

reorder components

24

Cmp

u8 kind

scalar compare → bool (lt/le/gt/ge/eq/ne)

25

Logic

u8 kind

and/or/not

26

BrFalse

i16 rel

pop bool; branch if 0

27

Jmp

i16 rel

relative jump

28

Hash1

—

scalar → [0,1)

29

Hash3

—

vec3 → scalar

30

Hsv2Rgb

—

vec3(h,s,v) → vec3 rgb

31

Palette

u8 id

scalar t → vec3 (0 fire, 1 ice, else rainbow)

32

Pop

u8 n

drop n slots

33

Ret

u8 n

return n slots (0 update, 3 shade)

34

Swap

u8 an, u8 bn

swap top bn with the an below

35–41

AddI…CmpI

(CmpI u8 kind)

i32 add/sub/mul/div/mod/neg/compare

42–43

MulFix DivFix

—

Q16.16 multiply / divide

44–49

I2F F2I Fix2F F2Fix I2Fix Fix2I

—

numeric conversions

50

Call

u16 target

push return pc, jump

51

RetFn

—

return from a user function

52–55

LoadStateIdx StoreStateIdx LoadLocalIdx StoreLocalIdx

base,stride,off,n,count

indexed array access (clamped)

56

GraphQuery

u8 kind

topology query (seg/node/term kinds)

57–58

LoadBuf StoreBuf

u8 id

per-LED buffer element read/write

59–60

SampleTex PaintTex

u8 id

2D texture bilinear sample / nearest write

61

FloodFrom

—

re-root the geodesic field at a node id

62–66

MulFixN DivFixN FixRescale FixToF FixFromF

u8/i8 frac

narrow fixed-point scaling/conversion

67–69

SinFix CosFix ExpFix

u8 frac

integer-LUT sin/cos/exp (angle in turns)

70–73

AbsI MinI MaxI ClampI

—

native integer abs / min / max / clamp

74–79

SignI StepI FloorFix CeilFix FractFix MixFix

u8 frac

native fixed-point sign/step/floor/ceil/fract/lerp

Function ids for UnMath: sin cos abs floor ceil fract sqrt exp log sign tan (0–10). For BinMath: min max pow mod step atan2 (0–5). Context ids for LoadCtx: time, dt, frame, led.pos(3), led.idx, led.count, led.seg, led.s, led.branch, imu.accel(3), imu.gyro(3), led.dist, led.uv(2), led.room(3), room.min(3), room.max(3). A program reading the room frame sets .fxb flags bit 0x08 (FLAG_USES_ROOM) so the host fetches it only then.

time is seconds since the effect started — or, once a host sends set_time_base { epoch_ms } (the multi-fixture shared time base, in the device’s own clock), (now - epoch_ms) / 1000, held at 0 before the epoch, so fixtures animate in step. A fixture’s room pose (set_fixture_pose: translation, rotation quaternion, scale) maps each LED as room = t + R(q) * (scale * pos); the device caches it per LED in millimetres (6 B/LED).

The .fxb container

Magic "FXB1", version 1. An 18-byte fixed header followed by variable sections (all multi-byte fields little-endian):

Offset

Field

0

magic "FXB1" (4)

4

version = 1 (u8)

5

flags (u8; FLAG_BUFFERS = 0x01)

6

n_state (u8)

7

n_uniform_slots (u8)

8

manifest_len (u16)

10

n_consts (u16)

12

code_len (u16)

14

update_entry (u16, 0xFFFF = none)

16

shade_entry (u16)

Then: the manifest bytes (in practice empty — the uniform manifest travels out-of-band as JSON), the constant pool (n_consts × 4 bytes), the code stream (code_len bytes), and — only if FLAG_BUFFERS is set — one n_buffers byte followed by 7-byte descriptors [kind:u8, elem:u8, comp:u8, w:u16, h:u16]. kind = 0 is an LED-arity buffer (count = live LED count); kind = 1 is a W×H texture. comp selects component storage precision (F32, FIX16, FIX8, I16, I8, FIX16F, FIX8F, I32) so 8-bit formats cost one byte per component instead of a 4-byte slot.

Uniform plumbing

Uniforms are addressed by numeric slot, not name (a vec3 consumes 3 slots; the compiler assigns slots sequentially by width). The name↔slot mapping is not in the .fxb; the compiler returns it as Vec<UniformInfo> and serializes it to JSON via manifest_json (fields per uniform: name, slot, width, ui, default). The web app parses that JSON to build the control panel.

  • VM: Vm::set_uniform(slot, vals) writes vals.len() slots; programs read via LoadUniform slot, n.

  • Web: the editor’s slider/color/dropdown widgets call FxPreview.setUniform(slot, vals) (offline preview) and client.setUniforms(...) (to the device) — a knob move updates both identically.

  • Firmware: a SetUniforms control message carries UniformValue { slot, repeated float value } records; firmware/player_app/ffi.rs decodes them and calls lm_fx_set_uniform(slot, vals, n). get_effect_uniforms returns the manifest bytes to the app.

  • Wide uniforms (mat3/mat4): on the wire a UniformValue carries at most 4 floats; clients send a wider uniform as consecutive chunks {slot, v[0..4]}, {slot+4, v[4..8]}, … (exactly a whole write, on any firmware), and the device reports current values the same way. Firmware since #400 also accepts up to 16 floats in one value. OSC addresses: see firmware/osc (/pose + 16 floats, /pose/<i> flat component, /pose/<c>/<r> element).

Textures and video-texture streaming

Textures are backed by an external byte arena bound per-program via set_arena (size it to Program::arena_bytes(led_count)). buf_base(id, led_count) gives a buffer’s byte offset — deliberately public so the host can stream video frames straight into a texture. Shaders read via sample(tex, led.uv).

The web app can stream live video (a clip, a canvas, a camera) into a texture uniform on the device:

  1. Encode (browser). effects/editor/videoTexture.ts draws the source into a canvas at the texture’s declared W×H, reads back RGBA, and quantizes it with the codec in net/textureCodec.ts. Supported formats: rgb888, rgb565 (default), rgb332, gray8, indexed8, gray4, mono. Grayscale uses Rec.601 luma; gray4 packs two texels per byte, mono eight per byte (LSB-first). Two compression flags apply: FLAG_DELTA (XOR against the previous quantized frame) and FLAG_RLE (zero-run/literal-run). TextureStreamer keeps the previous frame and emits deltas.

  2. Transport. A fire-and-forget set_texture message ({ texIndex, format, width, height, flags, data, palette? }) goes over the wss control channel. Texture geometry is discovered by parsing the .fxb buffer table (net/fxbTextures.ts).

  3. Decode (device). firmware/player_app/ffi.rs handle_set_texture reverses keyframe/XOR-delta/RLE and dequantizes packed bytes into the arena at the texture’s precision using precomputed byte LUTs — no per-texel float on the FPU-less C6 (the “narrow arena” work). The region is then addressable as the texture uniform.

The gray4/mono formats and the delta/RLE codec came out of a codec/decoder hill-climb for narrow arenas (see the WORKLOG entries and the video_stream HITL test, which asserts a sustained ≥10 FPS on hardware).

MIDI

MIDI is a web-app feature; the VM and firmware are MIDI-agnostic. A MIDI control simply drives set_uniform(slot, …) like any other input. There are no MIDI opcodes, uniforms, or context ids.

The web layer (web/src/midi/) wraps the Web MIDI API and maps controls to uniforms in two layers (persisted in store/midiStore.ts):

  • Semantic (global): a physical control (CC / note / pitch-bend, keyed by device+channel+number) is named once (“speed”, “hue”), matched loosely by normalized name.

  • Binding (per-effect): a UniformBinding { uniform, semantic, min?, max?, invert? } wires a named control to a uniform, with optional sub-range and invert.

midi/router.ts resolveControlUpdates(...) is a pure function turning a physical event into scaled UniformUpdates (only scalar slider/toggle uniforms are drivable; toggles threshold at 0.5). In the editor the resulting updates fork to both the offline preview and the device. The AI can set MIDI bindings via its list_midi_controls / set_midi_mapping tools without touching effect source.

Performance on the ESP32-C6

The FX-VM is profiled on real hardware with the SoC’s cycle counters, not FPS. The PerfReport protobuf reports frame_cycles_{min,mean,max} (the FX-VM execution cost) and show_cycles_mean (the LED transmit path). The HITL fx_bench harness (pi/hitl/harness/fx_bench.py) runs a suite of calibration micro-programs on a reserved rig and compares measured frame cycles to a committed golden. See DEVELOPERS.md → HITL testing for how to run it.

  • Golden baseline: web/tests/testdata/device-bench-esp32c6.json (deviceKey: esp32c6-hitl-golden, cpuHz: 160_000_000; 65 fit programs + 7 held-out). Regenerate with fx_bench --emit-golden <path>.

  • Regression gate: a program fails if abs(measured/golden − 1) exceeds its margin — default 10%, with sweep16 at 15% (a deliberately safe band until per-effect run-to-run variance is characterized) — and it is more than 8,000 cycles (50 µs) off the golden. The absolute floor absorbs the fixed per-frame flash-cache cost that moves with code layout; it only matters for tiny programs like sweep16. Only frame cycles are gated; show cycles (transmit) are noisier and excluded.

Representative measured frame-cycle costs at 160 MHz (full set in the golden):

Program

Frame cycles

Notes

empty

~0.49 M

per-frame overhead floor

sweep16 / sweep256

~0.10 M / ~0.97 M

per-LED anchors (16 / 256 LEDs)

addM / mulM / negM

~2.5 M / ~2.5 M / ~1.8 M

cheap arithmetic

sinM / cosM / tanM

~3.9 M / ~4.1 M / ~6.8 M

float sin/cos ≈ 570 cy per call

expfM / powfM

~6.2 M / ~9.1 M

exp still soft-float poly

hash12M / hash32M

~2.9 M / ~4.2 M

hash ≈ 185 cy per call

smoothstep32M / distance32M

~21.4 M / ~14.4 M

heaviest float ops

hsv2rgbM / mix3M (held-out)

~7.1 M / ~6.7 M

validation set

lavalamp (held-out)

~3.6 M

a realistic 200-LED effect

show_cycles cluster tightly at ~1.0–1.1 M across programs (transmit-bound, not FX-bound). These numbers back the browser-side cost model (web/src/effects/costModel.ts, deviceProfile.ts): a linear sum-of-op-costs fit that predicts an effect’s device budget headroom (green ≤70% / yellow / red >90%) without a device present. The fit tracks expensive programs to ±3–7% but over-predicts the cheapest ones; the software estimator test (web/tests/deviceProfileHardware.test.ts) gates at 13%. Perf-model rationale and the calibration methodology live in docs/design/perf-monitoring.md.

The full per-platform numbers — fitted opcode costs (cycles + ns), the estimator’s held-out predicted-vs-measured accuracy, and every raw measured benchmark — are auto-generated from the goldens into docs/fx-vm-performance.md and pinned by a CI freshness gate (bazel run //web:gen_fx_vm_perf_doc to regenerate).

These costs are the target of an ongoing FX-VM hill-climb (see WORKLOG.md). Float sin/cos use a table internally but still cost ~570–650 cycles per call (range reduction + interpolation in soft-float), and hash ~185 — cheaper than exp/pow, not cheap: every opcode also pays ~70 cycles of dispatch. The cheap forms on the FPU-less C6 are the integer ones: per-LED maths in fixed (Q16.16) with every literal fixed (x * 0.6fx; x * 0.6 promotes a fixed x to soft-float — the compiler warns), sin/cos of a fixed angle in TURNS (~150), and hsv2rgb/atan2/sqrt with all-fixed arguments. The measured per-op-class table the authoring agents are given is PERF_GUIDE in web/src/effects/ai/system-prompt.ts (and “Performance” in agent/skill/effect-language.md); estimate_effect / estimate_performance return the top cost findings for a program (web/src/effects/perfFindings.ts).

The compiler now does the classic hoisting for you (docs/design/fx-memoization.md):

  • Per-frame values in shade() (sin(time * 2.0), uniform math, state reads) are computed once per frame in a prepare entry the VM runs before the first LED.

  • Per-LED-invariant values (atan2(led.pos.x, led.pos.z), length(led.pos.xz), hash(float(led.idx)), anything built only from led.pos/uv/idx/seg/s/branch) are computed once per LED per map and then read from a per-LED cache in the spare fx arena. A cached value is bit-identical to recomputing it.

So write the natural expression in shade(), and keep a value in a single let-style local (assigned once) so the pass can see that it is invariant. fx_compile --disasm lists what was hoisted and cached; --no-memo turns the pass off.

Long strips: what sets the frame rate

The C6 drives up to 1024 LEDs on each of its two channels (2048 total; firmware/player_app/led_caps.bzl). Two things bound an effect’s frame rate, and the autoscaler (firmware/fps, FUG-82) runs at whichever is lower:

  • The wire. Each bit is three 400 ns samples, so an LED takes 28.8 µs, plus a ~300 µs latch. Both channels clock out in parallel, so only the longest channel counts. The render of frame N+1 overlaps the transmit of frame N, so the wire is a floor under the frame period, not an addition to it. The ladder never climbs above the fastest rung whose 95% budget fits one wire frame (FpsState.max_fps reports it):

    Longest channel

    Wire time

    Wire-limited fps

    Ladder ceiling

    150

    4.6 ms

    216

    80

    300

    8.9 ms

    112

    80

    512

    15.0 ms

    66

    60

    768

    22.4 ms

    45

    40

    1024

    29.8 ms

    34

    30

    Measured on the analyzer (rig-2, 1024 + 1024): every frame 29,497 µs on both channels, no runts, gaps or bad bits, idle or under a ping flood.

  • The effect. Shading costs at least ~3.8 k cycles per LED (sweep256), so 2048 LEDs need ≥ 7.8 M cycles (~49 ms) per frame even for a trivial effect: at the full cap every effect is CPU-bound near 20 fps (measured: ledTimingRamp ~49 ms period; the plasma 7.25 M cycles, 20 fps, 10–15 fps during TLS reconnects). Up to ~1000 LEDs in total the wire is the limit for light effects.

The autoscaler’s floor (its lowest rung) scales with the strip: 25 fps up to 512 shaded LEDs, then the rung that keeps 25 × 512 = 12,800 shaded LEDs/s (rounded down, never below 5): 20 fps at 600, 10 fps at 1024, 5 fps at 2048. FpsState.min_fps reports it. An effect too heavy even for the floor is never parked: it keeps playing at whatever rate it achieves (FpsState.lagging, achieved_fps).

LED-arity buffer arrays are sized for the stored map / configured strip (at least 512 LEDs), not the 2048 cap, and both decks share the 24 KB FX_ARENA: a buffer vec4 costs 16 B per LED (32 KB at 2048 — it doesn’t fit); declare it : fixed8 (4 B per LED) for long strips. A deck whose buffers outgrow its slice reads 0 past the end rather than faulting.

AI effect generation

The editor can write and iterate on effects with Claude. The integration is a direct, keyless-server browser → Anthropic call (BYO key): the request goes straight to https://api.anthropic.com/v1/messages with the user’s key from localStorage (ledmapper.anthropicKey, set via the AI-key sheet) and the anthropic-dangerous-direct-browser-access header. The model is claude-opus-4-8 (web/src/effects/ai/generate.ts).

The system prompt (web/src/effects/ai/system-prompt.ts) is assembled from the language spec at load time — the built-in table, context table, and keyword list are interpolated from the same BUILTINS/CONTEXTS/KEYWORDS the compiler uses, so the prompt can never advertise a builtin the compiler lacks. It states the hard constraints (two entry points, no recursion, bounded loops, all values f32), documents uniform syntax, state, structs/arrays, buffers, topology sources, and textures, includes four worked examples, and ends with a repair directive (“given a previous script and compiler diagnostics, return a corrected script that fixes every error; change as little else as possible”). It is also perf-aware: the prompt teaches the fixed-point types and soft-float cost model, and the agent is fed the binding device’s per-builtin cost table (builtinCostsToPrompt in web/src/effects/perfContext.ts) so it can optimize toward a real budget.

There are two call paths:

  • One-shot generation — a streaming request with output_config: json_schema constraining the reply to { script, notes }; the in-progress script field is surfaced live into the editor as it streams.

  • Interactive chat (chatTurn, up to 8 rounds) — a tool-use loop where the model calls client-fulfilled tools: set_script (replace the editor content, compile, and return the compile result — ok/diagnostics/uniforms/disassembly so the model can self-repair), capture_preview (render the live FxPreview canvas to a PNG returned as a vision block), estimate_performance (run the cost model and report headroom), measure_on_device (offered only while a fixture with the perf feature is connected: run the current script there for seconds (1–10, default 3), optionally at given uniforms, and return the measured frame time mean / p95 / max, per-window means and the estimate’s error band next to it), and list_midi_controls / set_midi_mapping. The prompt tells the model to iterate with the estimate, confirm on the device before calling a performance goal met, and trust the measurement when the two disagree. Each user turn includes the current source and the latest compile summary as context.

So every AI-authored effect is compiled by the real wasm compiler and (optionally) previewed by the real VM before it ever reaches the device — the same path a hand-written effect takes.

Build targets

Target

What

//fx_compiler:fx_compiler

compiler library (ledmapper_fx_compiler)

//fx_compiler:fx_compile

CLI compiler

//fx_compiler:fx_compiler_web

wasm bundle consumed by the web editor (/fx-compiler)

//firmware/fx_vm:fx_vm

the VM library (also linked into the firmware)

//firmware/fx_vm:fx_vm_web

VM wasm bundle for the browser preview (/fx-vm)

//firmware/fx_vm:fx_vm_test, //fx_compiler:fx_compiler_test

unit tests (host)

The web app loads fx_compiler_wasm_pkg.js and fx_vm_wasm_pkg.js dynamically (web/src/fx/preview.ts, web/src/effects/editor/compile-worker.ts).

Sensor values and drivers

Live sensor values reach effects as uniforms bound by name (the uniform pool, docs/design/sensor-core.md §6): declare uniform vec3 imu_gravity = 0.0, -1.0, 0.0; (or imu_quat, imu_euler, imu_shake, imu_tap, …) and the firmware writes it every frame while a sensor runs; otherwise it is an ordinary knob. Sensor drivers (FUG-107) are .fxb programs too: void poll() instead of shade(), export declarations instead of uniforms, the i2c_write / i2c_read8 / i2c_read16 / i2c_read16s intrinsics, and FLAG_DRIVER (0x10) in the header with poll() in the shade-entry slot and the export table as the manifest. Ops I2cWrite (106) / I2cRead (107, u8 mode: bytes 1..2, bit 7 signed) are inert outside Vm::run_poll.