Workspace & Rust Rules
The Cargo workspace supplies the engine for games and tools on macOS/iOS Metal and browser WebGPU.
Layout
Cargo.tomldefines the workspace. Each module is one crate in its own folder (for example,render/device/isengine-render-device). Sources live insrc/, and tests sit beside the code (#[cfg(test)]) or intests/.- Each crate declares its dependencies through
[workspace.dependencies]. Cargo enforces the acyclic layering. - The
enginefacade crate (src/lib.rs) re-exports the core runtime and UI crates under their module names (engine::assets,engine::render::device, etc.). - Stage crates (
engine-stage,engine-stage-render, and the optional capability crates understage/) are not re-exported byengine; hosts opt in directly so unused stage code is never linked. engine-importis a build-time tool crate (thexodo-importbinary and a library for tests and exporters). No runtime crate depends on it, andenginedoes not re-export it, so glTF parsing and PNG decoding never reach a host's runtime.- Set
CARGO_TARGET_DIRto choose a build cache.
Implementation Rules
- Behaviour and precision — Preserve verified float operation order, numeric limits, and failure modes. Compare exact bytes for decoders, hashes, and packing where the format contract determines them.
- Performance — Use flat vectors and
#[repr(C)]GPU-facing structs. Keep allocation and dynamic dispatch out of per-draw and per-vertex loops. Usepointer_hash::WordHashfor pointer-address keys. Release builds usecodegen-units = 1, thin LTO, andpanic = "abort". - Ownership and errors — Use
Arcwhere ownership is shared andArc::make_mutfor copy-on-write lists. Contract violations useengine_core::require. Decode failures returnResultwhere callers can recover. - Unsafe — Permitted for FFI and measured hot loops. Document every block with a
// SAFETY:comment. - Evidence — Document the source of behaviour and numeric constants (a specification, a measurement, or a test).
Dependencies
The runtime uses Rust crates and minimal platform bindings. Native builds use SDL3 and Xcode's Metal tools. Browser builds use Emscripten.
| Need | Implementation |
|---|---|
| Metal | objc2-metal, objc2-quartz-core, objc2-foundation, block2 (completion handlers), dispatch2 (newLibraryWithData takes dispatch_data), objc2-core-foundation, and objc2-core-graphics; libcompression is linked directly |
| WebGPU | emdawnwebgpu's webgpu.h C API through hand-written extern "C" bindings in engine-render-device (wasm32-unknown-emscripten; the webgpu-typecheck feature compiles them on a host for cargo check) |
| SDL3 | sdl3-sys |
| Zstandard | zstd |
| Mesh optimization | Ported to Rust inside engine_scene::meshopt (also used by scene/world and render) |
| BC7 | Rust BC7 decoder in engine-assets (port of bc7decomp) |
| Fonts | skrifa (outlines, metrics) with ports of FreeType 2.14's automatic hinter (engine_gfx::autofit, including variable fonts) and smooth rasterizer (engine_gfx::raster), shared by engine-gfx and engine-ui; memmap2 maps font files |
| Text shaping | harfrust |
| SVG | usvg + tiny-skia, rendered by resvg (without text or raster-image features) |
| Vorbis | Port of libvorbis 1.3.7 and libogg 1.3.5 in engine_audio_output::vorbis (re-exported as engine_audio::vorbis), matching libvorbisfile PCM byte-for-byte at comparable speed |
| JPEG | zune-jpeg (decoding); jpeg-encoder writes screenshots with linear-quality tables and full-resolution chroma |
| PNG | SDL decodes (engine_platform::load_png); engine_platform::encode_rgba(width, height, &rgba) encodes 8-bit RGBA with miniz_oxide without SDL calls |
| zlib | miniz_oxide |
Generated Shaders
From the repository root, bun build/build.ts shaders compiles Slang into Rust modules in target/build/device-shaders/. Add --web to produce WGSL inputs for Emscripten, or --ios to also compile iOS Metal libraries.
The generator (build/rust-shaders.ts) builds ShaderCode and ComputeCode values using engine-render-device types. Native builds include metal/NAME.rs (embedding LZFSE-compressed Metal libraries from metal/NAME.lzfse), and browser builds embed WGSL raw strings.
Each crate's build.rs locates generated modules via ENGINE_SHADER_DIR (defaulting to target/build/device-shaders/). Crates include! their generated files, and Cargo rebuilds whenever they change.
Crate Boundaries
Callers supply renderers, resources, and platform inputs across crate boundaries:
engine-core— Holds shared math,LoadProfile,DynamicResolution, andFixedClock(fixed_clock), a fixed-timestep accumulator fed each frame's elapsedDuration(Game loop helpers).engine-scene-materials— Depends onengine-coreandengine-assets.native_device_capstakes the renderer's maximum texture extent, and the host implementsSceneLetteringfor scripted textures.engine-scene— Reachesscene/worlddata without depending on its types:ScenePackages' far landmark geometry and impostor caches storeArc<dyn Any + Send + Sync>values thatengine-scene-worldbuilds and downcasts, and coastal water geometry on a mesh draw is&dyn Any.ScenePackagesaccepts the host'sSceneLettering(with_lettering), andmaterial()returns anArc<SceneMaterial>lease.engine-scene-pawns— Depends onengine-core,engine-assets, andengine-scene(notengine-scene-materials), naming materials only throughScenePackages.engine-render— Depends onengine-scene-materialsas well for the CPU reference rasterizer.VideoOutputholds no renderer; each call takesengine_gfx::Renderer. The Metal ray tracer (create_ray_tracer, aRayTracertrait object) takes typedobjc2-metalhandles and compiles its kernels (render/src/ray_tracing.metal) on its device.engine-scene-world—WorldScenekeeps its state on the owning thread and gives level imports, far builds, and ambient-occlusion bakes anArcof shared state (WorldShared). Borrowed renderer views (directional_lighting(),resident_classes(),stream_ahead(),stream_resident_attributes()) return lightweight view types, and frame accessors build the frame lazily. It links no SDL: the free camera takes SDL3 keycodes and modifier flags as integers, and the water bump PNG inflates viaminiz_oxide.engine-audio-output— Depends only onengine-coreandengine-platform: the SDL playback device (AudioOutput), the Vorbis decoder, andclip(Clip,StreamClipwith a one-second worker-decoded PCM ring,ClipPlayer,Soundhandles,PlaySettings,Groupbuses with fading and ducking,AudioSource::from_ogg,suspend/resume, andmix::distance_gain).engine-audiodepends on it and re-exportsaudio_outputandvorbis.engine-audio— Needs only level sound data (WorldLevelAudio,AmbientObject), notWorldScene. Workers shareArc<Resources>,AmbientScheduler::updatetakes a duration lookup argument, published sounds areArc<PcmSound>, and players holdArc<AudioOutput>.