Shift Engine Docs

These docs follow the main branch and can describe changes newer than v0.3.2. Release archives and checksums are published on GitHub Releases.

Real-time audio & UI4

Both real-time audio (libs/audio) and UI4 scene rendering (libs/fh1data + apps/carview/ui.h) are strictly decoupled from platform I/O, enabling deterministic offline verification (--audio-out, --menu-png) alongside live gameplay.

Lock-Free 48 kHz Real-Time Audio 0 heap allocs · 0 mutexes on render thread · 480-frame blocks MAIN THREAD • RPM / throttle / load • Listener pose & ToD • Collision / UI / VO • Destroys retired players safely TripleBuffer SPSC Cmds Retire / Meters AUDIO RT THREAD • Engine XML + 100–250 Hz DSP • FEV1 envelopes & 39 snapshots • 12-slot burble/backfire pools • 615-pair surface collisions • 3D AMB_Quads (64 m hash) • 3 Radio stations (dr_mp3) • SatNav / Co-Driver / VO → SDL3 / AAudio 48 kHz out ASYNC DECODERS • In-process XMA2 + FSB4 • 62 AMB_*.fev beds • 429 ui4audio + 702 VO → PCM via SPSC (0 stalls) render_offline() Same DSP graph for --audio-out 4K UI4 Atlas & Instanced Pass 4096×4096 RGBA8 · GPU-only patch uploads · 1 draw pass ROWS 0 .. 1023 · VFONT GLYPH CACHE 11 retail vector fonts (up to 192 px) + CJK bitmaps Prewarmed HUD ASCII + active-frame eviction guard ROWS 1024 .. 2047 · PERSISTENT CHROME Tachometer, brushstroke masks, 9-slice panes, pad glyphs ROWS 2048 .. 4095 · LRU TRANSIENT ART Car thumbnails, event tickets, logos, upgrade icons Evicts cleanly without ever touching glyph rows 96 B ui::Instance → shaders/ui.frag (+ CPU raster) kRoundRect · kLine · kArc · kGlyph (512² SDF) · kImage · kImageMasked
Left: lock-free 48 kHz audio render thread with TripleBuffer/SPSC queues and off-thread XMA2 decoders. Right: 4K UI4 texture atlas band partitioning feeding the single-pass 96-byte instanced GPU renderer.
Lock-Free Real-Time Audio Engine & 4K UI4 Instanced Renderer:


Audio Architecture (libs/audio/include/audio/audio_engine.h):
- Dedicated 48 kHz stereo RT render thread (480-frame / ~10 ms blocks into ~50 ms ring), zero heap allocations or mutexes in steady state.
- Main Thread -> Audio Thread via lock-free TripleBuffer (per-frame engine RPM/load/throttle, listener pose, ToD) and SPSC Command Queue (collisions, landings, UI cues, SatNav/Co-Driver/VO).
- Audio Thread -> Main Thread via TripleBuffer (meters, radio popup, backfire counter) and Retire Queue so swapped player destructors run on Main Thread.
- Background Async Decoders decode FSB4/XMA2 (in-process), 62 AMB_*.fev beds, 429 ui4audio samples, and 702 VO samples off-thread.
- DSP features: XML engine/harmonic synthesis + 100-250 Hz low-mid punch DSP, FEV1 envelopes (Lowpass/Tremolo/Flange/SpeakerLevel/Compressor), 39 mix snapshots across 21 categories, 12-slot exhaust burble/backfire pools, 615-pair surface collisions, 3D AMB_Quads (64 m emitter hash, Doppler, tunnels), 3 disc radio stations via dr_mp3 + Custom Radio.
- Synchronous AudioEngine::render_offline() drives --audio-out WAV captures and headless tests.


Anark UI4 & 4K Atlas (apps/carview/ui.h, libs/fh1data/include/fh1data/ui_scene.h):
- Parses retail .bgf/.bsg/.fbf scenes (SUPER_STACKER, 9-slice BigPane01, dual-texture masks) and 11 vector fonts (A, B, C, D, E, SYM, DG1..DG5) + CJK bitmaps.
- 4096x4096 (or 2048x2048) RGBA8 GPU-only patch atlas partitioned into 3 horizontal bands:
  - Rows 0..1023: VFont glyph cache (up to 192 px buckets, prewarmed HUD ASCII, active-frame eviction guard).
  - Rows 1024..2047: Persistent HUD & UI4 chrome textures.
  - Rows 2048..4095: LRU-evicted transient artwork (thumbnails, tickets, logos).
