Design: Effects runtime (bytecode VM + language)¶
Status: proposed / in progress. The technical centerpiece of next_steps.md.
User-locked decisions: hybrid execution model (update() + shade());
GLSL-ish shader language; programs publish uniforms + ranges that become
live UI controls.
Goal¶
Users write small GPU-shader-style programs (“effects”) that color a mapped fixture from each LED’s 3D position, the skelgraph topology, time and sensors. Programs are compiled (in-browser, wasm) to a compact bytecode, pushed to the device’s LittleFS, and executed by a small no_std interpreter on the C6 in the render loop. The same VM runs in the browser for an offline preview. A program’s declared uniforms drive auto-generated sliders/buttons/dropdowns in the live control UI.
Execution model (hybrid)¶
Each frame the runtime does:
update()— optional, runs once/frame. Evolves persistentstate(integrators, phases, spawn logic). May readtime,dt,frame, sensors, uniforms. Writesstate.shade(led) -> vec3— runs once per LED. Returns the LED’s linear RGB (0..1). Readsled.*(position/index/topology),time, uniforms, andstate(read-only). Pure per-LED → parallelizable, cache-friendly.
Rationale: shade alone (pure per-LED) covers most spatial effects; update
adds cheap global state (a wavefront position, a spawn list) without per-LED
recomputation. This mirrors the existing pulse/flood sim but as user scripts.
Language (GLSL-ish)¶
// uniforms → UI controls (name : range/kind = default)
uniform float speed : 0.0 .. 5.0 = 1.0; // slider
uniform float width : 0.02 .. 1.0 = 0.2; // slider
uniform vec3 tint : color = vec3(1,0,0); // color picker
uniform bool mirror = false; // toggle
uniform int palette : {"fire","ice","rainbow"} = 2; // dropdown
// persistent state (written by update, read by shade). `shared`: a fixture that
// rejoins a mesh takes it from the gateway, so the phase stays in step
// (fx-mesh-state-and-agents.md); plain `state` is this fixture's own.
shared state float phase;
void update() {
phase = fract(phase + speed * dt); // advance a phase each frame
}
vec3 shade(Led led) {
float d = fract(led.pos.z * width - phase); // moving band along z
vec3 c = palette_lookup(palette, d);
return c * tint;
}
float is fine here (fract/palette_lookup are float built-ins). Reach for
int/fixed in the arithmetic-heavy inner math that doesn’t go through a
float built-in — counters, indices, and smooth Q16.16 ramps you fold yourself:
// integer stripe index + Q16.16 ramp, no soft-float in the hot path
vec3 shade(Led led) {
int stripe = int(led.pos.x * 8.0) % 3; // native int math
fixed t = fixed(led.pos.y) * fixed(0.5); // Q16.16 mul
return vec3(float(stripe) / 2.0, float(t), 0.0);
}
Types:
float,vec2/3/4,int,fixed,fixed8,fixed16,bool. Colors arevec3/vec4.intis a native 32-bit integer with real integer opcodes (+ - * / %), not float-emulated — cheap on the FPU-less core.fixedis Q16.16 fixed-point: use it for the smooth quantities (phase, positions, palette coords) that would otherwise pay soft-float cost.+/-are plain integer adds;*//use the Q16.16 mul/div opcodes. Literals like0.5becomefixedin afixedcontext; convert explicitly withfixed(x)/float(x)/int(x).Reduced-precision fixed-point (FUG-10) —
fixed8(Q1.6, s1.1.6) andfixed16(Q1.14, s1.1.14), both range [-2, 2). Same scaled-integer representation asfixed(so+/-/compare are plain integer ops;*//shift by the fraction width), just narrower. Their reason to exist is the accelerated transcendentals:sin/cos/expon afixed8/fixed16value compile to a pure-integer LUT (no soft-float at all), so trig-heavy per-LED math stops depending onf32.sin/costake the angle in turns (1.0= 2π), so there is no range reduction; they return a value in[-1, 1].expsaturates at+2(it is a decay curve; useful forx ≲ 0.69). Convert withfixed8(x)/fixed16(x)/float(x). The±2range means these are for normalized quantities (colors, phases, sensor readings), not world coordinates — reach forfixed(Q16.16) when you need range.Mixed-type arithmetic promotes along
int → fixed8 → fixed16 → fixed → float(widest operand wins), sofixed16 * floatyieldsfloat. Keep a hot expression all-fixed16(or all-int) to stay off soft-float.
Contexts (built-in reads):
global:
time(s, float),dt(s),frame(int)Led led:led.pos(vec3, in the map’s gravity-leveled frame, roughly normalized to a unit box),led.idx(int),led.count(int), and topology:led.seg(int segment id, -1 if none),led.s(float 0..1 along its segment),led.branch(bool at a junction).sensors (when present):
imu.accel(vec3),imu.gyro(vec3).
Built-ins:
sin cos tan abs floor ceil fract mod min max clamp mix step smoothstep length distance dot cross normalize pow exp log sqrt sign+hash11/hash31(cheap noise),hsv2rgb,palette_lookup(int,float).Vector/scalar broadcast: a scalar combines with a
vecNelement-wise (c + 0.1,1.0 - c), operand order preserved for-//.User functions:
float f(float x) { ... }(any scalar/vec params + return) — plain, no recursion (bounded stack). Defined before use; called withCALL/RET_FN, a small return-address stack, and locals allocated disjointly across functions (no per-call frame). Control flow:if/else,forwith a C-style header (for (int i=0; i<n; i=i+1) { ... }).Structs & arrays: user
struct Name { float a; vec3 b; };(scalar/vec fields) and fixed-size arraysType name[N];instate/locals, with.fieldanda[i]access. A struct/array is a contiguous slot range;a[i].fieldcompiles to base +i*stride + field-offset, and a runtime (non-constant) index emits the*_IDXload/store ops (one dynamic index per access path, the index clamped in-bounds at runtime). This is what lets a script keep an array of agents instateand simulate them inupdate(). Slot budgets:stateand locals are each ≤128 slots (MAX_STATE/MAX_LOCALS).Packed narrow storage (FUG-10) — hidden
buffers andtextures (the per-LED / 2D feedback arrays that dominate the runtime’s RAM) store each element at a chosen component precision, not a fixed 4-byte f32 slot. The arena is byte-addressed, so an 8-bit component costs one byte:buffer fixed8 trail;— onefixed8per LED = 1 B/LED (a quarter of an f32 buffer). Reads back asfixed8.fixed16/int8/int16likewise (int8/int16read back asint).buffer vec3 col : fixed8;— a: fixed8/: fixed16annotation compresses a float/vec element’s storage while reading/writing as float (vec3→ 3 B/LED instead of 12). Same annotation on atexture.sample(tex, uv)always dequantizes to a float colour regardless of the stored precision; flatbuf[i]reads the declared element type. Default (unannotated) storage is f32, so existing effects are byte-for-byte unchanged. The on-device videoset_texturepath packs frames straight into the target texture’s format, so a narrow texture compresses the stream too.
Bytecode / VM¶
Stack-based, 32-bit slots (a vecN is N contiguous slots). Every slot is a
raw 32-bit word: float bits, an int, a Q16.16 fixed, or a bool (1.0/0.0).
The VM is untyped at runtime — the compiler types every operation and picks
the matching opcode, so the word is always interpreted correctly. Chosen over a
register VM for compiler simplicity; revisit if profiling demands it.
Opcode families:
float element-wise
Add/Sub/Mul/Div/Neg,Dot/Cross/Length/Normalize, unary math (Sin…) — soft-float.integer
AddI/SubI/MulI/DivI/ModI/NegI/CmpI— native RV32IM, no soft-float.fixed-point (Q16.16)
MulFix/DivFix(adds/subs/neg/compares reuse the int ops since the representation is a scaled integer).reduced-precision fixed-point (Q1.6 / Q1.14)
MulFixN/DivFixN(afracoperand selects the fraction width),FixRescale(shift between fixed formats),FixToF/FixFromF(thef32boundary), and the accelerated transcendentalsSinFix/CosFix/ExpFix— integer LUT + linear interpolation, no soft-float.conversions
I2F/F2I/Fix2F/F2Fix/I2Fix/Fix2I(Q16.16/int) and the generalFixRescale/FixToF/FixFromF(any fixed width) — emitted by casts and by mixed-type promotion.data/flow:
PushConst,LoadUniform(idx,size),LoadCtx(id)(time/led.*/imu),LoadState/StoreState(idx,size),LoadLocal/StoreLocal,MakeVec(n),Swizzle(mask)(also used for scalar→vec broadcast),Swap(operand reorder for promotion/broadcast), compares +BrFalse/Jmp,Call/RetFn(user functions, with a 16-deep return-address stack),Return.
The C6 (RV32IMAC) has no FPU → floats are soft-float. Budget: 256 LEDs × ~30
ops × 30 fps ≈ 230k ops/s ≈ low-single-digit % CPU even in soft-float. int/
fixed run natively, so hot paths written against them are effectively free —
fixed (Q16.16) is the recommended type for smooth per-LED quantities. The ISA
carries both, so a script chooses its precision/speed tradeoff without any VM
change.
Complete native datatype ISA (FUG-122)¶
The goal: it is possible to run an entire update()/shade() — inputs, math,
colour and output — without a single soft-float op. Three additions make that
reachable:
Every opcode has a strictly-integer/fixed twin. The vector, transcendental and colour ops that were float-only now have counterparts operating on the raw scaled-integer slot word:
SqrtFix,Dot/Length/Distance/Normalize/Cross/ Scale/Smoothstep/ClampV/MixV(fixed),Atan2Fix/LogFix/TanFix/PowFix,Hsv2RgbFix/PaletteFix,HashFix/Hash3Fix. A carriedfracoperand (0 = int, 6/14/16 = the fixed formats) lets one opcode serve every width. An exhaustive opcode-coverage test (fx_vm/tests/vm.rs) fails to compile if a new opcode is added without classifying its fixed path.Float-free inputs — fixed context, VM-side.
LoadCtxFix id comp fracpushes one scalar context component already in Q16.16 fixed/int, so no float op runs inside the VM forled.pos/uv/s/dist/time.Frame/Ledcarry both thef32and*_fixmirrors. The compiler sets a.fxbflag (FLAG_USES_CTXFIX) when it emitsLoadCtxFix; the host builds the fixed mirrors only for those programs (all-float effects pay zero hot-path overhead and the resident cache is unchanged). The mirrors are derived from the per-frame map fetch — a small footprint on the FPU-less core, and a natural place to fold in a precomputed per-map fixed-position table later if profiling wants it.Float-free output.
RetRgb8(int 0..255 channels) andRetRgbFix frac(Q1.frac channels in [0,1]) emit the 8-bit colour directly, skipping theclamp01()*255soft-float epilogue.
Compiler. Vectors stay float in v1; the fixed path is expressed by scalar
decomposition plus fixed colour assembly, which is exactly where the
soft-float cost sits on an FPU-less core. The compiler: lowers a cast of a scalar
context component (fixed16(led.pos.x)) to a single LoadCtxFix; folds literal
casts (fixed16(1.0)) to a fixed const at compile time; routes hsv2rgb/
palette/rgb/rgb8 on fixed/int args to the fixed colour ops; and returns them
via RetRgbFix/RetRgb8. Float effects compile byte-identically. Fixed-vector
algebra opcodes exist in the ISA (with tests) but the front-end does not emit
them yet — a follow-up if element-typed vectors are added.
File format (.fxb)¶
A flat, mmap-friendly container so the VM executes directly from the flash range LittleFS stored it in (no per-frame fs calls; open once, keep the mapped/pointer, run bytecode in place):
magic "FXB1" | version | flags
uniforms manifest: [ {name, type, ui_kind, min, max, step, default|options} ]
consts: [f32...]
entry offsets: update, shade
code: [u8 bytecode...] (little-endian, 4-byte aligned)
... (optional trailers: memo, then the `shared state` footer)
shared footer (flag 0x40): [(slot u8, width u8) × n] | n u8 — at the very end
The shared state footer names the state slots a mesh catch-up copies (a
set_fx_state frame: the words plus Program::layout_hash, so a snapshot only
restores into the same build). A reader that predates it ignores the flag.
The manifest is what the app turns into controls and what the device echoes on
request (get_effect_uniforms). Uniform values live in a separate small buffer
set live via protocol (set_uniform/set_uniforms) — no recompile to tweak a
slider.
Uniforms → UI¶
ui_kind ∈ {slider(min,max,step), color, toggle, dropdown(options)}. The webapp
renders these generically (replacing today’s hard-coded effect knobs). The same
manifest is used offline (preview) and online (device control), so a slider does
the identical thing in both.
Firmware integration¶
New arms:
submit_effect(upload.fxb→ LittleFS, likesubmit_map),set_effect(select the active script),set_uniforms.get_effect_uniformsreturns the manifest for UI hydration.The render task, when an effect is active, runs
update()thenshade()per LED into the FastLED buffer (replacing/added alongside the built-in pulse/flood). Persist the active effect + uniforms to LittleFS (auto-resume, like the current playback).Crate layout:
//firmware/fx_vm(no_std interpreter +.fxbreader, host test build), reused by the wasm preview.
Webapp integration¶
//fx_compiler(Rust) →fx_compiler_webwasm (like the pulse/solver wasm):compile(src) -> {bytecode, manifest, diagnostics}.Editor: start with a
<textarea>+ compile-on-idle + diagnostics; upgrade to CodeMirror + syntax highlight later (see effects-compiler doc).Preview: run the same VM compiled to wasm (
fx_vm→fx_vm_web) over the current map’s LED positions each rAF tick →MapView.setLedColors()(already exists). Works with no device connected.Auto uniform panel from the manifest; values drive both the preview and (when connected)
set_uniformsto the device.
Milestones¶
VM + ISA (
fx_vm), host tests: hand-assembled bytecode → correct LED colors; a golden shader vs a reference implementation.Compiler (
fx_compiler): lexer/parser/typecheck/codegen; host tests (source → bytecode → VM → expected). Uniform manifest extraction.wasm:
fx_compiler_web+fx_vm_web; a minimal editor + auto uniform panel +MapViewpreview (offline).Firmware:
.fxbupload/select/uniforms protocol + render-loop executionpersistence.
Perf counters (see perf-monitoring doc).
Open questions¶
Topology built-ins (
led.seg/s/branch): exact set — start minimal, extend.Palette source: built-in tables vs user-provided — start with a few built-ins.
forbound: currently a plain C-style loop (runtime trip count). Still open whether to hard-bound frame time via a global per-frame instruction budget (favored) vs a compile-time max-iterations cap. Not yet enforced. DECISION: include bounded execution (instruction count) in the impl. Also support bounded-time execution via continuation; save thread context on invocation and set up hardware timer to trigger at deadline (with some margin before frame) which cancels execution and resumes to timeout error branch at callsite.
Additional guidance: Come up with a few effects as candidates to explore the design space and requirements. As a few examples:
chaser effects that are topology-aware, such as the current “agentic pulse” effect
3D volumetric effects (plane waves traveling along 3D vectors, spherical waves propagating outwards from spawn points, “hue field” function paramterized over 3D coordinates
2D video mapping: stream bitmap frames from client, sample bitmap in uv space like
texture()
The effects runtime should have a dedicated arena allocator for any data structures needed so as to prevent the firmware from locking up because of heap exhaustion.
Bytecode optimizer + superinstructions (FUG-125)¶
The compiler front-end (fx_compiler/src/lib.rs) is a single-pass emitter. Rather
than rewrite it around an SSA IR, the optimizer (fx_compiler/src/opt.rs) runs
over the emitted .fxb code section just before serialization: it decodes the
flat byte stream into a linear instruction list where every branch/call/entry
target is a stable instruction id (not a byte offset), applies a fixed point
of passes, then re-encodes and recomputes the relative branches. Because targets
are ids, a pass can delete/splice/reorder instructions with no offset math; if a
pass ever leaves a dangling target or an out-of-i16 branch, encode bails and
the original bytes ship — correctness is absolute, optimization best-effort.
Passes (the issue’s “local and scalar” list):
Constant folding —
PushConst a; PushConst b; <scalar binop>andPushConst a; <unop>collapse to onePushConst. Every fold reproduces the VM’s exact arithmetic (wrapping ints,>>fracfixed, IEEEf32, guarded divide-by-zero), so a folded constant equals what the interpreter would compute.Algebraic simplification / strength reduction —
x*1,x/1,x-0,vec*1, and-(-x)involutions, on both float and int/Q16.16 twins. Floatx+0.0is deliberately excluded (it flushes-0.0→+0.0, not a true identity).Dead local elimination — a single-assignment/single-use temp (
StoreLocal s; LoadLocal swhose slot is touched nowhere else) drops both ops, leaving the value on the stack. Disabled in the presence of dynamic-index local ops (*LocalIdx), which could alias any slot.Constant-condition branch folding + dead-code elimination — a known
PushConst c; BrFalsebecomes an unconditionalJmpor vanishes, and a CFG reachability walk from both entries removes the orphaned code.
Superinstructions. Guided by the dynamic profiler (tools/fx_profile, which
ranks the hottest adjacent opcode pairs across the corpus), three fused opcodes
were added to the VM ISA (appended after FillLocal, so every existing
discriminant is stable) and emitted by an optimizer fusion pass:
TeeLocal s,n—StoreLocal; LoadLocal(the temp-reload idiom).IncLocalI s,c—LoadLocal; PushConst; AddI; StoreLocal(thei = i + kcounter/index idiom).BrCmpI k,rel—CmpI; BrFalse(the integer loop-condition tail).
Each is a byte-for-byte fusion of the sequence it replaces, trading two-to-four dispatches for one on the FPU-less core.
Correctness + hill-climb loop. fx_compiler/tests/optimizer.rs is both the
safety net and the simulator the issue asks for: it compiles every corpus program
with and without -O, runs BOTH through the real fx_vm (update() once +
shade() across an LED raster over several frames), and asserts bit-identical
RGB. It then hill-climbs the fx_vm retired-op counter (a direct proxy for device
dispatch cost, priced by the golden per-opcode profile) and fails on any
regression. Corpus-wide retired-op reduction is ~15%, up to ~36% on
constant-heavy programs and ~15% on the loop-dominated agentic effect.
On-device JIT (FUG-125)¶
The ESP32-C6 core is RISC-V RV32IMC, so a hot straight-line block of the cheap
integer/fixed opcodes (loop counters, index math, Q16.16 multiplies) can be
lowered to a short position-independent code (PIC) segment that runs natively,
skipping the interpreter’s per-opcode fetch/decode/dispatch/budget overhead.
firmware/fx_jit is that JIT:
rv32encodes the small RV32IM instruction set the translator emits (pinned by unit tests to known ISA words).compile(&[Ir]) -> Segmentlowers a supported straight-line block. The segment is PIC — it touches memory only through three base registers (operand stacka0, localsa1, const poola2) and returns viaret, so it can be copied into an executable IRAM window and called as a C function. The operand stack is modelled exactly like the interpreter; compile-time depth may go negative (consuming operands already on the stack), reported viaSegment::min_depthso the VM gates on the livesp, withnet_deltaadvancingspafter the call.Correctness is established entirely on the host:
emuis a minimal RV32IM interpreter that executes the emitted segment,referencere-runs the block withfx_vm’s exact integer semantics, and a differential test runs thousands of random blocks over random state through both and asserts identical results. Unjitable blocks (badfrac, out-of-range offsets) are rejected, never miscompiled.
fx_jit is no_std (the firmware links only the no-alloc compile_into /
plan_blocks_into path; the Vec-based API is behind an alloc feature).
On-device integration (wired + validated). At effect load the firmware
(ffi.rs::fx_build_jit) scans the resident bytecode with plan_blocks_into,
compiles each hot block into one executable region, patches Op::JitCall over the
block’s first bytes, and installs a JitBlock table on the VM; JitCall dispatch
calls the native segment against the live stack/locals/consts and resumes
interpreting at the block end. The interpreter runs everything else and is the
always-on fallback (JIT disabled, or the region full).
The executable memory is a bounded W^X carve-out. The arduino-esp32 C6 build
ships esp-idf as precompiled libs (no RWX heap: IRAM is RX, DRAM is NX), so rather
than weaken memory protection globally, the firmware makes one small static buffer
read+write+execute via two spare RISC-V PMP entries: entries 3/4 are free and
unlocked and, being lower-index than the SRAM data-region entries (PMP is
lowest-index-wins), an RWX TOR range there (entry 3 = base, entry 4 = top) overrides
the default NX for just that range — W^X stays enforced everywhere else. TOR rather
than NAPOT so the buffer needs only 4-byte alignment: a NAPOT region must be aligned
to its own size, which made all of .dram0.bss 2048-aligned and cost ~1.8 KB of
boot heap in padding.
Validated on a real ESP32-C6 over HITL with a fixed-point-heavy shade(): the
native JIT renders bit-identically to the interpreter and cuts per-LED
shade() cost by ~2.1× (≈10077 → 4723 cycles). The boot self-bench
:esp32c6_fxjitbench reproduces the A/B (lm_fx_set_jit_enabled toggles it on one
firmware).
Shipped off by default. This device is heap-critically-tight (mbedtls needs
~28 KB for a TLS session), so the JIT’s static footprint is kept to ~3.4 KB (the
segments compile straight into the 2 KB exec region — no scratch copy — plus a
small bounded planning scratch), and FX_JIT_ENABLED defaults false: the
interpreter is the shipped path (it is also what the golden cost model the app +
fx_bench validate against measures), and the PMP carve-out is armed only when the
JIT is explicitly enabled via lm_fx_set_jit_enabled. An earlier revision that
enabled it by default with ~15 KB of scratch starved the WiFi/TLS heap and broke
provisioning — the small-footprint, opt-in design keeps the default firmware’s RAM
profile ~unchanged from baseline.