openOMSI GitHub Discord

Docs / The project / Architecture

Threading

5 min readEdit this pagedocs/ARCHITECTURE.md

  • Tile loading (.map parse, terrain, spline tessellation, object type loading) runs on the rayon pool; GPU upload happens on the main thread. Object and spline types are shared through Arc caches guarded by mutexes.
  • Tile streaming (omsi-app::tiles::Streamer) has a thread of its own: it decides which tiles belong around the camera and the player's bus, stages them on the rayon pool and hands them over, and the frame places and uploads only a little of one tile at a time (and unloads within the frame budget) so that streaming never shows as a stutter.
  • Textures decode on worker threads through TextureCache and are compressed there (BC1/BC3) before they go up. A vehicle set read ahead for the timetable fleet is made on the GPU by the worker as well - wgpu takes device calls from any thread - so the drawing thread only puts it into the scene.
  • People are posed and skinned in parallel (par_iter_mut over those due this frame, at a rate that falls with distance and with what the camera sees).
  • The LAN socket lives in omsi-net on its own thread; the game's side of it is a stage of its own in OMSI_PROFILE ("lan": loading a joining player's bus used to be counted as the people's time).
  • Ending the game from outside (omsi-app::quit): the signal handler only sets an atomic, a watcher thread wakes the event loop, and the ordinary shutdown path runs.
  • AI vehicle scripts run in parallel (rayon par_iter_mut over the cars' VehicleInstances after the sequential lane/obstacle planning of a frame); a lane grid (50 m cells, Network::grid) answers nearest-lane queries without scanning the whole network.
  • Publisher packs are checked on the rayon pool, one asset per task (omsi_pack::VerifiedPack:: verify_all and the open-time change markers), each task holding one 64 KiB chunk at a time.
  • The renderer rewrites only the per-draw entries of instances that changed since the last frame (Scene::changed); a full rebuild happens only when instances are added or the render origin moves. Together these took Spandau with traffic from ~34 to ~240 fps.

    It also culls in parallel, records the main pass as render bundles and finishes the command buffers on helper threads, and the enhanced sky is computed on a helper thread and taken in when it is ready.

  • Audio: the mixer owns the voices and runs on the device's callback; the game (any thread: AudioEngine is Send + Sync) hands it commands (play, parameters, stop, the radio's effects) through a lock-free single-producer ring (omsi_audio::spsc) and gets the voices that ended back through another, to free their clips on its side.

    The callback takes no lock and allocates nothing (room for more voices is made on the game's side); the game keeps its own list of the voices playing and never waits for a block being mixed. The cpal stream lives on the "audio device" thread, which also follows the default output. The internet radio's frames come through a ring as well (stream::StreamBuf).

  • The sim thread (omsi_sim::sim_thread; the sim_thread setting, off by default, decided once as the game starts; OMSI_SIM_THREAD=0 / =1 over it): the main thread keeps the window, the input and the surface, and with the switch on the AI traffic's steps (TrafficSim::advance, one for each tick of the frame) run on a thread of its own while the main thread draws and presents the frame's picture;

    the traffic stage does the rest of the traffic on what the last step left, and the stepped traffic is back before the frame is counted. The cars are one frame later than the rest of the frame; the player's vehicle and its input stay in their stage (no added input latency). The offscreen run never reads the switch.

    The scenery objects' scripts go in the same job after the traffic's step (they read its lights, as the scripted stage did after the traffic stage); the scripted stage then shows the commands of the step made during the last picture and owes the next one with its inputs owned (OwnedSceneryInputs).

    The people's step goes in the same job, tick by tick after the traffic's step of the tick: the people go out whole and read the player's bus, which the picture keeps, from a copy made in the humans stage (omsi_sim::people::PlayerBusView, the same values the inline step reads);

    the humans stage then applies what the step made during the last picture (steps::humans_after_step) and owes the next. The timetable's step still runs in its stage (its fleet uploads sit between its two halves).

  • Scenery object scripts (omsi_sim::scenery::SceneryState, held by the session, not by World): the scripts and variables of the loaded tiles' scripted objects are one owned list in the simulation; the view's half (render instances, materials, texts, sounds) stays in World (omsi_scene::ScriptedSet) under the same key.

    The tile loader hands the simulation's halves over through a SceneryInbox (placed objects, tiles that went, the particle emitters' owners), which the list takes at the start of its step and before the player's clicks and the trains' points reach it.

    Only the objects within reach of the camera's 50 m cell are looked at (a candidate set made anew when the camera changes cell or a tile goes); the exact 800 m cut picks among them.

    Their scripts run side by side (frame_each) on inputs gathered before (light phases, boards, clock), and the step ends in commands in object order (SceneryCmd) that carry values - the meshes' transforms, the variables the view reads, changed texts, page pictures, fired triggers - so the view (World::apply_scripted) applies them without reading the simulation's list.

  • The fixed step (omsi_sim::tick, RATE_HZ = 60 ticks a second, always; at most 4 a frame, so the wall clock is kept down to 15 frames a second): the window's frame runs its stages as three lists (app_events/frame/stages.rs, run by ticked.rs): the housekeeping and the player's input once a frame (FRAME_BEFORE), the simulation (traffic, the player's bus with its scripts, the people, the duty, the clock and the weather) once per tick of 1/60 s (FRAME_TICK; what only the picture and the sound need of the traffic and the people - their view syncs, the AI's sounds - in the frame's last tick alone, SimTicks::view_due), then the view, the sounds, the LAN, the interface and the picture once a frame (FRAME_AFTER), with the player's bus and the camera drawn between the last two ticks.

    The offscreen run's step is one tick. Still on the frame, not the tick: the tile streaming's handover and the population's camera, so the AI traffic is not yet the same at every frame rate (the player's bus is).

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open