- Single-pass instanced GPU draw of 96-byte ui::Instance (kRoundRect, kLine, kArc, kGlyph using pre-baked 512x512 R8_UNORM StrokeSdfAtlas, kImage, kImageMasked), with identical CPU rasterizer for headless --menu-png tests, uniform multi-aspect scaling, and inverse hit-testing.

Subsystem highlights#

In-process XMA2 & engine punch DSP

Decodes FSB4/XMA2 banks off-thread (bundled FFmpeg XMA2 decoder, zero CLI dependency), crossfades RPM/load/throttle XML loops, and applies retail-matched 100–250 Hz low-mid punch DSP plus per-car AI/traffic engine players.

FEV1 envelopes, acoustics & radio

Evaluates FEV1 DSP (Lowpass, Tremolo, Flange, SpeakerLevel, Compressor), 39 mix snapshots, 12-slot burble/backfire pools (synced to exhaust flames), 615-pair surface collisions, 3D AMB_Quads (64 m hash, Doppler, tunnels), and all 3 disc radio stations via dr_mp3.

Anark UI4 scenes & 11 vector fonts

Parses .bgf/.bsg/.fbf hierarchies (SUPER_STACKER lists, 9-slice BigPane01, dual-texture masks), all 11 retail vector fonts (A..E, SYM, DG1..DG5), CJK bitmap fonts, and a pre-baked 512×512 stroke SDF atlas.

4K atlas bands & multi-aspect hit-testing

Dedicated glyph rows (0..1023, up to 192 px buckets with active-frame eviction guard) prevent thumbnail streaming from ever blurring menu text. Scales uniformly across 4:3, 16:10, 16:9, ultrawide, and portrait with exact inverse hit-testing.

Offline inspection flags#

Render any audio mix or UI4 screen headlessly without opening a window:

# Offline 48 kHz stereo WAV capture (same DSP graph as live audio thread)
carview --play <dir> --headless --audio-out out.wav --audio-mix --seconds 5

# Offline UI4 menu, HUD, map, career, and screen captures
carview --play <dir> --menu-png menu.png
carview --play <dir> --map-png map.png --career-png career.png
Audio headers (libs/audio/include/audio/): audio_engine.h (AudioEngine 48 kHz, block_frames=480 / 10 ms, target_buffer_frames=2400 / 50 ms, render_offline, TripleBuffer & SPSC command/retire queues, AudioTelemetry with backfire_count & codriver_completed_cues), rt.h, fsb4.h, xma_decode.h, engine_sound.h, engine_player.h, xml.h, fev.h, sfx_engine.h (405 FEV/FSB4 + 429 ui4audio samples), snapshots.h (39 snapshots, 21 categories), world_acoustics.h (4675 .soundscape tiles, 1318 reverb zones, 1745 ambience zones, 62 AMB_*.fev beds, 64 m spatial hash, CarDspMode Off/Rewind/Countdown/Cockpit), radio.h, radio_dj.h, runtime_radio.h, user_music.h, satnav_vo.h, codriver_vo.h, vo_player.h (344 triggers, 702 samples). UI4 headers: libs/fh1data/include/fh1data/ui_scene.h (.bgf/.bsg/.fbf, SUPER_STACKER, BigPane01 9-slice), vfont.h (11 roles: A, B, C, D, E, SYM, DG1..DG5 via fontmap.xml), cjk_font.h (CHTB/CHSB/JPB/KORB); apps/carview/ui.h (96-byte ui::Instance with Kind: kRoundRect=0, kLine=1, kArc=2, kGlyph=3, kImage=4, kImageMasked=5; flags kMaskBorderTransparent, kMaskRepeat, kUiFlagFullscreen, kUiFlagFullscreenCover, kUiFlagFullscreenX, kUiFlagAdditive, kUiFlagUvXform; StrokeSdfAtlas 512x512 R8_UNORM 8x8 grid of 64x64 cells; 4096x4096 RGBA8 atlas partitioned into rows 0..1023 glyph cache, rows 1024..2047 persistent HUD/UI4 chrome, rows 2048..4095 LRU transient artwork; identical GPU shaders/ui.frag and CPU rasterize() paths).