Workspace & Rust Rules

The Cargo workspace supplies the engine for games and tools on macOS/iOS Metal and browser WebGPU.

Layout

  • Cargo.toml defines the workspace. Each module is one crate in its own folder (for example, render/device/ is engine-render-device). Sources live in src/, and tests sit beside the code (#[cfg(test)]) or in tests/.
  • Each crate declares its dependencies through [workspace.dependencies]. Cargo enforces the acyclic layering.
  • The engine facade 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 under stage/) are not re-exported by engine; hosts opt in directly so unused stage code is never linked.
  • engine-import is a build-time tool crate (the xodo-import binary and a library for tests and exporters). No runtime crate depends on it, and engine does not re-export it, so glTF parsing and PNG decoding never reach a host's runtime.
  • Set CARGO_TARGET_DIR to 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. Use pointer_hash::WordHash for pointer-address keys. Release builds use codegen-units = 1, thin LTO, and panic = "abort".
  • Ownership and errors — Use Arc where ownership is shared and Arc::make_mut for copy-on-write lists. Contract violations use engine_core::require. Decode failures return Result where 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.

NeedImplementation
Metalobjc2-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
WebGPUemdawnwebgpu'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)
SDL3sdl3-sys
Zstandardzstd
Mesh optimizationPorted to Rust inside engine_scene::meshopt (also used by scene/world and render)
BC7Rust BC7 decoder in engine-assets (port of bc7decomp)
Fontsskrifa (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 shapingharfrust
SVGusvg + tiny-skia, rendered by resvg (without text or raster-image features)
VorbisPort 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
JPEGzune-jpeg (decoding); jpeg-encoder writes screenshots with linear-quality tables and full-resolution chroma
PNGSDL decodes (engine_platform::load_png); engine_platform::encode_rgba(width, height, &rgba) encodes 8-bit RGBA with miniz_oxide without SDL calls
zlibminiz_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, and FixedClock (fixed_clock), a fixed-timestep accumulator fed each frame's elapsed Duration (Game loop helpers).
  • engine-scene-materials — Depends on engine-core and engine-assets. native_device_caps takes the renderer's maximum texture extent, and the host implements SceneLettering for scripted textures.
  • engine-scene — Reaches scene/world data without depending on its types: ScenePackages' far landmark geometry and impostor caches store Arc<dyn Any + Send + Sync> values that engine-scene-world builds and downcasts, and coastal water geometry on a mesh draw is &dyn Any. ScenePackages accepts the host's SceneLettering (with_lettering), and material() returns an Arc<SceneMaterial> lease.
  • engine-scene-pawns — Depends on engine-core, engine-assets, and engine-scene (not engine-scene-materials), naming materials only through ScenePackages.
  • engine-render — Depends on engine-scene-materials as well for the CPU reference rasterizer. VideoOutput holds no renderer; each call takes engine_gfx::Renderer. The Metal ray tracer (create_ray_tracer, a RayTracer trait object) takes typed objc2-metal handles and compiles its kernels (render/src/ray_tracing.metal) on its device.
  • engine-scene-world — WorldScene keeps its state on the owning thread and gives level imports, far builds, and ambient-occlusion bakes an Arc of 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 via miniz_oxide.
  • engine-audio-output — Depends only on engine-core and engine-platform: the SDL playback device (AudioOutput), the Vorbis decoder, and clip (Clip, StreamClip with a one-second worker-decoded PCM ring, ClipPlayer, Sound handles, PlaySettings, Group buses with fading and ducking, AudioSource::from_ogg, suspend/resume, and mix::distance_gain). engine-audio depends on it and re-exports audio_output and vorbis.
  • engine-audio — Needs only level sound data (WorldLevelAudio, AmbientObject), not WorldScene. Workers share Arc<Resources>, AmbientScheduler::update takes a duration lookup argument, published sounds are Arc<PcmSound>, and players hold Arc<AudioOutput>.