Native Platform
Native platform (@downdraft/platform-native)
Section titled “Native platform (@downdraft/platform-native)”A native platform layer providing direct GPU rendering via a single Rust cdylib (libdowndraft_platform, crate at native-rs/), accessed over runtime-adapted FFI (bun:ffi under Bun, koffi under Node, Deno.dlopen under Deno — see ffi/ffi-adapter.ts). Located in packages/platform-native/.
Architecture
Section titled “Architecture”- GPU: the
wgpucrate accessed throughwgpu_shim_*exports (native-rs/src/gpu/) that flatten complex WebGPU descriptors into FFI-friendly functions — same ABI the old C shim exposed. The TypeScript wrapper (src/gpu/wgpu-wrapper.ts) implements the standard WebGPU JS API (GPU,GPUAdapter,GPUDevice,GPUQueue, etc.) on top of the FFI calls.installGPU()setsglobalThis.navigator.gpuso the engine’sGPUDeviceManagerworks unchanged. - Window:
winitfor window creation, input polling, and surface handles (native-rs/src/window/, exported asdd_window_*for ABI compat). TheNativeWindowclass runs the event loop, translates events to DOM-compatible events, and providesrequestAnimationFrame.NativeSurfaceimplements theRenderSurfacecontract (core/platform/render-surface.ts) plus aGPUCanvasContext-shapedgetContext("webgpu"). After each rAF dispatch the window auto-presents viaqueueMicrotask(DOM end-of-frame semantics), so bespoke render loops that never callcontext.present()still present —present()itself skips frames that acquired nothing or acquired-but-never-wrote (__ddWritten— flipped atqueue.submit(), not at encode time, viaWgpuCommandBuffer.writeTargets, so a dropped/invalid/abandoned buffer can’t mark a texture written). Frame retention trap: dirty-skip renderers (my-app) rely on skipped frames never acquiring the swapchain. Any engine/module code that callscontext.getCurrentTexture()unconditionally per-frame (an auto-clear, a UI composite/blit) fabricates a frame and presents it over black — usecontext.peekCurrentTexture()instead to join a frame that already exists; viewportCount=0 + dirty-skip apps must passclearColor: nullso AppRenderer’s auto-clear doesn’t acquire either. - Image decoding: the
imagecrate (native-rs/src/image.rs,image_shim_*exports) replacescreateImageBitmap.installImagePolyfills()setsglobalThis.createImageBitmap,ImageBitmap,OffscreenCanvas, andImageData. - WGSL validation:
dd_wgsl_validateruns naga (wgpu’s own frontend + validator) in-process for build-time shader checks — used bywgslValidatePluginandbun-preload.ts. No external tint binary. - Asset discovery:
nativeGlob()replacesimport.meta.globwith filesystem-based globbing. - Screenshot:
captureScreenshot()copies a render target to a buffer, reads back pixels, and encodes a PNG (acceptance mechanism for native rendering). - Native host:
createNativeHost()ties everything together — installs all polyfills, creates the window, gets the GPU device, configures the surface, starts the event loop, and installs the browser-global shims (window, storage,Worker, UI-event constructors, blob/fetch). There is nodocumentglobal —RenderSurfaceis the surface abstraction. - Desktop surface (optional FFI groups, lazy-probed — absent symbols degrade to “feature absent”, no ABI bump): window controls + close intercept (
winDesktop),rfddialogs +notify-rustnotifications (winDialogs),mudaapp menus (winMenu— win/mac native, in-window kitMenuBaron Linux),tray-iconsystem tray (winTray— win/mac native, LinuxksniStatusNotifierItem over session DBus with no GTK/libappindicator dependency). Menu and tray models share theAppMenuModelJSON parser innative-rs/src/menu.rs; tray item ids are namespacedtray:on the same action channel. Singleton lock (bridge/singleton-lock.ts) forwards{argv,cwd}from refused second instances over<userData>/instance.sock/\\.\pipe\downdraft-<appId>todowndraft.onSecondInstance, defaulting to raising the primary window. Per-app paths (resolveAppDirs→downdraft.paths) split XDG config/data/cache (roaming/local on Windows, Library on macOS);downdraft.settingsis a typed KV over<configDir>/settings.jsonwith atomic writes.
Layout: Rust cdylib at packages/platform-native/native-rs/ (src/gpu/ wgpu, src/window/ winit, src/image.rs, src/text.rs cosmic-text, src/gpu/validate.rs naga). TS side at packages/platform-native/src/ — ffi/ (ffi-adapter.ts, lib-paths.ts, worker-bootstrap.mjs, wgsl-loader.mjs), gpu/ (wgpu-ffi/wrapper/device/resources/encoder/install, shared-device.ts, pass-channel.ts, native-wgsl.ts), dom/ (dom-polyfills.ts, mini-event-target.ts), window/ (window-ffi.ts, native-window.ts, native-surface.ts), image/ (createImageBitmap/canvas2d/freetype), assets/ (nativeGlob), screenshot/, native-host.ts (createNativeHost), dev/ (dev-shell.mjs, native-dev-runtime.ts, vite-config.mjs), bridge/ (user-data-dir.ts).
Native binaries (prebuilt, no consumer-side toolchain)
Section titled “Native binaries (prebuilt, no consumer-side toolchain)”Native binaries are not committed and consumers never compile them:
- npm channel:
@downdraft/native-<platform>-<arch>optional dependencies of@downdraft/platform-nativecarry the prebuilt cdylibs underlib/. - GitHub-release channel:
bun run fetch:nativeinsidepackages/platform-native(it’s a script on that package, also run by itspostinstall— not a root script) downloadsdowndraft-native-<platform>-<arch>.tar.gzfrom thenative-v<version>release and unpacks into each crate’s staging dir. - Local dev:
bun run build:native(root) builds the Cargo workspace viascripts/build-native.mjs— the only native toolchain needed iscargo(pinned byrust-toolchain.toml). - Staging pitfall:
build-native.mjspasses--target <host-triple>, so artifacts land intarget/<triple>/<profile>/— a barecargo build --release(no--target) writestarget/release/instead. Copying a.sointo a cratedist/from the wrong dir silently stages a stale build (this once shipped anlibdowndraft_blitz_osr.somissingdd_osr_scroll_to→ blank windows). Preferbun run build:native; if hand-staging, copy fromtarget/<triple>/release/. - Vendored deps:
vendor/arboardis a patched arboard 3.6.1 (X11 serve path: replies to UTF-8 MIME-alias targets liketext/plain;charset=utf-8that TARGETS advertises — upstream only answers the exact stored atom, so Qt/Klipper requesters got NONE and the clipboard stayed stale). Pinned via[patch.crates-io]in the root Cargo.toml — it affects every workspace crate that uses arboard (blitz-osrFlagShell,dd_window_*_clipboard). If bumping arboard later, check whether upstream fixedhandle_selection_requestand drop the vendor dir. - Debug native builds:
build-native.mjs --debugstages to<dest>/<platform>-<arch>-debug/— a sibling of the release dir, never an overwrite. Apps load debug artifacts only when launched withDD_NATIVE_PROFILE=debug(ordraft dev --native-debug); release resolution never searches debug dirs, so a debug build cannot silently downgrade the runtime (a 318MB debuglibdowndraft_platform.soonce shipped to the release staging dir cost ~3× frame time — debug wgpu validates every call unoptimized). The loader warns whenDD_NATIVE_PROFILE=debugresolves a release artifact. - CI:
.github/workflows/native.ymlbuilds the per-platform matrix and uploads bundles to the release;publish.ymlrepacks them into the npm platform packages viascripts/stage-native-packages.mjs.
lib-paths.ts resolves libraries via env override → crate dist/ → native/ → native/<platform>-<arch>/ → workspace target/ → @downdraft/native-* package → <exe>/native/ → /usr/local/lib. With DD_NATIVE_PROFILE=debug, <platform>-<arch>-debug/ and target/debug dirs are prepended; under release they are excluded.
Runtime detection
Section titled “Runtime detection”packages/engine/core/src/platform/runtime.ts provides isBun, isDevMode, getHostCapabilities(), and getNativeHost() for feature-detecting the runtime. Engine code should use these instead of import.meta.env.DEV or typeof navigator !== "undefined".
packages/engine/core/src/platform/render-surface.ts defines RenderSurface — the host-neutral render target (WebGPU context, dims, event target, pointer-lock). AppRenderer/AppContext/startApp use it; NativeSurface satisfies it.
Shared GPU device across workers (@downdraft/platform-native gpu/): wgpu handles are process-global — a worker can attach to the host device via attachSharedDevice(handle, cells) (non-owning view; worker destroy() never releases the native device). Consumers must attach through nativeHost.gpuShare.payload() — the GpuShareBroker owns THE cells set per device; calling shareDevice() per consumer clobbers device.sharedState so only the last consumer’s cells get writeDead() on loss. Liveness propagates through the shared SAB cells — owner pollLost/destroy()/markDeviceLost all write them dead; workers must check isValid() before FFI calls (calling into a freed device is a UAF). Resource direction: worker→owner exportGpuResource/exportCommandBuffer (owner importGpu*/importCommandBuffers into OWNING wrappers); owner→worker borrowGpuTexture/borrowGpuBuffer (worker wraps non-owningly — deferred-destroy applies, see html-ui’s texBind/texAck). Ordered worker passes: postPass → nativeHost.passMailbox (GpuPassMailbox) → AppRenderer drains "preSubmit" into the frame batch and "preUi" ahead of the UI-composite submit — stale-tolerant, next-frame semantics, never rendezvous’d. createGpuWorkerContext is the conventional worker-side attach/detach prelude. Use for coarse subsystems (UI raster uploads, terrain bake, worker pass producers) — fine-grained per-pass splits lose to submission overhead, and one queue still serializes GPU work. createDeviceStateCells registers every cell set in a process-wide LIVE_CELLS set — teardown calls retireAllSharedDevices() (bounded detach wait) / markAllSharedDevicesDead() (sync kill-switch) against all of them without knowing who shared. Device loss currently means restart (__ddRequestRestart) — in-place worker reattachment is documented future work in docs/site/src/content/docs/architecture/gpu-worker-reattach.md.
Shutdown ordering (window close) — workers must die before GPU handles are freed, or a worker inside a wgpu FFI call wedges teardown behind a synchronous native call (which also defeats the dev shell’s force-exit timer — a real soft-lock). Canonical order: retireAllSharedDevices() first (raises detach flags → workers stop starting FFI calls), then __ddSession.teardown()/sim.stop() (graceful worker stop), then host.destroy(). host.destroy() is idempotent and marks all shared cells dead itself. Outside the dev shell nothing stopped sim workers historically — runNativeAppModule now stops ctx.sim/createdSim on close (via stop()/shutdown()); SimWorker declares optional stop/shutdown; BaseWorkerHost/SandStepPool workers are unref()ed so a leaked worker can’t pin process exit.
Runtime-specific import loaders
Section titled “Runtime-specific import loaders”packages/engine/core/src/platform/bun-preload.ts registers Bun plugin loaders for ?raw and ?url import suffixes, plus CSS imports — configured in root bunfig.toml, Bun-only. The Node+tsx path gets the same handling from packages/platform-native/src/ffi/wgsl-loader.mjs (passed via NODE_OPTIONS=--import); Deno has no loader hooks, so shared code must never depend on loader transforms.
Tri-runtime notes
Section titled “Tri-runtime notes”Native runs on three JS runtimes — Bun (default, bun:ffi), Node+tsx (koffi, wgsl-loader.mjs), Deno (Deno.dlopen, root deno.json + --allow-all --unstable-sloppy-imports). Steady-state perf is identical across runtimes (GPU + fixed-dt sim don’t depend on the JS runtime); differences are boot time (~0.5–1.0s Bun, ~1.0s Node/Deno) and footprint (~520MB Bun / ~1.05GB Node / ~690MB Deno RSS on my-app). OSR (devtools/web overlays) is modules/native-osr — in-process WebGPU, no webviews.
Running the native entry (tri-runtime)
Section titled “Running the native entry (tri-runtime)”# Bun (primary)bun run src/native-entry.ts
# Node (tsx + wgsl/?raw loader)NODE_OPTIONS="--import ../../packages/platform-native/src/ffi/wgsl-loader.mjs" \ ../../node_modules/.bin/tsx src/native-entry.ts
# Deno (root deno.json import map mirrors tsconfig paths; sloppy imports for# extensionless/dir imports; requires --allow-all for FFI+workers+fs)deno run --config ../../deno.json --allow-all --unstable-sloppy-imports src/native-entry.tsdeno.json at the repo root is a generated import map mirroring tsconfig.web.json paths (foo/* → dir/* trailing-slash form, required for @-scoped aliases). Regenerate with bun run gen:deno-import-map whenever tsconfig paths change (CI checks it stays in sync). Known Deno limitation: basis-universal ?url wasm imports resolve lazily with a disk fallback.
Verified on my-app (~20s steady state): all three runtimes boot the full stack — GPU via FFI, nested workers, MCP, saves, screenshots — and the sim holds its fixed 60t/s on each.
Cross-runtime landmines:
- tsconfig
pathsare type-only, but tsx and Deno honor them at runtime. The"xxh3-ts"alias maps to a.d.ts— under tsx/Denoimport "xxh3-ts"loads the declaration file as an empty module. Runtime workarounds must use a subpath the alias doesn’t cover (e.g.require("xxh3-ts/index.js")), never a bare specifier. - CSS imports must stay out of shared modules —
app-module.tsruns on native where.csscan’t load (Deno has no loader hooks at all). There is no browser-only entry where CSS can load — keepimport "./x.css"out of app code entirely. - JSON imports need
with { type: "json" }for Deno (supported by Node ≥20, Vite, Bun, browsers). node:moduleimports must be lazy dynamic imports in shared code (a static specifier resolves before runtime shims install).- Node has no global
Worker— dom-polyfills wraps worker_threads on the main thread andffi/worker-bootstrap.mjsre-installs the same wrapper inside workers for nested spawns. __ddRequestFrameprefers a renderer’s ownrenderOneFrame()over the inheritedAppRenderer.renderOnce()— renderers that draw outside the AppRenderer frame graph (tto) would otherwise acquire-but-not-write the surface texture and starve the capture hook via the write-tracking present-skip.
wgpu validation notes
Section titled “wgpu validation notes”The Rust shim validates every WGPUTextureFormat/WGPURenderPipelineDescriptor enum value before calling into wgpu and returns NULL with an error log instead of aborting. Hardened call-sites: wgpu-device.ts counts only non-null vertex-buffer slots (sparse buffers arrays desynced the flat-descriptor walk); bind groups created with hasDynamicOffset layouts must be bound with a dynamic-offsets array (setBindGroup(i, bg, [0]) — the shim plumbs them); PostProcessStack takes a sceneFormat option — apps whose scene pipelines target the surface format pass it (the default rgba16float requires HDR scene pipelines).
App-side verification env vars
Section titled “App-side verification env vars”Per-app conventions seen in app repos (not engine-wide): SCREENSHOT_FRAME=N (auto-capture at frame N), AUTO_EXIT=1 (exit after the shot), DEBUGGER_AUTO_SHOW=1, DEBUGGER_PANEL=<id> (panel ids: console elements performance gpu + provider ids app sim memory render-graph materials doctor workers input postfx assets).